var timer;
var features = Array();
var currentFeature = 0;
var wasClickFromTimer = false;

$(document).ready(function () {
	
	var clickGo = "#";	
	
	$("#homeFeaturesList .homeFeaturesItem").each(function(){
		
		features.push(this);
		
		$(this).click(function(){
		
			clearTimeout(timer);
			var n = 0;
			var me = this;
			
			
			$("#homeFeaturesList .homeFeaturesItem").each(function(i){
				$(this).removeClass("active");
				if(this == me){
					n = i;
				}
			});
			
			$("#hfdTitle").html($(this).find(".homeFeatureTitle").html());
			$("#hfdDesc").html($(this).find(".homeFeatureDescription").html());
			$("#homeFeaturesImage").attr("href",$(this).find(".homeFeatureUrl").html());
			var goto = 290 - $("#homeFeaturesDescription").height() - 20;
			$("#homeFeaturesDescription").css("margin",goto+"px 0 0 1px");
			
			$("#homeFeaturesImage img").each(function(x){
				$(this).removeClass("active");
				if(x == n) $(this).addClass("active");
			});
			
			$(this).addClass("active");
			
			if(wasClickFromTimer) makeTimer();
			
			return false;
		});
		
	});
	
	$('ul#primaryLinks').superfish(
            {speed:0}); 
	
	var goto = 290 - $("#homeFeaturesDescription").height() - 20;
	$("#homeFeaturesDescription").css("margin",goto+"px 0 0 1px");
	
	makeTimer();

	
});

if(typeof sIFR == "function"){
	sIFR.replaceElement(named({sSelector:".content h2", sFlashSrc:"/flash/sifr.swf", sWmode:"transparent", sColor:"#a39f81", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:13}));
}

function makeTimer(){
	clearTimeout(timer);
	timer = setTimeout(timerHandler, 5000);
}

function timerHandler(){
	currentFeature++;
	if(currentFeature >= features.length) currentFeature = 0;
	wasClickFromTimer = true;
	$(features[currentFeature]).trigger('click');
	wasClickFromTimer = false;
}