// global timer object
var timer = null;
// global counter of current image
var curEl = 1;
// Rotation running:
var rotationRunning = false;

var buttons = new (function() {
    this.current = 0;

    this.deselect = function(i) {
        if (i == 0) return;
		$("submenu" + i).removeClassName("hovering");
        $("rollover-image-" + i).fade({duration: 0.5});
    };

    this.select = function(i) {
        this.deselect(this.current);
        if (i == 0) return;
        this.current = i;
        $("submenu" + i).addClassName("hovering");
        $("rollover-image-" + i).appear({duration: 0.5});
    }
})();

// function that selects next element
function nextImage()
{
	if(curEl >= 5) curEl = 1;
	else curEl++;
	// set next image
	buttons.select(curEl);
}

// function to start auto looping timer
function startRotation(interval)
{
	timer = setInterval("nextImage()",(interval));
    rotationRunning = true;
}
// function to stop auto looping timer
function stopRotation()
{
	clearInterval(timer);
    rotationRunning = false;
}
// dom ready event 
document.observe('dom:loaded', function() {
    /*
    // Only do this if we have a container
    var container = $("rollover-container"); // div with id rollover-container
    if (container == null) return;
	
	var submenu = $("submenu"); // div with id submenu
	if (submenu == null) return;
	
	submenu.observe('mouseover', function(event)
	{
		if (rotationRunning) { stopRotation(); }
	});
	submenu.observe('mouseout', function(event)
	{
		if (!rotationRunning) { startRotation(5000); }
	});
	
	var submenuitems = submenu.down(0).childElements();
	
	// loop door submenu items
	submenuitems.each(function(el,index)
	{
		var selectedItem = (index+1);
		// add mouseover event to each a element inside li submenuitem
		el.observe('mouseover', function(event)
		{
			//buttons.deselect(curEl);
			if (rotationRunning) { stopRotation(); }
			buttons.select(selectedItem);
		});
		
		// add mouseout event to each a element inside li submenuitem
		el.observe('mouseout', function(event)
		{
			if (rotationRunning) { stopRotation(); }
			//buttons.deselect(selectedItem);
		});
	});
	
	// start auto rotation
    // Samy: allow some page elements to load first.. and select the first button to start with
	buttons.select(1);
    */
    setTimeout('startRotation(5000)', 500); // interval 5 sec
});
