$('html').addClass('hideJSContent');

function printpage() {
	window.print();
}
/* *******************************************************************
Run the hide functions first to try avoid seeing the hide on page load
******************************************************************* */
$(document).ready(function() {
    $('.formreveal').hide();
	$('.userHintDiv').hide();
	$('.content-item ul:not(:first)').hide();
	$('.sitebrowse-item ul:not(:first)').hide();
	//$('div.info').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
 /*   $('.password').after('<span class="passwordResult"></span>');
    $('.password').keyup(function(){
       $('.passwordResult').html(passwordStrength($('.password').val()));
    });
    $('.confirmPassword').after('<span class="confirmPasswordResult"></span>');*/
});


/* *******************
Other custom functions
******************* */
$(document).ready(function() {
		
	/* ***********************************************
	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.orderform input:checked').parent().addClass('bold');
		$('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).parents('ol.orderform').find('bold').removeClass('bold');			
            $(this).parent()/*.addClass('bold')*/.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;
		} );

	/* ************************
	Dynamically created buttons
	************************ */
		//Add a span to all links with the specified class name
		//$('a.button-primary, a.button-secondary').wrapInner("<span>" + "</span>");
		
		//Change all form submit buttons (inputs) to <a> + <span> for styling reasons and still submit form correctly
		//$(':submit:not(.searchBtn, .buttonCheckout)').each(function() {	//Get all inputs on the page with a type of submit
			//$(this).wrap('<a href="#"><span></span></a>');	//Wrap this HTML markup around the input
			
			//var buttonValue = $(this).val();
			//var className = $(this).attr("class");
			
			//$(this).parents('a').addClass(className += " submit");	//Add class name from the input to the <a>
			
			//$(this).parent().append(buttonValue);	//Append the value from the input to inbetween the <span>
			//$(this).css({'position':'absolute', 'left':'-10000px', 'visibility': 'hidden'});	//Then hide the <input> by positioning it waaay of the users screen!
		//});
		//We've put this backgroundImage here so users with JS won't see half a button image
		//$('a.button-primary, a.button-secondary').css('backgroundImage','url(/DWBase/images/buttons/bg_button_a.gif)');
		
		//Submit the form
		//$('form a.submit').click(function() {			
			//$(this).children().find('input[type=submit]').trigger('click');
			//return false;	//Stop the default event of the <a>
		//});
	
	
	/* *********************************************
	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() {
	$('#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 */
	
		$('#voucher-box a.tooltip').qtip({
	   content: "<p>This field is for a promotional or offer voucher that you have received from us or one of our partners. Please check that you have read and agreed to the terms and conditions applicable to the voucher as orders may be checked and held if they do not conform to these Ts & C's.</p><p>Gift vouchers that have been sent as an apology or as a thank-you, or that have been bought online or in our stores should be entered in the box below.</p> ",
	   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'
			   }
	})	
	
		$('#gift-certificat-section a.tooltip').qtip({
	   content: "Gift vouchers can been bought online or in our stores as money off a purchase, or you may have been sent it as a way of us saying sorry or thank-you for something. An example of a gift voucher code is X97KD3C1KTP.",
	   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'
			   }
	})	
});