// SHOWINFO

// use this function to show and position the following elements:

//	popup		 	- appears centered in the screen
//	vertical menu	- aligns with the left and bottom borders of the launching element (usually a button)
//	horizonal menu	- aligns with the right and top borders of the launching element (usually a navigation option in a vertical list)
//	tooltip			- left corner of the tooltip aligns in the center of the launching element			

// DEFAULT BEHAVIOR:
	// 	only one element can be open at a time
	// 	there is no default positioning, one must be specified
	//	auto-close:		- the element closes when you click within the element or elsewhere in the document.  

	// Three parameters are required:
	// showInfo(obj, parent, position)
	//		- obj = the popup/menu/tooltip's unique id
	//		- parent = a reference to the launching element. use "this" to refer to the link you clicked to show the element, 
	//		  		or reference the launching element's unique id if called remotely
	//		- position = popup (centered in screen), vMenu, hMenu, or tooltip

// OPTIONAL BEHAVIOR:
	// disable auto-close:	- disable the auto-close behavior so that the element stays open while the user interacts with the popup/menu/tooltip.
	// make modal:			- add modal properties so that the element opens with a semi-transparent layer that obscures the rest of the page.
	//						  auto-close is disabled and explicit action must be taken within the modal element to close it
	// add parent class:	- add a class to the or launching element to change it's style (this class is removed when the element closes)
	// multiple elements:	- disable the clean-up function that closes all other showInfo elements so that you can launch
	
	// Optional parameters should be appended in the following order:
	// showInfo(obj, parent, position, optionalClickBehavior, optionalParentClass, optionalMultiElements)
	// 		- optionalClickBehavior = persist (disable auto-close) or modal
	// 		- optionalParentClass = name of class
	// 		- optionalKeepElementOpen = true or false

function pausecomp(millis)
	{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
	}


function showInfo( obj, parent, position, clickBehavior, the_message){
	
	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(parent) == 'string') parent = getObject(parent);
	
	hideInfo();
	
	//var head1 = document.getElementById(the_id);
	obj.firstChild.nodeValue=the_message;

	//alert( getScreenWidth());
	
	var sw = getScreenWidth();
	var sh = getScreenHeight();
	var px = findPosX(parent);
	var py = findPosY(parent);
	var pw = getWidth(parent);
	var ph = getHeight(parent); 
	var w = getWidth(obj);
	var h = getHeight(obj);
	var xpos, ypos;
	
	var c = getChildren(obj);
	
	switch(position){
		case 'popup':			
			var st = getScrollTop();
			
			xpos = (sw - w)/2;			
			
			if (st != null) {
				ypos = st + 50;
			}
			else {
				ypos = (sh - h)/2;
				scrollTo(0,0);
			}
			
			break;
			
		case 'vMenu':
			xpos = px;
			ypos = py + ph + 7;
			
			// vMenu collision detection
			if (!fitVertical(obj, ypos)){ 
				ypos = (py + ph) - (ph + h);
			}
			if (!fitHorizontal(obj, xpos)){ 
				xpos = px - (w - pw);
			}
			break;
			
		case 'hMenu':
			xpos = px + pw;
			ypos = py;
			
			// hMenu collision detection
			if (!fitVertical(obj, ypos)){ 
				ypos = (py + ph) - h;
			}
			if (!fitHorizontal(obj, xpos)){ 
				xpos = px - w;
			}
			break;
			
		case 'tooltip':
			xpos = px + (pw/2);
			ypos = py + (ph/2);
			
			// tooltip collision detection 	
			if (!fitVertical(obj, ypos)) { 
				ypos = (py + ph/2) - h;
			}
			if (!fitHorizontal(obj, xpos)){
				xpos = (px + pw/2) - w;
			}
			break;
	}
	
	obj.style.top = ypos + 'px';
   	obj.style.left = xpos + 'px';
//   	obj.style.left = (xpos - 1000) + 'px';
	


	addProtector(w,h,xpos,ypos);
	if (c.length > 0) {
		effectFadeIn(c[0],5);
	}
	
	// set global variables for closing later
	//document.onclick = autohideInfo;
	document.infoLoaded = false;
	document.currentInfo = obj;
	document.currParent = parent;
	
	// handle optional parameters - disable auto-close (persist) v. modal, parent class, multi elements
	if (arguments.length > 3){
		if (clickBehavior){
			if (clickBehavior == 'persist' && getObject('clearScreen') != null){
				show('clearScreen');
				setStyle('clearScreen','height',(getHeight('page')) + 'px');
				stretchWidth('clearScreen',20);		
				document.isModal = true;
			}
			else if (clickBehavior == 'modal' && getObject('screen') != null){				
				show('screen');
				setStyle('screen','height',(getHeight('page')) + 'px');
				stretchWidth('screen',20);		
				document.isModal = true;
			}
			else {
				return null;
			}
		}
	}
}



function showInfo_full( obj, parent, position, clickBehavior, parentClass, multiElements, the_message){

	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(parent) == 'string') parent = getObject(parent);
	
	if (!multiElements){ 
		hideInfo();
	}
	else {
		alert('keep open');
	}
	
	//var head1 = document.getElementById(the_id);
	obj.firstChild.nodeValue=the_message;

	//alert( getScreenWidth());
	
	var sw = getScreenWidth();
	var sh = getScreenHeight();
	var px = findPosX(parent);
	var py = findPosY(parent);
	var pw = getWidth(parent);
	var ph = getHeight(parent); 
	var w = getWidth(obj);
	var h = getHeight(obj);
	var xpos, ypos;
	
	var c = getChildren(obj);
	
	switch(position){
		case 'popup':			
			var st = getScrollTop();
			
			xpos = (sw - w)/2;			
			
			if (st != null) {
				ypos = st + 50;
			}
			else {
				ypos = (sh - h)/2;
				scrollTo(0,0);
			}
			
			break;
			
		case 'vMenu':
			xpos = px;
			ypos = py + ph + 7;
			
			// vMenu collision detection
			if (!fitVertical(obj, ypos)){ 
				ypos = (py + ph) - (ph + h);
			}
			if (!fitHorizontal(obj, xpos)){ 
				xpos = px - (w - pw);
			}
			break;
			
		case 'hMenu':
			xpos = px + pw;
			ypos = py;
			
			// hMenu collision detection
			if (!fitVertical(obj, ypos)){ 
				ypos = (py + ph) - h;
			}
			if (!fitHorizontal(obj, xpos)){ 
				xpos = px - w;
			}
			break;
			
		case 'tooltip':
			xpos = px + (pw/2);
			ypos = py + (ph/2);
			
			// tooltip collision detection 	
			if (!fitVertical(obj, ypos)) { 
				ypos = (py + ph/2) - h;
			}
			if (!fitHorizontal(obj, xpos)){
				xpos = (px + pw/2) - w;
			}
			break;
	}
	
	
   	obj.style.left = xpos + 'px';
	obj.style.top = ypos + 'px';
   
	
	addProtector(w,h,xpos,ypos);
	if (c.length > 0) {
		effectFadeIn(c[0],5);
	}
	
	// set global variables for closing later
	//document.onclick = autohideInfo;
	document.infoLoaded = false;
	document.currentInfo = obj;
	document.currParent = parent;
	
	// handle optional parameters - disable auto-close (persist) v. modal, parent class, multi elements
	if (arguments.length > 3){
		if (clickBehavior){
			if (clickBehavior == 'persist' && getObject('clearScreen') != null){
				show('clearScreen');
				setStyle('clearScreen','height',(getHeight('page')) + 'px');
				stretchWidth('clearScreen',20);		
				document.isModal = true;
			}
			else if (clickBehavior == 'modal' && getObject('screen') != null){				
				show('screen');
				setStyle('screen','height',(getHeight('page')) + 'px');
				stretchWidth('screen',20);		
				document.isModal = true;
			}
			else {
				return null;
			}
		}
		if (parentClass){			
			addClass(parent,parentClass);
			document.hasParentClass = parentClass;
		}
	}
}

// check to see if the background iframe exists, and if so, show and position it under the object
function addProtector(w,h,xpos,ypos){	
	// typeof(document.all) != 'function' prevents Opera from showing the iframe - otherwise it positions the iframe over the object
	if (typeof(document.all) != 'function' && getObject('ghost') != null){
		
		// show protector iframe
		var g = getObject('ghost');
		
		g.style.width = (w + 2) + 'px';
		g.style.height = (h + 2) + 'px';
		g.style.left = (xpos - 1) + 'px';
		g.style.top = (ypos - 1) + 'px';	
	}
}

function autohideInfo(e, caller) {
	if (!document.infoLoaded) {
		document.infoLoaded = true;
	}
	else {
		if (!e) var e = window.event;
		var t = (e.target) ? e.target : e.srcElement;
		
		// it's OK for this one to propagate
		if (!caller) caller = this;
		
		// only fire if the target isn't the current object and if the current object isn't modal
		if (t != document.currentInfo && !document.isModal){
			hideInfo();
		}
	}
}

function hideInfo() {
	if (document.currentInfo != null) {	
	
		// close the info
		document.currentInfo.style.left = '-1000px';
		
		// reset the click handler
		if (document.backupClickHandler != null){
        	document.onclick = document.backupClickHandler;
		}
		
		// if we added a class to the parent element, remove it
		if (document.hasParentClass != null){
			removeClass(document.currParent,document.hasParentClass);
		}		
		
		// clear all document variables		
		document.currentInfo = null;
		document.backupClickHandler = null;
		document.infoLoaded = false;
		document.currParent = null;
		document.isModal = false;
		
		// hide protector iframe
		if (getObject('ghost') != null){
			var g = getObject('ghost');		
			g.style.left = '-1000px';
		}
		
		// if it exists, hide div under the modal element
		if (getObject('screen') != null){
			hide('screen');
		}	
		
		// if it exists, hide clickable div under the persistant element
		if (getObject('clearScreen') != null){
			hide('clearScreen');
		}		
	}		
}
