function setClass(obj, name)
{
	obj.className = name;
}

var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var mLeftPos = 326;
var vert = 30;
var initMailMenus = new Array();
var currClassName = null;

var agt = navigator.userAgent.toLowerCase ();
var is_firefox = ((agt.indexOf ("firefox") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));


function MenuLink(textVal, linkVal, subVal){
	this.text = textVal;
	this.action = linkVal;
	this.submenu = subVal;
	if(subVal != undefined && subVal != null) subVal.parentLink = this;
	this.obj = null;
}

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){
    if(arguments.length == 2)
	    this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this));
    else
        this.links[this.links.length] = new MenuLink(textVal, linkVal, new MenuSheet(this, arguments[2]));
}

function menuShow(leftVal, topVal){
	this.block.style.left = leftVal;
	this.block.style.top = topVal;
	this.block.style.display = "block";
	var curr_cssClass = currClassName;
	if(arguments.length == 3)
	{
	    curr_cssClass = arguments[2];
    }
	var els = this.block.getElementsByTagName('td');
	for(var i = 0; i < els.length; i++)
	{
	    if(els[i].AlRender)
	    {
	        els[i].AlRender();
	    }
	}
	if(document.documentElement && (document.documentElement.clientHeight + document.documentElement.scrollTop) < ((topVal.replace('px', '')*1) + this.block.offsetHeight) && ((topVal.replace('px', '')*1) - this.block.offsetHeight + (arguments.length == 3?arguments[2].offsetHeight:0)) > 0)
	{
		this.block.style.top = (topVal.replace('px', '')*1) - this.block.offsetHeight + (arguments.length == 3?arguments[2].offsetHeight:0) + 'px';
	}
	if(document.documentElement && (document.documentElement.clientWidth + document.documentElement.scrollLeft) < ((leftVal.replace('px', '')*1) + this.block.offsetWidth) && ((leftVal.replace('px', '')*1) - this.block.offsetWidth - (arguments.length == 3?arguments[2].offsetWidth:0)) > 0)
	{
		this.block.style.left = (leftVal.replace('px', '')*1) - this.block.offsetWidth - (arguments.length == 3?arguments[2].offsetWidth:0) - 3 + 'px';
	}
	var el = this.block.getElementsByTagName('div');
	if(this.header != null)
	{
		for(var i = 0; i < el.length; i++)
		{
			if(el[i].className && el[i].className.indexOf('menu-sh_header') != -1)
			{
				el[i].innerHTML = this.header;
				break;
			}
		}
	}
	if(curr_cssClass != null)
	{
		this.block.className = curr_cssClass;
	}
}

function menuHide(){
     this._closeHider(this.hider);
     
	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 obj = this.block;
	obj.onmouseout = menuHideTimerSet;
	obj.onmouseover = menuHideTimerReset;
	var res = obj;
	el = document.createElement('div');
	el.className = "menu-sh_header";
	res.appendChild(el);
	el = document.createElement('div');
	el.className = "menu-sh_body";
	res.appendChild(el);
	res = el;
	el = document.createElement('table');
	el.cellSpacing = '0px';
	el.cellPadding = '0px';
	el.border = '0px';
	el.style.width = '100%';
	res.appendChild(el);
	res = el;
	el = document.createElement('tbody');
	res.appendChild(el);
	res = el;
	var curLink;
	var newPath;
	if (path == null) path = "mSheets[" + this.id + "]";
	for (var c in this.links){
		curLink = this.links[c];
		
		var tr = document.createElement('tr');
		res.appendChild(tr);
		var td = document.createElement('td');
		tr.appendChild(td);
		curLink.obj = td;
		td.className = 'blk-menu-sh';
		if (curLink.submenu) td.className += " blk-menu-arr";
		td.parentItem = this.parentLink;
		td.setActive = function(flg)
		{
			if(this.parentItem != undefined && this.parentItem != null)
			{
				this.parentItem.obj.setActive(flg);
			}
			if(flg)
			{
				setClass(this, 'blk-menu-sh-act blk-menu-arr-act');
			}
			else
			{
				setClass(this, 'blk-menu-sh blk-menu-arr');
			}
		}
		var str = '';
		str += "function() { if(this.parentItem){ this.parentItem.obj.setActive(true); } setClass(this, 'blk-menu-sh-act";
		if (curLink.submenu) str += " blk-menu-arr-act";
		str += "'); ";
		str += path + ".hideCh(); ";
		if (curLink.submenu){
			newPath = path + ".links[" + c + "].submenu";
			str += "; " + newPath + ".show(((getLeftPos(this) - document.documentElement.scrollLeft) < (screen.availWidth - this.offsetWidth*2)?(getLeftPos(this) + this.offsetWidth + 3): (getLeftPos(this) - this.offsetWidth - 3)) + 'px', getTopPos(this) + 1 + 'px')";
			curLink.submenu.create(newPath);
		}
		str += " }";
		eval('td.onmouseover = ' + str);
		str = "function() { if(this.parentItem){ this.parentItem.obj.setActive(false); } setClass(this, 'blk-menu-sh";
		if (curLink.submenu) str += " blk-menu-arr";
		str += "'); }";
		eval('td.onmouseout = ' + str);
		
		if(curLink.action.indexOf('javascript:') != -1)
			eval("td.onclick= function(){ " + curLink.action.substr(11) + " }");
		else
			eval("td.onclick= function(){ gotoURL('" + curLink.action + "'); }");
		if(typeof(curLink.text) == "function")
		{
		    td.AlRender = curLink.text;
		}
		else
		    td.innerHTML = curLink.text;
	}
	
	//this.block.className = "menu-sh";
	this.block.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=90)";
	this.block.style.opacity = .90;
}

function _createHider()
{
	var f=null;
	var filter = 'filter:progid:DXImageTransform.Microsoft.alpha(style=0,opacity=0);';
	var id = 'dynarch-menu-hider-' + (this.id);
	//window.document.documentElement.insertAdjacentHTML('beforeEnd', '<iframe id="'+id+'" scroll="no" frameborder="0" '+'style="position:absolute;visibility:hidden;'+filter+'border:0;top:0;left:0;width:0;height:0;" '+'src="javascript:false;"></iframe>');
	//f = window.self.document.getElementById(id);
	//return f;
};

function _setupHider(f, x, y, w, h)
{
	if (f)
	{
		var s=f.style;
		s.left=x+"px";
		s.top=y+"px";
		s.width=w+"px";
		s.height=h+"px";
		s.visibility="visible";
	}
};

function _closeHider(f)
{
	if (f)
		f.style.visibility="hidden";
};

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.header = null;
	this._createHider = _createHider;
     this._setupHider = _setupHider;
     this._closeHider = _closeHider;
	this.id = lastMenuId;
	lastMenuId++;
	this.parent = parentObj; 
	this.block = document.createElement("DIV");
	this.block.className = (arguments.length < 2?"blk-menu":arguments[1]);
	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);
	
	this.hider = this._createHider();
}

function showMenu(objVal, numVal){
	if (mReady){
		menuHideAll();
		objVal.className = "item act";
		if ( is_firefox ) {
			vert = 22;
		} else {
			vert = 22;
		}
		currClassName = null;
		if(arguments.length == 2)
			mSheets[numVal].show(getLeftPos(objVal) + 'px', (getTopPos(objVal) + vert)  + 'px');
		else
		{
			if(arguments.length == 3)
				mSheets[arguments[2]].show(arguments[0], arguments[1]);
			else
			{
				if(arguments.length < 4 || (arguments.length == 5 && arguments[4] == true))
				{
				    currClassName = arguments[3];
				    mSheets[arguments[2]].show(arguments[0], arguments[1]);
			    }
			    else
			    {
			        mSheets[arguments[2]].show(arguments[0], arguments[1], arguments[3]);
			    }
			}
		}
		menuHideTimerReset();
	}
}

function showMenuOnLeft(objVal, numVal){
	if (mReady){
		menuHideAll();
		
		currClassName = null;
		if(arguments.length == 2)
			mSheets[numVal].show((getLeftPos(objVal)+65) + 'px', (getTopPos(objVal))  + 'px');
		else
		{
			if(arguments.length == 3)
				mSheets[arguments[2]].show(arguments[0], arguments[1]);
			else
			{
				if(arguments.length < 4 || (arguments.length == 5 && arguments[4] == true))
				{
				    currClassName = arguments[3];
				    mSheets[arguments[2]].show(arguments[0], arguments[1]);
			    }
			    else
			    {
			        mSheets[arguments[2]].show(arguments[0], arguments[1], arguments[3]);
			    }
			}
		}
		menuHideTimerReset();
	}
}

function hideMenu(objVal, numVal){
    currClassName = null;
	if (mReady){
		menuHideTimerSet();
		objVal.className = "item";
	}
}

function hideMenuOnLeft(objVal, numVal){
    currClassName = null;
	if (mReady){
		menuHideTimerSet();
	}
}

function createMailMenus(){
        //for (c = 0; c < 8; c++) mSheets[c] = new MenuSheet();
		
		/*mSheets[1].hWidthL = '68px';
		mSheets[1].hWidthR = '25px';
		mSheets[1].hHeight = '16px';
          mSheets[1].addLink("Change folder", abs_path + "#");
          //mSheets[1].addLink("Create subfolder", abs_path + "#");
          mSheets[1].addLink("Empty folder", abs_path + "#");
		
          mSheets[2].hWidth = '30px';
          mSheets[2].addSubmenu("Folders", abs_path + "#");
          mSheets[2].addSubmenu("Preferences", abs_path + "#");*/
     for (var c in initMailMenus)
     {
          eval(initMailMenus[c]);
     }

     mReady = true;
}
