$(function(){
	/* add js class to body */
	$("body").addClass("js");
	
	/* add resize listener to window */
	$(window).bind("resize", resizeListener).bind("scroll", resizeListener).trigger("resize");
	
	/* light email protection */
	ml();
	
	/* product request email button */
	$("a.request").each(function(){
		console.log(this.href);
		this.href = this.href.replace("{apem}",ml(true));
	});
	
	/* fading background image */
	if ($("body").hasClass("ipad"))
		$("body").css("background-image","url("+$("#underlay img")[0].src+")").find("#underlay").hide();
	else if (!$.browser.msie)
		$("#underlay").each(function(i,el){
			var img = $(this).find("img").remove()[0];
			var newImg = $("<img/>").bind("load",function(){$(this).parent("div").fadeTo("slow",1);$(window).trigger("resize");}).attr("src",img.src);
			$(this).fadeTo(0,.001).append(newImg);
		});
	
	/* collection behaviour */
	if (!$.browser.msie)
		$(".collection li").css({opacity:.001}).delay($("body").hasClass("cash")?1000:1).each(function(i,el){
			var img = $(this).find("img");
			if (img.length && !$(this).hasClass("blank")) {
				var newImg = $("<img/>").bind("load",function(){$(this).parents("li").fadeTo(500,1)}).attr("src", img[0].src);
				img.remove();
				$(this).find("a").prepend(newImg);
			}
			else $(el).fadeTo(500,1);
		});
	
	/* polaroid click */
	$(".collection li a").removeClass($.browser.msie?"":"hover").hover(function(){
		if (!$.browser.msie) $(this).find(".overlay").fadeIn("fast");
	},function(){
		if (!$.browser.msie) $(this).find(".overlay").fadeOut("fast");
	}).bind("click",function(){
		if (!$("body").hasClass("iphone")) {
			$("body").append($("<div class='full-overlay loading'/>").hide().click(function(){$(this).fadeOut(200,function(){$(this).remove()})}).fadeIn(500));
			var href=$(this).attr("href");
			$.get(
				href,
				function (result) {
					var pola = $(result).find(".item-pola").find(".request").click(function(e){e.stopPropagation()}).end().hide();
					$(".full-overlay").removeClass("loading").append(pola.fadeIn(500));
				},
				"html"
			);
			return false;
		}
	});
	
	/* preload polaroid */
	if ($(".collection").length) {
		(function(){
			var pola = new Image;
			pola.src = "/assets/img/pola.png";
		})();
	}
	
	/* iphone/ipad tel numbers */
	if ($("body").hasClass("ipad") || $("body").hasClass("iphone")) {
		$('.tel .value:not(.nodial)').each(function(){
			var telNo = $(this).text().replace(/\(.*\)/,'').replace(/\s+/g,'');
			var wrapLink = '<a href="tel:'+telNo+'"></a>';
			$(this).wrap(wrapLink);
		});
	}
});

function resizeListener () {
	if (!$("body").hasClass("ipad")) {
		resizeUnderlay();
		$(".full-overlay").height($(document).height()-200);
		positionCard();
	}
}

function resizeUnderlay () {
	var img = $("#underlay img"),
	    imgRatio = img.width()/img.height(),
	    port = $("#underlay"),
	    portRatio = port.width()/port.height();
	    
	if (typeof origWidth == "undefined") origWidth = img.width();
	if (typeof origHeight == "undefined") origHeight = img.height();
	
	if (port.height() > origHeight || port.width() > origWidth) {
	    if (portRatio >= imgRatio) {
	    	img.width(port.width()).css({"height":"auto"});
	    }
	    else {
	    	img.height(port.height()).css({"width":"auto"});
	    }
	}
}

function positionCard () {
	var card = $(".vcard"), glnav = $("#glnav"), port = $(window);
	
	if (port.height() >= 80+glnav.height()+40+card.height()) {
		card.css({"top":port.height()-80-card.height()-glnav.height()-40});
	}
	else {
		card.css({"top":0});
	}
}

function ml (noOutput) {var d=window.location.host.split("."),a="info",a=a+"@"+d[d.length-2]+"."+d[d.length-1],h="mailto:"+a;if(noOutput)return a; else $(".email").html('<a href="'+h+'">'+a+'</a>')}
