var lastMenuId = 0;
var timer;
var mSheets = new Array();
var currZ = 100;
var mReady = false;
var mLeftPos = 170;
var mLeftPosH = 397;
var abs_path = 'http://soft-person:81/';
var BType = 1;
var initMailMenus = new Array();

if(navigator.appName == 'Microsoft Internet Explorer')
{
	if(navigator.userAgent.indexOf('Opera') == '-1') BType = 1;
	else BType = 2;
}
else
{
	BType = 4;
	if(navigator.appName == 'Netscape')
	{
		if(navigator.vendor == 'Firefox') BType = 3;
	}
}

/*for(i in navigator)
{
	BType += i + ' = ' + navigator[i] + "\n";
}*/

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 menuMailCreate(path){
        var res = "<div class=\"menu-sh\" onmouseout=\"menuHideTimerSet()\" onmouseover=\"menuHideTimerReset()\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"tab-menu-sh\">";
        var curLink;
        var newPath;
          var hFlag = false;
        if (path == null) path = "mSheets[" + this.id + "]";
        for (var c in this.links){
				if(!hFlag)
				{
					res += "<tr><td class=\"blk-menu-htr\" width=\"" + this.hWidthL + "\" style=\"height: " + this.hHeight + "; font-size: 10px;";
					if(this.hLink) res += " cursor: pointer;";
					res += "\"";
					if(this.hLink) res += " onclick=\"location.href='" + this.hLink + "';\"";
					res += ">&nbsp;</td><td width=\"" + this.hWidthR + "\" style=\"height: " + this.hHeight + ";\" class=\"blk-menu-htl\">&nbsp;</td></tr>";
				}
                curLink = this.links[c];
                res += "<tr><td colspan=\"2\" class=\"blk-menu-sh";
				if(hFlag) res += " blk-menu-shb";
                if (curLink.submenu) res += " blk-menu-arr";
                res += "\" onmouseover=\"setClass(this, 'blk-menu-sh-act";
				if(hFlag) res += " blk-menu-shb";
                if (curLink.submenu) res += " blk-menu-arr-act";
                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, 'blk-menu-sh";
				if(hFlag) res += " blk-menu-shb";
                if (curLink.submenu) res += " blk-menu-arr";
                res += "')\" onclick=\"gotoURL('"+curLink.action+"')\" nowrap=\"nowrap\">" + curLink.text + "</td></tr>";
				if(!hFlag) hFlag = true;
        }
        res += "</table></div>";
        this.block.innerHTML = res;
        //this.block.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=90)";
}

function MenuSheet(parentObj)
{
     this.hWidthL = '50px';
     this.hWidthR = '50px';
     this.hHeight = '22px';
     this.hLink = false;
     this.links = new Array();
     this.addLink = menuAddLink;
     this.addSubmenu = menuAddSubmenu;
     this.create = menuMailCreate;
     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 showMailMenu(objVal, numVal, cPsTop, cPsLeft){
                var ps = 0;
				switch(BType)
				{
					case 1: ps = 30; break;
					case 2: ps = 20; break;
					case 3: ps = 20; break;
					default: ps = 30; break;
				}
				if (mReady){
                        menuHideAll();
                        objVal.className = "item act";
                        mSheets[numVal].show(getLeftPos(objVal) + cPsLeft, getTopPos(objVal) + cPsTop + ps);
                        menuHideTimerReset();
                }
}

function hideMailMenu(objVal, numVal){
        if (mReady){
                menuHideTimerSet();
                objVal.className = "item";
        }
}

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;
}
