$(document).ready(function(){
		
	//Fix Errors - http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup/
	
	//Remove outline from links
	/*$("#gns a").click(function(){
		$(this).blur();
	});
	*/
	/* For bug in chrome.. */
	jQuery.fn.aPosition = function() {
		thisLeft = this.offset().left;
		thisTop = this.offset().top;
		thisParent = this.parent();
		parentLeft = thisParent.offset().left;
		parentTop = thisParent.offset().top;
		return {
			left: thisLeft-parentLeft,
			top: thisTop-parentTop
		}
	}

	$("#language-selector li a img").not('.active').hover(
		function() {
			var image_name = this.src;
			this.src = image_name.replace("out","over");
		},
		function() {
			var image_name = this.src;
			this.src = image_name.replace("over","out");
		}
	);
	var menusizes = [];

	
	
	$("ul.submenu-0 li.has-children").each(function(index, el){
		offsets = $(el).aPosition();
		//$(el).find('ul.submenu-1').css("left", parseInt(offsets.left)+((index+1)*-6)-4+"px");
		$(el).find('ul.submenu-1').css("left", parseInt(offsets.left)+"px");

		//console.log("settings "+index+" to "+parseInt(offsets.left)+"px")
	});
	
	//When mouse rolls over
	$("ul.submenu-0 li.has-children").mouseover(function(){
		if (menusizes[this.id]==null)		
		{
			menusizes[this.id] = $(this).find('ul.submenu-1 li').size();
		}

		el_height = menusizes[this.id]*28+2;
		
		
		$(this).find('ul.submenu-1').stop().animate({height:el_height+'px', display:'block'},{queue:false, duration:600, easing: 'easeOutBounce'});
		//$(this).find('ul.lns').stop().slideDown();
	});
	
	//When mouse is removed
	$("ul.submenu-0 li.has-children").mouseout(function(){
		$(this).find('ul.submenu-1').stop().animate({height:'0px', display:'none'},{queue:false, duration:600, easing: 'easeOutBounce'});
		//$(this).find('ul.lns').stop().slideUp();
	});
	
 	
	if ($("body").hasClass("tpl_kartsida")) {
		var map = new GMap2(document.getElementById('map'));
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,40));
		
		map.addControl(new GSmallMapControl(), topRight);  
		
		//map.setCenter(new GLatLng(20.651033, 12.939453), 4);
		var marker = new GMarker(new GLatLng(66.072874, 20.981703));
		map.addOverlay(marker);
		
		map.setCenter(new GLatLng(68.09604, 21.88477), 5);
		marker.openInfoWindowHtml('<div id="google-maps-info-window"><strong>Treehotel</strong><p>Brittas pensionat<br/>Edeforsväg 2 A<br/>960 24 Harads<br/>Sweden<br/>phone + 46 928 10403<br/>e-mail: <a href=\"mailto:info@treehotel.se\">info@treehotel.se</a></p></div>');

		$("#google-map-overview").addClass("active");
	} else {
		var map = new GMap2(document.getElementById('map'));
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,40));
		
		map.addControl(new GSmallMapControl(), topRight);  
		
		//map.setCenter(new GLatLng(20.651033, 12.939453), 4);
		var marker = new GMarker(new GLatLng(66.072874, 20.981703));
		//20.96611976623535
		//66.07903878991085
		
		map.addOverlay(marker);
		map.setCenter(new GLatLng(68.49604, 21.88477), 5);
		marker.openInfoWindowHtml('<div id="google-maps-info-window"><strong>Treehotel</strong><p>Brittas pensionat<br/>Edeforsväg 2 A<br/>960 24 Harads<br/>Sweden<br/>phone + 46 928 10403<br/>e-mail: <a href=\"mailto:info@treehotel.se\">info@treehotel.se</a></p></div>');
		
		$("#google-map-overview").slideUp("fast", function() {
				$("#google-map-overview").addClass("active");
			}
		);		
	}

	$("body.tpl_startsida #hide-map").click(function(e) {
		e.preventDefault();
		$("#google-map-overview").slideUp("fast");
		return false;
	});
	
	$("body.tpl_startsida #btn-location").click(function(e) {
		e.preventDefault();
		map.setCenter(new GLatLng(67.49604, 21.88477), 5);
		$("#google-map-overview").slideDown(100, function() {
			map.checkResize();
		});
		return false;
	});
	
	$("body.tpl_standardsida #btn-location, body.tpl_rumssida #btn-location, body.tpl_formulärsida #btn-location").click(function(e) {
		e.preventDefault();
		map.setCenter(new GLatLng(67.49604, 21.88477), 5);
		$("#text-content-area").hide();
		$("#image-content-area").hide();		
		$("#head").stop().animate({height:'528px'},{queue:false, duration:200});
		$("#google-map-overview").slideDown(200, function() {
			map.checkResize();
		});
		return false;
	});	

	$("body.tpl_standardsida #hide-map, body.tpl_rumssida #hide-map, body.tpl_formulärsida #hide-map").click(function(e) {
		e.preventDefault();

		$("#google-map-overview").slideUp(200);	
		/*$("#head").stop().animate({height:'308px'},{queue:false, duration:600, easing: 'easeOutBounce'}, function() {
			$("#text-content-area").show();
			$("#image-content-area").show();
			console.log("y u no work.");		
		});*/
		$("#head").stop().animate({queue:false, duration:600, height:'308px'},200, function() {
			$("#text-content-area").show();
			$("#image-content-area").show();		
		});		
		
		
		return false;
	});
	
	
	/* ENDAST PÅ STARTSIDA, utan pager.. */
	$('#section-default').cycle({
        //fx:     'turnDown',
		//
        speed:  3000,
        timeout: 100,
		delay:-2000,
		slideExpr: 'img',
		random: 0,
		nowrap: true
    });
    $('#image-content-area').cycle({
        //fx:     'turnDown',
        speed:  1000,
        timeout: 6000,
        pager:  '#pager',
		slideExpr: 'img',
		random: 0
    });
	
	$("a[rel=gallery], a.fancybox").fancybox();
	
	$('a[href^=mailto:]').each(
		function(index) 
		{
			this.href = this.href.replace(/\(at\)/,'@');
			this.innerHTML = this.innerHTML.replace(/\(at\)/,'&#64;');
			//$(this).html().replace('\(at\)','@');
			//console.log(this.href);
		}
	);
	$(".fancybox_ajax").fancybox();

	$("img.hoverpuff").hover(
		function () {
			var image_name = this.src;
			this.src = image_name.replace("_out","_over");
		},
		function () {
			var image_name = this.src;

			this.src = image_name.replace("_over","_out");
		}
	);
	

});

//http://maps.google.com/maps?f=q&source=s_q&hl=sv&geocode=&q=Brittas+Pensionat,+Boden,+Sverige&sll=66.095595,20.942691&sspn=0.002026,0.008234&ie=UTF8&hq=Brittas+Pensionat,&hnear=Boden,+Sverige&ll=67.339861,24.960938&spn=15.565544,67.456055&z=5&iwloc=A
//http://maps.google.com/maps?f=q&source=s_q&hl=sv&geocode=&q=Brittas+Pensionat,+Boden,+Sverige&sll=66.095595,20.942691&sspn=0.002026,0.008234&ie=UTF8&hq=Brittas+Pensionat,&hnear=Boden,+Sverige&ll=67.651033,22.939453&spn=7.603742,33.728027&z=6&iwloc=A