// JavaScript Document
(function ($) {
	var a = {},
	c = "doTimeout",
	d = Array.prototype.slice;
	$[c] = function () {
		return b.apply(window, [0].concat(d.call(arguments)))
	};
	$.fn[c] = function () {
		var f = d.call(arguments),
		e = b.apply(this, [c + f[0]].concat(f));
		return typeof f[0] === "number" || typeof f[1] === "number" ? this: e
	};
	function b(k) {
		var l = this,
		g, i = {},
		m = arguments,
		h = 4,
		f = m[1],
		j = m[2],
		o = m[3];
		if (typeof f !== "string") {
			h--;
			f = k = 0;
			j = m[1];
			o = m[2]
		}
		if (k) {
			g = l.eq(0);
			g.data(k, i = g.data(k) || {})
		} else {
			if (f) {
				i = a[f] || (a[f] = {})
			}
		}
		i.id && clearTimeout(i.id);
		delete i.id;
		function e() {
			if (k) {
				g.removeData(k)
			} else {
				if (f) {
					delete a[f]
				}
			}
		}
		function n() {
			i.id = setTimeout(function () {
				i.fn()
			},
			j)
		}
		if (o) {
			i.fn = function (p) {
				o.apply(l, d.call(m, h)) && !p ? n() : e()
			};
			n()
		} else {
			if (i.fn) {
				j === undefined ? e() : i.fn(j === false);
				return true
			} else {
				e()
			}
		}
	}
})(jQuery);

/***************************************************************************/
$(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);
	}); 
	

	var HEIGHT_OPEN = 136;
	var HEIGHT_CLOSED = 37;
		
	//hover for IE6
	if (isIE6) {
		$('.splash-nav li').hover(function(){
			$(this).addClass('hover_')
		}, function(){
			$(this).removeClass('hover_')
		});
	}

	//keep link from executing
	$('.splash-nav .headerNav 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('.headerNav').animate({
				opacity: '1'
			}, function(){
				
				if (isIE) {
					$opensib.find('h3.headerNav').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.headerNav').animate({opacity: '0'}, { queue: false, complete: function() {
			//IE is not fond of all the filters being used here
			if(isIE) {
				$this.find('h3.headerNav').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;
	});
		
	
});

