var curMenu = null;
var menuTimer;
var layersloaded;
document.body.onload = init;

function init() {
	layersloaded = true;
	homepageresizer();
	document.body.onresize = homepageresizer;
	rotators[0].init();
	rotators[1].init();
}


var rotators = new Array();
rotators[0] = new ProductRotator('ColadRotatorContainer');
rotators[1] = new ProductRotator('HamachRotatorContainer');

function ProductRotator(containerElement) {
	this.containerElement = document.getElementById(containerElement);
	this.numberOfItems = this.containerElement.children.length;
	this.index = Math.round(Math.random() * (this.numberOfItems -1));
	return this;
}

ProductRotator.prototype.next = rotatorNext;
ProductRotator.prototype.previous = rotatorPrevious;
ProductRotator.prototype.init = rotatorInit;

function rotatorInit() {
	if (this.numberOfItems > 0) this.containerElement.children[this.index].style.display = 'block';
}

function rotatorNext() {
	if (this.numberOfItems > 0) {
		this.containerElement.children[this.index].style.display = 'none';
		this.index = (this.index + 1 + this.numberOfItems) % this.numberOfItems;
		this.containerElement.children[this.index].style.display = 'block';
	}
}

function rotatorPrevious() {
	if (this.numberOfItems > 0) {
		this.containerElement.children[this.index].style.display = 'none';
		this.index = (this.index - 1 + this.numberOfItems) % this.numberOfItems;
		this.containerElement.children[this.index].style.display = 'block';
	}		
}

function updateRotator(iRotator, direction) {
	if (direction > 0) {
		rotators[iRotator].next();
	} else {
		rotators[iRotator].previous();
	}
}

function homepageresizer() {
	if (layersloaded) {
		var offset_left;
		if (document.body.clientWidth < 761) {
			offset_left = 756;
		} else if (document.body.clientWidth > 1004) {
			offset_left = 999;
		} else {
			offset_left = document.body.clientWidth-5;
		}
		iconmenu.style.left = offset_left - iconmenu.clientWidth;
	}
}

function hideMenu() {
	if (curMenu) {
		curMenu.style.display = 'none';
		curMenu = null;
	}
}

function menuOver() {
	if (menuTimer != null) {
		window.clearTimeout(menuTimer);
		menuTimer = null;
	}

	var currentTR = getParentElementByTagName(event.srcElement, "TR");
	if (currentTR && currentTR.subMenuID) {
		hideMenu();

		curMenu = document.all(currentTR.subMenuID);

		curMenu.style.left = getOffsetLeft(currentTR) + currentTR.offsetWidth;
		curMenu.style.top = getOffsetTop(currentTR);
		curMenu.style.display = 'block';
	}
}

function menuOut() {
	menuTimer = window.setTimeout("hideMenu()", 800);
}
