var lastIndex = 0;

function loadMap() {
var myLatlng = new google.maps.LatLng(59.33597, 18.075779);
	var myOptions = {
	   scrollwheel: false,
	   zoom: 16,
	   center: myLatlng,
	   draggable:false,
	   mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	
	var map = new google.maps.Map(document.getElementById("google-map"), myOptions);
	var marker = new google.maps.Marker({
		   position: myLatlng, 
		   map: map, 
		   title:"Storåkers",
		   draggable: false
   });
}

$(document).ready(function() {	
	
	$('#background').npFullBgImg(SITE.imgPath, {fadeInSpeed: 2000, center: true}); //the cool bg image effect
	$(".boxes .content").jScrollPane({dragMaxHeight: 10, scrollbarWidth: 19, animateTo:true, animateInterval:50, animateStep:5} ); //setting up the scrollbars
	
	// sets z-index while boxes are being dragged
	$(".boxes, .popupBox").draggable({
		start: function(event, ui){
			lastIndex++;
			$(event.target).css('z-index', lastIndex);
		},
		stop: function(event, ui){
			lastIndex++;
			$(event.target).css('z-index', lastIndex);
		}
	});
	
	// sets z-index on boxes that are clicked (but not dragged)
	$(".boxes, .popupBox").click(function(){
		lastIndex++;
		$(this).css('z-index', lastIndex);
	});
	
	// Scrolling for the links
	$("a.scroll").click(function(){
		$this = $(this);
		var destinationSelector = $(this).attr('rel');
		$('.content', $this.parent().parent().parent()).each(
			function(){
				this.scrollTo(destinationSelector);
			});
		return false;
	});						 
	
	//reinitializes scrolling when boxes get resized
	$(".boxes, .popupBox").each(function(i, elm){
		$(elm).resizable({
			resize: function(event, ui) { 
				$(".content", elm).jScrollPaneRemove();
				$('.content', elm).css('height', ($(elm).height() - 50 - $('.nav', elm).outerHeight()) +'px');
				$(".content", elm).jScrollPane({dragMaxHeight: 10, scrollbarWidth: 19, animateTo:true, animateInterval:50, animateStep:5} );
			}
		}); 						  
	});
	
	//creates the accordion effect
	$(".boxes").accordion({
		collapsible: true,
		autoHeight: false, 
		//set "active: false" so that the accordion is collapsed on pageload
		header: 'span.states',
		changestart: function(event, ui) {
			if(ui.oldHeader.length){
				try{
					var new_style = ui.oldHeader.parent().attr('style'); 
					if(new_style != undefined){
						new_style = new_style.replace(/height: [0-9]{1,3}px;/i, '');
						ui.oldHeader.parent().attr('style', new_style);	
					}
				}catch(ex){
					// Silent error
				}
			}
		}
	});	
	
	$(".clock").resizable("disable"); //disables resizing of clock widget
	$(".map").resizable("disable"); //disables resizing of clock widget
	//$(".news").accordion("disable"); set this to disable the accordion on this element
	
	// POPUP BOXES
		$(".popupBox .states").click(function (){
			$(".popupBox").hide();
		});
		
		$(".popupBox").hide();
		
		// popup boxes 
		$(".show").click(function (){
			var whatever = $(this).attr("id");
			
			$("." + whatever).show();
			lastIndex++;
			$("." + whatever).css("z-index", lastIndex + 2);	
			$("." + whatever + " .subBox").jScrollPane({dragMaxHeight: 10, scrollbarWidth: 19, animateTo:true, animateInterval:50, animateStep:5} );
			
			if(whatever == "map") 
				loadMap();
			
		});
		
		
});

Cufon.replace("h1");


