$(document).ready(function(){

$(document).pngFix(); 



var $masthead_height = $('#masthead').height();
$('#masthead li dd').hide();
$('#masthead li ul li').hide();

// Function to expand the masthead
function expandMenu(){
	$('#masthead li dd').css({display:'block',width:1,opacity:0}).animate({width:150}, 'fast').animate({opacity:1}, 'fast');
	$('#masthead li ul li').css({display:'block',width:1,opacity:0}).animate({width:150}, 'fast').animate({opacity:1}, 'fast');
	$('#masthead').animate({ height: 480},'fast');
}

// Function to contract the masthead
function contractMenu(){
	$('#masthead li dd').animate({opacity:0}, 'fast');
	$('#masthead li dd').animate({width:0}, 'fast',function() { $(this).hide()});
	$('#masthead li ul li').animate({opacity:0}, 'fast').animate({width:0}, 'fast');
	$('#masthead').animate({ height: $masthead_height},'fast');
}

// Function to delay masthead expansion
$('#masthead').hoverIntent({
	     over: expandMenu,
	     timeout: 500,
	     out: contractMenu
});


// Expand hit target on product grid
if ($(".product-grid li").length > 0){
	 $(".product-grid li  a").bigTarget({
    	hoverClass: 'active', 
	    clickZone : 'li:eq(0)' 
	    });
}

$(".external").attr("target","_blank");

// Front page & index layout
$('#frontpage .product-grid,#suggests .product-grid').masonry({
    columnWidth: 240,
   	itemSelector: 'li'
});

// FancyZoom setup
if ($('#thumbnails').length >0){
	$("#thumbnails a").fancybox({
		"padding":	"0",
		"overlayOpacity":	"0.8",
		"overlayColor": "#000000"
	});
}

// Flipbook thumbnail effect
if($('.flip').length > 0 ) {
	$('.flip').hover(function () {
	var numberOfImages = $(this).find('img').length;
	var dividers = $(this).width() / numberOfImages;
	$(this).mousemove(function(e){
		var offset = $(this).offset();
		var flipX = offset.left;
		var imgToShow = Math.floor((e.pageX- flipX)/dividers);
		$(this).find('img').hide();
		$(this).find('img').eq(imgToShow).show();
	})
},function () {});

$('.flip').mousewheel(function(event, delta) {
	var imgToShow = Math.floor(Math.abs(delta));
	$(this).find('img').hide();
	$(this).find('img').eq(imgToShow).show();
	});
}

// In field labels
$('#discount label').inFieldLabels();
$('#login label,#mailinglist label').inFieldLabels();
$('#respond label').inFieldLabels();

if(!Modernizr.input.placeholder){
	$("#search input").each(
		function(){
			if($(this).val()=="" && $(this).attr("placeholder")!=""){
				$(this).val($(this).attr("placeholder"));
					$(this).focus(function(){
						if($(this).val()==$(this).attr("placeholder")) $(this).val("");
					});
					$(this).blur(function(){
						if($(this).val()=="") $(this).val($(this).attr("placeholder"));
					});
				}
		});
	}

// Search results
$(".results li a").bigTarget({
   	hoverClass: 'hover', 
    clickZone : 'li:eq(0)' 
});

$("#live-results li a").bigTarget({
	hoverClass: 'hover', 
	clickZone : 'li:eq(0)'
	});

// Customer details 
$('#shipping-details input[type="checkbox"]').change(function(){
	if ($(this).is(":checked")){
		$('#shipping-details ol').slideUp();
		}
	else {
		$('#shipping-details ol').slideDown();
	}
})
$('.gift-options label').click(function(){
	$(this).find('input').attr("checked", true);
	var $container = $(this).closest(".gift-options");
	$container.find('li').removeClass('selected');
	$(this).closest('li').addClass('selected').find('img').fadeTo("fast",1);
	$container.find('li:not(.selected) img').fadeTo("slow",.3);
})

if ($('#blog').length > 0) {
	var tgt = location.hash;
	var offset = 0 - $("#masthead").height();
    $.smoothScroll({scrollTarget: tgt, offset: offset});
}

})
