/*
 * MegaMenu Generator by kuhlaGen.org 
 */

MyTopMenu = function(menuCount, menuPrefix, buttonPrefix)
{
	this.menuPrefix = menuPrefix;
	this.buttonPrefix = buttonPrefix;
	this.displayMask = 0;
	this.lastMask = 0;
	this.menuCount = menuCount;
};

MyTopMenu.prototype.initialize = function()
{
	for (var i=1; i <= this.menuCount; i++) {
		var t = $(this.menuPrefix + i);
		
		var button = $(this.buttonPrefix + i);
		if (!button) {
			continue;
		}
		button.store('topMenuItem', i);
		button.observe('mouseover', function(event) { this.enterButton(event.findElement("a").retrieve('topMenuItem')); }.bind(this));
		button.observe('mouseout', function(event) { this.leaveButton(event.findElement("a").retrieve('topMenuItem')); }.bind(this));
		
		var menu = $(this.menuPrefix + i);
		if (!menu) {
			continue;
		}
		menu.store('topMenuItem', i);
		menu.observe('mouseover', function(event) { this.enterMenu(event.findElement("div.megamenu").retrieve('topMenuItem')); }.bind(this));
		menu.observe('mouseout', function(event) { this.leaveMenu(event.findElement("div.megamenu").retrieve('topMenuItem')); }.bind(this));
		
		var topValue = Math.floor((menu.getHeight() - 246) / 2);
		var selector = new Selector(".megamenu-column-separator");
		var seps = selector.findElements(menu);
		seps.each(function (element) {
			element.setStyle({
				top: topValue+"px"
			});
		});
	}
};

MyTopMenu.prototype.enterButton = function(i)
{
	this.displayMask = 1 << ((i-1) * 3);
	this.updateMenuDisplay();
};

MyTopMenu.prototype.leaveButton = function(i)
{
	this.displayMask |= 4 << ((i-1) * 3);
	this.displayMask &= ~(1 << ((i-1) * 3));
	window.setTimeout(function() {
		this.displayMask &= ~(4 << ((i-1) * 3));
		this.updateMenuDisplay();
	}.bind(this), 50);
};

MyTopMenu.prototype.enterMenu = function(i)
{
	this.displayMask = 2 << ((i-1) * 3);
	this.updateMenuDisplay();
};

MyTopMenu.prototype.leaveMenu = function(i)
{
	this.displayMask |= 4 << ((i-1) * 3);
	this.displayMask &= ~(2 << ((i-1) * 3));
	window.setTimeout(function() {
		this.displayMask &= ~(4 << ((i-1) * 3));
		this.updateMenuDisplay();
	}.bind(this), 50);
};

MyTopMenu.prototype.updateMenuDisplay = function()
{
	if (this.displayMask == this.lastMask) {
		return;
	}
	if (this.lastMask == 0) {
		$('mega-navigation').addClassName('active');
	}
	for (var i=0; i < this.menuCount; i++) {
		var flags = this.displayMask & 7 << (i*3);
		if (flags > 0) {
			this.showMenu(i);
		} else {
			this.hideMenu(i);
		}
	}
	
	this.lastMask = this.displayMask;
	if (this.displayMask == 0) {
		$('mega-navigation').removeClassName('active');
	}
};

MyTopMenu.prototype.showMenu = function(i)
{
	var button = $(this.buttonPrefix + (i+1));
	if (!button) {
		return;
	}
	button.addClassName("active");
	if (i > 0) {
		var leftnumber = $(this.buttonPrefix + i);
		if (leftnumber) {
			leftnumber.addClassName("right-active");
		}
	}
	
	var menu = $(this.menuPrefix + (i+1));
	if (!menu) {
		return;
	}
	if (this.lastMask == 0) {
		window.setTimeout(function() {
			var obj = this[0];
			var menu = this[1];
			var i = this[2];
			var flags = obj.displayMask & 7 << (i*3);
			if (flags > 0) {
				menu.show();
			}
		}.bind([this, menu, i]), 200);
	} else {
		menu.show();
	}
};

MyTopMenu.prototype.hideMenu = function(i)
{
	var button = $(this.buttonPrefix + (i+1));
	if (!button) {
		return;
	}
	button.removeClassName("active");
	if (i > 0) {
		var leftnumber = $(this.buttonPrefix + i);
		if (leftnumber) {
			leftnumber.removeClassName("right-active");
		}
	}
	
	var menu = $(this.menuPrefix + (i+1));
	if (!menu) {
		return;
	}
	menu.hide();
};

