var defaultsearchstring="Producer or product...";
var defaultyearsearchstring="Year (e.g. 1965)";
var defaultemailsignupstring="Your email address";
var storeid=1;

function loopPromo(){
	var getnext = $("#promotabs .promotabactive").next();
	var current = $("#promotabs .promotabactive");
	if (current.is(':last-child')) { var getnext = $(".promotab:first"); }
	nextid=getnext.attr('id');
	current.removeClass('promotabactive');
	getnext.addClass('promotabactive');
	$('.promopanel').hide();
	$('#promo'+nextid).show();
}

function showNewsletterDialog(evilpopup) {
	$("#newslettertabdiv").hide();

	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  
	$('#mask2').css({'width':maskWidth,'height':maskHeight});  
	$('#mask2').fadeTo("fast",0.5);    
  
	var winH = $(window).height();  
	var winW = $(window).width();  
			
	$('#newslettersignup').css('top',  winH/2-$('#newslettersignup').height()/2);  
	$('#newslettersignup').css('left', winW/2-$('#newslettersignup').width()/2);  
	$('#newslettersignup').css('position', 'fixed');
	if (evilpopup) {
		$("#newsletterformsource").val('Popup')
		$(".tabpopup").hide();
		$(".autopopup").show();
	} else {
		$("#newsletterformsource").val('Signup Form')
		$(".autopopup").hide();
		$(".tabpopup").show();
	}
  	$('#newslettersignup').fadeIn(300);   
	$('#newsletterformemail').focus();
}
function hideNewsletterDialog() {
	$('#mask2, #newslettersignup').hide();
	$("#newslettertabdiv").show('slide');
	hidecookie=readCookie('hidenewsletterpopup');
	if (hidecookie!='1') {
		createCookie('hidenewsletterpopup','1',1);
	}
}
function showGauntleyDialog(dialogcontent) {
	var maskHeight = $(document).height();  
	var maskWidth = $(window).width();  
	$('#mask2').css({'width':maskWidth,'height':maskHeight});  
	$('#mask2').fadeTo("fast",0.5);    
  
	var winH = $(window).height();  
	var winW = $(window).width();  
			
	$('#gauntleydialog').css('top',  winH/2-$('#gauntleydialog').height()/2);  
	$('#gauntleydialog').css('left', winW/2-$('#gauntleydialog').width()/2);  
	$('#gauntleydialog').css('position', 'fixed');
	$('#gauntleydialogcontents').html(dialogcontent);
  	$('#gauntleydialog').fadeIn(300);   
}
function hideGauntleyDialog() {
	$('#mask2, #gauntleydialog').hide();
}
	

$().ready(function() {
	//$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	//$("#featured").tabs({opacity: 'toggle', height: 'toggle', duration: 'fast'}).tabs("rotate",5000,true);
	//$("#featured").tabs({event: "mouseover"}).tabs("rotate",5000,false);

	//Escape key pressed
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$('#mask, .window').hide();
			hideNewsletterDialog();
			hideGauntleyDialog();
		}
	});
    //Mask is clicked  
    $('#mask').click(function () {  
		$(this).hide();
		$('.window').hide();
    });        
    //Mask2 is clicked  
    $('#mask2').click(function () {  
		$(this).hide();
		hideNewsletterDialog();
		hideGauntleyDialog();
    });        
	
	$('#basketvoucherlink').click(function(e) {
		$('#basketvoucherlinkwrapper').hide();
		$('#basketvoucherform').show();
		$('#voucher').focus();
        e.preventDefault();  
	});
	
	//*******************************************************************************
	//Crossfade quotes on home page
	$('#quotes').cycle({
		timeout: 8000,
        speed: 1000
	});

	
	//*******************************************************************************
	//Product Page (item.php) Info Box
	$('.infoboxtab').mouseover(function () {
		$(this).css('cursor', 'pointer');
	});
	$('.infoboxtab').click(function() {
		id=$(this).attr('id');
		$('.productinfoboxpanel').hide();
		$('#panel'+id).show();
		$('.infoboxtab').removeClass('tabactive');
		$(this).addClass('tabactive');
	});


	//*******************************************************************************
	//Contact Form
	$('#enquiryform').submit(function() {
		//event.preventDefault();
		var email=$("#email").val();
		var firstname=$("#firstname").val();
		var lastname=$("#lastname").val();
		var phone=$("#phone").val();
		var message=$("#message").val();
		var source=$("#source").val();
		var productid=$("#productid").val();
		var productname=$("#productname").val();
		var producturl=$("#producturl").val();
		if ($('#subscribe').is(':checked')) {
			var subscribe='1';
		} else {
			var subscribe='0';
		}
		$('#sendbutton').attr('src','http://cdn.gauntleys.com/images/ajax-loader-indicator.gif');
		
		$.ajax({
			type: "POST",
			cache: false,
			url: "_contact.ajax.php",
			data: "email="+email+"&source="+source+"&firstname="+firstname+"&lastname="+lastname+"&phone="+phone+"&message="+message+"&productid="+productid+"&productname="+productname+"&producturl="+producturl+"&subscribe="+subscribe,
			dataType: "json",
			success: function(jsonresponse) {
				$('#sendbutton').attr('src','http://cdn.gauntleys.com/images/btn_send.png');
				msg=jsonresponse.message;
				$("#enquirymessage").hide();
				$("#enquirymessage").html(msg);
				$("#enquirymessage").fadeIn('fast');
				if (jsonresponse.success) {
					//Clear the message and the form
					$('#firstname, #lastname, #email, #phone, #message').val('');
					//showGauntleyDialog('Thank you');
				} else {

				}
			}
		 });
		return false;
	});



	//*******************************************************************************
	//Newsletter signup dialog box
	/*
	$("#newslettersignup").dialog({
		modal: true,
		resizable: false,
		width: 600,
		height: 400,
	});
	$("#newsletterformemail").focus();
	$("#nothanks").click( function() {
		$("#newslettersignup").dialog( "close" );
	});
	*/
	$("#newslettertabimg").mouseenter(function() {
		$(this).attr("src", "http://cdn.gauntleys.com/images/newslettertab_on.png");
	});
	$("#newslettertabimg").mouseleave(function() {
		$(this).attr("src", "http://cdn.gauntleys.com/images/newslettertab_off.png");
	});
	$("#newslettertabdiv, .showNewsletterForm").click(function(e) {
		e.preventDefault();
		$('#newslettertabdiv').hide();
		showNewsletterDialog(false);
	});
	$("#newslettersignupform").submit(function(event) {
		event.preventDefault();
		var email=$("#newsletterformemail").val();
		var fname=$("#newsletterformfirstname").val();
		var lname=$("#newsletterformlastname").val();
		var source=$("#newsletterformsource").val();
		var optionwine=$("#newsletterformoptionwine:checked").val();
		var optionwhisky=$("#newsletterformoptionwhisky:checked").val();
		var optioncigars=$("#newsletterformoptioncigars:checked").val();
		$.ajax({
			type: "POST",
			cache: false,
			url: "_newsletter.ajax.php",
			data: "email="+email+"&source="+source+"&firstname="+fname+"&lastname="+lname+"&optionwine="+optionwine+"&optionwhisky="+optionwhisky+"&optioncigars="+optioncigars,
			success: function(msg){
				msg=jQuery.trim(msg);
				if (msg=='Success') {
					createCookie('hidenewsletterpopup','1',99999);
					$("#newsletterholder").html('<div id="newsletterformthankyou">Thank you</div><div id="newsletterformthankyou2"><p><strong>Almost finished...</strong></p><p>We just need to confirm your email address.<p><p>To complete the subscription process, please click the link in the email we just sent you.</p></div>');
					$("#closebutton").show();
				} else {
					$("#newslettersignupmessage").hide();
					$("#newslettersignupmessage").html(msg);
					$("#newslettersignupmessage").fadeIn('fast');
				}
			}
		 });
	});
	$("#newslettersignupform #nothanks").click(function (e) {  
        e.preventDefault();  
		createCookie('hidenewsletterpopup','1',99999);
        $('#mask2, #newslettersignup').hide();
		$("#newslettertabdiv").show('slide');
    });       
	$("#newslettersignupform #remindmelater").click(function (e) {  
        e.preventDefault();  
		createCookie('hidenewsletterpopup','1',1);
        $('#mask2, #newslettersignup').hide(); 
		$("#newslettertabdiv").show('slide');
    });       
	$("#closewindow, #closebutton").click(function (e) {  
		e.preventDefault();
		hideNewsletterDialog();
		hideGauntleyDialog();
    });       
	//for debugging...
	$('#resethidenewsletterpopupcookie').click(function(e) {
        e.preventDefault();  
		eraseCookie('hidenewsletterpopup');
		alert('Evil popup cookie reset');
	});
	$('#resetsession').click(function(e) {
        e.preventDefault();  
		eraseCookie('PHPSESSID');
		eraseCookie('session-id');
		eraseCookie('hidenewsletterpopup');
		alert('PHP Session killed');
	});


	//*******************************************************************************
	//Promo panel - replaces 'featured' panel
	$('.promotab:first').addClass('promotabactive');
	$('#promopanels div:first-child').show();

	/*
	var promotimerid=setInterval ('loopPromo()', 10000);
	$('#promo').mouseover(function(){
		clearInterval(promotimerid);
	});
	$('#promo').mouseout(function(){
		promotimerid=setInterval ("loopPromo()", 10000);
	});
	*/
	
	$(".promotab").mouseover(function(){
		id=this.id;

		$(".promotab").removeClass('promotabactive');
		$(this).addClass('promotabactive');

		$('.promopanel').hide();
		$('#promo'+id).show();
	});


	//*******************************************************************************
	//Mixed Case Popup Info
    $('a[name=modal]').click(function(e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        //Get the A tag  
        var id = $(this).attr('href');  
      
        //Get the screen height and width  
        var maskHeight = $(document).height();  
        var maskWidth = $(window).width();  
      
        //Set height and width to mask to fill up the whole screen  
        $('#mask').css({'width':maskWidth,'height':maskHeight});  
          
        //transition effect       
        //$('#mask').fadeIn(1000);      
        $('#mask').fadeTo("fast",0.5);    
      
        //Get the window height and width  
        var winH = $(window).height();  
        var winW = $(window).width();  
                
        //Set the popup window to center  
        $(id).css('top',  winH/2-$(id).height()/2);  
        $(id).css('left', winW/2-$(id).width()/2);  
      
        //transition effect  
        $(id).fadeIn(300);   
    });  
      
    //if close button is clicked  
    $('.window .close').click(function (e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        $('#mask, .window').hide();  
    });       
      	
	//*******************************************************************************
	//Help Popups
	$('.helplink').click(function(e) {
        e.preventDefault();  
		var page = $(this).attr('id');
        var id = $(this).attr('href');  
        var maskHeight = $(document).height();  
        var maskWidth = $(window).width();  
        $('#mask').css({'width':maskWidth,'height':maskHeight});  
        $('#mask').fadeTo("fast",0.5);    
        var winH = $(window).height();  
        var winW = $(window).width();  
        $(id).css('top',  winH/2-$(id).height()/2);  
        $(id).css('left', winW/2-$(id).width()/2);  
        $(id).fadeIn(300);

		//Get content
		$.get('/_help.ajax.php?page='+page, function(data) {
			$('#helpcontent').html(data);
		});
	});
	$("#closehelpwindow, #closehelpbutton").click(function (e) {
        e.preventDefault();  
        $('#mask, .window').hide();  
	});

	//*******************************************************************************
	//Footer Newsletter Signup
	newsletteremail=$("#footernewslettersignupinput").attr("value");
	if(newsletteremail=="") {
		$("#footernewslettersignupinput").attr("value", defaultemailsignupstring);
	} else {
		$("#footernewslettersignupinput").css("color", "black");
	}
	$("#footernewslettersignupinput").focus(function() {
		newsletteremail=$("#footernewslettersignupinput").attr("value");
		if(newsletteremail==defaultemailsignupstring) {
			$("#footernewslettersignupinput").attr("value", "");
		}
		$("#footernewslettersignupinput").css("color", "black");
	});
	$("#footernewslettersignupinput").blur(function() {
		newsletteremail=$("#footernewslettersignupinput").attr("value");
		if(newsletteremail=="") {
			$("#footernewslettersignupinput").attr("value", defaultemailsignupstring);
			$("#footernewslettersignupinput").css("color", "#999");
		}
	});


	//*******************************************************************************
	//Promo panel newsletter Signup
	newsletteremail2=$("#promonewslettersignupinput").attr("value");
	if(newsletteremail2=="") {
		$("#promonewslettersignupinput").attr("value", defaultemailsignupstring);
	} else {
		$("#promonewslettersignupinput").css("color", "black");
	}
	$("#promonewslettersignupinput").focus(function() {
		newsletteremail2=$("#promonewslettersignupinput").attr("value");
		if(newsletteremail2==defaultemailsignupstring) {
			$("#promonewslettersignupinput").attr("value", "");
		}
		$("#promonewslettersignupinput").css("color", "black");
	});
	$("#promonewslettersignupinput").blur(function() {
		newsletteremail2=$("#promonewslettersignupinput").attr("value");
		if(newsletteremail2=="") {
			$("#promonewslettersignupinput").attr("value", defaultemailsignupstring);
			$("#promonewslettersignupinput").css("color", "#999");
		}
	});
	

	//*******************************************************************************
	//Search
	$("#searchquery").autocomplete("/_searchautocomplete.php?storeid="+storeid, {
		minChars: 1,
		max: 20,
		autoFill: false,
		mustMatch: false,
		width: 166,
		selectFirst: false,
		highlight: false,
		scroll: false
	});

	query=$("#searchquery").attr("value");
	if(query=="") {
		$("#searchquery").attr("value", defaultsearchstring);
	} else {
		$("#searchquery").css("color", "black");
	}
	$("#searchquery").focus(function() {
		query=$("#searchquery").attr("value");
		if(query==defaultsearchstring) {
			$("#searchquery").attr("value", "");
		}
		$("#searchquery").css("color", "black");
	});
	$("#searchquery").blur(function() {
		query=$("#searchquery").attr("value");
		if(query=="") {
			$("#searchquery").attr("value", defaultsearchstring);
			$("#searchquery").css("color", "#999");
		}
	});
	

	//*******************************************************************************
	//Anniversary/Year Search
	query=$("#yearquery").attr("value");
	if(query=="") {
		$("#yearquery").attr("value", defaultyearsearchstring);
	} else {
		$("#yearquery").css("color", "black");
	}
	$("#yearquery").focus(function() {
		query=$("#yearquery").attr("value");
		if(query==defaultyearsearchstring) {
			$("#yearquery").attr("value", "");
		}
		$("#yearquery").css("color", "black");
	});
	$("#yearquery").blur(function() {
		query=$("#yearquery").attr("value");
		if(query=="") {
			$("#yearquery").attr("value", defaultyearsearchstring);
			$("#yearquery").css("color", "#999");
		}
	});


	//*******************************************************************************
	// Navigation
	$("#navigation ul li").mouseenter(function() {
		$(".tn", this).addClass("activenav");
		$(".tn", this).removeClass("topnav");
		$(".bigmenu", this).show();
	});
	$("#navigation ul li").mouseleave(function() {
		$(".tn", this).addClass("topnav");
		$(".tn", this).removeClass("activenav");
		$(".bigmenu", this).hide();
	});
	/*
	$("#navigation ul li").hoverIntent({ 
		sensitivity: 20, 
		interval: 50,
		timeout: 50,
		over: function () {
			$(".tn", this).addClass("activenav");
			$(".tn", this).removeClass("topnav");
			$(".bigmenu", this).show();
		}, 
		out: function () {
			$(".tn", this).addClass("topnav");
			$(".tn", this).removeClass("activenav");
			$(".bigmenu", this).hide();
		}
	});
	*/
	

	//*******************************************************************************
	//Rotator
	$("#rotator #rotatortabs div").click(function() {
		$("#rotator #rotatortabs div").removeClass("rotatoractivetab");
		$(this).addClass("rotatoractivetab")
	});
	

	//*******************************************************************************
	//Search page - left column - show more filter options
	$(".morediv").click(function() {
		$(this).hide();
		$(this).parent("div").children("ul").children(".morefilters").show("fast");
	});
	

	//*******************************************************************************
	//Search page - change display order
	$(".order").change(function() {
		var neworder=$(this).val();
		var newurl='/search.php?' + basequerystring + '&o=' + neworder;
		window.location.href=newurl;
	});


	//*******************************************************************************
	$("a#productimage").fancybox({
		'overlayShow': true,
		'overlayOpacity': 0.5,
		'overlayColor': '#000',
		'enableEscapeButton': true,
		'showCloseButton': true
	});
});


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
	//alert(name+"="+value+expires+"; path=/");
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


