// JavaScript Document for Aussie SE collection pages.

$(document).ready(function() {
	
	showHideCtaLimitedEdition();
	
	// Array of urls for the collection pages, used for the dynamic assignation of links for the scrolling carousel effect.
	//var collectionUrls = ['miracle-moist.php', 'colour.php', 'luscious-long.php', 'volume.php']
	//var collectionUrls = ['miracle-moist.php', 'colour.php', 'luscious-long.php', 'volume.php', 'frizz.php', 'mega.php']
	var collectionUrls = ['miracle-moist.php', 'colour.php', 'luscious-long.php', 'aussome.php', 'frizz.php', 'mega.php', 'take-the-heat.php']
	
	// We are setting this for the purposes of the carousel effect below.
	$('#containerProductsTxt').css('overflow', 'hidden');
		
	// 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');
		
		//alert(href);
		
		// Assign urls to the left and right arrows.
		for (var i = 0; i < collectionUrls.length; i++) {
			if (href == collectionUrls[i]) {
				if (href == collectionUrls[0]) {
					$('#linkLeftArrow').attr('href', collectionUrls[collectionUrls.length - 1]);
					$('#linkRightArrow').attr('href', collectionUrls[1]);
				} else if (href == collectionUrls[collectionUrls.length - 1]) {
					$('#linkLeftArrow').attr('href', collectionUrls[i - 1]);
					$('#linkRightArrow').attr('href', collectionUrls[0]);
				} else {
					$('#linkLeftArrow').attr('href', collectionUrls[i - 1]);
					$('#linkRightArrow').attr('href', collectionUrls[i + 1]);
				}
			}
		}
		
		// Checking to see if the TAF overlay is open.
		if ($('#containerTaf').css('display') == 'block') {																																												
			$('#containerTaf').css({'display': 'none', 'height': 0, 'opacity': 0});
		}
		
		// 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/collections-functions.js');
					$.getScript('../scripts/products-functions.js');
					$(this).css('left', '-700px');		
					$(this).animate({'left': '0', 'opacity': 1});
					showHideFrizzWidget();
					showHideCtaLimitedEdition();
				});
			});
		}
		
		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/collections-functions.js');
					$.getScript('../scripts/products-functions.js');
					$(this).css('left', '700px');		
					$(this).animate({'left': '0', 'opacity': 1});
					showHideFrizzWidget();
					showHideCtaLimitedEdition();
				});
			});
		}
		
		return false;
	});
	
	// Adding container for send to a friend mechanism.
	$('#productsContent').after(
		'<div id="containerTaf">' +
			'<span>CLOSE X</span>' +
			'<div id="contentTaf"></div>' +
			'<div id="containerPreview">' +
			'</div>' +
		'</div>'
	);
	
	// Adding container for select a collection mechanism.
	$('#containerTaf').after(
		'<div id="containerSelectCollection">' +
			'<div id="contentSelectCollection"></div>' +
		'</div>'
	);
	
	// Initialising css for containerTaf and containerProductFinder.
	$('#containerTaf, #containerSelectCollection').css('opacity', 0);
	
	// Click function to launch and close an overlay for the product finder mechanism.
	$('#linkSelectCollection').click(function() {
																				
		// Get the URL from the clicked element.																	
		var href = $(this).attr('href');
		if ($('#containerSelectCollection').css('display') == 'none') {
			$('#linkSelectCollection').css('backgroundImage', 'url(../images/icon-minus.gif)');
			$('#contentSelectCollection').load(href + ' .overlay', function() {
				window.setTimeout (sifrTxt, 1);
				showHideCtaLimitedEdition();
				$(this).parent().css('display', 'block')
					.animate({'height': $('#containerProducts').height() - 78, 'opacity': 1}, {duration: 800, queue: false});
			});
		} else {
			$('#linkSelectCollection').css('backgroundImage', 'url(../images/icon-plus.gif)');
			$('#containerSelectCollection').animate({'height': 0, 'opacity': 0}, 800, function() {																																													
				$(this).css('display', 'none');
			});
		}
				
		return false;
	});
	
});

function sifrTxt() {
	
	// Collections page headings.
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsFrizz',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #003592; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsColour',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #cd5a13; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsLusciousLong',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #ad1015; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsMiracleMoist',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #0072b1; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsMega',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #96004b; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsVolume',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #006579; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsAussome',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #317023; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});
	
	//added by Fin 09.08.10
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#collectionsTakeTheHeat',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 36px; color: #e31836; leading: -26; }',
		],
		wmode: 'transparent',
		tuneHeight: -21,
		offsetTop: -9,
		offsetLeft: 0
	});

	// 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
	});
	
	// Select a Collection header.
	sIFR.replace(amwayOptiBold, {
		selector: 'h1#hdrSelectCollection',
		css: [
			'.sIFR-root { text-transform: uppercase; font-size: 19px; color: #970bb3; }'
		],
		wmode: 'transparent',
		tuneHeight: -13,
		offsetTop: -4
	});
	
}

/* this function is to show hide the flash frizz widget*/

function showHideFrizzWidget(){
	if($('h2.frizz').length > 0){
		$('.volume .widgetContainer').show();
		$('.aussome .widgetContainer').show();
		$('.mega .widgetContainer').show();
		$('.colour .widgetContainer').show();
		$('.lusciousLong .widgetContainer').show();
		$('.miracleMoist .widgetContainer').show();
		//added by Fin 09.08.10
		$('.take-the-heat .widgetContainer').show();
		initFlash();
	}else{
		$('.widgetContainer').hide();
	}
}

/* this function is to show hide the cta limited edition*/

function showHideCtaLimitedEdition(){
	if($('h2.colour').length){
		$('.blockLimitedEdition').show();
	}else{
		$('.blockLimitedEdition').hide();
	}
}

function initFlash(){
var flashvars = {};
	// these parameters are required for badge install:
	flashvars.airversion = "1.1"; // version of AIR runtime required
	flashvars.appname = "My Widget"; // application name to display to the user
	flashvars.appurl = "../frizzwidget/aussie_frizz.air"; // absolute URL (beginning with http or https) of the application ".air" file
	// these parameters are required to support launching apps from the badge (but optional for install):
	flashvars.appid = "MyWidget"; // the qualified application ID (ex. com.gskinner.air.MyApplication)
	flashvars.pubid = ""; // publisher id
	// this parameter is required in addition to the above to support upgrading from the badge:
	flashvars.appversion = "1.0"; // AIR application version
	// these parameters are optional:
	flashvars.image = "../frizzwidget/DemoImage2.jpg"; // URL for an image (JPG, PNG, GIF) or SWF to display in the badge (205px wide, 170px high)
	flashvars.appinstallarg = "installed from web"; // passed to the application when it is installed from the badge
	flashvars.applauncharg = "launched from web"; // passed to the application when it is launched from the badge
	// flashvars.helpurl = "help.html"; // optional url to a page containing additional help, displayed in the badge's help screen
	flashvars.hidehelp = "false"; // hides the help icon if "true"
	flashvars.skiptransition = "false"; // skips the initial transition if "true"
	flashvars.titlecolor = "#ffffff"; // changes the color of titles
	flashvars.buttonlabelcolor = "#ffffff"; // changes the color of the button label
	flashvars.appnamecolor = "#ffffff"; // changes the color of the application name if the image is not specified or loaded
	// these parameters allow you to override the default text in the badge:
	// supported strings: str_error, str_err_params, str_err_airunavailable, str_err_airswf, str_loading, str_install, str_launch, str_upgrade, str_close, str_launching, str_launchingtext, str_installing, str_installingtext, str_tryagain, str_beta3, str_beta3text, str_help, str_helptext
	//flashvars.str_err_airswf = "<u>Running locally?</u><br/><br/>The AIR proxy swf won't load properly when this demo is run from the local file system.";
	var params = {};
	params.menu = "false";
	params.bgcolor = "#ffffff";
	params.wmode ="transparent";
	var attributes = {};
	attributes.id = "widgetFrizz";
	// version 9.0.115 or greater is required for launching AIR apps.
	swfobject.embedSWF("../frizzwidget/AIRInstallBadge.swf", "widgetFrizz", "335", "212", "9.0.115", "../scripts/expressInstall.swf", flashvars, params, attributes);	
}