

$(function(){
		   	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* shared */
	/*---------------------------------------------------------------------------------------------------------------------------*/	
		   
	//font replacement
// Comment these out for the @font-face fix
	//Cufon.set('fontFamily', 'Futura Std').replace('h2')('h3')('.bottom-boxes h4')('h5')('#divManifesto h1'); //400
	//Cufon.set('fontFamily', 'Futura Std Medium').replace('.col1 h2')('p.feature-copy'); //500
	//Cufon.set('fontFamily', 'Futura Std Heavy').replace('.main-intuition .top strong')('p.feature-copy strong'); //650
	//Cufon.set('fontFamily', 'Futura Std Bold').replace('.bucket-hydro p'); //700	
	//Cufon.now();
// Comment these out for the @font-face fix
	
	//preload images
	var imgDir = 'images/';
	$.preLoadImages(
		imgDir + 'bg-nav.png',
		imgDir + 'bg-main-hydro.jpg',
		imgDir + 'bg-main-index.jpg',
		imgDir + 'bg-main-intuition.jpg',
		imgDir + 'bg-main-quattro.jpg',
		imgDir + 'bg-main-quattro-w.jpg',
		imgDir + 'Q4WKP_Facebook_landing-04.jpg',
		imgDir + 'bg-main-xtreme.jpg',
		imgDir + 'bg-bucket-hydro.png',
		imgDir + 'bg-bucket-intuition.png',
		imgDir + 'bg-bucket-quattro.png',
		imgDir + 'bg-bucket-quattro-w.png',
		imgDir + 'bg-bucket-quattro-w-en-fys.png',
		imgDir + 'bg-bucket-xtreme.png',
		imgDir + 'bg-manifesto.png',
		imgDir + 'bg-sub-default.jpg',
		imgDir + 'bg-sub-intuition.jpg',
		imgDir + 'bg-sub-quattro.jpg',
		imgDir + 'bg-sub-quattro-w.jpg',
		imgDir + 'bg-sub-xtreme.jpg',
		imgDir + 'intuition-history.gif',
		imgDir + 'intuition-tips.gif',
		imgDir + 'intuition-facts.gif',
		imgDir + 'quattro-history.gif',
		imgDir + 'quattro-tips.gif',
		imgDir + 'quattro-facts.gif',
		imgDir + 'qw-history.gif',
		imgDir + 'qw-tips.gif',
		imgDir + 'qw-facts.gif',
		imgDir + 'xtreme-history.gif',
		imgDir + 'xtreme-tips.gif',
		imgDir + 'xtreme-facts.gif',
		imgDir + 'slide-arrow-left.png',
		imgDir + 'slide-arrow-right.png'
	);

	//populate current year (footer - copyright)
	$('.current-year').text((new Date()).getFullYear());
	
	//hide noscript (ie)
	$('noscript').hide();
	
	//initiate all new window links
	$('.new-win').click(function(){
		$(this).attr({ 'target' : '_blank' });
	});		
	
	//clear "search" text on click
	$('#txtSearch').click(function(){ this.value = ''; });
	
	//remove last item border
	$('.col1 .item:last').css({ 'border' : 0 });
	$('.single-col .item:last').css({ 'border' : 0 });
	
	//main nav opacity effect (ie6 excluded)
	if(!($.browser.msie && $.browser.version.substr(0, 1) < 7)){
		//var opacity = 0.5, toOpacity = 1, duration = 250;
		//get the current page from body class							
		var curPage = $('body').attr('class');
		$('.nav-button').hover(
			function(){	
				//mouseover, fade in
				$(this).fadeTo(duration, 1); 
			}, 
			function(){ 
				//mouseout, fade out unless it's the current page
				var curNavLink = $(this).attr('class').substring($(this).attr('class').indexOf(' ') + 1, $(this).attr('class').length);
				if(curPage != curNavLink){ $(this).fadeTo(duration, 0.5); }
			}
		);
	}
	

	
	//set the width of each slider item to current view port
	var viewports = new Array();
	viewports = $.getViewPorts();
	$('.wrapper ul li').css({ 'width' : viewports[0] });
	
	//initiate colorbox for manifesto (header 'free your skin' link)
	//$(".manifesto").colorbox({
	//	'innerWidth'	:'983px',
	//	'innerHeight'	:'588px',
	//	'width'		:'1000px', 
	//	'height'	:'640px',
	//	'scrolling'	:'false',
	//	'inline'	:'true', 
	//	'href'		:'#divManifesto'
	//});	
	
	// Trimstyle wayspa call out
	
	$("#bucket_right").colorbox({width:"476px", height:"420px", inline:true, href:"#lightbox" });
	
	//hover effect for contest call outs
	var opacity = 0.8, toOpacity = 1, duration = 250;
	$('.btn-contest').hover(
		function(){	
			//mouseover, fade in
			$(this).fadeTo(duration,toOpacity); 
		}, 
		function(){ 

			$(this).fadeTo(duration,opacity);
		}
	);
	
	
	
	
	
	
	/****************************************************************************************************************************************************************/
	/* search form */
	
	/* validation */
	jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
	 return this.optional(element) || value != param;
	 }, "This has to be different...");

	$("#commentForm").validate();
	
	//if contact form is available
	if($('#frmSearch').length){ 
		//validate it
		$('#frmSearch').validate({
			errorClass: 'error-highlight',
			
			rules: {
				txtSearch: {
					required: true,
					notEqualTo: 'Search'
				}
			},
			invalidHandler: function(form, validator){
				var errors = validator.numberOfInvalids();
				if(errors){
					$('#txtSearch').addClass('error-highlight');
				}else{
					$('#txtSearch').removeClass('error-highlight');
				}
			},
			showErrors: function(errorMap, errorList){ }
		}); 
	}

	/* submit the form */
	$('#btnSubmitContact').click(function(){
		//check that form passes validation
		if($('#frmSearch').valid()){		
			$('#frmSearch').submit();
		}//if valid
	});	







	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* landing page */
	/*---------------------------------------------------------------------------------------------------------------------------*/		
	
	//initiate slider for landing page
	if($('.anythingSlider-landing').length){
		$('.anythingSlider-landing').anythingSlider({
			easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
			autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
			delay: 9000,                    // How long between slide transitions in AutoPlay mode
			startStopped: false,            // If autoPlay is on, this can force it to start stopped
			animationTime: 900,             // How long the slide transition takes
			hashTags: true,                 // Should links change the hashtag in the URL?
			buildNavigation: false,         // If true, builds and list of anchor links to link to each slide
			pauseOnHover: true,            // If true, and autoPlay is enabled, the show will pause on hover
			startText: "Go",            	// Start text
			stopText: "Stop",               // Stop text
			navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
		});
	}
	
	//randomize product bucket
	var buckets = new Array();
	buckets[3] = '.bucket-hydro';
	buckets[1] = '.bucket-intuition';
	buckets[2] = '.bucket-quattro';
	buckets[0] = '.bucket-quattro-w';
	buckets[4] = '.bucket-xtreme';
	var x = Math.floor(Math.random() * buckets.length);
	$(buckets[x]).show();
	
	
	
	//png fix for ie6
	if($.browser.msie && $.browser.version.substr(0, 1) < 7){
		$(document).pngFix();
	}	
	
	
	
	
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* sub pages */
	/*---------------------------------------------------------------------------------------------------------------------------*/		

	//faq
	$('.faq-wrapper > a').click(function(){
		if($(this).next('p').is(':hidden')){
			//show answer
			$(this).next('p').show();
		}else{
			$(this).next('p').hide();
		}
	});

	
	//in page navigation scrolling
	if($('.in-page-nav').length){ $('.in-page-nav').localScroll(); }
	




	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* all products page */
	/*---------------------------------------------------------------------------------------------------------------------------*/
	var opacity = 1, toOpacity = 0.8, duration = 250;
	$('.prod-shot').hover(
		function(){	
			//mouseover, fade in
			$(this).fadeTo(duration,toOpacity); 
		}, 
		function(){ 

			$(this).fadeTo(duration,opacity);
		}
	);
	
	
	
	
	
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* contact us page */
	/*---------------------------------------------------------------------------------------------------------------------------*/

	//custom validation method for phone number formatting	
	$.validator.addMethod('phone', function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ''); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, 'Please enter a valid telephone number.');

	//custom validation method for postal code
	$.validator.addMethod("postalCode", function(postalcode, element) {
		return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
	}, "Please specify a valid postal/zip code");
	
	jQuery.validator.messages.required = "";
	$("#frmFeedback").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. Please correct the highlighted text box.'
					: 'You missed ' + errors + ' fields.  Please correct the highlighted text boxes.';
				$("div.errors span").html(message);
				$("div.errors").show();
			} else {
				$("div.errors").hide();
			}
		},
		//onkeyup: false,
		submitHandler: function(form) {
			$("div.errors").hide();
			form.submit();
		},
		messages: {
			password2: {
				required: " ",
				equalTo: "Please enter the same password as above"	
			},
			email: {
				required: " ",
				email: "Please enter a valid email address, example: you@yourdomain.com"
			}
		},
		debug:true
	});




});//domready




$(window).resize(function() { 
	//initiate slider for landing page
	if($('.anythingSlider-landing').length){
		$('.anythingSlider-landing').anythingSlider({
			easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
			autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
			delay: 6000,                    // How long between slide transitions in AutoPlay mode
			startStopped: false,            // If autoPlay is on, this can force it to start stopped
			animationTime: 900,             // How long the slide transition takes
			hashTags: true,                 // Should links change the hashtag in the URL?
			buildNavigation: false,         // If true, builds and list of anchor links to link to each slide
			pauseOnHover: true,            // If true, and autoPlay is enabled, the show will pause on hover
			startText: "Go",            	// Start text
			stopText: "Stop",               // Stop text
			navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
		});
	}
});








/****************************************************************************************************************************************************************/
/* functions */
/****************************************************************************************************************************************************************/	

//initiate anything slider for landing page
function formatText(index, panel) {
	return index + "";
}


var preLoadCache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function(){
	var args_len = arguments.length;
	for (var i = args_len; i--;){
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		preLoadCache.push(cacheImage);
	}
};



//this function is called from jquery.anything-slider.js
//it is used for changing the background colour of the main nav in IE because of IE's lack of support for layered transparencies
$.changeNavColour = function(param){
	//set nav bacground colour for IE
	if($.browser.msie){
		var bgColor = '';
		//alert('slide: ' + currSlide);
		switch(param){
			case 1:
				bgColor = '#c5c5c5'; //index
				break;
			case 2:
				bgColor = '#00529c'; //hydro
				break;
			case 3:
				bgColor = '#019797'; //intuition
				break;
			case 4:
				bgColor = '#a8a9ad'; //quattro for men
				break;
			case 5:
				bgColor = '#cd365c'; //quattro for women
				break;
			case 6:
				bgColor = '#006a9c'; //Xtreme 3
				break;
			default: 
				bgColor = '#c5c5c5'; //default
		}
		$('.nav-bg').css({ 'background-color' : bgColor });
		$('.nav1 li a').css({ 'background-color' : bgColor });
		$('.nav2 li a').css({ 'background-color' : bgColor });
	}
	
}

$.getViewPorts = function(){
	//get the available view port
	//http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
	var viewportwidth;
	var viewportheight;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if(typeof window.innerWidth != 'undefined'){
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		 viewportwidth = document.documentElement.clientWidth,
		 viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE
	else{
		 viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		 viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return ports = new Array(viewportwidth, viewportheight);	
}



function slideToggle(el, bShow){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 250});
  } else {
    $el.animate({height: 0}, {duration: 250, complete:function (){
        $el.hide();
      }
    });
  }
}



function showDesc(prod, hotSpot){
	
	//get current page name
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);	
	
	//set an array of each product depending on the page
	switch(sPage){
		//intuition
		case 'intuition.shtml':
			var currProdArray = new Array('ip', 'ip2', 'ip3');	
			break;	
		//quattro for men
		case 'quattro-for-men.shtml':
			var currProdArray = new Array('qtt', 'qt', 'qm', 'qtd');			
			break;
		//quattro for women
		case 'quattro-for-women.shtml':
			var currProdArray = new Array('qw', 'qwt');
			break;
	}//switch
	
	//hide all prod desc
	$.each(currProdArray, function(index, value){
		$('#' + value + ' div').hide();				  
	});
	//show user selected hotspot copy
	$('#' + prod + '-' + hotSpot).show();
	
}









