/**********************************************/
/* QuickTips JavaScript code for Glossary.com */
/**********************************************/
var ScrollAtX;
var ScrollAtY;

function getLayer(layerId) {
	var theLayerToRetrieve;
	
	if (document.getElementById) {
		theLayerToRetrieve = document.getElementById(layerId);
	} else if (document.all) {
		theLayerToRetrieve = document.all[layerId];
	}
	
	if (theLayerToRetrieve) {
		return theLayerToRetrieve.style;
	} else {
		return null;
	}
}

function setQTimages(im) {
	return true; //this line skpis this function and returns all blue
	
	window.document.getElementById("qt_header").style.backgroundImage = 'url(../images/quicktips/' + im + '-top.gif)';
	window.document.getElementById("qt_title").style.backgroundImage = 'url(../images/quicktips/' + im + '-text-header.gif)';
	window.document.getElementById("qt_content").style.backgroundImage = 'url(../images/quicktips/' + im + '-text-background.gif)';
	window.document.getElementById("qt_footer").style.backgroundImage = 'url(../images/quicktips/' + im + '-bottom.gif)';
	document.qt_point_ur.src = '../images/quicktips/' + im + '-pointer-right.gif';
	document.qt_point_ul.src = '../images/quicktips/' + im + '-pointer-left.gif';
}

var origDrayAtX = 0;
var onqt = false;
window.onload = init;
function init() {
	if (window.Event) {
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = getXY;
}

var mouseX;
var mouseY;
var lastMouseXdiff = 0;
function getXY(e) {
	mouseX = (window.Event) ? e.pageX : event.clientX;
	mouseY = (window.Event) ? e.pageY : event.clientY;
	
	if (onqt==true && origDrayAtX != 0) {
		xDiff = mouseX - origDrayAtX;
		//document.getElementById('aform').searchquery.value = xDiff + "--" + mouseX + "," + origDrayAtX;
		if (lastMouseXdiff > 0 && xDiff < lastMouseXdiff) {
			//lastMouseXdiff = 1;
			//alert('neg reversal -' + lastMouseXdiff + '-' + xDiff + "-");
		} else if (lastMouseXdiff < 0 && xDiff > lastMouseXdiff) {
			//lastMouseXdiff = 1;
			//alert('pos reversal -' + lastMouseXdiff + '-' + xDiff + "-");
		}
		if (xDiff < -50 || xDiff > 50) {
			var theLayer = getLayer('articletip');
			origDrayAtX = mouseX;
			
			// disabled right now.  However, this allows the x axis to move every 50px.  (just by uncommenting the next line)
			// did not 100% finish testing - and still the orginal way seemed better.
			//drawQtOnXAxis(origDrayAtX);
		}
		if (xDiff > 50) {
			lastMouseXdiff = 1;
		} else {
			lastMouseXdiff = xDiff;
		}
	}
}

var tqt;
var rolltimeout = 25;
var lastLayerIdOn = '';
function rollon(layerId, event, qtType) {
	//startTime=new Date().getTime();
	
	// slow, commented out are for big pages!
	// slow, commented out
	//document.body.style.cursor = 'pointer';
	
	
	// slow, commented out too
	//if (layerId != lastLayerIdOn) {
	//	clearTimeout(tqt);
	//	forceHideQt();
	//	clearTimeout(tqt);
	//	onqt = false;
	//}
	lastLayerIdOn = layerId;
	if (onqt == false) {
		// slow, commented out
		//onResizeFixAllElements();
	
		if (qtType == undefined) {
			qtType = 'Quick Definition';
		}
		
		var theLayer = getLayer('articletip');
	
		if (theLayer) {
			//var d=document;
			qwe = layerId + '_t';
			qwer = layerId + '_b';
			
			//populate the div
			document.getElementById('glossaryQTheader').innerHTML = document.getElementById(qwe).innerHTML;
			document.getElementById('glossaryQTtext').innerHTML = document.getElementById(qwer).innerHTML;
			
			if (qtType == 'glossary') {
				qtTypeText = 'Glossary Overview';
				setQTimages('glossary');
			} else if (qtType == 'wordnet') {
				qtTypeText = 'Dictionary';				
				setQTimages('wn');		
			} else if (qtType == 'history') {
				qtTypeText = 'History Item';				
				setQTimages('glossary');				
			} else if (qtType == 'link') {
				qtTypeText = 'External Link';
				setQTimages('link');
			} else if (qtType == 'ref') {
				qtTypeText = 'Reference';
				setQTimages('ref');
			} else {
				qtTypeText = 'Encyclopedia'; //Definition
				setQTimages('wiki');
			}
	
			document.getElementById('glossaryQtType').innerHTML = qtTypeText;
			
			val = document.getElementById('glossaryQTheader').innerHTML;
			
			var scrollatxy = new Array();
			scrollatxy = getEventClickXYCoords(event);
			ScrollAtX = scrollatxy[0];
			ScrollAtY = scrollatxy[1];
			
			if (ff) {
				// firefox has issue with clicking if theLayer is not far enough down.  (very bottom pixel is still cut off from the click)
				theLayer.top = ScrollAtY+16 + "px";
			} else {
				theLayer.top = ScrollAtY + "px";
			}
			
			drawQtOnXAxis(ScrollAtX);
	
			theLayer.visibility = 'visible';
			theLayer.display = 'block';
			theLayer.width = '385px';
			theLayer.position = 'absolute';
		}
	} else {
		// quicktip already on
		document.body.style.cursor = 'pointer';
		tqt=setTimeout("hideQuickTip()",rolltimeout);
		document.body.style.cursor = 'pointer';
	}
	//endTime=new Date().getTime();
	//document.getElementById('rtc').innerHTML=endTime-startTime;
}

function drawQtOnXAxis(withWhatX) {
	var theLayer = getLayer('articletip');
	
	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	
	var halfs = winW / 2;
	
	if (withWhatX > halfs) {
		origDrayAtX = withWhatX; // - 360
		theLayer.left = (withWhatX - 360) + "px";
		
		if (navigator.appName=="Netscape") {
			document.getElementById('qt_body').style.cssFloat = 'left'; //ff
		} else {
			document.getElementById('qt_body').style.styleFloat = 'left'; //ie
		}
		
		document.getElementById('qt_point_ul').style.visibility = 'hidden';
		document.getElementById('qt_point_ul').style.display = 'none';
		document.getElementById('qt_point_ur').style.visibility = 'visible';
		document.getElementById('qt_point_ur').style.display = 'block';
	} else {
		origDrayAtX = withWhatX - 10;
		theLayer.left = (withWhatX - 10) + "px";
		
		if (navigator.appName=="Netscape") {
			document.getElementById('qt_body').style.cssFloat = 'right'; //ff
		} else {
			document.getElementById('qt_body').style.styleFloat = 'right'; //ie
		}
		
		document.getElementById('qt_point_ur').style.visibility = 'hidden';
		document.getElementById('qt_point_ur').style.display = 'none';
		document.getElementById('qt_point_ul').style.visibility = 'visible';
		document.getElementById('qt_point_ul').style.display = 'block';
	}
}

function hideQuickTip() {
	document.body.style.cursor = 'pointer';
	var workingEle = document.getElementById(lastLayerId);
	
	var eleAtX = findPosX(workingEle);
	var eleAtY = findPosY(workingEle);
	
	var eleRightX = eleAtX + getElementWidth(workingEle);
	var eleBottomY = eleAtY + getElementHeight(workingEle);
	var inbounds = false;
	if (mouseX > eleAtX && mouseX < eleRightX && mouseY > eleAtY && mouseY < eleBottomY) {
		inbounds = true;
	}
	//document.getElementById('aform').searchquery.value = mouseX + "," + mouseY + ",(" + eleAtX + "," + eleAtY + "," + eleRightX + "," + eleBottomY + ") " + inbounds + " ... " + Math.floor(Math.random()*100);
	if (!inbounds) {
		forceHideQt();
	} else {
		//reset the timeout
		document.body.style.cursor = 'pointer';
		tqt=setTimeout("hideQuickTip()",rolltimeout);
		document.body.style.cursor = 'pointer';
		//document.getElementById('aform').searchquery.value = "reset timeout... " + mouseX + "," + mouseY + ",(" + eleAtX + "," + eleAtY + "," + eleRightX + "," + eleBottomY + ") " + inbounds + " ... " + Math.floor(Math.random()*100);
	}
}

var lastLayerId;
var lastEx;
var lastEy;
function rolloff(layerId) {
	document.body.style.cursor = 'pointer';
	lastLayerId = layerId;
	document.body.style.cursor = 'pointer';
	tqt=setTimeout("hideQuickTip()",rolltimeout);
	document.body.style.cursor = 'pointer';
}

function forceHideQt() {
	var theLayer = getLayer('articletip');
	
	if (theLayer) {
		theLayer.visibility = 'hidden';
	}
	
	document.getElementById('qt_point_ul').style.visibility = 'hidden';
	document.getElementById('qt_point_ul').style.display = 'none';
	document.getElementById('qt_point_ur').style.visibility = 'hidden';
	document.getElementById('qt_point_ur').style.display = 'none';
	
	clearTimeout(tqt);
	document.body.style.cursor = '';
	onqt = false;
}

function preloadQtImages() {
	if (document.images) {
		preload_image_object = new Image();
		
		image_url = new Array();
		image_url[0] = "pointer-right.gif";
		image_url[1] = "pointer-left.gif";
		image_url[2] = "top.gif";
		image_url[3] = "text-header.gif";
		image_url[4] = "text-background.gif";
		image_url[5] = "bottom.gif";
		
		image_url_type = new Array();
		image_url_type[0] = "wiki";
		image_url_type[1] = "wn";
		image_url_type[2] = "glossary";
		
		var i = 0;
		var j = 0;
		
		for (j=0; j<3; j++) {
			for(i=0; i<=5; i++) {
				preload_image_object.src = "http://www.glossary.com/images/quicktips/" +  image_url_type[j] + "-" + image_url[i];
			}
		}
	}
}