var mainMenus={
	animateDuration: {over: 150, out: 100}, //duration of slide in/ out animation, in milliseconds
	buildMenus:function(){
	function show() {
		var menu = $(this);
		$(this).addClass("active");
		menu.children("ul").css({display:'block'});
		var $siblings=$(this).siblings();
		$siblings.each(function(i){
			$(this).removeClass("active");
			$(this).children("ul").css({display:'none'});
		}) //end $siblings.each()
	}
	function hide() { 
		var menu = $(this);
		$(this).removeClass("active");
		menu.children("ul").css({display:'none'});
	}
		var $mainMenu=$(".main_menu")
		var $headers=$mainMenu.find("ul").parent()
		$headers.each(function(i){
			$(this).addClass("has_submenu");
	$(this).hoverIntent({
		interval: 0, // milliseconds delay before onMouseOver
		over: show, 
		timeout: 0, // milliseconds delay before onMouseOut
		out: hide
	});
		}) //end $headers.each()
		$mainMenu.find("ul").css({display:'none', visibility:'visible'})
	} //end buildMenus:function()
} //end var mainMenus

//if the document is ready, apply all functions
$(document).ready(function() {
	//set up animations for drop down menus
	mainMenus.buildMenus();
	//loop through all links and set external ones to target="_blank"
	$('a[rel*=external]').attr("target","_blank");
	$('a[rel*=external]').attr("title","(opens in new window)");
	$('a[rel*=nofollow]').attr("target","_blank");
	$('a[rel*=nofollow]').attr("title","(opens in new window)");
	//$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
	//$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("title","(opens in new window)");
	//set items per page form autosubmission
	$("#items_per_page").bind("change", function() { this.form.submit(); });
	//show/hide text in postcode search in header
	$("#nearest_store_postcode").attr("value","nearest store");
	$("#nearest_store_postcode").click( function() { if ($(this).attr("value")=="nearest store") { $(this).attr("value",""); } });
	$("#nearest_store_postcode").blur( function() { if ($(this).attr("value")=="") { $(this).attr("value","nearest store"); } });
	//show/hide text in product search in header
	$("#search_products_keywords").attr("value","search products");
	$("#search_products_keywords").click( function() { if ($(this).attr("value")=="search products") { $(this).attr("value",""); } });
	$("#search_products_keywords").blur( function() { if ($(this).attr("value")=="") { $(this).attr("value","search products"); } });
	//apply lightbox to links
	$('a[rel*=lightbox]').lightBox();
	//autosubmission for shopping basket updates
	$("table#view_basket select[name*=quantity]").bind("change", function() { this.form.submit(); }); //change quantity
	$("input[name*=add_homeserve]").click( function() { this.form.submit(); }); //add homeserve
	//$("select[name*=delivery_option]").bind("change", function() { this.form.submit(); }); //change delivery option
	$("input[name*=delivery_option]").click( function() { this.form.submit(); }); //change delivery option
	//checkout form show/hide delivery details
	$("#delivery_same_as_billing").click( function() { if ($(this).attr('checked')) { $("#checkout_delivery_details").slideUp(200); } else { $("#checkout_delivery_details").slideDown(200); } });
    if ($("#delivery_same_as_billing").attr('checked')) { $("#checkout_delivery_details").css({display:'none', visibility:'visible'}); }

	//products
	if ($('#delivery_details').length) {
		$('#delivery_details').hide();
		$('#payment_options').hide();
		$('#product_details').before('<ul id="product_tabs"><li><a href="#" id="product_description_tab">Description</a></li><li><a href="#" id="delivery_details_tab">Delivery Details</a></li><li><a href="#" id="payment_options_tab">Payment Options</a></li></ul>');
		$('#product_description_tab').click(function() {
			$('#product_details').show();
			$('#delivery_details').hide();
			$('#payment_options').hide();
			return false;
		});
		$('#delivery_details_tab').click(function() {
			$('#product_details').hide();
			$('#delivery_details').show();
			$('#payment_options').hide();
			return false;
		});
		$('#payment_options_tab').click(function() {
			$('#product_details').hide();
			$('#delivery_details').hide();
			$('#payment_options').show();
			return false;
		});
	} //end if ($('#technical_description').exists())

	//apply the animation to the homepage top advert panel
	if ($('#homepage_advert').length) {
		$('#homepage_advert').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout:  10000, 
			//pause:  1
		});
	} //end if ($('#homepage_offers').exists())
});
