
Cufon.replace(".splash-nav h3, .splash-nav .detail h3, .splash-nav .detail p");

Cufon.replace("p.title-item", {
	fontFamily: "GEInspiraBook"
});


function animateTitle() {
	$('.title-item')
	.css({
		'opacity': '0'
	})
	.show();
	
	var time = 0;
	$('.title-item').each(function(i) {
		var $this = $(this);
		function dropInItem() {
			$this.animate({ 'opacity': '1' },{ duration: 1000, easing: 'easeOutSine', complete: function() {
				if(i+1 === $('.title-item').length) {
					$('.splash-nav').fadeIn({ easing: 'easeInSine' });
				}				
			}});
		}
		//ease in
		var total = $('.title-item').length;
		var adjust = (time * i)/6;
		var delay = Math.ceil((time * i - adjust)/(total-1));
		setTimeout(dropInItem, delay);
		time = time + 200;	
	});

}




$(function() {

	var isIE = typeof screen.fontSmoothingEnabled != "undefined";
	var isIE6 = isIE && parseInt($.browser.version, 10);
	
	//track mouseover element
	var $overElem;	
	$(document).mousemove(function(e){
	     $overElem = $(e.target);
	}); 
	
	/*
	 * HERO IMAGE
	 */
	var $hero = $('#hero-image');
	var imgs = $hero.attr('data-altimgs').split(',');
		imgs.push($hero.attr('src'))
	var randIndex = (Math.floor(Math.random()*imgs.length));
	$hero.attr('src',imgs[randIndex]).animate({ 'opacity': 'toggle' }, { duration: 1500, easing: 'easeInSine', complete: animateTitle })


	/*
	 * STORY IMAGE
	 */
	//preload
	$('li.splash-item').each(function() {
		var $this = $(this);
		var $img;
		if($this.attr('data-refimage')) {
			$img = $('<img />').attr({
				className: 'story-image',
				src: $this.attr('data-refimage')
			}).insertAfter('#hero-image');
		}
		//store ref
		$this.data('refimage', $img);
	});

	/*
	 * STORY NAV
	 */
	// these values are coded in ie.css as well
	var HEIGHT_OPEN = 145;
	var HEIGHT_CLOSED = 35;
		
	//hover for IE6
	if (isIE6) {
		$('.splash-nav li').hover(function(){
			$(this).addClass('hover_')
		}, function(){
			$(this).removeClass('hover_')
		});
	}

	//keep link from executing
	$('.splash-nav .header a').click(function(e) {
		e.preventDefault()
	});

	//animate bkgd on hover
	$('.splash-nav li').mouseenter(function () {
		var $this = $(this).addClass('active');
		var $bkgd = $(this).find('.bkgd');
		$.doTimeout(100,function() {
			if ($this.hasClass('active')) {
				$bkgd.animate({
					opacity: '0.9'
				},{ queue: false });
			}
		});	
	}).mouseleave(function() {
		var $this = $(this).removeClass('active');
		var $bkgd = $(this).find('.bkgd');
		if (!$this.hasClass('open')) {
			$bkgd.animate({
				opacity: '0'
			},{ queue: false });
		}
	});

	//close nav function
	function closeNavItem() {
		//check for open items
		var $opensib = $('.splash-nav li.open');
		var $bkgd = $opensib.find('.bkgd');
		if ($opensib.length) {
			
			$bkgd.animate({
					opacity: '0'
				}, { 
				duration: 100, 
				queue: false 
			});
			
			$opensib.removeClass('open').animate({
				height: HEIGHT_CLOSED
			}).find('.detail-content').fadeOut().siblings('.header').animate({
				opacity: '1'
			}, function(){
				
				if (isIE) {
					$opensib.find('h3.header').css('text-indent','0');
				}
				if (!$('li.open').length) {
					//fade in the title
					$('#splash-title').fadeIn();
				}
			});
		}
	}

	//attach click event
	$('.splash-nav li').click(function () {
		var $this = $(this);
		if ($this.hasClass('open')) {
			//execute link
			return true;
		}
		
		//REVERSE OUT OPEN ITEM
		closeNavItem();

		//fade out the title
		$('#splash-title').fadeOut();
		
		//DO OPEN ANIMATION
		//mark as open
		$this.addClass('open');

		//handle story images
		$('img.story-image:visible').fadeOut();
		$this.data('refimage').fadeIn();
		
		//do animation
		$this.find('h3.header').animate({opacity: '0'}, { queue: false, complete: function() {
			//IE is not fond of all the filters being used here
			if(isIE) {
				$this.find('h3.header').css('text-indent','-9999px')
			}
		}});		
		$this.animate({
			height: HEIGHT_OPEN
		}, { queue: false, complete: function () {
			$this.find('.detail-content').fadeIn();			
		}});

		//do timeout to revert
		$.doTimeout(10000, function(){
			if ($overElem.parents('ul.splash-nav').length === 0) {
				closeNavItem();
				$('img.story-image:visible').fadeOut();
				return false;
			}
			return true;
		});
		
		//force it
		$('.detail-content').hide()
		
		return false;
	});
		
	
});

