$(document).ready(function(){


	$("DIV.kdm-expandables")
		.find("A")
		.not(".link")
		.each(function(){
			//toH = $(this).next().height();
			//alert(toH);
			if($(this).hasClass("open"))
				$(this).parents("h3,h4").next().show();
				
			$(this).click(function(){
				if($(this).hasClass("open"))
					$(this).parents("h3,h4").next().slideUp(300).end().end().blur().removeClass("open");
				else
					$(this).parents("h3,h4").next().slideDown(300).end().end().blur().addClass("open");
				return false;
			});
		});


	$("A.kdm-expand").each(function(){
		toH = $(this).next().height();
		$(this).click(function(){
			//alert(toH);
			if($(this).hasClass("open"))
				$(this).next().slideUp(300).end().blur().removeClass("open");
			else
				$(this).next().slideDown(300).end().blur().addClass("open");
				
			return false;
		});
	});
	/*
	$("H2.ctry").each(function(){
		//$this = $(this);
		//$next = $this.next();
		toH = $(this).next().height();
		//alert(toH);
		$(this).click(function(){
			//alert(toH);
			if($(this).hasClass("open")){
				//$next.slideUp(300);
				//$(this).blur().removeClass("open");
				$(this).next().slideUp(300).end().blur().removeClass("open");
			}
			else{
				//$next.slideDown(300);
				//$(this).blur().addClass("open");
				$(this).next().slideDown(300).end().blur().addClass("open");
			}
				
			return false;
		});
		$(this).next().hide();
	});
	*/

	$("DIV#kdm-gmap").each(function(){
		var coords = g_text_tocoord($(this).attr("rel"));
		//alert(coords.toString());
	    var gmOptions = {
			zoom: 8,
			center: coords,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var gmap = new google.maps.Map(document.getElementById("kdm-gmap"), gmOptions);
		var gmarker;
		google.maps.event.addListenerOnce(gmap, 'tilesloaded', function(){
			gmarker = new google.maps.Marker({
					map: gmap, 
					position: coords
			});
		});

	});

	//alert("hej");
	//alert($('a.youtubin').parent().innerWidth());
	$('a.youtubin').each(function(){
		var thumb = new Image();
		$(thumb).attr({
			src: $.jYoutube($(this).attr("href"), 'big')
		});
		var thumbplay = new Image();
		$(thumbplay).attr({
			src: "/_assets/img/play_overlay.png",
			"class": "play_overlay"
		});
		
		$(this).html(thumbplay).append(thumb).fancybox({
			overlayShow: true,
			href: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'), 
			type: 'swf',
			width:640,
			height:360,
			swf: { 
				'wmode': 'opaque', 
				'allowfullscreen': 'true' 
			}, 
			centerOnScroll: true,
			overlayColor: '#000'
		});

	});
	$("A.g-translate").fancybox({
		type: 'iframe',
		width: '95%',
		height: '95%',
		centerOnScroll: true,
		overlayColor: '#000'
	});
	//confirm age
	if($.cookie("confirmage")!='confirmed'){
		var confirmText = '<div id="confirm"><h1>Bekr&auml;fta din &aring;lder</h1>'+
						'<b>V&auml;lkommen till WineWorks! Denna sida inneh&aring;ller information<br /> om alkoholhaltiga drycker och riktar sig till dig som fyllt 25 &aring;r. </b>'+
						'<center><h4><a href="#" class="age-confirm">Jag &auml;r 25 &aring;r eller &auml;ldre och f&aring;r d&auml;rf&ouml;r ta del av informationen p&aring; WineWorks.se</a></h4></center></div>';
		confirmbox = $.fancybox({
			content: confirmText,
			modal: true,
			width:300,
			height:200,
			centerOnScroll: true,
			overlayColor: '#000'
		});
		$("A.age-confirm").click(function(){
			//set cookie
			$.cookie("confirmage", "confirmed", { path: '/' });
			$.fancybox.close();
			return false;
		});
	}
	
/*
	$('a.youtubin').each(function(){
		w = $(this).parent().innerWidth();
		//alert(w);
		$(this).youtubin({
			swfWidth : w
		});
	});
*/

	$(window).resize(kdm.adjust_body);
	kdm.adjust_body();
});
var kdm = {};
kdm.adjust_body = function(){
	//resize to fit screen height
	//avail content height
	winH = $(window).height();
	conH = $("#CONTENT").outerHeight();
	avaH = winH - ($("#HEADER").outerHeight()+$("#FOOTER").outerHeight());
	
	if(avaH > conH){
		$("#CONTENT").css("min-height",avaH);
	}
	/*
	$("#BODY").height($(window).height());
	avail = $(window).height() - ($("#HEADER").outerHeight()+$("#FOOTER").outerHeight());
	if(avail > $("#CONTENT").outerHeight()){
		$("#CONTENT").height(avail);
	}
	*/
};




function g_text_tocoord(strloc){
	strarr = strloc.split(';');
	return strarr.length == 2 ? new google.maps.LatLng(strarr[0], strarr[1]): new google.maps.LatLng(-34.397, 150.644);
}

