var tabs = null; // jQuery object pointing at $("#home-hero-container li")
var heroTimeoutId = -1; // setTimeout id for tab auto-advance
var currentTabIndex = -1; // current tab's index in 'tabs'
var heroAutoTime = 9000; // time in milliseconds to switch to the next tab

function heroSelectTab(tab) // 'tab' is the tab to switch to
{
	// if 'tab' isn't the current tab
	if (tabs.index(tab) != currentTabIndex)
	{
		var currentTab = tabs.get(currentTabIndex);
	
		// check to see if currentTabIndex was referring to a real tab
		// if not, this is probably right after the page loaded
		// and there is no tab selected
		if (currentTab) 
			$(currentTab).stop(true).animate({backgroundColor: "#444444", opacity: 0.75}, 200);

		// 0.99 opacity to work around a crazy rendering bug
		$(tab).stop(true).animate({backgroundColor: "#788C07", opacity: 0.99}, 200);

		// make 'tab' our new current tab
		currentTabIndex = tabs.index($(tab));

		// again, check that currentTab is real as we dont 
		// want to fade the background out and in on page load
		if (currentTab)
			$("#home-hero-image")
				.stop(true)
				.animate({opacity: 0}, 250)
				.queue(function(){
					$(this).css('background-position', "0 " + -($(this).height() * currentTabIndex) + "px");
					$(this).dequeue();
				})
				.animate({opacity: 1}, 250);

		// copy the copy from the tab's <p> into the upper area
		// and render it with a custom font
		$("#home-hero-copy").html($(tab).children(".copy").html());
		_typeface_js.replaceText(document.getElementById("home-hero-copy"));

		return true;
	} 
	else
	{
		return false;
	}
};

function heroSetTimeout()
{
	function onTimeout() {
		heroSelectTab(tabs.get((currentTabIndex + 1) % tabs.length));
		heroSetTimeout();	
	};

	heroTimeoutId = setTimeout(onTimeout, heroAutoTime);
};

$(document).ready(function(){

	// Slideshows
	$('#home-hero-slideshow').innerfade({
			animationtype: 'fade',
	        speed: 1500,
	        timeout: 8000,
	        children: 'div'
	});
	
	$('#home-left-slideshow').innerfade({
	        speed: 500,
	        timeout: 5000
	});

	$('#home-logo-marquee').innerfade({
	        speed: 1200,
	        timeout: 5000,
	        containerheight: '86px',
	        children: 'div'
	}).click(function(){window.location="/about/customers/"});
	
	$('#home-logo-marquee-scale').innerfade({
	        speed: 1200,
	        timeout: 5000,
	        containerheight: '86px',
	        children: 'div'
	}).click(function(){window.location="/about/customers/"});

	// Resources Tabs
	$("#home-resource-tabs div a").click(function(){
		$("#home-resource-content").find("div").hide();

		if( $(this).html() == 'Webinars' ) {
			$("#home-resource").css('backgroundPosition', 'left top');
			$("#home-resource-content-webcasts").show();
		} else if( $(this).html() == 'Podcasts' ) {
			$("#home-resource").css('backgroundPosition', 'center top');
			$("#home-resource-content-podcasts").show();
			
		} else if( $(this).html() == 'News' ) {
			$("#home-resource").css('backgroundPosition', 'center top');
			$("#home-resource-content-news").show();

		} else if( $(this).html() == 'Papers' ) {
			$("#home-resource").css('backgroundPosition', 'right top');
			$("#home-resource-content-papers").show();
		}
		$(this).parent('div').siblings().find("a").removeClass('selected-tab');
		$(this).addClass('selected-tab');

		return false;
	});

	// Homepage Hero 
	tabs = $("#home-hero-nav li");
	tabs.bind("mouseenter", function(){heroSelectTab($(this)); clearTimeout(heroTimeoutId)});
	tabs.bind("mouseleave", function(){heroSetTimeout()});

	// set the first tab as the current
	// and setup the auto advance
	heroSelectTab(tabs.get(0));
	heroSetTimeout();
});

