var fPos;
var fCount;
var intervalLength = 8000;
var cycleFeaturesInterval;
var hideTimeout;

$(document).ready(function() {
	window.onload = function(){
	}
	$(".featuredArrows").css("display","block");
	hideTimeout = setTimeout("hideArrows()", 700);
	
	fPos = 0;
	fCount = $("#featuredSections").children(".featuredSection").length;
	resetCycle();
	
	$(".featuredArrows #down").click(function(){
		cycleFeatures(true);
	});
	$(".featuredArrows #up").click(function(){
		cycleFeatures(false);
	});
	
	$("#featured").mouseover(function(){
		showArrows();
	});
	$(".featuredArrows").mouseover(function(){
		showArrows();
	});
	$("#featured").mouseout(function(){
		hideTimeout = setTimeout("hideArrows()", 700);
	});
	$(".featuredArrows").mouseout(function(){
		hideTimeout = setTimeout("hideArrows()", 700);
	});
});
function showArrows(){
	$(".featuredArrows").fadeIn("fast");
	clearTimeout(hideTimeout);
}
function hideArrows(){
	$(".featuredArrows").fadeOut("slow");
}
function resetCycle(){
	clearInterval(cycleFeaturesInterval);
	cycleFeaturesInterval = setInterval ( "cycleFeatures(true)", intervalLength );
}
function nextFeatured(){
	$("#featuredSections").animate({marginTop:"-=400px",}, 200, "swing" );
	fPos+=400;
	resetCycle();
}
function previousFeatured(){
	$("#featuredSections").animate({marginTop:"+=400px",}, 200, "swing" );
	fPos-=400;
	resetCycle();
}
function toFirstFeatured(){
	$("#featuredSections").animate({marginTop:"0px",}, 200, "swing" );
	fPos=0;
	resetCycle();
}
function toLastFeatured(){
	var lastPos = (fCount-1) * -400;
	$("#featuredSections").animate({marginTop: lastPos + "px",}, 200, "swing" );
	fPos= lastPos * -1;
	resetCycle();
}

function cycleFeatures (forward){
	if(!forward)
		forward=false;
	else
		forward=true;
	if(forward){
		if(fPos < (fCount-1)*400)
			nextFeatured();
		else
			toFirstFeatured();
	} else {
		if(fPos > 0)
			previousFeatured();
		else
			toLastFeatured();
	}
}
