// JavaScript Document

function QuickTip() {
	this.showTimeoutId = null;
	this.hideTimeoutId = null;
	this.existingQuickTip = null;
	this.scrollbarBuffer = 20;
	this.lastTipOverObject = null;
	this.lastTipOutObject = null;
	
	this.show = function(obj) {
		if (this.showTimeoutId != null) {
			clearTimeout(this.showTimeoutId);
			this.showTimeoutId = null;
		}
		
		if ((this.lastTipOutObject == obj) && (this.hideTimeoutId != null)) {
			clearTimeout(this.hideTimeoutId);
			this.hideTimeoutId = null;
		}
		
		this.lastTipOverObject = obj;
		this.showTimeoutId = setTimeout("quickTip.showNow();", 500);
	}
	
	this.showNow = function() {
		var obj = this.lastTipOverObject;
		if (arguments.length > 0) obj = arguments[0];
		
		var winSize = getWindowSize();
		var objLeftPos = findPos(obj)[0];
		var qt = document.createElement("div");
		var qt_x = document.createElement("div");
		var qt_t = document.createElement("div");
		var qt_tl = document.createElement("div");
		var qt_tc = document.createElement("div");
		var qt_tr = document.createElement("div");
		var qt_m = document.createElement("div");
		var qt_ml = document.createElement("div");
		var qt_mc = document.createElement("div");
		var qt_mr = document.createElement("div");
		var qt_b = document.createElement("div");
		var qt_bl = document.createElement("div");
		var qt_bc = document.createElement("div");
		var qt_br = document.createElement("div");
		var arrow = document.createElement("div");
		var container = document.createElement("div");
		var positionContainer = document.createElement("span");
		var w, h, lw;
		var done;
		var distances;
		var qtOffsetLeft;
		var qtArrowOffset;
		
		//get div tags their class names
		positionContainer.className = "quickTip_popupPositionContainer";
		qt.className = "quickTip_popup";
		qt_x.className = "close";
		qt_t.className = "top";
		qt_tl.className = "left";
		qt_tc.className = "center";
		qt_tr.className = "right";
		qt_m.className = "middle";
		qt_ml.className = "left";
		qt_mc.className = "center";
		qt_mr.className = "right";
		qt_b.className = "bottom";
		qt_bl.className = "left";
		qt_bc.className = "center";
		qt_br.className = "right";
		arrow.className = "arrow";
		
		//attach listeners to the qt
		qt.setAttribute("onmouseover", "quickTip.show(quickTip.lastTipOverObject);");
		qt.setAttribute("onmouseout", "quickTip.hide(quickTip.lastTipOverObject);");
		
		//determine the size of the popup window and put contents into qt_mc
		container.innerHTML = hexToStr(obj.getAttribute("media"));
		container.style.visibility = "hidden";
		container.style.position = "absolute";
		container.style.fontSize = "150%";
		container.style.left = "0px";
		container.style.top = "0px";
		document.body.appendChild(container);
		done = false;
		lw = -1;
		while (!done) {
			w = container.offsetWidth;
			h = container.offsetHeight;
			if (w == lw) {
				done = true;
			} else if (h / w < .5) {
				container.style.width = (w - 1) + "px";
			} else {
				done = true;
			}
			lw = w;
		}
		w = w + 5;
		if (w > winSize[0]) w = winSize[0];
		w = container.offsetWidth;
		h = container.offsetHeight;
		qt_mc.innerHTML = container.innerHTML;
		document.body.removeChild(container);
		
		//write the close button
		qt_x.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"quickTip.hideNow();\">&nbsp;</a>";
		
		//determine the distance from the link to each wall of the browser
		distances = getDistancesFromLimits(obj, winSize);
		
		//more placement
		qtOffsetLeft = getQtLeftPosition(winSize[0] - this.scrollbarBuffer, obj, w + 26);
		qtArrowOffset = (((w + 26) / 2) + qtOffsetLeft[1]);
		if (w < qtArrowOffset) qtArrowOffset = w;
		qt.style.left = qtOffsetLeft[0] + "px";
		qt_m.style.height = h + "px";
		qt_mc.style.width = w + "px";
		qt_tc.style.width = w + "px";
		qt_bc.style.width = w + "px";
		qt.style.width = (w + 26) + "px";
		arrow.style.left = qtArrowOffset  + "px";
		
		//put the divs together
		qt_t.appendChild(qt_tl);
		qt_t.appendChild(qt_tc);
		qt_t.appendChild(qt_tr);
		qt_t.appendChild(qt_x);
		qt_m.appendChild(qt_ml);
		qt_m.appendChild(qt_mc);
		qt_m.appendChild(qt_mr);
		qt_b.appendChild(qt_bl);
		qt_b.appendChild(qt_bc);
		qt_b.appendChild(qt_br);
		qt.appendChild(qt_t);
		qt.appendChild(qt_m);
		qt.appendChild(qt_b);
		positionContainer.appendChild(qt);
		
		//determine the placement of the quick tip window, preference being just above the link, centered
		if ((distances[0] < h + 53) && (distances[2] > h + 53)) { //popup too big for above, but not below so place below
			qt.style.top = obj.offsetHeight + "px";
			qt_t.appendChild(arrow);
		} else { //popup placed above
			qt.style.top = (-1 * (h + 53)) + "px";
			qt_b.appendChild(arrow);
		}
		
		//if (this.existingQuickTip != null) this.existingQuickTip.parentNode.removeChild(this.existingQuickTip);
		if (this.existingQuickTip == null) {
			this.existingQuickTip = positionContainer;
			obj.parentNode.insertBefore(positionContainer, obj);
			obj.parentNode.insertBefore(obj, positionContainer);
			
			qt_mc.style.height = qt_ml.offsetHeight + "px";
		}
		
		if (this.showTimeoutId != null) clearTimeout(this.showTimeoutId);
		this.showTimeoutId = null;
		
		//console.info("show now");
	}
	
	this.hide = function(obj) {
		if ((obj == this.lastTipOverObject) && (this.showTimeoutId != null)) {
			clearTimeout(this.showTimeoutId);	//if not yet shown but about to show, then hide
			this.showTimeoutId = null;
		} 
		if (this.hideTimeoutId != null) {
			clearTimeout(this.hideTimeoutId);
			this.hideTimeoutId = null;
		}
		this.hideTimeoutId = setTimeout("quickTip.hideNow()", 200);
		this.lastTipOutObject = obj;
	}
	
	this.hideNow = function() {
		if (this.existingQuickTip != null) {
			this.existingQuickTip.parentNode.removeChild(this.existingQuickTip);
			this.existingQuickTip = null;
		}
		if (this.showTimeoutId != null) clearTimeout(this.showTimeoutId);
		if (this.hideTimeoutId != null) {
			clearTimeout(this.hideTimeoutId);
			this.hideTimeoutId = null;
		}
		
		//console.info("hide now");
	}
	
	function getQtLeftPosition(winWidth, linkObj, qtWidth) {
		var linkWidth = linkObj.offsetWidth;
		var linkX = findPos(linkObj)[0];
		var qtOverlapOneSide = Math.ceil((qtWidth - linkWidth) / 2);
		var qtOffset = qtOverlapOneSide - qtWidth;
		var slide = 0;
		
		if (linkWidth > 490) return [0, 0];
		
		if (qtWidth >= winWidth) return [qtOffset, 0];
		
		if (linkX - qtOverlapOneSide < 0) {
			slide = (linkX - qtOverlapOneSide);
		} else if (linkX + linkWidth + qtOverlapOneSide > winWidth) {
			slide = (linkX + linkWidth + qtOverlapOneSide - winWidth);
		}
		
		return [qtOffset - slide, slide];
	}
	
	function getDistancesFromLimits(obj, winSize) {
		var scrollbarBuffer = 20;
		var width = obj.offsetWidth;
		var height = obj.offsetHeight;
		var pos = findPos(obj);
		var scrollXY = getScrollXY();
		var top = pos[1] - scrollXY[1];
		var left = pos[0] - scrollXY[0];
		var bottom = winSize[1] - top - height - scrollbarBuffer;
		var right = winSize[0] - left - width - scrollbarBuffer;
		
		//alert("Pos: " + pos[0] + ", " + pos[1] + "\nObj W/H: " + width + ", " + height + "\nWin W/H: " + winSize[0] + ", " + winSize[1] + "\nScroll: " + scrollXY[0] + ", " + scrollXY[1] + "\nDistance: " + top + ", " + right + ", " + bottom + ", " + left);
		
		return [top, right, bottom, left];
	}
	
	function getWindowSize() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return [myWidth, myHeight];
	}
	
	function getScrollXY() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return [curleft,curtop];
		}
	}
	
	function hexToStr(hexValue) {
		var string = "";
		var hexCode;
		var value;
		var i;
		
		for (i = 0; i < hexValue.length - 1; i += 2) {
			hexCode = hexValue.substr(i, 1) + hexValue.substr(i + 1, 1);
			value = parseInt(hexCode, 16);
			string += String.fromCharCode(value);
		}
		return string;
	}
}

quickTip = new QuickTip();