$(window).load(function() {
	
	if($('div#beauty-moment').length > 0) {
		if ($.cookie("homepage-fadeout") != 'true') {
			homepageSplash(true);
			
		}	
	}
	
	//setHeight($('body div.page-wrapper'));
})

function setHomePageColumnHeights(parentElement) {
	//taking a parent element, set all it's direct descendents to be the same height as it.
	parentHeight = parentElement.height();
	parentElement.children('div').each(function(){
		$(this).css('height', parentHeight+'px');
	})
}

/* depending on a cookie value, show the homepage splash image after the window has loaded. */
function homepageSplash(runFade) {
	
	if (runFade) {
		$('div.wrapper.bottom-margin').css('min-height','896px');
		
		// get position of the page content
		var pagePosition = $('div.highlight-col.highlight-left').offset();
		$('div#beauty-moment').css('top',pagePosition.top - 12+'px');
		
		//image takes long to load so hide it until it is fully loaded then show it 
		$('div#beauty-moment').css('visibility','visible');
		$('div#beauty-moment').css('height',$('div.section.content').height()+$('div.section.in-focus-carousel').height()+'px')

	
		// Fade in the js-enabled hidden content
		$('div.splash-hidden').each(function(intIndex) {
			$(this).css('visibility','visible');

			//if we've faded in the last hidden content then fade out the splash image
			if((intIndex+1) == $('div.splash-hidden').length) {
				$('div#beauty-moment').delay(3000).fadeOut(1500);
				homepageCycle(3000);
				$.cookie("homepage-fadeout", true);
				setHomePageColumnHeights($('div.section.content div.splash-hidden'));
			}
		});
		
	}
	else {
		homepageCycle(0);
		//if the cookie is set then the user has already seen the image so don't show it.
		$('div#beauty-moment').css('display','none');
	
		// Fade in the js-enabled hidden content
		$('div.splash-hidden').each(function(intIndex) {
			$(this).css('visibility','visible');
		});
	
	}
	
}

function homepageCycle(delayTime){

	$('div.homepage-carousel-unique')
	.cycle({
		fx:     'fade',
		prev:   '.home-left', 
		next:   '.home-right',
		speed:  500, 
		timeout: 7000, 
		pause: 1,
		delay: delayTime,
		after: onAfter
	});
}
