var currentRallyFighterFeature = 1;
var transitionDelay = 1400;
var slideDuration = 6000;
var showHideDuration = 450;
var rallyFighterFeatureCount;
var intervalVar;
var hitAreaTopX, hitAreaTopY, hitAreaBottomX, hitAreaBottomY;
var featureSelectOffSrc = "/images/template/slideshowDotted/rallyFighterFeatureSelectOff.png";
var featureSelectOnSrc = "/images/template/slideshowDotted/rallyFighterFeatureSelectOn.png"

$(document).ready( function(){

	// add feature heading mouseover event
//	$("div.rallyFighterFeature").hover(function() {
//		clearInterval(intervalVar);
//		$(this).children(".rallyFighterFeatureSlide").fadeIn(showHideDuration);
//	}, function() {
//		$(this).children(".rallyFighterFeatureSlide").fadeOut(showHideDuration);
//		startSlideshow();
//	});
	
	// feature navigation
	$("div#rallyFighterFeatureChoose a").click(function() { 
		clearInterval(intervalVar); // stop slideshow
		this.blur();
		var currentId = this.href.split("#");
		currentId = parseInt(currentId[1]);
		faderallyFighterFeatures(currentRallyFighterFeature, currentId);
		updateCurrent(currentId);
		startSlideshow(); // resume slideshow
		return false;
	});

	// auto slideshow
	rallyFighterFeatureCount = $(".rallyFighterFeature").length;
	startSlideshow();
	
});

function startSlideshow() {
	if (rallyFighterFeatureCount > 1) {
		intervalVar = setInterval("advanceSlideshow()", slideDuration);
	}
}

function advanceSlideshow() {
	// determine what next slide is
	if (currentRallyFighterFeature == rallyFighterFeatureCount) {
		nextSlide = 1;
	} else {
		nextSlide = currentRallyFighterFeature + 1;
	}
	faderallyFighterFeatures(currentRallyFighterFeature, nextSlide);
	updateCurrent(nextSlide);
}

function faderallyFighterFeatures(oldNum, newNum) {
	$("#rff_" + oldNum).fadeOut(transitionDelay);
	$("#rff_" + newNum).fadeIn(transitionDelay)
}

function updateCurrent(newNum) {
	$("#currentCount").text(newNum.toString());
	currentRallyFighterFeature = newNum;
	$("#rallyFighterFeatureOptions ul li").removeClass("selectedFeature");
	$("#hfl_" + newNum).parent().addClass("selectedFeature");
	$("#rallyFighterFeatureChoose a img").attr("src", featureSelectOffSrc);
	$("#rallyFighterFeatureChoose a img#rff_select_" + newNum).attr("src", featureSelectOnSrc);
}

function showFeatureList(mouseX, mouseY) {
	if ((mouseX < hitAreaTopX) || (mouseY < hitAreaTopY)) {
		return false;
	} else if ((mouseX > hitAreaBottomX) || (mouseY > hitAreaBottomY)) {
		return false;
	} else {
		return true;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}