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

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

BikemarketTopMenu.prototype.initialize = function()
{
	for (var i=1; i <= this.menuCount; i++) {
		var t = $(this.menuPrefix + i);
		var menu = $(this.menuPrefix + i);
		var button = $(this.buttonPrefix + i);
		if (!button) {
			continue;
		}
		
		button.store('topMenuItem', i);
		button.observe('mouseover', function(event) { this.enterButton(event.findElement().retrieve('topMenuItem')); }.bind(this));
		button.observe('mouseout', function(event) { this.leaveButton(event.findElement().retrieve('topMenuItem')); }.bind(this));
		
		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));
		
	}
};

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

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

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

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

BikemarketTopMenu.prototype.updateMenuDisplay = function()
{
	if (this.displayMask == this.lastMask) {
		return;
	}
	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;
};

BikemarketTopMenu.prototype.showMenu = function(i)
{
	var menu = $(this.menuPrefix + (i+1));
	var btn = $(this.buttonPrefix + (i+1));
	
	if (this.lastMask == 0) {
		window.setTimeout(function() {
			var obj = this[0];
			var menu = this[1];
			var btn = this[2];
			var i = this[3];
			var flags = obj.displayMask & 7 << (i*3);
			if (flags > 0) {
				if (menu) menu.show();
				btn.addClassName('top-menu-button-active');
			}
		}.bind([this, menu, btn, i]), 200);
	} else {
		if (menu) menu.show();
		btn.addClassName('top-menu-button-active');
	}
};

BikemarketTopMenu.prototype.hideMenu = function(i)
{
	var menu = $(this.menuPrefix + (i+1));
	var btn = $(this.buttonPrefix + (i+1));
	
	if (menu) menu.hide();
	if (btn) btn.removeClassName('top-menu-button-active');
};
