﻿var lastMenuId = 0;
var timer;
var mSheets = new Array();
var mSheets2 = new Array();
var currZ = 100;
var mReady = false;
var mLeftPos = 326;
var initMailMenus = new Array();

var agt = navigator.userAgent.toLowerCase ();
var is_firefox = ((agt.indexOf ("firefox") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));

function gotoURL(url){
  if (!url) url = "/";
  if (window.event){
    var src = window.event.srcElement; 
    if((src.tagName != 'A') && ((src.tagName != 'IMG') || (src.parentElement.tagName != 'A'))){
      if (window.event.shiftKey) window.open(url);
      else document.location = url;
    }
  } else document.location = url;
}

function setClass(obj, cl){
  if (!cl) cl = '';  
  obj.className = cl;
}

function getParentLeftPos(obj){
  var res = 0;
  while (obj){
	obj = obj.offsetParent;
	if(obj)
	{
		res += obj.offsetLeft;
    }
  }
  return res;
}

function getLeftPos(obj){
  return obj.offsetLeft + getParentLeftPos(obj);
}

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 = 0; c < mSheets.length; c++)  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 menuAddSubmenu(textVal, linkVal, subSheet){
	this.links[this.links.length] = new MenuLink(textVal, linkVal, subSheet);
}

function menuShow(leftVal, topVal){
	this.block.style.left = leftVal + "px";
	this.block.style.top = (topVal+1) + "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(){
    var b=0;
    for (b = (this.links.length-1); b >=0; b--)
    {
        curLink = this.links[b];
        if (curLink.submenu) 
            curLink.submenu.hide();		
    }
}
function menuCreate(path){
	var res = "<div class=\"menu-sh\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"tab-menu-sh\">";
	var curLink;
	var newPath;
	if (path == null) path = "mSheets[" + this.id + "]";
	for (var c = 0; c < this.links.length; c++){
	    curLink = this.links[c];
		res += "<tr><td class=\"";	
		if (curLink.submenu) res += " blk-menu-arr";
		if (!curLink.submenu) res += " blk-menu-sh";	
		if(curLink.action == "")
		    res += " mmncl";	
		res += "\" onmouseover=\"setClass(this, '";
		if (curLink.submenu) res += " blk-menu-arr-act";
		if (!curLink.submenu) res += " blk-menu-sh-act";
		if(curLink.action == "")
		    res += " mmncl";	
		res += "'); ";
		res += path + ".hideCh()";
		
		
		if (curLink.submenu){
			newPath = path + ".links[" + c + "].submenu";
			res += "; " + newPath + ".show(getLeftPos(this) + this.offsetWidth, getTopPos(this)-1)";
			curLink.submenu.create(newPath);
		}
		res += "\" onmouseout=\"";				
		res += "setClass(this, '";
		if (!curLink.submenu) res += "blk-menu-sh";
		if (curLink.submenu) res += " blk-menu-arr";
		if(curLink.action == "")
		    res += " mmncl";	
		res += "')\"";
		if(curLink.action != "")
		    res += "onclick=\"location.href='"+curLink.action+"'\"" 
		res += "nowrap=\"nowrap\">" + curLink.text;		
		res +="</td></tr>";
	}
	res += "</table></div>";
	this.block.innerHTML = res;
	//this.block.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
}

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();
		mSheets[numVal].show(getLeftPos(objVal) + objVal.offsetWidth, getTopPos(objVal)-1);
		menuHideTimerReset();
	}
}

function hideMenu(objVal, numVal){
	if (mReady){
		menuHideTimerSet();
	}
}
