/*=======================================
	Home JS functions
	(c)Chem-Dry :: Modern Blue
========================================*/

$(document).ready(function() {

	//get the links ready
	$("#rotateNav a").each( function() {
		$(this).mouseover(function() {
			var id = $(this).attr("id");
			activateHeroLink(id,1);				   
		});
	});

	//auto rotate the links
	$.t = setTimeout("heroAutoRotate()",5000);
	
	//GA event tracking
	$('a').click(function () {
		
		var tlink = $(this);
		
		try
		{
			_gaq.push(['_trackEvent', 'Homeclick', tlink.attr('rel'), tlink.attr('title')]);
		}
		catch (err) {}

    });
	
});

//active the link and move the image
var activateHeroLink = function(id,stopTimer) {
	removeHeroActiveLinks();
	$("#"+id).addClass("active");
	showHeroImage(id);	
	if(stopTimer == 1) {
		clearTimeout($.t);	
	}
}

//switch to the correct hero image base on click
var showHeroImage = function(id) {
	$("#rotateImg li").each( function() {
		$(this).hide();								   
	});
	$("#"+ id + "Img").fadeIn("slow");
}

//removes any active state links (orange bg)
var removeHeroActiveLinks = function() {
	$("#rotateNav a").each( function() {
		$(this).removeClass("active");						   
	});
}

//auto rotate through the headers until there is a click
var heroAutoRotate = function() {
	//detect the active link and move forward
	var id = $("#rotateNav li").children(".active").parent("li").next().children("a").attr("id");
	if(!id) {
		var id = 'advantage';	
	}
	activateHeroLink(id,0);
	$.t = setTimeout("heroAutoRotate()",5000);
}