// Swaps out the current promo with the new promo
function changePromo(currentID, newID) {
	if (currentID != newID) {
		// Gets the arrow marker and current position
		oMarker = $('mainFeatureMarker');
		markerLocationX = oMarker.offsetLeft;
		markerLocationY = oMarker.offsetTop;

		// Create an array of all possible marker locations
		var aMarkerLocations = [24,94,164,234,304];
		
		newMarkerLocationY = aMarkerLocations[newID-1];
		
		new Effect.Move (oMarker,{ duration: .5, x: markerLocationX, y: newMarkerLocationY, mode: 'absolute', 
			afterFinish: function() {
				oCurrentNav = $('promoNav'+currentID);
				oCurrentNav.removeClassName('selected');
				oNewNav = $('promoNav'+newID);
				oNewNav.addClassName('selected');
				
				// Gets the promo elements to be switched
				oCurrent = $('mainFeature'+currentID);
				oNew = $('mainFeature'+newID);
				
				// Cross fades the promo elements
				Effect.Fade(oCurrent, { duration: .5, from: 1.0, to: 0.0, queue: { position: 'end', scope: 'promo', limit: 4 } });
				Effect.Appear(oNew, {queue: { position: 'start', scope: 'promo', limit: 4 } });
			}
		});

		// Updates variables and resets timer				
		iCurrent=newID;
		clearInterval(timer);
		timer = setInterval("changePromo(iCurrent, getNextID())", iSeconds*1000);
	}
}

function getNextID() {
	if (iCurrent==iMax) {
		iCurrent=1;
	} else {
		iCurrent++;
	}
	return iCurrent;
}
		
function sameHeightColumns(columnOne, columnTwo) {
	// Get the two columns to compare
	var oColumnOne = $(columnOne);
	var oColumnTwo = $(columnTwo);

	// Check that the columns exist and exit if they do not
	if (!oColumnOne || !oColumnTwo) return false;

	if (oColumnOne.className == 'featureHome') {
		if (oColumnOne.offsetHeight < oColumnTwo.offsetHeight) oColumnOne.style.height = (oColumnTwo.offsetHeight-25) + 'px';
	}
	else if (oColumnOne.className == 'featureAux') {
		// Gets the special promo object
		var oPromo = $('specialPromotion');
		if (!oPromo) return false; 
	
		if ((oColumnOne.offsetHeight + oPromo.offsetHeight) < oColumnTwo.offsetHeight) {
			oPromo.style.height = (oColumnTwo.offsetHeight-oColumnOne.offsetHeight-20) + 'px';
		} else {
			oColumnTwo.style.height = (oColumnOne.offsetHeight + oPromo.offsetHeight - 5) +'px';
		}
	}
}
		
		
Event.observe(window, 'load', function() {
	if ($('mainFeatureBox')) {
		sameHeightColumns('mainFeatureBox', 'auxFeatureBox');
	
		iCurrent=1;
		iMax=5; // Change the iMax number below when adding more promos
		iSeconds=10; // Number of seconds between promos
		
		// Fade in the initial promo and sets the timer
		var oCurrent = $('mainFeature'+iCurrent);
		Effect.Appear(oCurrent, {queue: { position: 'start', scope: 'promo', limit: 4 } });
		timer = setInterval("changePromo(iCurrent, getNextID())", iSeconds*1000);
	}
});