

com.st.popupUtils = new Object();
com.st.popupUtils.popupPos=function(l,t,w,h,ovL)
{
	var dfBottom = t + h;
	var dfRight = l +w;
	var st = document.body.scrollTop;
	var sl = document.body.scrollLeft;
	var cw=document.body.clientWidth;
	var ch=document.body.clientHeight;
	var scBott = ch + st;
	var scRight = cw + sl;
	var retVal={x:l,y:t}//new Array(l,t);
	
	if(dfBottom > scBott){
		retVal.y= (h>ch)?st:scBott -h; //scBott - h;
	}else if(t<st){
		retVal.y= st;
	}
	if(dfRight > scRight)
	{
		if(ovL){
			retVal.x = l-(ovL+w)+1;
		}else
			retVal.x = (w>cw)?sl:scRight -w;
	}else if(l<sl){
		retVal.x=sl;
	}
	return retVal;
}
com.st.__popupObj__=[];
com.st.popup=new Object();
com.st.popup.hide=function(oExclude)
{
	if(document.stPopupCurrent)
	{
		var oCurrent=document.stPopupCurrent;
		if(oCurrent!=oExclude)
		{
			//if(oCurrent.opener)
			//oCurrent.opener.className=oCurrent.classes[0];
			if(oCurrent.classes)
			{
				for(i=0;i<oCurrent.classes.length;i++)
				{
					var ar=oCurrent.classes[i];
					document.getElementById(ar[0]).className=ar[1][0];
				}
			}
			oCurrent.hideAll();
		}
	}
	document.stPopupCurrent=false;
}

com.st.popup.show=function(id,objref, x,y)
{
	var popupObj =com.st.__popupObj__[id];
	var popup   = popupObj.popup;
	
	if(popup.style.visibility!="hidden"){
		//popup.hideAll();
		com.st.popup.hide();
		return;
	}
	com.st.popup.hide(popup);
	
	var posRef= com.st.utils.getAbsPos(objref);
	var point = com.st.popupUtils.popupPos(
	((isNaN(x))?posRef.x:posRef.x + x),
	((isNaN(y))?posRef.y+objref.offsetHeight -1:posRef.y + y),
	popup.offsetWidth,popup.offsetHeight);
	popup.style.left=point.x;
	popup.style.top=point.y;
	popup.style.visibility="";
	
	if(objref.offsetWidth>popup.offsetWidth)
	{
		//popup.table.width=objref.offsetWidth-2;
	
	}
	
	var classes=objref.getAttribute("classes");
	if(classes)
	{
		var ar=classes.split(";");
		popup.classes =[];
		for(i=0;i<ar.length;i++)
		{
			var ar2=ar[i].split(":");
			var cls = ar2[1].split(",");
			popup.classes[i]=[ar2[0],cls];
			document.getElementById(ar2[0]).className=cls[1];
		}
		//popup.opener=objref;
		//popup.classes=classes.split(",");
		//objref.className=popup.classes[1];
	}
	document.stPopupCurrent=popup;
	document.onmousedown=function(){}
	
	popupObj.eventHandler.fire("onpopup",{sender:popupObj});
	window.setTimeout("document.onmousedown=function(e){var srcElem=e!=undefined?e.target:event.srcElement;if(document.cursorOnPopup!=true){if(srcElem!=document.getElementById('"+popup.id+"'))com.st.popup.hide();}}");

	/*
		window.setInterval(function(){
		document.onmousedown=function(e)
		{
			//alert(e);
			var srcElem=e!=undefined?e.target:event.srcElement;
			if(document.cursorOnPopup!=true){
				if(srcElem!=popup)
				com.st.popup.hide();
			}
		}});
	*/
}
com.st.popup.showSubMenu=function(popup,objref)
{
	var posRef= com.st.utils.getAbsPos(objref);
	var point = com.st.popupUtils.popupPos(
	posRef.x + objref.offsetWidth, posRef.y,
	popup.offsetWidth,popup.offsetHeight,objref.offsetWidth);
	popup.style.left=point.x;
	popup.style.top=point.y;
	popup.style.visibility="";
}
com.st.popupMenu=function(id,oInnerId)
{
	this.eventHandler = new com.st.eventHandlerList();
	this.id=id;

	this.popup= document.createElement("span");
	this.popup.id=id;
	document.body.insertBefore(this.popup,document.body.firstChild);

	
	this.popup.className="st_mnuPopup";
	this.popup.style.position="absolute";
	this.popup.style.visibility="hidden";
	//
	this.minWidth=140;//isNaN(minWidth)?140:minWidth;
	
	com.st.__popupObj__[id]=this;
	this.popup.onmouseover=function()
	{
		document.cursorOnPopup=true;
	}
	this.popup.onmouseout=function()
	{
		document.cursorOnPopup=false;
	}
	if(typeof(oInnerId)== "string")
	{
		o=document.getElementById(oInnerId);
		this.popup.innerHTML=o.innerHTML;
		o.parentNode.removeChild(o);
	}
	this.popup.table=null;
	this.popup.hideChild=function()
	{
		if(this.table!=null)
		{
			for(i=0;i<this.table.rows.length;i++)
			{
				var tr=this.table.rows[i];
				
				if(tr.submenu){
					tr.className="st_mnuItem";
					if(tr.submenu.style.visibility=="")
					tr.submenu.hideAll();
				}
			}
		}
	}
	this.popup.hideAll=function()
	{
		this.style.visibility="hidden";
		this.hideChild();
		var popupObj=com.st.__popupObj__[id];
		popupObj.eventHandler.fire("onhide",{sender:popupObj});
	}
	this.hide=function()
	{
		this.popup.hideAll();
	}
}
com.st.popupMenu.prototype.addItem = function(caption,icon, href, callBack)
{
	if(this.popup.table==null) {
		this.popup.table = document.createElement("table");
		this.popup.appendChild(this.popup.table);
		this.popup.table.border=0;
		this.popup.table.cellSpacing=0;
		this.popup.table.cellPadding=1;
	}
	//this.popup.table.width=this.popup.clientWidth>this.minWidth?this.popup.clientWidth:this.minWidth;
	
	var tr= this.popup.table.insertRow(-1);
	if(caption=="-")
	{
		var td= tr.insertCell(-1);
		td.colSpan=3;
		td.innerHTML= "<div class=st_mnuSeparator style='overflow:hidden;background-color:gray;height:1px;margin:2px;'></div>";
		return;
	}
	tr.className="st_mnuItem";
	var td1= tr.insertCell(-1);
	if(typeof(icon)=="string" && icon!="")
	{
		if(com.st.utils.isOldIE())
		{
			td1.innerHTML= "<div class=st_mnuIcon"
			+"style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+icon+"', sizingMethod='scale')\""
			+"></div>";
		}else
		{
			td1.innerHTML= "<img xclass=st_mnuIcon src=\""+icon+"\">";
		}
	}
	var td2= tr.insertCell(-1);
	td2.className= "st_mnuCaption";
	td2.noWrap=true;
	td2.width=this.minWidth;
	td2.innerHTML=caption;
	var td3= tr.insertCell(-1);
	td3.width=16;
	if(typeof callBack!="object")
	{
		tr.onclick=function()
		{
			com.st.popup.hide();
			if(typeof callBack=="function")
			{
				callBack(this,caption,href);
			}else{
				window.location=href;
			}
		}
	}else{
		
		td3.innerHTML="<div class=st_mnuArrow>3</div>";
	}
	var popup=this.popup;
	var zIndex=popup.style.zIndex;
	tr.onmouseover=function(e)
	{
		var obj=e!=undefined?e.target:event.srcElement;
		this.className="st_mnuItem_hover";
		
		if(typeof callBack=="object")
		{
			this.submenu=com.st.__popupObj__[callBack.id].popup;
			this.submenu.style.zIndex= ++zIndex;
			popup.currSub=this;
			com.st.popup.showSubMenu(this.submenu,this);
		}else{
			if(popup.currSub)
			{
				popup.currSub.submenu.hideAll();
				popup.currSub.className="st_mnuItem";
			}
		}
	}
	tr.onmouseout=function(e)
	{
		if(typeof callBack!="object")
		{
			var obj=e!=undefined?e.target:event.srcElement;
			this.className="st_mnuItem";
		}
	}
	return td2;
	//this.popup.table.width=this.popup.clientWidth>this.minWidth?this.popup.clientWidth:this.minWidth;
}
