/**
  * Menu class
  * 
  * @menuType int - (0 - horizontal menu, 1 - vertical menu, 2 - use both coordinates)
  * @menuPrefix string - prefix to menu item id's in html
  * @submenuPrefix string - prefix to submenu item id's in html
  * @timer int - timeout to hide menu
  * @menuName string - name of created object of this class
  * @parentBlock string - block in which menu must place
  * @mainOverAction function(elem, id) - action when showmenu event occurs above object
  * @mainOutAction function(elem, id) - action when hidemenu event occurs above object
  **/

function Menu(menuType, menuPrefix, submenuPrefix, timer, menuName, parentBlock, mainOverAction, mainOutAction, blockCorrection)
{
	this.elem = null;
	this.id = null;
	this.type = menuType;
	this.menuPrefix = menuPrefix;
	this.submenuPrefix = submenuPrefix;
	this.timer = timer;
	this.menuName = menuName;
	this.parentBlock = null;
	this.mainOverAction = mainOverAction;
	this.mainOutAction = mainOutAction;
	this.blockCorrection = blockCorrection;

	if (parentBlock != null) this.parentBlock = document.getElementById(parentBlock);

	this.show = function (id)
	{
		if (!id) return;
		if (this.elem) this.hideNow();

		this.over(id);
	}

	this.hide = function (id)
	{
		if (this.mainOutAction != null) this.mainOutAction(this.elem, this.id);

		this.elem.display = "none";
		this.elem = null;
		this.id = null;
	}

	this.findPos = function (obj)
	{
		var curleft = curtop = 0;

		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curtop, curleft];
	}

	this.over = function (id)
	{
		var open;
		var docwidth, docheight;

		if(!id) return;
		this.id = id;

		if(this.menu_timer){
			clearTimeout(this.menu_timer);
			this.menu_timer=0;
		}

		open = document.getElementById(this.submenuPrefix + id);

		if(!open) {
			if(this.ie4) open = document.all[this.submenuPrefix + id];
			else if(this.nn4) open = document.layers[this.submenuPrefix + id];
		}

		if(!open)return;

		var open_el = open;

		if(!this.nn4)open = open.style;

		if(this.elem && open!=this.elem) this.hideNow();
		this.id = id;

		//opera Netscape 6 Netscape 4x Mozilla 
		if (window.innerWidth || window.innerHeight){ 
			docwidth = window.innerWidth; 
			docheight = window.innerHeight; 
		} 
		//IE Mozilla 
		if (document.body.clientWidth || document.body.clientHeight){ 
			docwidth = document.body.clientWidth; 
			docheight = document.body.clientHeight; 
		} 	

		var menu_item = document.getElementById(this.menuPrefix + id);

		var item_coord = this.findPos(menu_item);

		if (this.mainOverAction != null) this.mainOverAction(open, this.id);

		if (this.type == 0) {
			open.left = 0;
			open.display = "block";
			if (this.parentBlock != null) {
				var blockWidth = this.parentBlock.offsetWidth;
				var blockLeft = this.findPos(this.parentBlock);
				blockLeft = blockLeft[1];
				var elWidth = open_el.offsetWidth;
				var offset = item_coord[1];

				if (elWidth + offset > blockWidth) {
					open.left = item_coord[1] - (elWidth + offset - blockWidth - blockLeft + this.blockCorrection);
				} else open.left = item_coord[1];
			} else {
				open.left = item_coord[1];
			}
		}
		if (this.type == 1) {
			open.top = 0;
			open.display = "block";
			open.top = item_coord[0];
		}

		this.elem = open;
	}

	this.out = function ()
	{
		if (this.elem) this.menu_timer = setTimeout(this.menuName + '.hideNow()', this.timer);
	}

	this.hideNow = function ()
	{
		if (this.elem) this.hide(this.id);
		this.elem = 0;
		this.id = 0;
	}

}
