/*
--------------------------------------------------------------------------------------
VARIABLES
--------------------------------------------------------------------------------------
*/

	var changePageActive = 0;
	var currentPageHeight = 497;
	var currentVisiblePage = "0";
	
	var arrHeights = [500, 585, 2500, 920, 515, 650]


	var google_map = new Image();
	google_map.src="gfx/location_map.png"; 

/*
--------------------------------------------------------------------------------------
DOCUMENT READY
--------------------------------------------------------------------------------------
*/

	$(document).ready(function() {


		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
		
		//$('#popup_bg').css('z-index

		
		// MAIN: Spot hover
		$('.spot', this).hover(
			function () {
				$('p', this).css('textDecoration', 'underline');
			},
			function () {
				$('p', this).css('textDecoration', 'none');
			}
		);
		
		
		// FACEBOOK: Spot hover
		$('#fb_spot', this).hover(
			function () {
				$(this).css('backgroundPosition', '0 -101px');
			},
			function () {
				$(this).css('backgroundPosition', '0 0');
			}
		);
		
		
		// TOP MENU HANDLER
		$(".main_link",this).hover(
			function () {
				if($(this).attr("id") != "link" + currentVisiblePage){

					$(this).animate({backgroundColor: '#f6f4ee'}, 300, "easeOutCubic")
				}
			},
			function () {
				if($(this).attr("id") != "link" + currentVisiblePage){

					$(this).animate({backgroundColor: '#FFFFFF'}, 200, "easeInOutCubic")

				}
			}
		);
			
		$(".main_link", this).click(function() {
			

			changePages(this);
		});		

		$(".spot", this).click(function() {
			changePages(this);
		});	
		
		
		$(".subsection").fadeIn(0, "easeInOutCubic");	


	
	});
	

	
/*
--------------------------------------------------------------------------------------
TOP
--------------------------------------------------------------------------------------
*/

	function changePages(selector) {

		if(changePageActive == 0) {
			changePageActive = 1;	
				if(Number($(selector).attr("id").match(/\-?\d+(\.\d+)?/g).join(' ')) != currentVisiblePage){
			
				var currentID = Number($(selector).attr("id").match(/\-?\d+(\.\d+)?/g).join(' '));

				$("#" + "link" + currentVisiblePage).animate({backgroundColor: '#FFFFFF'}, 600, "easeInOutExpo");
				$(".main_link").css('borderColor','#F0EDE3');
			
				currentVisiblePage = currentID;

				$("#" + "link" + currentID).css('borderColor','#d6d3cb');
				$("#" + "link" + currentVisiblePage).animate({backgroundColor: '#f6f4ee'}, 300, "easeOutCubic")
			
				changePage(currentID);
			}
		}
	}


	function showHideHeaderInfo(section){
		if(section == "about"){
			if($('#header_info').height() < 265){
				$("#contact").hide();
				$("#about").show();
				$("#header_info").animate({height: "265px"}, 600, "easeInOutCubic");
			} else {
				$("#contact").fadeOut(100, function(){
					$("#about").fadeIn(200);
				});
			}
		} else if(section == "contact") {
			if($('#header_info').height() < 265){
				$("#about").hide();
				$("#contact").show();
				$("#header_info").animate({height: "265px"}, 600, "easeInOutCubic");
			} else {
				$("#about").fadeOut(100, function(){
					$("#contact").fadeIn(200);
				});
			}
		} else if(section == "") {
			if($('#header_info').height() == 265){			
				
				$("#header_info").animate({height: "3px"}, 600, "easeInOutCubic");
			}
		}
	}
	
	
	function changePage(targetPage){

			$("#main_container").fadeOut(500, "easeInOutCubic", function(){
				$("#main_container").animate({height: "300px"}, 800, "easeInOutExpo", function(){
					$("#main_container").append("<div class='preloader_white'><img src='gfx/loading.png'><br><img src='gfx/preloader_white.gif'></div>");
					$(".preloader_white").fadeIn(400, "easeInOutCubic");
			
					$.ajax({
						url: "subsection"+targetPage+".html",
						cache: false,
						success: function(html){
						
							setTimeout(function() {
							
								
							
								$(".preloader_white").fadeOut(400, "easeInOutCubic", function(){
									$("#main_container").append(html);
									$("#main_container").animate({height: arrHeights[currentVisiblePage]+"px"}, 800, "easeInOutExpo", function(){
										$(".subsection").fadeIn(500, "easeInOutCubic");
										changePageActive = 0;
									});
								});
							}, 900);
						}
					});
				});
				$('#main_container > *').remove();
				$("#main_container").show();
			});
		
	}
	
	
	function showPopupInfo(showInfo) {
		
		if(showInfo == "google"){

			popupBG(1);
			
			$("#header_info").animate({height: "3px"}, 500, "easeInOutCubic",function(){

				$("#main_header").append("<div id='popup' class='popup_google'><a id='close' href='javascript:closePopupInfo();'></a>" +
				"<img src='gfx/google_headline.png' class='google_headline'>" +
				"<p class='adress'><b>Bes&ouml;ksadress</b><br>Viaredsv&auml;gen 32,<br>50494 Bor&aring;s</p>" +
				"<p class='adress'><b>Tel:</b> 033-10 25 87<br><b>Fax:</b> 033-12 31 35</p>" +
				"<p class='adress'><b>E-post: </b><a href='mailto:info@prram.se'>info@prram.se</a></p><div id='map_canvas'></div></div>");
						
				$("#popup").fadeIn(400, "easeInOutCubic", function(){

					// GOOGLE MAP
					$("#map_canvas").gMap({
						markers: [{
							address: 'Viaredsvägen 32, Sweden'
						}],
						zoom: 14,
						controls: ["GSmallMapControl"],
						maptype: G_NORMAL_MAP
					});
			
				});
				
			
			});

		
		}
		
		
	}
	
	
	function closePopupInfo() {
		
			$('#popup').remove();
			popupBG(0);
	}	
	
	function popupBG(visibileStatus){
		if(visibileStatus == 1){
			
			$("#main_header").append("<div id='popup_bg' onClick='closePopupInfo();'></div>"); 

			if ($.browser.msie) { 
				$("#popup_bg").show();		
			
			} else {
				$("#popup_bg").fadeIn(350, "easeInOutCubic");		
					
			}
			
		} else {
			
			if ($.browser.msie) { 
				$("#popup_bg").hide(0, function(){
					
					$("#popup_bg").remove();
		
				});	
			
			} else {
				$("#popup_bg").fadeOut(350, "easeInOutCubic", function(){
					
					$("#popup_bg").remove();
		
				});	
					
			}			
			

		}
	}
	
	
	function showReference(imgUrl, className) {
		
		popupBG(1);


		$("#header_info").animate({height: "3px"}, 500, "easeInOutCubic",function(){

			$("#main_header").append("<div id='popup' class='popup_" + className + "'><a id='close' href='javascript:closePopupInfo();'></a><img src='gfx/" + imgUrl + ".jpg' class='" + className + "_headline'></div>");
			$("#popup").fadeIn(400, "easeInOutCubic");
			
			scrollTo('#popup', 500);
		});

		
	}
	

	function showVideo() {

		popupBG(1);

		$("#header_info").animate({height: "3px"}, 500, "easeInOutCubic",function(){


			$("#main_header").append("<div id='popup' class='popup_video'><a id='close' href='javascript:closePopupInfo();'></a><div id='video_wrapper'></div></div>");
			$("#popup").fadeIn(400, "easeInOutCubic");

			scrollTo('#popup', 500);

			
			var s2 = new SWFObject('http://www.prram.se/ASSETS/player.swf','player','700','414','9');
			s2.addParam('allowfullscreen','true');
			s2.addParam('allowscriptaccess','always');
			s2.addParam('wmode','transparent');
			s2.addParam('flashvars','file=http://www.prram.se/ASSETS/lansforsakringar.flv&bufferlength=4&image=http://www.prram.se/GFX/video_preview.jpg');
			s2.write('video_wrapper');
				
			
		});

	}
	

	function scrollTo(variable, time) {
		$('html, body').animate({
			scrollTop: $(variable).offset().top-20
		}, time, 'easeInOutExpo');
	}	

