/* (c) Copyright 2008-9 Together London Limited. http://togetherlondon.com */

$(function(){
	$("body").addClass("scripted");
	
	if($("ul.prints").length)
	{
		$("ul.prints li").hover(glowOver, glowOut);
		parentHover($("ul.prints li a"));
		preloadImage("/images/glow.png");
	}
	
	if($("ul.thumbnails").length)
	{
		$("ul.thumbnails li:first").addClass("active");
		$("ul.thumbnails a").click(function(){
			if($(this).parent().hasClass("active"))
				return false;
			vHref = this.href;
			$("#fullPrint").fadeOut("fast", function(){
				$(this).attr("src", vHref).fadeIn("def");
			});
			$("ul.thumbnails li.active").removeClass("active");
			$(this).parent().addClass("active").mouseout();

			if($.browser.msie && $.browser.version <= 7) // until they support "outline"
				$(this).blur();

			return false;
		}).each(function(){
			preloadImage(this.href);
		});
		
		$("ul.thumbnails li, #contextual li.alternate").hover(glowOver, glowOut);
		parentHover($("ul.thumbnails li a, #contextual li.alternate a"));	
		preloadImage("/images/thumbnailGlow.png");		
		
		vInput = $("#submit");
		vBasketButton = $("<a><span>Add to basket</span></a>").addClass("submit").attr(
			{title: vInput.attr("title"), href:"#"}).
			click(function(){
				$("#paypal").submit();
				return false;
		});
		vInput.replaceWith(vBasketButton);
		
		vForm = $("form#paypal");
		vForm.submit(function(){
			vSelect = $(this).find("select#shipping");
			if(!vSelect.serializeArray()[0].value) 
			{
				alert("Please select a delivery destination, so we can set the charge correctly. Thanks!")
				vSelect.focus();
				return false;
			}
			// set cookie?
			
			return true;
		});
		vForm.find("select#shipping").change(function(){
			vValue = $(this).find("option:selected").text().split(" ")[0];
			if(vValue.match(/choose/i))
				return;
			$.post("/setShipping", {shipping:vValue});
		});
	}
	
});

function glowOver()
{
	if($(this).hasClass("active"))
		return false;
	vDiv = $("<div></div>").addClass("glow");			
	$(this).prepend(vDiv);
	vDiv.fadeIn(100);	
}

function glowOut()
{
	$(this).find("div.glow").fadeOut(400, function(){
		$(this).remove();
	});	
}

function parentHover(pQuery) // enable li:focus
{
	pQuery.focus(function(){
		$(this).parent().mouseover();		
	}).blur(function(){
		$(this).parent().mouseout();					
	});
}

function preloadImage(pURL)
{
	vImage = $(document.createElement("img")).attr("src", pURL);
}
