// From Sitepoint The Javascript Anthology
// Modified for use with technihowlib version
// Nigel Dahl 15/05/06
//

function initTooltips(topElem) {
	var tips;
	if (typeof topElem == "string") {
		tips = getDOMElementsByAttribute(topElem, "class", "hastooltip");
	} else {
		tips = getElementsByAttribute("class", "hastooltip");
	}

	for (var i = 0; i < tips.length; i++) {
    	addEvent(tips[i], "mouseover", showTip);
	    addEvent(tips[i], "mouseout", hideTip);
  	}
  	return true;
}

// Initialize after loading the page
addEvent(window, 'load', initTooltips);

function showTip(event) {

 	var browserName=BrowserDetect.browser; 
	var browserVer=BrowserDetect.version; 
	if (browserName=="Explorer" && browserVer < 7) {
		// THIS IS A RIGHT ROYAL PAIN!! 
		// essentially ie6 and 7 deal with these boxes inconsistently 
		// so we need one version for ie before v7 
		// and one version for everyone else.
		if (typeof event == "undefined") {
			event = window.event;
		}
		var target = getEventTarget(event);
		while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className)) {
			target = target.parentNode;
		}
		var tip = document.createElement("div");
		var content = target.getAttribute("tipcontent");
		
		target.tooltip = tip;
		//target.setAttribute("title", "");
		if (target.getAttribute("id") != "") {
		tip.setAttribute("id", target.getAttribute("id") + "tooltip");
		}
		tip.className = "tooltip";
		var scrollingPosition = getScrollingPosition();
		var cursorPosition = [0, 0];
		
		if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
			cursorPosition[0] = event.pageX;
			cursorPosition[1] = event.pageY;
		} else {
			cursorPosition[0] = event.clientX + scrollingPosition[0];
			cursorPosition[1] = event.clientY + scrollingPosition[1];
		}
		tip.style.position = "absolute";
		tip.style.left = cursorPosition[0] + 10 + "px";
		tip.style.top = cursorPosition[1] + 10 + "px";
		tip.style.visibility = "hidden";
		
		document.getElementsByTagName("body")[0].appendChild(tip);
		//alert(ShadowWindow(content));
		//tip.innerHTML = content;
		tip.innerHTML = ShadowWindow(content);
		ShadowFix();

		var viewportSize = getViewportSize();
		if (cursorPosition[0] - scrollingPosition[0] + 10 + tip.offsetWidth > viewportSize[0] - 25) {
			tip.style.left = scrollingPosition[0] + viewportSize[0] - 25 - tip.offsetWidth + "px";
		} else {
			tip.style.left = cursorPosition[0] + 10 + "px";
		}
		if (cursorPosition[1] - scrollingPosition[1] + 10 + tip.offsetHeight > viewportSize[1] - 25) {
			if (event.clientX > (viewportSize[0] - 25 - tip.offsetWidth)) {
			  tip.style.top = cursorPosition[1] - tip.offsetHeight - 10 + "px";
			} else {
			  tip.style.top = scrollingPosition[1] + viewportSize[1] - 25 - tip.offsetHeight + "px";
			}
		} else {
			tip.style.top = cursorPosition[1] + 10 + "px";
		}
		tip.style.visibility = "visible";
		return true;
	} 

	if (typeof event == "undefined") {
		event = window.event;
	}
	var target = getEventTarget(event);
	
	while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className)) {
		target = target.parentNode;
	}
	var tip = document.createElement("div");
	var content = target.getAttribute("tipcontent");
	target.tooltip = tip;
	//target.setAttribute("title", "");
	if (target.getAttribute("id") != "") {
	tip.setAttribute("id", target.getAttribute("id") + "tooltip");
	}
	tip.className = "tooltip";

	var scrollingPosition = getScrollingPosition();
	var cursorPosition = [0, 0];
	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
		cursorPosition[0] = event.pageX;
		cursorPosition[1] = event.pageY;
	} else {
		cursorPosition[0] = event.clientX + scrollingPosition[0];
		cursorPosition[1] = event.clientY + scrollingPosition[1];
	}

	document.getElementsByTagName("body")[0].appendChild(tip);
	tip.innerHTML = ShadowWindow(content);
	ShadowFix();

	// firstly get the mouse position
	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
		mouseX = event.pageX;
		mouseY = event.pageY;
	} else {
		mouseX = event.clientX + scrollingPosition[0];
		mouseY = event.clientY + scrollingPosition[1];
	}
	// get the tip width and height
	var winContent = document.getElementById("winContent");
	var tpWd = winContent.offsetWidth;
	var tpHt = winContent.offsetHeight;
	// if we're in a scrolling window then we need to add that to our winWd/winHt
	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;
	}
	// find out the size of the window/viewing area and add the scroll values
	var winWd = document.body.clientWidth+scrOfX;
	var winHt = document.body.clientHeight+scrOfY;
	// set the distance of the tip from the mouse
	var offX = 10;
	var offY = 10;
	// now do the maths so that the tip is ALWAYS shown inside the visible area
	if ((mouseX+offX+tpWd)>winWd) 
		tip.style.left = mouseX-tpWd-offX+"px";
	else tip.style.left = mouseX+offX+"px";
	if ((mouseY+offY+tpHt)>winHt) 
		tip.style.top = winHt-(tpHt+offY)+"px";
	else tip.style.top = mouseY+offY+"px";
	// finish up the changes to the tip and target style
	tip.style.position = "absolute";
	tip.style.visibility = "visible";
	target.style.cursor = "help";
	return true;
}

function hideTip(event) {
  if (typeof event == "undefined") {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.className == null || !target.className.match(/(^| )hastooltip( |$)/)) {
    target = target.parentNode;
  }

  if (target.tooltip != null) {
    //target.setAttribute("title", target.tooltip.innerHTML);
	target.tooltip.parentNode.removeChild(target.tooltip);
  }

  return false;
}


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// EOF infobox.js