// JavaScript Document for Aussie SE product pages.

$(document).ready(function() {
													 
	// We need to create the product indicator line based on how many products are in the list.
	// We do this by finding the number of products, subtracting the total from from the width we are allowed to play with, 820px, in order to cater for 1px right margins, and finally dividing what we are left with by the the total again.
	
	// Find the number of products
	var numProducts = $('#productIndicator ul').children('li').size();
	// Subtract the number of products from the width of the ul container element.
	var listWidth = $('#productIndicator ul').width() - numProducts;
	// Calculate the width of each li element.
	var listElementWidth = Math.floor(listWidth / numProducts);
	
	// Let's apply the numbers to our page elements along with the appropriate CSS.
	$('#productIndicator ul li').width(listElementWidth)
		.height(4)
		.css({float: 'left', marginRight: '1px'});
		
	$('#productIndicator ul li a').width(listElementWidth)
		.height(4)
		.css({display: 'block', overflow: 'hidden', backgroundColor: '#000', opacity: '0.1'})
		.text('');
		
	// Highlight div that sits behind the list.
	$('#productIndicator ul').after(
		'<div id="productHighlight"></div>'
	);
	
	// Variable to determine the left position of the productHighlight div.
	//var window.onPos;
	
	// We'll establish the default number of window.onPos based on what page we are on.
	if (typeof(window.onPos) == 'undefined') {
		$('#productIndicator ul li a').each(function() {	
			if ($('body').attr('class') == $(this).attr('href').slice(10, $(this).attr('href').length - 4)) {
				window.onPos = $(this).parent().position().left;
			}
		});
	}
	
	// We need to set the div productHighlight's position to be whatever link was clicked.
	$('#productHighlight').width(listElementWidth)
		.height(4)
		.css({position: 'absolute', top: '0px', left: window.onPos, overflow: 'hidden', backgroundColor: '#9900cc', zIndex: '0'});
		
	// We'll move productHighlight onMouseOver.
	$('#productIndicator ul li a').hover(
		function() {
			$('#productHighlight').animate({'left': $(this).parent().position().left + 'px'}, {queue: false});
		},
		function() {
			//alert(window.onPos);
			$('#productHighlight').animate({'left': window.onPos}, {queue: false});
		}
	);
	
	// We are setting this for the purposes of the carousel effect below.
	$('#containerProductsTxt').css('overflow', 'hidden');
		
	// Click function to fade in and out product pages without page reload.
	$('#productIndicator ul li a').click(function() {
		// We need to get the offset of the clicked element so that it acts as the default point of return for the mouseOver effect.
		window.onPos = $(this).parent().position().left;	
		$('#productHighlight').css('left', window.onPos);
			
		// Get the URL from the clicked element.
		var href = $(this).attr('href');
		
		// We also dynamically replace the href with the previous and next ones in the list.
		assignUrls(href);
		
		// Checking to see if the TAF overlay is open.
		$('#containerTaf').css({'display': 'none', 'height': 0, 'opacity': 0}).width(205);
		$('#containerPreview').css('display', 'none');
		
		$('#productIndicator ul li a').hover(
			function() {
				$('#productHighlight').animate({'left': $(this).parent().position().left + 'px'}, {queue: false});
			},
			function() {
				$('#productHighlight').animate({'left': window.onPos}, {queue: false});
			}
		);
		
		$('#swapContent').fadeOut(600, function() {
			//$('#productsContent').addClass('loading');
			$('#swapContent').load(href + ' #swapContent', function() {
				
				$.getScript('../scripts/products-functions.js');
				window.setTimeout (sifrTxt, 1);
				$('#swapContent').fadeIn(600);
				
			})
			
		});
		
		return false;
																								
	});
	
	// Click function to slide in previous and next product pages without reload.
	$('.cycle').click(function() {															
		// Get the URL from the clicked element.																	
		var href = $(this).attr('href');
		
		// Iterate over all the products in our list. If the href attribute of the one we clicked on matches any in the list then we'll adjust window.onPos so we can move productHighlight to that position below.
		$('#productIndicator ul li a').each(function() {	
			if ((href) == $(this).attr('href')) {
				window.onPos = $(this).parent().position().left;
			}
		});
		
		// We also dynamically replace the href with the previous and next ones in the list.
		assignUrls(href);
		
		// Checking to see if the TAF overlay is open.																																										
		$('#containerTaf').css({'display': 'none', 'height': 0, 'opacity': 0}).width(205);
		$('#containerPreview').css('display', 'none');
		
		// We create the illusion of a carousel effect. What really happens is that we shift the content of swapContent 700px to the left or right depending on which arrow has been clicked. It becomes invisible because of the overflow: hidden attribute on the parent container. 
		// We then load in the content of the next product and move the position of swapContent 700px to the right or left again. 
		// We then shift this back into view.
		if ($(this).attr('id') == 'linkLeftArrow') {
			$('#swapContent').animate({'left': '700px', 'opacity': 0}, function() {	
				//$('#productsContent').addClass('loading');
				$(this).load(href + ' #swapContent', function() {
					//$('#productsContent').removeClass('loading');
					window.setTimeout (sifrTxt, 1);
					$.getScript('../scripts/products-functions.js');
					$(this).css('left', '-700px');		
					$(this).animate({'left': '0', 'opacity': 1});
					$('#productHighlight').animate({'left': window.onPos}, {queue: false});
					$('#productIndicator ul li a').hover(
						function() {
							$('#productHighlight').animate({'left': $(this).parent().position().left + 'px'}, {queue: false});
						},
						function() {
							$('#productHighlight').animate({'left': window.onPos}, {queue: false});
						}
					);
				});
			});
		}
		
		if ($(this).attr('id') == 'linkRightArrow') {
			$('#swapContent').animate({'left': '-700px', 'opacity': 0}, function() {	
				//$('#productsContent').addClass('loading');
				$(this).load(href + ' #swapContent', function() {
					//$('#productsContent').removeClass('loading');
					window.setTimeout (sifrTxt, 1);
					$.getScript('../scripts/products-functions.js');
					$(this).css('left', '700px');		
					$(this).animate({'left': '0', 'opacity': 1});
					$('#productHighlight').animate({'left': window.onPos}, {queue: false});
					$('#productIndicator ul li a').hover(
						function() {
							$('#productHighlight').animate({'left': $(this).parent().position().left + 'px'}, {queue: false});
						},
						function() {
							$('#productHighlight').animate({'left': window.onPos}, {queue: false});
						}
					);
				});
			});
		}
		
		return false;
	});
	
	// Adding container for send to a friend mechanism.
	$('#productIndicator').after(
		'<div id="containerTaf">' +
			'<span>CLOSE X</span>' +
			'<div id="contentTaf"></div>' +
			'<div id="containerPreview">' +
			'</div>' +
		'</div>'
	);
	
	// Adding container for product finder mechanism.
	$('#containerTaf').after(
		'<div id="containerProductFinder">' +
			'<div id="contentProductFinder"></div>' +
		'</div>'
	);
	
	// Initialising css for containerTaf and containerProductFinder.
	$('#containerTaf, #containerProductFinder').css('opacity', 0);
	
	//Click function to launch and close an overlay for the product finder mechanism.
	$('#linkProductFinder').click(function() {
																				
		// Get the URL from the clicked element.																	
		var href = $(this).attr('href');
		if ($('#containerProductFinder').css('display') == 'none') {
			$('#linkProductFinder').css('backgroundImage', 'url(../images/icon-minus.gif)');
			$('#contentProductFinder').load(href + ' .overlay', function() {
				$(this).parent().css('display', 'block')
					//.animate({'height': $('#containerProducts').height() - 48, 'opacity': 1}, {duration: 800, queue: false});
					.animate({'height': 560, 'opacity': 1}, {duration: 800, queue: false});
				$(this).parent().parent().animate({'height': 615, 'opacity': 1}, {duration: 800, queue: false}, function() {	
					$('#containerFooter').css('position','absolute');
				});
				initFormButtons();
			});
		} else {
			$('#linkProductFinder').css('backgroundImage', 'url(../images/icon-plus.gif)');
			$('#containerProductFinder').animate({'height': 0, 'opacity': 0}, 800, function() {																																													
				$(this).css('display', 'none');
			});
			$('#containerProducts').animate({'height': 500, 'opacity': 1}, {duration: 800, queue: false});
			$('#containerFooter').css('position','relative');
			$('#containerFooter').animate({'bottom': 0}, {duration: 800, queue: false});
		}
				
		return false;
	});
	
	initCtaLimitedEdition();
	
	initCallOverlayUrl();
	
});

function initFormButtons(){
	$('#containerFooter').animate({'bottom': -100}, {duration: 800, queue: false});
	$('#check').click(function(){
		$("form input[type='checkbox']").attr('checked',true);
		return false;
	});
	$('#uncheck').click(function(){
		$("form input[type='checkbox']").attr('checked',false);	
		return false;
	});
	
}

function assignUrls(href) {
	$('#productIndicator ul li a').each(function(i) {	
		if ((href) == $(this).attr('href')) {
			if (i == 0) {
				$('#linkLeftArrow').attr('href', $('#productIndicator ul li').eq($('#productIndicator ul').children('li').size() - 1).children('a').attr('href'));
				$('#linkRightArrow').attr('href', $('#productIndicator ul li').eq(1).children('a').attr('href'));
				$('#productIndicator span em').text(1);
			} else if (i == ($('#productIndicator ul').children('li').size() - 1)) {
				$('#linkLeftArrow').attr('href', $(this).parent().prev().children('a').attr('href'));
				$('#linkRightArrow').attr('href', $('#productIndicator ul li').eq(0).children('a').attr('href'));
				$('#productIndicator span em').text($('#productIndicator ul').children('li').size());
			} else {
				$('#linkLeftArrow').attr('href', $(this).parent().prev().children('a').attr('href'));
				$('#linkRightArrow').attr('href', $(this).parent().next().children('a').attr('href'));
				$('#productIndicator span em').text((i + 1));
			}
		}
	});
}

function sifrTxt() {
	
	initCtaLimitedEdition();
	
	// Product page headings.
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.colour',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #cd5a13; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.colour2',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #cd5a13; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
		sIFR.replace(amwayOptiBold, {
		selector: 'h1.colour3New',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #d63a20; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.frizz',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #003592; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.frizz2',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #003592; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.lusciousLong',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #ad1015; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.lusciousLong2',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #ad1015; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.miracleMoist',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #0072b1; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.miracleMoist2',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #0072b1; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.miracleMoist3',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 28px; color: #0072b1; text-align: right; leading: -18; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.miracleReconstructor',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #ad1015; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.mega',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #96004b; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.mega2',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #96004b; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
	  selector: 'h1.volume2',
	  css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #006579; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'		
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});

	sIFR.replace(amwayOptiBold, {
	  selector: 'h1.volume',
	  css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #006579; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
	  selector: 'h1.aussome',
	  css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #317023; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	sIFR.replace(amwayOptiBold, {
	  selector: 'h1.aussome2',
	  css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #317023; text-align: right; leading: -26; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -12,
		offsetTop: -5,
		offsetLeft: 3
	});
	
	// Add to my Hair Must-Haves link.
	sIFR.replace(amwayOptiBold, {
		selector: 'div#containerAddHairMustHaves a',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 19px; color: #970bb3; leading: -7; cursor: pointer; }'
		],
		wmode: 'transparent',
		tuneHeight: -10,
		offsetTop: -5
	});
	
	// You may also like... heading.
	sIFR.replace(amwayOptiBold, {
		selector: 'div#containerAlsoLike h3',
		css: [
			'.sIFR-root { font-size: 20px; color: #999999; }'
		],
		wmode: 'transparent',
		tuneHeight: -10,
		offsetTop: -5
	});
	
	// Frizz forecast callout.
	sIFR.replace(helveticaNeueLtStd, {
		selector: 'div#calloutFrizzForecast a',
		css: [
			'.sIFR-root { font-size: 13px; font-weight: bold; color: #ffffff; leading: -4; cursor: pointer; }',
			'.span { font-size: 10px; }'
		],
		wmode: 'transparent',
		tuneHeight: -8,
		offsetTop: -3
	});
	// New 3MM Product Color
	sIFR.replace(amwayOptiBold, {
		selector: 'h1.colour3New',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #d63a20; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	//Aussome New
			sIFR.replace(amwayOptiBold, {
		selector: 'h1.aussomeNew',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 35px; color: #317023; text-align: right; leading: -26; }',
			'.txtTop { font-size: 25px; leading: -13; display: block; }',
			'.txtBtm { font-size: 15px; display: block; }'
		],
		wmode: 'transparent',
		tuneHeight: -22,
		offsetTop: -15,
		offsetLeft: 3
	});
	
	
}

//CREATE LIMITED EDITION CTA
function initCtaLimitedEdition(){
	$('.blockLimitedEdition').remove();
	if($('.limitedColor').length){
		var limitedHtml = '<div class="blockLimitedEdition"><img src="../images/img_limited_edition.jpg" alt="Limited Edition"  /></div>';
		$('#productIndicator').after(limitedHtml);
		$('.blockLimitedEdition').show();
	}
}

function initCallOverlayUrl(){
	//trigger overlay from param on url query string ?overlay=liz_tips
	var overlay = $.getUrlVar('overlay');

	if(overlay != null || overlay != undefined){
		overlay = overlay;
		
		if($('#overlayLiz').length && overlay == 'liz_tips'){
			window.setTimeout(function(){
				$('#overlayLiz').trigger('click');						   						   
			},100);
		}
	}
}





