﻿/*********************************************************************
*** (C) OK-Systems,2007 (All rights reserved)                      ***
*** Don't make any changes without reporting to: oks@olmedo.com    ***
**********************************************************************
*** File____: ui_popup.js                                          ***
*** Project_: VirtOf                                               ***
*** Language: JavaScript                                           ***
*** Tier____: Presentation-services                                ***
**********************************************************************
*** Description: Pop-up menu functions                             ***
*********************************************************************/


//==================================================
// GLOBAL VARIABLES
//==================================================
var cPopName			='Pop_Menu';
var nPopDragObject	= null;
var nPopTX;
var nPopTY;
//---
var nPopDelay		=	400; // Delay to hide in milliseconds
var nPopNSWidth	=	130; // Default width for netscape
var nPopTimer		=	null;
var oPopLastItem;


//==================================================
// POP-UP MENU CONSTRUCTION FUNCTIONS
//==================================================

//--------------------
//- Modified: 08-Nov-2001, Antonio Olmedo Soler
//- Syntax__: Pop_Item (cLabel,[cAction],[cStatus],[cStyle],[cIcon])
//- Description: 
//--------------------
function Pop_Item (cLabel) {
	var cAction='';
	var cStatus='';
	var cStyle='';
	var cIcon=null;
	//--- Optional arguments
	if (arguments.length>1) if (arguments[1]) cAction=arguments[1];
	if (arguments.length>2) if (arguments[2]) cStatus=arguments[2];
	if (arguments.length>3) if (arguments[3]) cStyle=arguments[3];
	if (arguments.length>4) cIcon=arguments[4];
	//--- Parse parameters
	if (cAction) cStyle='cursor:pointer;'+cStyle;
	if (cStyle)  cStyle=' Style="'+cStyle+'"';
	if (cIcon!=null) {
		if (cIcon=='') {
			//<<<cIcon='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			cLabel='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+cLabel;
		} else {
			//--- If only a filename is given, add default path
			try { if (cIcon.indexOf('/')<0) cIcon=DIR_IMG+cIcon; } catch (oError) {}
			cIcon='<img src="'+cIcon+'" Class=ImgButton Align=AbsMiddle';
			cIcon+=' onerror="if (this.src!=DIR_IMG+\'btn_bullet1.gif\') { this.src=DIR_IMG+\'btn_bullet1.gif\';this.width=20;this.height=20;}">';
			cLabel=cIcon+'&nbsp;'+cLabel;
		}
	}
	//--- Build the Item row
	var cOut='<td '+cStyle; // NOWRAP 
	cOut+=' unselectable="on"';
	if (cStatus) cOut+=' onMouseOver="window.status=\''+cStatus+'\'" onMouseOut="window.status=\'\'"';	
	if (cAction!='') {
		cOut+=' class="Pop" onclick="Pop_Hide();'+cAction+'"';
	} else {
		cOut+=' class="Pop-Disabled"';
	}
	cOut+='>'+cLabel+'</td></tr>';
	//---
	//alert(cOut);
	return cOut;
}

//--------------------
//- Modified: 08-May-2001, Antonio Olmedo Soler
//- Syntax__: Pop_Sep()
//- Description: 
//--------------------
function Pop_Sep () {	
	return '<tr><td Class=Pop-Sep Height=1></td></tr>';
}

//--------------------
//- Modified: 01-Dec-2000, Antonio Olmedo Soler
//- syntax__: Pop_Item (cLabel,[cClass])
//- Description: 
//--------------------
function Pop_Label (cLabel) {
	//--- Optional arguments
	var cClass='Pop-Label';
	if (arguments.length>1) if (arguments[1]) cClass=arguments[1];
	//---
	return '<tr><td class="'+cClass+'">'+cLabel+'</td></tr>';
}

//--------------------
//- Modified: 08-May-2001, Antonio Olmedo Soler
//- Syntax__: Pop_Table (cItems,[cTitle])
//- Description: 
//--------------------
function Pop_Table (cItems) {
	var cOut='';
	var cTitle='';
	//--- Optional arguments
	if (arguments.length>1) cTitle=arguments[1];
	//---
	cOut='<table  Class=Pop Id=Pop_Table Border=0 CellSpacing=0 CellPadding=2';
	cOut+=' onSelectStart="return false" onMouseOver="checkHighlight(true)" onMouseOut="checkHighlight(false)"';
	cOut+='>';
	if (cTitle!='') cOut+='<tr><th Class=Pop>'+cTitle+'</th></tr>';
	cOut+=cItems+'</table>';
	//---
	//cOut='<div unselectable="on">'+cOut+'</div>';
	//---
	return cOut;
}


//==================================================
// POP-UP MENU ACTIVATION FUNCTIONS
//==================================================

//--------------------
//- Created_: 07-May-2001, Antonio Olmedo Soler
//- Modified: 28-Jun-2006, Antonio Olmedo Soler
//- syntax__: Pop_Show (cDest,[nIncLeft],[nIncTop],[nAbsLeft],[nAbsTop])
//- Description: 
//--------------------
function Pop_Show (cDest) {
   //alert('>Pop_Show')
	if (!document.all) return false;
	//---	
	try {
		var e				=window.event.srcElement;
		var nLeft		=Pop_GetRealPos(e,'Left');
		var nTop			=Pop_GetRealPos(e,'Top');
		var nWidth		=e.offsetWidth;
		//--- Optional arguments
		var nIncLeft	=12;
		var nIncTop		=6;
		var nAbsLeft	=null;
		var nAbsTop		=null;
		if (arguments.length>1) if (arguments[1]!=null) nIncLeft=arguments[1];
		if (arguments.length>2) if (arguments[2]!=null) nIncTop=arguments[2];
		if (arguments.length>3) if (arguments[3]!=null) nAbsLeft=arguments[3];
		if (arguments.length>4) if (arguments[4]!=null) nAbsTop=arguments[4];
		//---	
		Pop_TimerStop();
		var oTarget=document.all[cDest];
		if ((oPopLastItem!=null) && (oPopLastItem!=oTarget)) Pop_Hide();
		if (oTarget) {
		   nLeft+=nIncLeft;
			nTop+=nIncTop;
			if (nAbsLeft==null) {
				//nLeft+=nIncLeft;
				if (e.offsetWidth==0) e.offsetWidth=nWidth?nWidth:nPopNSWidth;
				nLeft=(nLeft?nLeft:(Pop_GetRealPos(e,'Left')+e.offsetWidth+5));
			} else {
				//alert('e.offsetLeft='+e.offsetLeft);
				nLeft=nAbsLeft;
			}
			if (nAbsTop==null) {
				nTop=(nTop?nTop:Pop_GetRealPos(e,'Top'));
			} else {
				nTop=nAbsTop;
			}			
			oTarget.style.pixelLeft=nLeft;
			oTarget.style.pixelTop=nTop;
			oTarget.style.visibility='visible';
		}
		oPopLastItem=oTarget;
	} catch (oError) { Debug_Function('Pop_Show',oError,arguments); }
	return false;
}

//--------------------
//- Created_: 29-Aug-2000, Antonio Olmedo Soler
//- Modified: 29-Aug-2000, Antonio Olmedo Soler
//- Syntax__: Pop_Build(cId,cHTML)
//- Description: Set the HTML content of the Pop-up DIV's tag.
//--------------------
function Pop_Build(cId,cHTML) {
   if (document.all) {
		if (document.all[cId]) {
			document.all[cId].innerHTML = cHTML;
			return document.all[cId];
		}
		return null;
	} else if (ns4) {
		if (!document) return false;
		if (document.layers.length==0) return null;
		//--- Note. This doesn't work if the layer is embedded into a table (!)
		var lyr=document.layers[cId];
		if (lyr) {
			lyr.document.open();
			lyr.document.write(cHTML);
			lyr.document.close();
			return lyr;	
		}
	}
	return null;
}

//--------------------
//- Created_: 29-Aug-2000, Antonio Olmedo Soler
//- Modified: 17-Nov-2000, Antonio Olmedo Soler
//- Syntax__: Pop_BuildAndShow(cItems,[cTitle],[nIncLeft],[nIncTop],[nAbsLeft],[nAbsTop])
//- Description: Builds the HTML code (a Table) of the menu and activates it.
//--------------------
function Pop_BuildAndShow(cItems) {
   //alert('Pop_BuildAndShow')
	var cTitle='';
	//--- Optional arguments
	if (arguments.length>1) cTitle=arguments[1];
	//---
	Pop_Build(cPopName,Pop_Table(cItems,cTitle));
	return Pop_Show(cPopName,arguments[2],arguments[3],arguments[4],arguments[5]);
}

//--------------------
//- Created_: ??-???-2000, Antonio Olmedo Soler
//- Modified: 04-May-2001, Antonio Olmedo Soler
//- Syntax__: Pop_Hide()
//- Description: 
//--------------------
function Pop_Hide() {
	if (oPopLastItem)	oPopLastItem.style.visibility='hidden';
	return true;
}


//==================================================
// POP-UP MENU ITEM HIGHLIGHTING FUNCTIONS
//==================================================

//--------------------
//- Modified: 04-May-2001, Antonio Olmedo Soler
//- Syntax__: checkHighlight(which)
//- Description: 
//--------------------
function checkHighlight(which) {
	var el = event.srcElement;
	if (el.tagName!='TD') {
		el=el.parentElement;
		if (el.tagName!='TD') {
			if (el.tagName!='A') return false;
			el=el.parentElement;
			if (el.tagName!='TD') return false;
		}
	}
	if (el.className=='') return false;
	if (el.className=='Pop-Sep') return false;
	if (el.className=='Pop-Disabled') return false;
	if (el.className=='Pop-Label') return false;
	if (el.className=='Pop-Subheader') return false;
	el.className=which?"Pop-Highlight":"Pop";
	return true;
}


//==================================================
// POP-UP MENU DRAGGING FUNCTIONS
//==================================================

//--------------------
//- Modified: 09-Aug-2000, Antonio Olmedo Soler
//- Syntax__: getElementParent (el,cTag)
//- Description: Return the superior element which tag is like the given one.
//--------------------
function getElementParent(el,cTag) {
	var eTmp=el;
	//---
	while ((eTmp != null) && (eTmp.tagName != "BODY")) {
		if ((eTmp.tagName == cTag)) return eTmp;
		eTmp=eTmp.parentElement;
	}
	return null;
}

//--------------------
//- Modified: 04-May-2001, Antonio Olmedo Soler
//- Syntax__: Pop_OnMouseDown ()
//- Description: Return the superior element which tag is like the given one.
//--------------------
function Pop_OnMouseDown() {
	var el=window.event.srcElement;
	//---
	if ( ((el.tagName == "TH") && (el.className=="Pop"))  || (el.className=="Pop-Disabled") || (el.className=="Pop-Code") || (el.className=="Pop-Subtitle") ) {
		//alert('moveme_onmousedown: '+el.tagName);
		nPopDragObject=getElementParent(el,'DIV');
		//alert('moveme_onmousedown: '+nPopDragObject);
		if (nPopDragObject) {
			nPopTY = (window.event.clientY - nPopDragObject.style.pixelTop);
			nPopTX = (window.event.clientX - nPopDragObject.style.pixelLeft);
			window.event.returnValue = false;
			window.event.cancelBubble = true;
			return true;
		}
	//} else if (el.className=="Pop-Highlight") {
	//		nPopDragObject=getElementChild(el,'A');
	//    >>> Do the action of the link !
	}
	nPopDragObject=null;
	return false;
}

//--------------------
//- Modified: ??-Jun-2000, Antonio Olmedo Soler
//- Syntax__: Pop_OnMouseUp()
//- Description: 
//--------------------
function Pop_OnMouseUp() {
	if(nPopDragObject) nPopDragObject=null;
}

//--------------------
//- Modified: ??-Jun-2000, Antonio Olmedo Soler
//- Syntax__: Pop_OnMouseMove()
//- Description: 
//--------------------
function Pop_OnMouseMove() {
	if (nPopDragObject) {
		if(window.event.clientX >= 0) {
			nPopDragObject.style.left = window.event.clientX - nPopTX;
			nPopDragObject.style.top = window.event.clientY - nPopTY;
		}
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
}


//==================================================
// AUXILIAR FUNCTIONS
//==================================================

function Pop_TimerStop() {
	clearTimeout(nPopTimer);
	return true;
}

//--------------------
//- Modified: 15-Jan-2002, Antonio Olmedo Soler
//- Syntax__: Pop_TimerStart(el)
//- Description: 
//--------------------
function Pop_TimerStart(el) {
	//alert('Pop_TimerStart: '+el);
	if (!el) return false;
	if (!el.contains(event.toElement)) {
		Pop_TimerStop();
		nPopTimer=setTimeout("Pop_Hide()",nPopDelay);
	}
	return true;
}

//--------------------
//- Modified: 19-Dec-2005, Antonio Olmedo Soler
//- Syntax__: Pop_GetRealPos(e,cDimension)
//- Description: 
//--------------------
function Pop_GetRealPos(e,cDimension) {
	var nPos=0;
	while (e!=null) {
	 	//nPos+=e['offset'+cDimension];
	 	nPos+=e['offset'+cDimension];
		e=e.offsetParent;
		if (e) {
			switch(cDimension) {
			case 'Top':		nPos-=e.scrollTop; break;
			case 'Left':	nPos-=e.scrollLeft; break;
			}
		}
	}
	//
	return nPos;
}

function Pop_CheckOver() {
	if ((oPopLastItem) && (oPopLastItem.contains(event.srcElement))) Pop_TimerStop();
	return true;
}

function Pop_CheckOut() {
	//alert("Pop_CheckOut()");
	if (oPopLastItem==event.srcElement)	Pop_TimerStart(event.srcElement);
	return true;
}



//--------------------
//- Modified: 26-Apr-2005, Antonio Olmedo Soler
//- Modified: 26-Apr-2005, Antonio Olmedo Soler
//- Syntax__: PopUp_Help(cHTML,[cTitle])
//- Description: 
//--------------------
function PopUp_Help(cHTML) {
	//Debug_Function('PopUp_Help',null,arguments);
	try {
		//--- Optional arguments
		var cTitle		='';
		if (arguments.length>1) if (arguments[1]) cTitle=arguments[1];
		//----
		Pop_Hide();
		if (!cHTML) return false;
		return Pop_BuildAndShow(Pop_Item(cHTML),cTitle,24,16);
	} catch (oError) { Debug_Function('PopUp_Help',oError,arguments); }
	return false;
}

//==================================================
// EXECUTE CODE WHEN LOADING THE PAGE
//==================================================

//--------------------
//--- INTERCEPT PAGE EVENTS
//- NOTE: This only works in IE4 or better .. ?
//--------------------
if (document.all) { 
	document.onmouseover=	Pop_CheckOver;
	document.onmouseout=		Pop_CheckOut;
	document.onmousedown=	Pop_OnMouseDown;
	document.onmouseup=		Pop_OnMouseUp;
	document.onmousemove=	Pop_OnMouseMove;
}

//--------------------
//--- ADD HTML CODE TO THE PAGE, CONTAINING THE EMPTY HIDDEN POP-UP MENU 
//--------------------
var cTag=(document.all)?'DIV':'LAYER';
var cOut='<'+cTag+' class="tooltip" id="'+cPopName+'"';
cOut+=' style="position:absolute;visibility:hidden;width:200px;Z-index:99;"';
cOut+=' onMouseOut="Pop_TimerStart(event.srcElement)">';
cOut+='</'+cTag+'>';
//alert(cOut);
document.write(cOut);

//--------------------
//--- ADD CSS TO THE PAGE
//--------------------
//try {	document.styleSheets(0).addImport('/VirtOf2/ui_popup.css'); } catch(oError) {}

//==================================================

