var currentFeature = 0;
var totalFeatures = 0;
var timer = 0;
//
$().ready(function () {
	totalFeatures = $("#main_feature").children().length;
	$("#main_feature").children().hide();
	$("#main_feature_nav p").html(currentFeature + " of " + totalFeatures);
	for (var i=0; i<totalFeatures; i++) {
		$("#main_feature_pagethrough").append("<li><a id=\"mfa"+i+"\"><span>" + (i+1) + "</span></li>");
		$("#mfa"+i).bind("click", function () { selectFeature($(this)); });
	}
	$(".buttons .previous").bind("click", function () { goPreviousFeature(); });
	$(".buttons .next").bind("click", function () { goNextFeature(); });
	//
	showFeature(0);
});
function startTimer () { timer = setInterval(function () { goNextFeature(); }, 5000); }
function stopTimer () { clearInterval(timer); }
function showFeature (which) {
	stopTimer();
	$("#main_feature_pagethrough a.active").removeClass("active");
	$("#mfa"+which).addClass("active");
	$("#main_feature_nav p").html((which+1) + " of " + totalFeatures);
	$("#main_feature").children().hide();	
	var child = $("#main_feature").children()[which];
	$(child).show();
	currentFeature = which;
	startTimer();
}
function selectFeature (which) {
	var idstr = $(which).attr("id");
	var index = idstr.substring(idstr.length-1);
	showFeature(parseInt(index));
}
function goNextFeature () {
	var next = currentFeature + 1;
	if (next >= totalFeatures) next = 0;
	showFeature(next);
}
function goPreviousFeature () {
	var prev = currentFeature - 1;
	if (prev < 0) prev = totalFeatures-1;
	showFeature(prev);
}