// jquery hover intent plugin
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

$(document).ready(function(){

	$('#country-selector').val($('option:first', this).val());

	Date.firstDayOfWeek=0;
	Date.format = 'yyyy-mm-dd';	
	$('.date-pick').datePicker({
		startDate  :'2005/01/01',
		clickInput :true
	});
	
	$('#country-selector').chainSelect('#city','/ajax/store_finder.inc.php',{ 
		before:function (target){
			$("#ajax-loading").css("display","block");
			$('#city-selector').css("display","none");
		},
		after:function (target){ 
			$("#ajax-loading").css("display","none");
			$('#city-selector').css("display","block");
		}
	});
	
	$('a').click(function(){
		$(this).addClass('active-link');
		return true;
	});
	
	$('#tabs').tabs();
	$('#ingredient-tabs').tabs({ idPrefix: 'ui-tabs-primary' });
	
	$('#press_id').change(function(){
		$('#press_form').submit();
	});
	
	$('.pageSelector').change(function(){
		$('#search_form').submit();
	});

	$('.sortOrderSelector').change(function(){
		$('#sortForm').submit();
	});

	/*	
	$('#ingredients-selector').change(function() {
		var ingedient_id = $(this).attr("value");
		if(ingedient_id){
			$.post("/includes/ajax-ingredient.php", { 
				ingedient_id: ingedient_id
			}, function(data) {
				$("#ajax-ingredient").css('display','block');
				$("#ajax-ingredient").html(data);
				$('#ingredient-ajax-hide').click( function() {
					$("#ajax-ingredient").hide();
					return false;
				});
			});
		}
		else{
			$("#ajax-ingredient").hide();
			return false;
		}
	});
	
	$('.ingredient-thumb, .ingredient, .ingredient-link,').click(function() {
		var ingedient_id = $(this).attr("rel");
		$.post("/includes/ajax-ingredient.php", { 
			ingedient_id: ingedient_id
		}, function(data) {
			$("#ajax-ingredient").css('display','block');
			$("#ajax-ingredient").html(data);
			$('#ingredient-ajax-hide').click( function() {
				$("#ajax-ingredient").hide();
				return false;
			});
			$('html,body').animate({scrollTop : 0}, 10);
		});
		return false;
	});
	*/
	
	$('#faq .toggle').click(function() {
		//$(this).find('.viewanswer').toggle();
		$(this).next().slideToggle('normal');
	});
	$('.closefaq').click(function() {
		//$(this).parent().prev().find('.viewanswer').toggle();
		$(this).parent().slideToggle('normal');
	});
	
	$('#treatment .toggle').click(function() {
		$(this).next().slideToggle('normal', function(){
			$(this).prev().find('.viewtreatment').toggle();
		});
	});
	$('.closetreatment').click(function() {
		$(this).parent().slideToggle('normal', function(){
			$(this).prev().find('.viewtreatment').toggle();
		});
	});

	var basketConfig = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: showBasket, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: hideBasket // function = onMouseOut callback (REQUIRED)    
	};
	$("#minibasket-summary").hoverIntent(basketConfig);
			
	function showBasket(){$('#minibasket-detail').show(); }
	function hideBasket(){ 
		$('#minibasket-detail').mouseleave(function() {
			setTimeout(function() { $('#minibasket-detail').hide(); }, 50);
		});
	}
	
	$('img.imgswap').hover(function() {
		$(this).attr("src",$(this).attr("src").replace('/Off/','/On/'));
	}, function() {
		$(this).attr("src",$(this).attr("src").replace('/On/','/Off/'));
	});

    // deluxe banner on thank_you page )for deluxe promotion)
    $('#deluxe_banner_link').hover(function() {
		$('#deluxe_banner_image').attr("src",$('#deluxe_banner_image').attr("src").replace('_off','_on'));
	}, function() {
		$('#deluxe_banner_image').attr("src",$('#deluxe_banner_image').attr("src").replace('_on','_off'));
    });

//GiftWarp checkbox
    $("")
});


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
