// JavaScript Document

var promos, slider, bannerSectionW, overLis, innerBackgrounds, current, t, timeout = 4000;

$(function() {
	promos = $(".homePromos > li");
	current = Math.floor(Math.random()*promos.length);
	slider = $("#bannerSlider");
	innerBackgrounds = $(".innerBackground");
	innerBackgrounds.eq(current).fadeTo(1000, 1);
	bannerSectionW = $(".bannerSection").outerWidth();
	promos.css({ position:"relative" }).each(function() {
		$(this).prepend("<div class=\"overLi\"></div>");
	});
	slider.css({ left:-bannerSectionW*current });
	overLis = $(".overLi");
	overLis.css({ position:"absolute", top:0, left:0, width:promos.eq(0).outerWidth(), height:promos.eq(0).outerHeight(), zIndex:15, cursor:"pointer", background:"#000000" })
		.fadeTo(0, 0)
		.mouseover(mouseOver)
		.mouseout(mouseOut)
		.click(mouseClick);
	t = setTimeout(next, timeout);
});

function mouseOver(forced) {
	clearTimeout(t);
	index = forced >= 0 ? forced : overLis.index(this);
	current = index;
	innerBackgrounds.clearQueue().each(function() {
		if(innerBackgrounds.index(this) == index) {
			$(this).hide().fadeTo(200, 1);
		} else {
			$(this).fadeTo(200, 0);
		}
	});
	slider.clearQueue().animate({ left:-bannerSectionW*index });
}

function mouseOut() {
	clearTimeout(t);
	t = setTimeout(next, timeout);	
}

function mouseClick() {
	window.location = $(this).parent().find("a").attr("href");
}

function next() {
	clearTimeout(t);
	current = current + 1 < promos.length ? current + 1 : 0;
	mouseOver(current);
	t = setTimeout(next, timeout);
}
