var cmenuEnabled = new Array();
var cmenuTimer = 0;

function CMenu(id)
{
    this.id = id;
    this.sn = 0;
	this.container = null;
    this.menu = "";

	this.enableItem = enableItem;
	this.addItem = addItem;
	this.addSeparator = addSeparator;
	this.getMenu = getMenu;
	this.show = show;

    function addItem(caption, uid)
    {
    	if (uid) 
    	{
    		this.sn = uid;
    	}
        if (this.sn == 0)
        {
			cmenuEnabled[this.id] = new Array();
            this.menu = "<table border=0 cellspacing=1 cellpadding=0 bgColor='#ffffff'><tr><td><table cellspacing=1 id=cmenu" + this.id + " class=cmenu>";
        }
		cmenuEnabled[this.id][this.sn] = 1;
        this.menu = this.menu + 
					"<tr><td id=cmenu" + this.id + "_" + this.sn + " class=cmenu " +
						"onclick='cmenuOnClick(" + this.id + ", " + this.sn + ")'" +
						"onmouseover='cmenuMouseover(" + this.id + ", " + this.sn + ")' " +
						"onmouseout='cmenuMouseout(" + this.id + ", " + this.sn + ")'>" + caption +
					"</td></tr>";
        this.sn ++;
    }
    function addSeparator()
    {
        this.menu = this.menu + "<tr><td width=100% height=2px bgColor='#dddddd'></td></tr>";
    }

	function enableItem(itemID, value)
	{
		cmenuEnabled[this.id][itemID] = value;
	}

    function getMenu()
    {
        this.menu = this.menu + "</td></tr></table></table>";
        return this.menu;
    }
  
    
    function show(x, y)
    {

		if (!this.container)
		{			
			var obj = document.getElementById("cmenu");
			if (!obj)
			{
				obj = document.createElement("<span id='cmenu'></span>");
				document.body.insertAdjacentElement("AfterBegin", obj);
				obj.style.position = "absolute";
				obj.style.pixelLeft = 0;
				obj.style.pixelTop = 0;
			}
			this.container = obj;
		}
		this.container.innerHTML = this.getMenu();
		this.container.style.display = "block";

		
		y = y + this.container.scrollTop; 		
		
        if (y + this.container.offsetHeight > document.body.clientHeight) y = y - this.container.offsetHeight + 50;

        if (x + this.container.offsetWidth + document.body.scrollLeft + 5 > document.body.clientWidth) x = x - this.container.offsetWidth;

        this.container.style.pixelLeft = x + document.body.scrollLeft;
        this.container.style.pixelTop = y + document.body.scrollTop;
		

		for (i=0; i<cmenuEnabled[this.id].length; i++)
		{
			var ctrl = document.getElementById("cmenu" + this.id + "_" + i);
			if (ctrl) ctrl.className = (cmenuEnabled[this.id][i]) ? "cmenu" : "cmenuDisabled";
		}
		
		cmenuTimer = window.setTimeout("cmenuHideCheckTimer()", 2000);
    }
}


function cmenuHideCheckTimer()
{
    cmenuHide();
    cmenuTimer = 0;
}

function cmenuHide()
{
	var ctrl = document.getElementById("cmenu");
	if (ctrl)
	{
	     ctrl.style.display = "none";
	     //showDiv(ctrl, false); 
	}
}



function cmenuMouseover(mnuID, itemID)
{
	// alert(mnuID + ", " + itemID);
    var id = 'cmenu' + mnuID + '_' + itemID;
    var ctrl = document.getElementById(id);
    var style = (cmenuEnabled[mnuID][itemID] == 1) ? 'cmenuOver' : 'cmenuDisabled';
    ctrl.className = style;
    
    if (cmenuTimer != 0) window.clearTimeout(cmenuTimer);
}

function cmenuMouseout(mnuID, itemID)
{
    var id = 'cmenu' + mnuID + '_' + itemID;
    var ctrl = document.getElementById(id);
    var style = (cmenuEnabled[mnuID][itemID] == 1) ? 'cmenu' : 'cmenuDisabled';
    ctrl.className = style;
    if (cmenuTimer != 0)
    {
        window.clearTimeout(cmenuTimer);
        cmenuTimer = window.setTimeout("cmenuHideCheckTimer()", 2000);
    }
}