
instantaspContextMenu = new Object();

function InstantASPContextMenuItem(key,capt,url,ico,enb) {
    this.key=key;
    this.intKey="";
    this.capt=capt;
    this.url=(!url||url==""?"javascript:void(0);":url);
    this.ico=ico;
    this.enable= (enb==null?true:enb);
    return this;
}
 
function InstantASPContextMenu(mId) {
    this.lsItm=null;
    this.genMenu=genMenu;
    this.mId=mId;
    this.items=new Object();
    this.container=null;
    this.count = 0;
    this.stlprf = "";
    this.active = false;
    this.absWidth = 0;
    this.add = addItem;
    this.addSeparator = addSeparator;
    this.enableItem = enableItem;
    this.itemClick = itemClick;
    this.showMenu = showMenu;
    this.hideMenu = hideMenu;
    this.menuOnClick = null;
    this.menuOnShow = menuOnShow;

    if (instantaspContextMenu[mId] != null) {
        return instantaspContextMenu[mId];
    } else {
        instantaspContextMenu[mId] = this;
    }
    return this;
 
}

function itemClick(itemId) {
    if (!this.items[itemId].enable) { return; }
    var ids=itemId.split("|"); 
    return this.menuOnClick(this.container, this.container.SelectedNode,ids[0],ids[1]);
}

function genMenu() {

    var smenu="";
    for(it in this.items) { 
        if(this.items[it].capt=="-") {
            smenu+="<tr><td class=\""+this.stlprf+"ctxsidebar\" align=\"center\" style=\"font-size:3px\">&nbsp;</td><td style=\"height:7px;vertical-align:middle;padding-top:3px\"><div class=\"ctxseparator\">&nbsp;</div></td></tr>"
        } else {
        smenu += "<tr unselectable=\"on\" id=\"" + it + "\" onmouseover=\"ctxItemOver('" + it + "')\""
        if (this.menuOnClick != null) {
            smenu += " onclick=\"return instantaspContextMenu." + this.mId + ".itemClick('" + it + "');\" ";
        }
         
         smenu += ">";
         
        smenu += "<td class=\"" + this.stlprf + "ctxsidebar\" align=\"center\">" + (this.items[it].ico ? "<img class=\"ctximage\" src='" + this.items[it].ico + "' valign=middle>" : "&nbsp;") + "</td>" + "<td class=\"" + this.stlprf + "ctxitem\">";
        smenu += "<a class=\"" + this.stlprf + "ctxtext" + (this.items[it].enable ? "" : "disable") + "\" href=\"" + this.items[it].url + "\">" + this.items[it].capt + "</a></td></tr>"
        }
    } 
    
    smenu = "<table border=0 cellpadding=0 cellspacing=0 " + (this.absWidth == 0 ? "" : "width='" + this.absWidth + "'") + ">" + smenu + "</table>";
    smenu = "<div id='" + this.mId + "' class='" + this.stlprf + "ctxmenu' style='display:none' onselectstart=\"return false\">" + smenu + "</div>";

    var isIE = (navigator.userAgent.indexOf("MSIE") >= 0);

    var orgEvent = (iasp_IsIE ? document.body.onclick : window.onclick);
    if (!orgEvent || orgEvent.toString().search(/orgEvent/gi) < 0) {
        var newEvent = function() { if (orgEvent) orgEvent(); hideAllMenu(); };
        if (iasp_IsIE) {
            document.body.onclick = newEvent;
        } else {
            window.onclick = newEvent;
        }
    }

    return smenu;
  
}

function addItem(key,capt,url,ico,enb) { 
     var intKey = this.mId+"|"+key;
     var it = new InstantASPContextMenuItem(key,capt,url,ico,enb);
     it.intKey = intKey;
     this.items[intKey]=it;this.count++
}

function addSeparator() {
 this.add("auto"+this.count,"-","","");
}

function showMenu(x, y, e) {

    if (x == 0) { x = e.clientX; }
    if (y == 0) { y = e.clientY; }
    
     hideAllMenu();
     if(this.lsItm!=null) { 
	    setStyle(this.lsItm,"N");
	    this.lsItm=null
     }
     var flag = this.menuOnShow(this.container.SelectedNode);
     if (flag==false) {return;}

     var ctx = iasp_FindControl(this.mId);
     ctx.style.display = "block";

     var x1 = -475;
     var y1 = -305;
     
     if (iasp_IsIE) {     
         var tmpX = e.clientX;
         var tmpY = e.clientY;
         tmpX = tmpX - x1;
         tmpY = tmpY - y1;   
        ctx.style.top = tmpY + "px";
        ctx.style.left = tmpX + "px";       
     } else {
         var tmpX = m.pageX;
         var tmpY = m.pageY;
         tmpX = tmpX - x1;
         tmpY = tmpY - y1;
         ctx.style.top = tmpY + "px";
         ctx.style.left = tmpX + "px";
     }
 
     this.active=true;

}

function showMenuAbs(x,y) {
    hideAllMenu();
    var ctx = iasp_FindControl(this.mId);
    ctx.style.top=y+"px";
    ctx.style.left=y+"px";
    ctx.style.display="";
    this.active=true
}
 
function hideMenu() {
     var ctx=iasp_FindControl(this.mId);
     ctx.style.display="none";
     this.active=false;
     if (this.lsItm!=null) {
       setStyle(this.lsItm,"N");this.lsItm=null;
     }
}
 
function hideAllMenu() {
     for (it in instantaspContextMenu) { 
        if(instantaspContextMenu[it].active) {instantaspContextMenu[it].hideMenu();}
     }
}
 
function enableItem(key,b) { 
 var intKey = this.mId+"|"+key;
 this.items[intKey].enable = b;
 setStyle(iasp_FindControl(intKey),(b?"N":"D"))
}

function setStyle(it,s) { 
    var suff=(s=="O"?"over":"");
    it.cells[0].className="ctxsidebar"+suff;
    it.cells[1].className="ctxitem"+suff;
    it.cells[1].childNodes[0].className="ctxtext"+(s=="D"?"disable":(s=="OD"?"overdisable":suff));
}

function ctxItemOver(it) {
    var m = it.split("|");
    var oIt = iasp_FindControl(it);
    var li = instantaspContextMenu[m[0]].lsItm;
    if (li != null && li.intKey == it) {return;}
    if(li != null) 
    setStyle(li,(instantaspContextMenu[m[0]].items[li.id].enable?"N":"D")); 
    setStyle(oIt,(instantaspContextMenu[m[0]].items[it].enable?"O":"OD")); 
    instantaspContextMenu[m[0]].lsItm=oIt
}
 
function menuOnClick(container, selNode,menuId,itemId) {} 
function menuOnShow(selNode) {}
