$(document).ready(function(){
	
	if ( $("body").hasClass("home") ) {
	
	$("#products .nav a").bind("click.select",function(){
		var offset = $("#products ul a").index(this) * (960 + 200); // width of .product + margin
		$("#products .active").removeClass("active");
		$(this).addClass("active");
		$("#products .prod-wrap").animate({
			left: -offset
		},1200,"swing");
		$(this).trigger("pause");
		return false;
	});
	
	$("#products").append('<div class="prev pn-nav"><a href="#prev" title="Previous Product">Previous Product</a></div> <div class="next pn-nav"><a href="#next" title="Next Product">Next Product</a></div>');
	
	$("#products .pn-nav").bind("click.prevnext",function(){
		var curlink = parseInt($("#products .nav .active").parent().find("em").text());
		if ( $(this).hasClass("prev") ) {
			curlink = (curlink == 1) ? 5 : curlink-2;
			$("#products .nav a:eq("+curlink+")").click();
		}
		if ( $(this).hasClass("next") ) {
			curlink = (curlink == 6) ? 0 : curlink;
			$("#products .nav a:eq("+curlink+")").click();
		}
		$(this).trigger("pause");
		return false;
	});
	
	// setup
	$("#products ul a:first").click();
	
	// auto-click through
	var playing = true;
	$(".pn-nav, #products .nav a").bind("mouseup keyup",function(e){
		playing = false;
	});
	function stepProds(){
		if (playing) {
			$(this).click();
			$(this).animate({ borderWidth: 0 },8000,stepProds);
		}
	}
	$("#products .next").animate({ borderWidth: 0 },8000,stepProds);
	
	}
	
	
	// form field highlighting
	var previnput;
	$(".primary form li input, .primary form li select, .primary form li textarea").focus(function(){
		$(this).parents("li").addClass("active");
		previnput = $(this);
	}).blur(function(){
		if ( previnput.parents("li") !== $(this).parents("li") )
			$(this).parents("li").removeClass("active");
	});

	
	$('a[href^="http://"]').filter(function(){
		return this.hostname && this.hostname !== location.hostname; })
        .addClass("external")
        .attr({ title: "Link will open in new window" })
        .bind("click.new",function() {
            window.open(this.href);
            return false;
        });

	
	// DatePicker
	
	$.datepicker.setDefaults({
		showOn: 'both',
		buttonImageOnly: true, 
		buttonImage: '/utils/icons/fatcow/16x16_0180/calendar.png',
		buttonText: 'Calendar',
		dateFormat: 'dd/mm/yy',
		mandatory: true
	});
	
	var min = new Date();
	var minLead = $("#min-lead-time").val();
	min = min.setDate(min.getDate() + parseInt(minLead));
	$("form .date-field")
		.append('<input type="hidden" class="linkedDates" name="ld" value="" />');
	$(".linkedDates").datepicker({
		minDate: new Date(min),
		beforeShow: readLinked,
		onSelect: updateLinked
	});
	
	$("form .date-and-time")
		.append('<input type="hidden" class="customLinkedDates" name="ld" value="" />');
	$(".customLinkedDates").datepicker({
		beforeShow: readLinked,
		onSelect: updateLinked
	});
		
	// Prepare to show a date picker linked to three select controls
	function readLinked(input) {
		var e = $(input).parents("li");
		$(input).val(
			( parseInt($('.day',e).val()) > 9 ? $('.day',e).val() : "0" + $('.day',e).val() ) + '/' +
			( parseInt($('.month',e).val()) > 9 ? $('.month',e).val() : "0" + $('.month',e).val() ) + '/' +
			$('.year',e).val()
		);
		return {};
	}
		 
	// Update three select controls to match a date picker selection 
	function updateLinked() {
		var date = $(this).datepicker("getDate");
		var e = $(this).parents("li");
		$('.day',e).val(date.getDate());
		$('.month',e).val(date.getMonth()+1);
		$('.year',e).val(date.getFullYear());
	}
	
	$('.month, .year').change(checkLinkedDays).change(); 
		 
	// Prevent selection of invalid dates through the select controls 
	function checkLinkedDays() {
		var $current = $(this).parents("li");
		var daysInMonth = 32 - new Date($('.year',$current).val(), 
		$('.month',$current).val() - 1, 32).getDate(); 
		$('.day option',$current).attr('disabled', ''); 
	    $('.day option:gt(' + (daysInMonth - 1) +')',$current).attr('disabled', 'disabled'); 
	    if ($('.day',$current).val() > daysInMonth) { 
	        $('.day',$current).val(daysInMonth); 
	    }
		readLinked($(".linkedDates",$current));
	}
	
});