// TOOLTIP
// opens a div w/ the top left corner centered on it's launching point

function showInfo(obj,parent,type){
	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(parent) == 'string') parent = getObject(parent);
	
	hideInfo();
	
	if (type == 'tooltip'){
		positionToolTip(obj,parent);
	}
	if (type == 'menu'){
		positionMenu(obj,parent);
	}
	if (type == 'menuComplex'){
		positionMenu(obj,parent);
		if (getObject('clearScreen') != null){
			show('clearScreen');
			var header = getHeight('header');
			var primnav = getHeight('primaryNav');
			var content = getHeight('content');
			setStyle('clearScreen','height',(header + primnav + content) + 'px');
			stretchWidth('clearScreen',20);
		}
	}
	if (type == 'modal'){
		positionPopup(obj);
		if (getObject('screen') != null){
			show('screen');
			stretchHeight('screen',0);
			stretchWidth('screen',20);
		}
	}
	
	// set global variables for closing later
	document.onclick = autohideInfo;
	document.infoLoaded = false;
	document.currentInfo = obj;
	document.currentType = type;
}

function positionToolTip(obj,parent){
	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(parent) == 'string') parent = getObject(parent);

	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;
	var ypos;
	
	xpos = px + (pw/2);
	ypos = py + (ph/2);
	
	// popup collision detection 	
	if (!fitVertical(obj, ypos)) { 
		ypos = (py + ph/2) - h;
	}
	if (!fitHorizontal(obj, xpos)){
		xpos = (px + pw/2) - w;
	}

	obj.style.left = xpos + 'px';
	obj.style.top = ypos + 'px';	
	
	// check to see if the background iframe exists, and if so, show and position it under the object
	// typeof(document.all) != 'function' prevents Opera from showing the iframe - 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 positionMenu(obj,parent) {
	if (typeof(obj) == 'string') obj = getObject(obj);
	if (typeof(parent) == 'string') parent = getObject(parent);

	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;
	var ypos;
	
	xpos = px;
	ypos = (py + ph);
	
	// menu collision detection
	if (!fitVertical(obj, ypos)){ 
		ypos = (py + ph) - (ph + h);
	}
	if (!fitHorizontal(obj, xpos)){ 
		xpos = px - (w - pw);
	}	

	obj.style.left = (xpos + 2) + 'px';
	obj.style.top = (ypos + 1) + 'px';
	
	// check to see if the background iframe exists, and if so, show and position it under the object
	// typeof(document.all) != 'function' prevents Opera from showing the iframe - 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 + 'px';
	}
}

function positionPopup(obj) {
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	var sw = getScreenWidth();
	var sh = getScreenHeight();
	var w = getWidth(obj);
	var h = getHeight(obj);
	var xpos;
	var ypos;
	
	xpos = (sw - w)/2;
	ypos = (sh - h)/2;
	
	obj.style.left = xpos + 'px';
	obj.style.top = ypos + 'px';
	
	// check to see if the background iframe exists, and if so, show and position it under the object
	// typeof(document.all) != 'function' prevents Opera from showing the iframe - 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 + 12) + 'px';
		g.style.height = (h + 12) + 'px';
		g.style.left = (xpos - 6) + 'px';
		g.style.top = (ypos - 6) + '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 it isn't a modal popup which should be closed manually		
		if (t != document.currentInfo && document.currentType != 'modal' && document.currentType != 'menuComplex'){
			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;
		
		// clear the variable we set		
		document.currentInfo = null;
		document.currentType = null;
		document.backupClickHandler = null;
		document.infoLoaded = false;
		
		// hide protector iframe
		if (getObject('ghost') != null){
			var g = getObject('ghost');		
			g.style.left = '-1000px';
		}
		
		// hide clickable div under the complex menu
		if (getObject('clearScreen') != null){
			hide('clearScreen');
		}
	}		
}

// close the modal popup manually
function closePopup(){
	hideInfo(); 
	if (getObject('screen') != null){
		hide('screen');
	}
}