/**
 * JS class for handling the top menu and mega menu of grimm-berlin.de
 * 
 * requires Prototype 1.6.1
 * 
 * @copyright  (c)2009 Spion Media GmbH
 * @author     Uwe Mesecke <um@spion-media.eu>
 */

GrimmTopMenu = function(menuCount, menuPrefix, buttonPrefix)
{
	this.menuPrefix = menuPrefix;
	this.buttonPrefix = buttonPrefix;
	this.displayMask = 0;
	this.lastMask = 0;
	this.menuCount = menuCount;
};

GrimmTopMenu.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.mega-menu").retrieve('topMenuItem')); }.bind(this));
		menu.observe('mouseout', function(event) { this.leaveMenu(event.findElement("div.mega-menu").retrieve('topMenuItem')); }.bind(this));
		
		var topValue = Math.floor((menu.getHeight() - 246) / 2);
		var selector = new Selector(".mega-menu-column-separator");
		var seps = selector.findElements(menu);
		seps.each(function (element) {
			element.setStyle({
				top: topValue+"px"
			});
		});
	}
};

GrimmTopMenu.prototype.enterButton = function(i)
{
	this.displayMask = 1 << ((i-1) * 3);
	this.updateMenuDisplay();
};

GrimmTopMenu.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);
};

GrimmTopMenu.prototype.enterMenu = function(i)
{
	this.displayMask = 2 << ((i-1) * 3);
	this.updateMenuDisplay();
};

GrimmTopMenu.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);
};

GrimmTopMenu.prototype.updateMenuDisplay = function()
{
	if (this.displayMask == this.lastMask) {
		return;
	}
	if (this.lastMask == 0) {
		$('header-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) {
		$('header-navigation').removeClassName('active');
	}
};

GrimmTopMenu.prototype.showMenu = function(i)
{
	var button = $(this.buttonPrefix + (i+1));
	if (!button) {
		return;
	}
	button.addClassName("active");
	if (i > 0) {
		var leftBtn = $(this.buttonPrefix + i);
		if (leftBtn) {
			leftBtn.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();
	}
};

GrimmTopMenu.prototype.hideMenu = function(i)
{
	var button = $(this.buttonPrefix + (i+1));
	if (!button) {
		return;
	}
	button.removeClassName("active");
	if (i > 0) {
		var leftBtn = $(this.buttonPrefix + i);
		if (leftBtn) {
			leftBtn.removeClassName("right-active");
		}
	}
	
	var menu = $(this.menuPrefix + (i+1));
	if (!menu) {
		return;
	}
	menu.hide();
};
