$('html').addClass('hideJSContent');

function printpage() {
	window.print();
}
/* *******************************************************************
Run the hide functions first to try avoid seeing the hide on page load
******************************************************************* */
$(document).ready(function() {
    $('.userHintDiv' ).hide();
	$('.content-item ul:not(:first)').hide();
	$('.sitebrowse-item ul:not(:first)').hide();
	$('div.further-information').hide();	
	$('div.review div:not(".show")').hide();
	$('#signinExistingNotOnline').hide();
	$('#mycarousel li').show();
	$('.jcarousel-control').show();
	$('#prevoffers').show();
	$('#verify').show();
	$('.hideElementWithJS').hide();
	$('#deliveryInstructionMenu').removeClass("showElementWithJS");
});

/* ***********************************
Initialise various site-wide functions
*********************************** */
$(document).ready(function() {
    $('.jqzoom').jqzoom();		//Allows the image to zoom in onto a larger image - this appears to the right of the selected image
	$('ol li label').labelize('radio-label-hover');		//Allows labels to be 'hoverable' in IE
});


$(document).ready(function() {
//show hide on contact us form
function changecontactorderoptions() {
	if($("input[name='aboutorderradio']:checked").val() == 'yes'){
		$("input[name='returningCustomer']").attr('checked', 'checked');
			$('.contactaboutorder_yes').show();
			$('.contactaboutorder_no').hide();
		}
		else {
			$('.contactaboutorder_yes').hide();
			$('.contactaboutorder_no').show();
		}
}

changecontactorderoptions();


	$(".contact-order-relate").click(function(){
		changecontactorderoptions();	
	});
});
/* *******************
Other custom functions
******************* */



$(document).ready(function() {
		if ($(".quarter").length){
			$("#basket-page .threequarter").addClass("upsell-page");
}
	/* ***********************************************
	Make LIs hoverable in IE6 for site drop-down menus
	*********************************************** */
		$('li.sitenavTab').hoverIntent(function() {
			$(this).find('.sitenavDropDown').show();
			$(this).find('a.sitenavLink').css('backgroundPosition', '0 -25px');
		}, function() {
			$(this).find('.sitenavDropDown').hide();
			$(this).find('a.sitenavLink').css('backgroundPosition', '0 0');
		});
		$('ul.secondary-tabs li:not(.selected)').hover(function() {
			$(this).css('backgroundPosition', '0 0');
		}, function() {
			$(this).css('backgroundPosition', '0 -34px');
		});
	
	/* **************************
	STWC Homepage promo show/hide
	************************** */
        $('.homepage-right-promo').hoverIntent(function(){
			$(this).stop().children('.homepage-reveal').animate({bottom:'-1px'}, 400);
		},function(){
			$(this).stop().children('.homepage-reveal').animate({bottom:'-38px'}, 200);
		});

		var tabContainers = $('div.homepage-tabbed-promo > div');
		tabContainers.hide().filter(':first').show();
		
		$('.homepage-tabbed-promo').css({'height':'160px'});
		$('#prevoffers').removeClass('prevoffersDivide');
		$('div.homepage-tabbed-promo ul.homepage-tabbed-nav a').click(function () {
		
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			
			$('div.homepage-tabbed-promo ul.homepage-tabbed-nav a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		})
		.filter(':first').click();
		
	
	/* **********************************
	Various Form 'prettifying' functions!
	********************************** */
		//On page load attach class of bold to the checked item in an orderform

		$('ol.information input:checked').parent().find('label:first').addClass('bold').find('div.formreveal').show();
		$('ol.information input:checked').parent().find('div.formreveal').show();
		
		$('ol.orderform input:radio').click(function () {
			$(this).parents('ol.orderform').find('div.formreveal:visible').slideUp();
		
            $(this).parent().find('div.formreveal').slideDown();
		});
		
		$('ol.information input:radio').click(function () {
			$(this).parents('ol.information').find('div.formreveal:visible').slideUp();
			//$('label').removeClass('bold');
			//$(this).parent().find('label:first').addClass('bold');
			$(this).parent().find('div.formreveal').slideDown();
		});
		
		$('input:checkbox').click(function() {
			if ($(this).attr('checked') === true) {
				$(this).parent().find('.formreveal').slideDown();
				//$(this).parent().addClass('bold');
			} else {
				$(this).parent().find('.formreveal').slideUp();
				//$(this).parent().removeClass('bold');
			}
		});
		
		$('input:checkbox#addToAllCases').click(function() {
			if ($(this).attr('checked') === true) {
				$('.caseMessage').not(':first').fadeOut();
			} else {
				$('.caseMessage').not(':first').fadeIn();
			}
		});
		
		$(".hint").focus(function() {
			$(this).parent().addClass('userHint');
		}).blur(function() {
			$(this).parent().removeClass('userHint');
		});
		
		//Form field focus - auto remove text from input fields on focus
		$(".fieldfocus").focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
			
		//Focus the first input in the first form unless otherwise stated below in the :not
		var firstInput = $('form:not(#shoppingBasket, #searchForm, #signUp):first').find(':input:enabled:visible:first');
		$(firstInput).focus();
				
		//Disable forms submitting any default value text due the above script!
		$(".button-primary, .searchBtn").click( function() {	
			if ($(this).parents('form').find('input.fieldfocus').attr("value") == "Search for wine") {
				$(this).parents('form').find('input.fieldfocus').focus();
				return false;
			} else if ($(this).parents('form').find('input.fieldfocus').attr("value") == "Email address") {
				$(this).parents('form').find('input.fieldfocus').focus();
				return false;
			} else {
				return true;
			}
		});
		
		//Switch between signin existing customer forms		
		$("#signinExistingLink").click( function() {
			$("#signinExisting").hide();
			$("#signinExistingNotOnline").show();
			return false;
		} );
		
		$("#signinExistingNotOnlineLink").click( function() {
			$("#signinExisting").show();
			$("#signinExistingNotOnline").hide();
			return false;
		} );

	/* *********************************************
	Show/hide inputs from :selected option drop-down
	********************************************* */
		//Shows an additonal element when a specifed option is selected from a select box
		$('div.hideElementWithJS').hide();
		//show the element is option is selected as default
		if ($('.showExtraOptions').is(':selected')) {
			$('div.hideElementWithJS').show();
		} else {
			$('div.hideElementWithJS').hide();
		}
		
		
		//Shows an additonal text box from a select box
		$('div.otheroption').hide();
		$(".options").change(function () {
			if ($('.otheroptions').is(':selected')) {
				$('div.otheroption').fadeIn();
			} else {
				$('div.otheroption').fadeOut();
			}   
		});
		
		//Shows an additonal text box from a select box
		$('.options').each( function () {
			var name = $(this).attr('name');
			
			if ($(this).val()!='otheroptions') {
				$(this).next().removeAttr('name').hide();
			 }
		});
		
		$('.options').change(onChange);		
		function onChange(){
			var desiredName = $(this).attr('name');
			if ($('#'+desiredName).val()=='otheroptions') {
				$('#'+desiredName).next().attr('name',desiredName).fadeIn('fast');
			}
			else {
				$('#'+desiredName).next().removeAttr('name').fadeOut('fast');
			}
		}


	/* **********************************
	CSS style switcher for browse results
	********************************** */		
		
		
		/* Browse results style switcher */
		$('#switchLinks a').click(function(){   // This applies the following functions to any links within #switchLinks
			$('#contentArea').removeClass();   // This removes any the classes on any link within #contentArea
			return false;   // Return false here stops the browser from following the link's '#' target
		});
		
		$('#linklarge').click(function(){
			$('#contentArea').addClass("large");
		});
		$('#linkmedium').click(function(){
			$('#contentArea').addClass("medium");
		});
		$('#linksmall').click(function(){
			$('#contentArea').addClass("small");
		});
		
		/* Browse results drop down */
		$('div.browseresult').hoverIntent(function() {
		
			var offset = $(this).offset();
			var parentHeight = $(this).height();
			
			$(this).children('.wine-info').slideToggle(500);
			$(this).children('.wine-info').css({top: offset.top + parentHeight + 21, left: offset.left});
		}, function() {
			$(this).children('.wine-info').slideToggle(200);
		});
	
}); // Close main function

/* ****************
Accordian functions
**************** */
$(document).ready(function() {
	$('.content-item ul:first').show();
	$('.content-item h2:first').addClass('open');	
	$('.content-handle').click(function () {
		$('.content-item ul:visible').slideUp().prev().removeClass('open');
		$(this).addClass('open').next().slideDown();
	});
	$('.sitebrowse-item ul:first').show();
	$('.sitebrowse-item h2:first').addClass('open');	
	$('.sitebrowse-handle').click(function () {
		$('.sitebrowse-item ul:visible').slideUp().prev().removeClass('open');
		$(this).addClass('open').next().slideDown();
	});
});

/* ****************
Show/Hide functions
**************** */
$(document).ready(function() {
	// Add a click function to all a tags with class of showhide-toggle, then toggle the next element.
		$('p a.review-toggle').click(function() {	
			$(this).parent().next().slideToggle(400);	
			return false;	
		});
	// Show/Hide script including the code for a close button/link
		$('a.showhide-toggle').click(function() {
			$(this).next().slideToggle(300);
			return false;	
		});
		$('a.close').click(function() {
			$(this).parents().filter('div.info').slideUp(300);
			return false;
		});
		$('a.close').click(function() {
			$(this).parents().filter('div.further-information').slideUp(300);
			return false;
		});
});

/* **************
UI Tabs functions
************** */
$(document).ready(function() {
	
	// no tabs on choosedelivery      
	if (window.location.href.indexOf('orderpipe/choosedelivery.jsp') == -1) { 
		$('#ui-tabs > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
		$('#homepage-ui-tabs > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 7000);
	}
});

/* ***********************
Tooltips for AAW world map
*********************** */
this.aawTooltip = function(){
	
	xOffset = 84;
	yOffset = -163;
		
	$("area.aawmap-area").hoverIntent(function(e){
		var aawTooltipText = $(this).attr("title");
		$("body").append('<div class="aawmap-hover"></div>');
		$("body").find("div.aawmap-hover").text(aawTooltipText);
		$(".aawmap-hover")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.animate({opacity: "show"}, "fast");
    },
	function(){
		$(".aawmap-hover").animate({opacity: "hide"}, "fast");
		$("body").find("div.aawmap-hover").remove();
    });	
	$("area.aawmap-area").mousemove(function(e){
		$(".aawmap-hover")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
// Starting the AAW Tooltip script on page load
$(document).ready(function(){
	aawTooltip();
});

/* **************
Ride the carousel
************** */
function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    $('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}

$(document).ready(function() {
    $("#mycarousel").jcarousel({
        scroll: 1,
		wrap: 'last',
        initCallback: mycarousel_initCallback
	});
	$('.memberFavourites').css({'height': '144px'});
});


/********
QTIP  
********/
$(document).ready(function() {
$('table.basket_contents a.tooltip').qtip({
	   content: "The product code is a letter followed by seven numbers, for example C6054700.",
	   show: 'mouseover',
	   hide: 'mouseout',
	   position: {
   			corner: {
			  target: 'topMiddle',
			  tooltip: 'bottomMiddle'
		   	}
		},
	   style: {
				background: '#fcc9c3',
				color: '#000',
				border: {
					 width: 5,
					 radius: 6,
					 color: '#990000'
				  },
				  width: 230,
				  tip: 'bottomMiddle'
			   }
	})
	
	$('#promotional-voucher-section a.tooltip').qtip({
	   content: "A promotional code or wine voucher is a code that has been sent to you as part of our literature. Gift vouchers that have been bought online or in our stores should be entered in the payment page.",
	   show: 'mouseover',
	   hide: 'mouseout',
	   position: {
   			corner: {
			  target: 'topMiddle',
			  tooltip: 'bottomMiddle'
		   	}
		},
	   style: {
				background: '#fcc9c3',
				color: '#000',
				border: {
					 width: 5,
					 radius: 6,
					 color: '#990000'
				  },
				  width: 230,
				  tip: 'bottomMiddle'
			   }
	})	
				
	$('a#special-offer-saving').qtip({
	   content: "Usual case prices and case savings are based on the normal single bottle prices.",
	   show: 'mouseover',
	   hide: 'mouseout',
	   position: {
   			corner: {
			  target: 'topMiddle',
			  tooltip: 'bottomMiddle'
		   	}
		},
	   style: {
				background: '#fcc9c3',
				color: '#000',
				border: {
					 width: 5,
					 radius: 6,
					 color: '#990000'
				  },
				  width: 230,
				  tip: 'bottomMiddle'
			   }
	})
	$('#signin-form .tooltip').qtip({
	   content: "You'll find your customer number on the back cover of your catalogue (just below your address), or on the top right hand side of emails you've received from us.",
	   show: 'mouseover',
	   hide: 'mouseout',
	   position: {
   			corner: {
			  target: 'topMiddle',
			  tooltip: 'bottomMiddle'
		   	}
		},
	   style: {
				background: '#fcc9c3',
				color: '#000',
				border: {
					 width: 5,
					 radius: 6,
					 color: '#990000'
				  },
				  width: 230,
				  tip: 'bottomMiddle'
			   }
	})			
				
	if( $("input[id^=flexibleDelivery]").length !=0) {		
	   //Start Flexible Delivery
			var geti = function(str){
				return str.substr(6,1);
			}
			
	   		$.ajaxSetup({cache: false});
				
				var $loading = $('<img src="/LW/images/icons/processing.gif" alt="loading">');

				var $dialog = $('<div></div>').append($loading.clone());

					var ajaxLoadQuery = function(){
					var ajaxUrl = '/DWBase/js/jquery/jquery.weekcalendar.js';
		       		
	
		       		 $.ajax({
 					 url: ajaxUrl,
 					 cache: false,
 					 dataType: "script"
 					 });
					}
					
					//ajaxLoadQuery();
					
					$dialog
						.load('/DWBase/html/popups/flexible-delivery.html', function() {ajaxLoadQuery();}) 
						.dialog({
						autoOpen: false,
						title: 'Flexible delivery',
						position: 'top',
						modal: true,
						resizable: false,
						width: 690,
						height: 595,
						buttons: {'Confirm selection': function() {
								 var selectedStatus = false;
				
    				 	var selectedRadioElem = $("table td input:radio:checked");
    				 	var i = geti(selectedRadioElem.attr("id"));
    				 	var elem = $("#price-"+i+"-"+j);
	    				 	
    				 		if(true==selectedRadioElem.attr("checked"))
    				 		{
    				 			selectedStatus = true;
    	
    				 			var dateSelected = $(".popup-day-"+i).html();
    				 			var deliveryDetails = selectedRadioElem.val().split("~");
    				 			
    				 			//Code for multipledelivery
								var multipleFlexIndex = $("#flexibleDeliveryIndex");
								var indextoAppend = "";

								if((multipleFlexIndex.length !=0)&& (multipleFlexIndex.val() >= 0))
								{
									indextoAppend = multipleFlexIndex.val();
								}

								
    				 			$("#flexDeliveryMethod"+indextoAppend).val(deliveryDetails[0]);
    				 			$("#flexDeliveryDate"+indextoAppend).val(dateSelected);
    				 			$("#flexDeliveryPrice"+indextoAppend).val(deliveryDetails[1]);
    				 			$("#flexDeliveryDesc"+indextoAppend).val(deliveryDetails[2]);
    				 			
    				 			//Set parent radio button delivery message
    				 			$("#flexibleDeliveryMsg"+indextoAppend).addClass("flexibleDeliveryMsg").html("Your chosen delivery slot: "+ deliveryDetails[2] +" on <strong>"+dateSelected+"</strong>.");
    				 	
    				 		}
    				 	
    				$dialog.dialog('close');
							}
						}

					});


					$("input[id^=flexibleDelivery]").each(function(flexIndex,flexElement){

					 $("#"+flexElement.id).click( function(){
					  $dialog.dialog('open');
					  
					  if ($("#flexibleDeliveryIndex").length !=0){
					  
					  	var thisRadioElem = this.name;
					  	var thisRadioElemIndex = thisRadioElem.substr(14,thisRadioElem.length-14);
					  	$("#flexibleDeliveryIndex").val(thisRadioElemIndex);
					  }
					  
        			return true;
					});
				
				
				});

		 //End Flexible Delivery		
		   
	};

	});

