var rotatorBlock = new Array();
var len = 0;
var itemIndex = 0;
var fTimerID = null;
var buttonTime = 100;
var rotateTime = 8000;

function rotateInit(){
        for (var x=0; x<arguments.length; x++){
            rotatorBlock[x] = arguments[x];
        }
        len = (rotatorBlock.length)-1;
}

function rotate(){
    if (fTimerID != null) {
    	// alert(fTimerID);
    	clearTimeout(fTimerID);
    }
    document.getElementById(rotatorBlock[itemIndex]).style.display = "none";
    document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#ffffff";
    itemIndex++;
    if(itemIndex > len)
        itemIndex = 0;
    document.getElementById(rotatorBlock[itemIndex]).style.display = "block";
    document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#dddddd";
    fTimerID = setTimeout('rotate()', rotateTime);
    return itemIndex;
}

function rotate_to(selection) {
	if (selection != null) {
		if (fTimerID != null) clearTimeout(fTimerID);
		document.getElementById(rotatorBlock[itemIndex]).style.display = "none";
	        document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#ffffff";
		itemIndex = selection;
		document.getElementById(rotatorBlock[itemIndex]).style.display = "block";
       		document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#dddddd";
	}
}
   
function forward() {
    if (fTimerID != null) {
    clearTimeout(fTimerID);
    }
    fTimerID = setTimeout('rotate()', buttonTime);
}


function backwards(){
    if (fTimerID != null) {
    clearTimeout(fTimerID);
    }
    document.getElementById(rotatorBlock[itemIndex]).style.display = "none";
    document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#ffffff";
    itemIndex--;
    if(itemIndex <= -1)
        itemIndex = len;
    document.getElementById(rotatorBlock[itemIndex]).style.display = "block";
    document.getElementById(rotatorBlock[itemIndex] + "_sel").style.backgroundColor = "#dddddd";
    fTimerID = setTimeout('rotate()',rotateTime);
    return itemIndex;
}