
var imgDir = 'images/';

$(function(){
		   
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* shared across pages */
	/*---------------------------------------------------------------------------------------------------------------------------*/
	
	//preload images
	$.preLoadImages(
		imgDir + 'bg-home.jpg',
		imgDir + 'bg-products.jpg',
		imgDir + 'header-fabulous.jpg',
		imgDir + 'header-gorgeous.jpg',
		imgDir + 'header-unstoppable.jpg',
		imgDir + 'logo-skintimate-home.jpg',
		imgDir + 'logo-skintimate-products.jpg',		
		imgDir + 'lower-buckets.jpg', 
		imgDir + 'prod-thumbs-home.jpg',
		imgDir + 'prod1.jpg',
		imgDir + 'prod2.jpg',
		imgDir + 'prod3.jpg',
		imgDir + 'prod4.jpg',
		imgDir + 'prod5.jpg',
		imgDir + 'prod6.jpg',
		imgDir + 'prod7.jpg',
		imgDir + 'prod8.jpg'
	);
	
	//add auto sprites to buttom buckets if not ie6, if ie6 default css sprites are used
	if($.browser.msie && $.browser.version <= 6 ){	
		//do nothing
	}else{
		$('#lower-buckets').autosprites({
			offset: '148px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('#lower-buckets li a').mouseover(function(){ $(this).css({ 'background' : 'none' }); });
	}
	
	//populate current year (footer - copyright)
	$('.current-year').text((new Date()).getFullYear());	
	

	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* landing page */
	/*---------------------------------------------------------------------------------------------------------------------------*/
	
	//load random header image
	var headerImages = new Array();
	headerImages[0] = 'header-fabulous.jpg';
	headerImages[1] = 'header-gorgeous.jpg';
	headerImages[2] = 'header-unstoppable.jpg';
	imgIndex = Math.floor(Math.random() * headerImages.length);
	$('.home .header').css({ 'background' : 'url(' + imgDir + headerImages[imgIndex] + ') no-repeat' });

	//auto srites for product thumbs
	if($.browser.msie && $.browser.version <= 6 ){
		//do nothing	
	}else{
		$('#prod-thumbs-home').autosprites({
			offset: '162px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('#prod-thumbs-home li a').mouseover(function(){ $(this).css({ 'background' : 'none' }); });
	}	

	



	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* product page */
	/*---------------------------------------------------------------------------------------------------------------------------*/

	//auto srites for product thumbs
	if($.browser.msie && $.browser.version <= 6 ){
		//do nothing	
	}else{
		$('#prod-thumbs-products').autosprites({
			offset: '162px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('#prod-thumbs-products li a').mouseover(function(){ $(this).css({ 'background' : 'none' }); });
	}
	
	$('#prod-thumbs-products li').click(function(){
		var id = $(this).attr('id');
		
		//hide all top copy blocks (there should only be one visibile)
		$('.prod-info-top > div').each(function(i, v){ $(this).hide(); });
		//set the feature image
		$('.feature-img').css({ 'background' : 'url(images/' + id + '.jpg) 0 0 no-repeat' });
		//show top copy block
		$('#' + id + '-top').show();
		//hide all bottom copy blocks (there should only be one visible)
		$('.prod-info-bottom > div').each(function(i, v){ $(this).hide(); });
		//show bottom copy block
		$('#' + id + '-bottom').show();
		//reset all active states
		$('#prod1').css({ 'background-position' : '0 0' });
		$('#prod2').css({ 'background-position' : '-70px 0' });
		$('#prod3').css({ 'background-position' : '-140px 0' });
		$('#prod4').css({ 'background-position' : '-210px 0' });
		$('#prod5').css({ 'background-position' : '-349px 0' });
		$('#prod6').css({ 'background-position' : '-419px 0' });
		$('#prod7').css({ 'background-position' : '-558px 0' });
		$('#prod8').css({ 'background-position' : '-628px 0' });
		//set active state for thumbnail
		switch(id){
			case 'prod1':
				$('#prod1').css({ 'background-position' : '0 162px' });
				break;
			case 'prod2':
				$('#prod2').css({ 'background-position' : '-70px 162px' });
				break;
			case 'prod3':
				$('#prod3').css({ 'background-position' : '-140px 162px' });
				break;
			case 'prod4':
				$('#prod4').css({ 'background-position' : '-210px 162px' });
				break;
			case 'prod5':
				$('#prod5').css({ 'background-position' : '-349px 162px' });
				break;
			case 'prod6':
				$('#prod6').css({ 'background-position' : '-419px 162px' });
				break;
			case 'prod7':
				$('#prod7').css({ 'background-position' : '-558px 162px' });
				break;
			case 'prod8':
				$('#prod8').css({ 'background-position' : '-628px 162px' });
				break;
		} 		
	});
	
	//set product to query string variable if provided
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == 'id') {
			//return pair[1];
			var prodToLoad = pair[1];
		}
	}
	//if there was a query string variable to load, load that product
	if(prodToLoad){
		
		//hide all top copy blocks (there should only be one visibile)
		$('#prod1-top').hide();
		//set the feature image
		$('.feature-img').css({ 'background' : 'url(images/' + prodToLoad + '.jpg) 0 0 no-repeat' });
		//show top copy block
		$('#' + prodToLoad + '-top').show();
		//hide all bottom copy blocks (there should only be one visible)
		$('#prod1-bottom').hide();
		//show bottom copy block
		$('#' + prodToLoad + '-bottom').show();		
		
		switch(prodToLoad){
			case 'prod1':
				$('#prod1').css({ 'background-position' : '0 162px' });
				break;
			case 'prod2':
				$('#prod2').css({ 'background-position' : '-70px 162px' });
				break;
			case 'prod3':
				$('#prod3').css({ 'background-position' : '-140px 162px' });
				break;
			case 'prod4':
				$('#prod4').css({ 'background-position' : '-210px 162px' });
				break;
			case 'prod5':
				$('#prod5').css({ 'background-position' : '-349px 162px' });
				break;
			case 'prod6':
				$('#prod6').css({ 'background-position' : '-419px 162px' });
				break;
			case 'prod7':
				$('#prod7').css({ 'background-position' : '-558px 162px' });
				break;
			case 'prod8':
				$('#prod8').css({ 'background-position' : '-628px 162px' });
				break;
		} 
	}
	
	
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* affiliates page */
	/*---------------------------------------------------------------------------------------------------------------------------*/	

	//auto srites for call outs
	if($.browser.msie && $.browser.version <= 6 ){
		//do nothing	
	}else{
		$('.fashion-week-cta').autosprites({
			offset: '193px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('.fashion-week-cta').mouseover(function(){ $(this).css({ 'background' : 'none' }); });
		$('.rethink-cta').autosprites({
			offset: '193px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('.rethink-cta').mouseover(function(){ $(this).css({ 'background' : 'none' }); });	
		$('.quattro-w-cta').autosprites({
			offset: '223px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('.quattro-w-cta').mouseover(function(){ $(this).css({ 'background' : 'none' }); });	
		//auto sprites for product thumbnails
		$('#prod-thumbs-affiliates').autosprites({
			offset: '162px',
			activeState: true,
			activeSprites: true
		});
		//strip default css sprites
		$('#prod-thumbs-affiliates li a').mouseover(function(){ $(this).css({ 'background' : 'none' }); });		
	}	
	
	
	

	

	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* language toggle */
	/*---------------------------------------------------------------------------------------------------------------------------*/	

	$('.lang-toggle').click(function(){
		var thePath = window.location.pathname;
		var thePage = thePath.substring(thePath.lastIndexOf('/') + 1);
		var thePagePos = thePath.lastIndexOf(thePage);
		var theDir = thePath.substring(thePagePos - 4, thePagePos);
		if(theDir === '/en/'){
			//window.location = '../fr/' + thePage;
			setTimeout(function(){ window.location = '../fr/' + thePage; }, 0);			
		}else if(theDir === '/fr/'){
			//window.location = '../en/' + thePage;
			setTimeout(function(){ window.location = '../en/' + thePage; }, 0);	
		}else{
			//window.location = '../en/';
			setTimeout(function(){ window.location = '../en/'; }, 0);	
		}
	}); //.lang-toggle click
	
	
	


//	//png fix for ie6
//	if($.browser.msie && $.browser.version.substr(0, 1) < 7){ $(document).pngFix(); }	




	
});//domready








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


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);
	}
};

