var iCurrentPromo, iMaxPromo, iPromoSeconds, promoTimer;

function changePromo(currentID, newID) {
	if (currentID != newID) {
		oCurrent = $('ws'+currentID);
		oNew = $('ws'+newID);

		// Cross Fade one panel into the other
		new Effect.Parallel(
			[
				Effect.DropOut(oCurrent, { sync: true }),
				Effect.Appear(oNew, { sync: true })
			],
			{ duration: .5 }
		);
		
		iCurrentPromo=newID;
		clearInterval(promoTimer);
		promoTimer = setInterval('changePromo(iCurrentPromo, getNextPromoID())', iPromoSeconds*1000);
	}
}

function getNextPromoID() {
	iCurrentPromo==iMaxPromo ? iCurrentPromo=1 : iCurrentPromo++;
	return iCurrentPromo;
}

function getPrevPromoID() {
	iCurrentPromo==1 ? iCurrentPromo=iMaxPromo : iCurrentPromo--;
	return iCurrentPromo;
}


Event.observe(window, 'load', function() {
	// Initialize values on page load
	iCurrentPromo = 1; //Start with first promo
	iMaxPromo = $('workMainPromo').getElementsByTagName('img').length - 2; // Set the number of promos by the number of images inside the promos container (minus the 2 nav button images)
	iPromoSeconds = 10; //Number of seconds between rotations

	// Enable previous and next buttons
	prevButton = $('btnRotatePrev');
	prevButton.onclick = function() { 
		changePromo(iCurrentPromo, getPrevPromoID()); return false;
	}
	nextButton = $('btnRotateNext');
	nextButton.onclick = function() {
		changePromo(iCurrentPromo, getNextPromoID()); return false;
	}
	
	// Start timer to rotate promos
	promoTimer = setInterval('changePromo(iCurrentPromo, getNextPromoID())', iPromoSeconds*1000); 
});