//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com
//
// Modified by Seve Savoie Teruel
// Added Preset Tip Box width and height and made boxes switch side if to close to right side or bottom.
//
// 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
//////////////////////////////////////////////////////////////////

var qTipTag = "area"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipWidth = 400;
var qTipHeight = 60;
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//


var mozilla = false;

var xmlObject = false;
	
	if (window.XMLHttpRequest)
	{
		xmlObject = new XMLHttpRequest;
		mozilla = true;
	} else if (window.ActiveXObject) {
		xmlObject = new ActiveXObject("Microsoft.XMLHttp");
	}
	
function removeWhiteSpace(xml)
{
	var loopIndex;

	
	for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++)
	{
		var currentNode = xml.childNodes[loopIndex];
		
		if (currentNode.nodeType == 1 ) 
		{
			removeWhiteSpace(currentNode);
		}

		if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
			xml.removeChild(xml.childNodes[loopIndex--]);
		}
	}
}



//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  width : qTipWidth,
  height : qTipHeight,
  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)};
	this.tip.style.width = this.width + "px";
	this.tip.style.minheight = this.height + "px";

	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)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	var windowWidth = getClientWidth();
	var windowHeight = getClientHeight();
	
	
	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;
	}
	if ( (x + this.offsetX + this.width) > windowWidth)
	{
		this.tip.style.left = (x - this.offsetX - this.width) + "px";
	} else {
		this.tip.style.left = (x + this.offsetX) + "px";
	}
	
	
	if ((y + this.offsetY + this.height) > windowHeight)
	{
		this.tip.style.top = (y - this.offsetY - this.height) + "px";	
	} else {
		this.tip.style.top = (y + this.offsetY) + "px";
	}
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = '<table width="100%" height="200px" border="0"><tr><td align="center" valign="middle">Loading Booth #' + text + '<br /><img src="images/ajax-loader.gif" width="220" height="19" alt="Loading" /></td></tr></table>';
	
	this.tip.style.display = "block";
	if (xmlObject) 
	{
		xmlObject.abort();
		var url = "xmlRequests.php?boothId=" + text;
		xmlObject.open("GET",url,true);
		
		thisTip = this.tip
		xmlObject.onreadystatechange = function()
			{
			// If XML object is returned succesfully
				if (xmlObject.readyState == 4 && xmlObject.status == 200) 
				{
					var xmlDoc = xmlObject.responseXML;
					//If End User is using Mozilla
					if (mozilla) 
					{
						removeWhiteSpace(xmlDoc);
					}
					
					var booth = xmlDoc.getElementsByTagName('booth');
					var name = booth[0].childNodes[0].firstChild.nodeValue;
					var address = booth[0].childNodes[1].firstChild.nodeValue;
					var url = booth[0].childNodes[2].firstChild.nodeValue;
					var logo = booth[0].childNodes[3].firstChild.nodeValue;
					var synopsis = booth[0].childNodes[4].firstChild.nodeValue;
					
					
					
					var boothPrint = '<table width="100%" height="100%"><tr><td width="75px" valign="top"><img src="' + logo + '" width="75" height="56" alt="' + name + '" /></td><td valign="top">'+ name +'<br />' + address +'<br /></td></tr><tr><td colspan="2" valign="top">' + synopsis + '</td></tr></table>';
					
					thisTip.innerHTML = boothPrint;
				}
			}
			xmlObject.send('');
	}
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}