var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var TopPos = 256; 

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.parentNode.parentNode.parentNode;
	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, 750);
}

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 - 13 + "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()">';
	res += '<div class="l"></div>';
	res += '<div class="r"></div>';
	
	var curLink;
	var newPath;
	if (path == null) path = "mSheets[" + this.id + "]";
	res += "<ul>";
	
	var i = 0;
	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 += '<li' + (i == 0 ? ' class="first"' : '') + '><a href="' + curLink.action + '">' + curLink.text + '</a></li>';
		
		i++;
	}
	
	res += '</ul>';
	res += '</div>';
	this.block.innerHTML = res;
	
	// this.block.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=90)";
}

/*
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 = "ddmenu";
	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.parentNode.parentNode.parentNode.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(){
	
	var base = '';
	
	for (c = 0; c < 5; c++) mSheets[c] = new MenuSheet(); 

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

	mSheets[1].addLink("Flowers language", base + "/flowers/language/");
	mSheets[1].addLink("Flower symbolic", base + "/flowers/symbols/");
	mSheets[1].addLink("Flowers horoscope", base + "/flowers/horoscope/");
	mSheets[1].addLink("Love poems", base + "/flowers/types/");
	mSheets[1].addLink("Flower Care", base + "/flowers/care/");
	
	mSheets[2].addLink("Christmas", base + "/holidays/christmas/");
	mSheets[2].addLink("New Year", base + "/holidays/newyear/");
	mSheets[2].addLink("Valentine's Day", base + "/holidays/valentine/");
	mSheets[2].addLink("Women's Day", base + "/holidays/march8/");
	mSheets[2].addLink("The Army Day - Men\'s Day", base + "/holidays/february23/");
	mSheets[2].addLink("Other holidays", base + "/holidays/other/");
	
	
	mSheets[4].addLink("Privacy", base + "/about_us/confidentiality/");
	mSheets[4].addLink("Guarantee", base + "/about_us/warranty/");
	mSheets[4].addLink("Order placement?", base + "/about_us/terms/");
	mSheets[4].addLink("Payment procedures", base + "/about_us/payment_method/");

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