$(window).load(function() {

	function initializeMap(){
	    $('#preload a').preload();
		$('#searchlocations').hide();
		$('#map').fadeOut(500, function(){
			$('#map').empty().css({
				width: '450px',
				height: '297px',
				backgroundImage: 'url(images/map/map_base.jpg)',
				position: 'relative'
			});
			$('#map').fadeIn();
			loadBullets('coast', false);
		});
	}

	function addZoomable(id, title, width, height, top, left){
		$('<img class="zoomable" src="images/map/blank.gif" id="' + id + '" title="' + title + '" />').css({
			border: '',
			position: 'absolute',
			width: width + 'px',
			height: height + 'px',
			top: top + 'px',
			left: left + 'px',
			cursor: 'pointer'
		}).appendTo('#map').click(function() {
			$(this).siblings().fadeOut();
			$(this).hide()
				   .attr('src', 'images/map/' + id + '_base.png')
                   //.fadeIn('slow')
                   .css({opacity: '0.25', filter: 'alpha(opacity=25)'}) //Erstatter fadein
				   .animate({
				        opacity: 1,				    				    
						width: '450px',
						height: '297px',
						top: '0px',
						left: '0px'
					}, 1000, '', function(){
						$('#map').css({backgroundImage: 'url(images/map/' + id + '_base.png)'}).empty();
						loadBullets(id, true);
					});
		}).appendTo('#map').hover(
                function () {
                    $('#map').css({background: 'url(images/map/' + id + '_hover.png)'});
                }, 
                function () {
                    $('#map').css({background: 'url(images/map/map_base.jpg)'});
            
		});
	}
	
	function loadBullets(id, back){
		$('#map').load('kart_punkt.php', {}, function(){
			//add back button
			if(back){
				$('<a id="mapback" href="javascript:void(0)"><span>Til hovedkartet &gt;</span></a>')
					.appendTo(this)
					.click(function(){initializeMap()});
			}
			else{			
				//addZoomable('sogn_og_fjordane', 'Klikk for &aring; se Sogn og Fjordane', 170, 83, 0, 20);
//                addZoomable('buskerud', 'Klikk for &aring; se Buskerud', 84, 84, 84, 160);
//                addZoomable('hordaland', 'Klikk for &aring; se Hordaland', 170, 153, 83, 0);
//                addZoomable('rogaland', 'Klikk for &aring; se Rogaland', 170, 102, 187, 0);			
			}
			//place bullets
			$(this).children('a.bullet').each(function(){
				var coords = $(this).attr('rel').split('-');
				$(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
					   .hide()
					   .fadeIn(250)
					   .click(function(){showPopup($(this).attr('id'));});
			});
		});
	}
	
	function showPopup(id){
		$('#map div.popup').fadeOut(); 
		var boxid = '#' + id + '-box';
		$(boxid).fadeIn(500);
        //$(boxid).css({opacity: '0', filter: 'alpha(opacity=0)', width: '0px', height: '0px', top: '50%', left: '50%'}) //Erstatter fadein
//				   .animate({
//				        opacity: 1,				    				    
//						width: '204px',
//						height: '306px',
//						top: '20px',
//						left: '20px'
//					}, 700);
		$('a.close').click(function(){
			$(this).parent().fadeOut();
		});
	}

	
	//initialize map
	initializeMap();
	

});
