// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com

// Multi-tag support by James Crooke
// http://www.cj-design.com

// Inspired by code from Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com

// Copyright (c) 2006 Craig Erskine

// Permission is granted to copy, distribute and/or modify this document
// under the terms of the GNU Free Documentation License, Version 1.3
// or any later version published by the Free Software Foundation;
// with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
// A copy of the license is included in the section entitled "GNU
// Free Documentation License".

var qTipTag = "img"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var TibAttribute = 'tiptitle';
var qTipX = 15; 		//This is qTip's X offset//
var qTipY = 15; 		//This is qTip's Y offset//
var optionIsSelected = false;
var imgName;

//There's no need to edit anything below this line//

tooltip =
	{
	name : "qTip",
	offsetX : qTipX,
	offsetY : qTipY,
	tip : null
	}

tooltip.hide = function ()
{
if (!this.tip)
	return;

this.tip.innerHTML = "";
this.tip.style.display = "none";
}




tooltip.init = function ()
{
var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";

	if (!tipContainerID)
		{
		var tipContainerID = "qTip";
		}

	var tipContainer = document.getElementById(tipContainerID);

	if (!tipContainer)
		{
		tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
		document.getElementsByTagName("body").item(0).appendChild(tipContainer);
		}

	if (!document.getElementById)
		return;

	this.tip = document.getElementById(this.name);

	if (this.tip)
		document.onmousemove = function (evt) {tooltip.move (evt)};


	var a, sTitle, elements;
	
	var elementList = qTipTag.split(",");

	for (var j = 0; j < elementList.length; j++)
		{
		elements = document.getElementsByTagName(elementList[j]);

		if (elements)
			{
			for (var i = 0; i < elements.length; i++)
				{
				a = elements[i];
				
				if (a && elementList[j] == 'select')
					{
					var selectedTitle = a.options[a.selectedIndex].getAttribute("title");
					var selectedTitle2 = a.options[a.selectedIndex].getAttribute("tiptitle");
					
					if (selectedTitle)
						{
						a.setAttribute("title", selectedTitle);
						}
					else if (selectedTitle2)
						{
						a.setAttribute("title", selectedTitle2);
						}

					a.setAttribute("onchange", a.getAttribute("onchange") + "; this.setAttribute('" + TibAttribute + "', this.options[this.selectedIndex].getAttribute('" + TibAttribute + "'));");
					}

				sTitle = a.getAttribute("title");
				
				if (sTitle)
					{
					a.setAttribute(TibAttribute, sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");

					if(elementList[j] == "option")
						{
						a.onmouseover = function()
							{
							optionIsSelected = true;
							tooltip.show(this.getAttribute(TibAttribute))
							};
						}
					else if (elementList[j] == "select")
						{
						a.onmouseover = function()
							{
							if (!optionIsSelected)
								{
								tooltip.show(this.getAttribute(TibAttribute));
								}
							};
						}
					else
						{
						a.onmouseover = function()
							{
							// DRH: save the image name so we can later get hgt/wid
							imgName = this.getAttribute("pic");

							tooltip.show(this.getAttribute(TibAttribute))
							};
						}

					if(elementList[j] == "option")
						{
						a.onmouseout = function()
							{
							optionIsSelected = false;
							tooltip.hide();
							};
						}
					else
						{
						a.onmouseout = function()
							{
							tooltip.hide();
							};
						}
						
					}
				}
			}
		}

	tooltip.hide();
	}

tooltip.move = function (evt)
{
var mousex = 0, mousey = 0;
var tipx = 0, tipy = 0;
var PicHgt, PicWid;
var newImg;
var edgesafety = 25;

if (document.all)
	{
	//IE
	mousex = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
	mousey = (document.documentElement && document.documentElement.scrollTop)  ? document.documentElement.scrollTop  : document.body.scrollTop;
	mousex += window.event.clientX;
	mousey += window.event.clientY;
	}
else
	{
	//Good Browsers
	mousex = evt.pageX;
	mousey = evt.pageY;
	}

// original code was simply:
//	this.tip.style.left = (mousex + this.offsetX) + "px";
//	this.tip.style.top = (mousey + this.offsetY) + "px";

// DRH: ensure we don't place the popup pic over the mouse (prevents flicker)

// get the height and width of the image we'll be displaying
newImg = new Image();
newImg.src = imgName;
PicHgt = newImg.height;
PicWid = newImg.width;

//alert("hgt = " + PicHgt);
//alert("wid = " + PicWid);

// first, try to place pic at upper left of mouse
tipx = mousex - PicWid - this.offsetX;
tipy = mousey - PicHgt - this.offsetY;

// if offscreen at left, move to right of the mouse
if (tipx < edgesafety)
	{
	tipx = mousex + this.offsetX;
	}

// if offscreen at top, move below the mouse	
if (tipy < edgesafety)
	{
	tipy = mousey + this.offsetY;
	}

// if offscreen at right, move midpoint to mouse
if (tipx + PicWid + edgesafety > document.documentElement.clientWidth)
	{
	tipx = mousex - (PicWid / 2) - this.offsetX;

	if (tipx < 0)
		tipx = 5;
	}

// if offscreen at bottom, move midpoint to mouse
if (tipy + PicHgt + edgesafety > document.documentElement.clientHeight)
	{
	tipy = mousey - (PicHgt / 2) - this.offsetY; 	

	if (tipy < 0)
		tipy = 5;
	}

this.tip.style.left = (tipx) + "px";
this.tip.style.top = (tipy) + "px";
}


tooltip.show = function (text)
{
if (!this.tip)
	return;

if (text != '')
	{
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
	}
}


window.onload = function ()
{
tooltip.init ();
}


