var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
//Позиция меню
var TopPos = 240; 

function setClass(obj, cl){
  if (!cl) cl = "";  
  obj.className = cl;
}

function setClassById(objid, cl){
  obj = document.getElementById(objid);
  setClass(obj, cl);
}

function changeDisplayById(objId){
  for (c = 0; c < changeDisplayById.arguments.length; c++){
    obj = document.getElementById(changeDisplayById.arguments[c]);
    if (obj.style.display == 'none') obj.style.display = 'block';
    else obj.style.display = 'none';
  }  
}

function getParentLeftPos(obj){
  var res = 0;
  while (obj){
	obj = obj.offsetParent;
	if(obj)
	{
		res += obj.offsetLeft;
    }
  }
  return res;
}

function getLeftPos(obj){
  var res = 0;
  while (obj){
    res += obj.offsetLeft-1;
    obj = obj.offsetParent;
  }
  return res;
}

function getTopPos(obj){
  var res = 0;
  while (obj){
    res += obj.offsetTop;    
    obj = obj.offsetParent;
  }
  return res;
}

function MenuLink(textVal, linkVal, subVal){
	this.text = textVal;
	this.action = linkVal;
	this.submenu = subVal;
}

function menuHideAll(){
	for (var c in mSheets) mSheets[c].hide();
}

function menuHideTimerSet(){
	timer = window.setTimeout(menuHideAll, 100);
}

function menuHideTimerReset(){
	if (timer) window.clearTimeout(timer);
}

function menuAddLink(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, null);
}

function menuAddSubmenu(textVal, linkVal){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this));
}

function menuShow(leftVal, topVal){
	this.block.style.left = leftVal + "px";
	this.block.style.top = topVal + "px";
	this.block.style.display = "block";
}

function menuHide(){
	this.hideCh();
	this.block.style.display = "none";
}

function menuFlip(leftVal, topVal){
	var disp = this.block.style.display;
	if (disp == "none") this.show(leftVal, topVal);
	else this.hide();
}

function menuHideCh(){
	for (var c in this.links){
		curLink = this.links[c];
		if (curLink.submenu) curLink.submenu.hide();
	}
}

function menuCreate(path){
	var res = "<div onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"submenu\"><tr>";
	var curLink;
	var newPath;
	if (path == null) path = "mSheets[" + this.id + "]";
	res += "<td class=\"left\">&nbsp;</td>";
	for (var c in this.links){
		curLink = this.links[c];
		res += "<td class=\"submenu_act";
		if (curLink.submenu) res += " blk-menu-arr";
			res += "\" onmouseover=\"setClass(this, 'submenu_inact";
		if (curLink.submenu) res += "";
			res += "'); ";
			res += path + ".hideCh()";
		if (curLink.submenu){
			newPath = path + ".links[" + c + "].submenu";
			res += "; " + newPath + ".show(getLeftPos(this) + this.offsetWidth, getTopPos(this) - 2)";
			curLink.submenu.create(newPath);
		}
		res += "\" onmouseout=\"setClass(this, 'submenu_act";
		if (curLink.submenu) res += " ";
		res += "')\" onclick=\"gotoURL('"+curLink.action+"')\" nowrap=\"nowrap\">" + curLink.text + "</td>";
	}
	res += "<td class=\"right\">&nbsp;</td></tr></table></div>";
	this.block.innerHTML = res;
	this.block.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=90)";
}

function MenuSheet(parentObj){
	this.links = new Array();
	this.addLink = menuAddLink;
	this.addSubmenu = menuAddSubmenu;
	this.create = menuCreate;
	this.show = menuShow;
	this.hide = menuHide;
	this.flip = menuFlip;
	this.hideCh = menuHideCh;
	this.id = lastMenuId;
	lastMenuId++;
	this.parent = parentObj; 
	this.block = document.createElement("DIV");
	this.block.className = "blk-menu";
	this.block.style.position = "absolute";
	this.block.style.display = "none";
	this.block.style.zIndex = currZ;
	currZ++;
	this.block.id = "ms" + this.id;
	document.body.appendChild(this.block);
}

function showMenu(objVal, numVal){
	if (mReady){
		menuHideAll();
		objVal.className = "item act";
		mSheets[numVal].show(mLeftPos, getTopPos(objVal) - 4);
		menuHideTimerReset();
	}
}

function showMenu(objVal, numVal){
	if (mReady){
    	menuHideAll();
		var itemLeftPosition = getLeftPos(objVal);
		var parentAbsoluteWidth = objVal.offsetParent.offsetWidth + getParentLeftPos(objVal);
		mSheets[numVal].block.style.display = "block";
		var subMenuWidth = mSheets[numVal].block.offsetWidth;
		if(itemLeftPosition + subMenuWidth > parentAbsoluteWidth)
		{
			itemLeftPosition = parentAbsoluteWidth - subMenuWidth;
		}
    mSheets[numVal].show(itemLeftPosition, TopPos);
    menuHideTimerReset();
  }  
}

function hideMenu(objVal, numVal){
	if (mReady){
		menuHideTimerSet();
		objVal.className = "item";
	}
}

function loadMenu(){
	for (c = 0; c < 5; c++) mSheets[c] = new MenuSheet(); 

	mSheets[0].addLink("", "");

	mSheets[1].addLink("История Чая", "/tea/history/");
	mSheets[1].addLink("Сорта", "/tea/sorts/");
	mSheets[1].addLink("Астрология", "/tea/astrology/");
	mSheets[1].addLink("Чайная церемония", "/tea/ceremony/");
	mSheets[1].addLink("Рецепты", "/tea/receipt/");

	mSheets[2].addLink("О благовониях", "/aroma/history/");
	mSheets[2].addLink("Ароматерапия", "/aroma/therapy/");
	mSheets[2].addLink("Рекомендации", "/aroma/recommendations/");
	mSheets[2].addLink("Фэн шуй", "/aroma/fengshui/");
	mSheets[2].addLink("Ароматы и гороскоп", "/aroma/hits/");

	mSheets[3].addLink("Украшения для тела", "/decoration/body/");
	mSheets[3].addLink("Украшения для лица", "/decoration/face/");
	mSheets[3].addLink("Фэн шуй", "/decoration/fengshui/");
	mSheets[3].addLink("Татуировки хной", "/decoration/henna/");
	mSheets[3].addLink("Танец живота", "/decoration/dance/");

	mSheets[4].addLink("Конфиденциальность", "/about_us/confidentiality/");
	mSheets[4].addLink("Гарантии", "/about_us/warranty/");
	mSheets[4].addLink("Как сделать заказ?", "/about_us/terms/");
	mSheets[4].addLink("Способы оплаты", "/about_us/payment_method/");

for (var c in mSheets) mSheets[c].create();
mReady = true;
}
