// 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".
// DG: Added a change for the x offset, if control on left hand side of page then display tooltip bottom right, otherwise bottom left

var qTipTag = "a,label,input,div,td,textarea,span,th,img"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipLeft = 0;  // For the left hand side of the page
var qTipRight = -125;  // For the right hand side of the page
var qTipY = 15;  //This is qTip's Y offset//
var qTipMaxWidth = 250;   // Maximum width for a tooltip 

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX: qTipLeft,
  offsetY: qTipY,
  leftOffset: qTipLeft,
  rightOffset: qTipRight,
  maxWidth: qTipMaxWidth,
  tip : null
}

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];
				sTitle = a.getAttribute("title");
				if(!sTitle){
				    sTitle = a.getAttribute("alt");
				}			
				if(sTitle)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function () { tooltip.hide() };
                    addEvent(a,"click", function () { tooltip.hide() });
				}
			}
		}
	}
}

tooltip.move = function(evt) {
    var x = 0, y = 0;
    if (document.all) {//IE
        x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        x += window.event.clientX;
        y += window.event.clientY;

    } else {//Good Browsers
        x = evt.pageX;
        y = evt.pageY;
    }

    var halfway, tipLeft, tipTop, pageWidth, pageHeight;
    if (window.innerWidth) {
        halfway = (window.innerWidth / 2);
        pageHeight = window.innerHeight;
        pageWidth = window.innerWidth;
    } else {
        halfway = (document.documentElement.clientWidth / 2);
        pageHeight = document.documentElement.clientHeight;
        pageWidth = document.documentElement.clientWidth;
    }
    if (x >= halfway) { this.offsetX = this.rightOffset; } else { this.offsetX = this.leftOffset; }

    if (x + this.offsetX + this.tip.offsetWidth > pageWidth)
        tipLeft = pageWidth - this.tip.offsetWidth;
    else
        tipLeft = x + this.offsetX;
        
    this.tip.style.left = tipLeft + "px";

    if (y + this.offsetY + this.tip.offsetHeight > pageHeight) {
        // No greater than the page height
        tipTop = pageHeight - this.tip.offsetHeight;
    }
    else {
        tipTop = y + this.offsetY;
    }
    this.tip.style.top = tipTop + "px";
}

tooltip.show = function(text) {
    if (!this.tip) return;
    this.tip.innerHTML = text;
    this.tip.style.display = "block";
    this.tip.style.maxWidth = this.maxWidth + "px";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

// AC: commented out and placed the init on master page
//window.onload = function () {
//	tooltip.init ();
//}
