// bigWebApps drop down menus

var TMenus = new Class.create();

TMenus.prototype = {
	items: [],
	drops: [],

	initialize: function() {
	},

	add: function(element, index) {
		Element.extend(element);
		this.items.push(element);
		if($("ddi-" + index.toString()) != null) {
			this.drops.push($("ddi-" + index.toString()));
			var de = this.drops[this.drops.length - 1].descendants();
			var last = Element.extend(de[de.length - 1]);
			Element.addClassName(last, "wb");
		}
		Event.observe(this.items[index], "mousemove", this.onMouseMoveListener.bindAsEventListener(this));
	},
	
	onMouseMoveListener: function(event) {
		var id = Event.element(event).parentNode.id;
		var index = parseInt(id.substr(id.indexOf("-") + 1, 1), 10);
		this.hideAllDrops();
		var aIndex = this.hasDrop(index);
		if(aIndex > -1) {
			this.show(aIndex, index);
		}
	},
	
	hasDrop: function(index) {
		for(var dI = 0; dI < this.drops.length; dI++) {
			if(this.drops[dI].id == "ddi-" + index.toString()) { return dI; }
		}
		return -1;
	},
	
	onMouseOutListener: function(event) {
		this.hideAllDrops();
	},
	
	hideAllDrops: function() {
		for(var dI = 0; dI < this.drops.length; dI++) {
			this.hide(dI);
		}
	},
	
	hide: function(index) {
		if(this.drops[index] != null) {
			this.drops[index].setStyle( { display: "none" } );
		}
	},
	
	show: function(index, oIndex) {
		if(this.drops[index] != null) {
			offset = Position.cumulativeOffset(this.items[oIndex]);
			offset[1] += this.items[oIndex].getHeight();
			this.drops[index].setStyle( { display: "block", top: offset[1] + "px", left: offset[0] + "px" } );
		}
	}
};

var menusGlobal = null;

Event.observe(window, "load", defineMenus);

function defineMenus() {
	for(var Y = 0; Y < 10; Y++) {
		if($("mmi-" + Y.toString()) == null) {
			break;
		}
	}
	menusGlobal = new TMenus();
	for(var I = 0; I < Y; I++) {
		menusGlobal.add($("mmi-" + I.toString()), I);
	}
	Event.observe($("m-block"), "mousemove", closeDrops);
	Event.observe($("searchForm"), "mousemove", closeDrops);
	Event.observe($("search-block"), "mousemove", closeDrops);
	Event.observe($("logotype"), "mousemove", closeDrops);
}

function closeDrops() {
	menusGlobal.hideAllDrops();
}