//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com | http://solardreamstudios.com
//
// Inspired by code from Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
//////////////////////////////////////////////////////////////////

///////////////////////////////////////
//Edited `function tooltip.move` by KpoHo
//E-mail: kpono.dk@gmail.com
///////////////////////////////////////


var qTipTag = "a"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipImg = "img";
var qTipInput = "input";
var qTipArea = "area";
var qTipDiv = "div";
var qTipX = -30; //This is qTip's X offset//
var qTipY = 25; //This is qTip's Y offset//



//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  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;
	var anchors = document.getElementsByTagName (qTipTag);

	for (var i = 0; i < anchors.length; i ++) {
		a = anchors[i];
		sTitle = a.getAttribute("title");
		if(sTitle) {
			a.setAttribute("tiptitle", sTitle);
			a.removeAttribute("title");
			a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			a.onmouseout = function() {tooltip.hide()};
		}
	}
*/	
	var aImg, sTitleImg;
	var imgs = document.getElementsByTagName (qTipImg);

	for (var i = 0; i < imgs.length; i ++) {
		aImg = imgs[i];
		sTitleImg = aImg.getAttribute("alt");
		if(sTitleImg) {
			aImg.setAttribute("tiptitle", sTitleImg);
			aImg.removeAttribute("alt");
			aImg.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			aImg.onmouseout = function() {tooltip.hide()};
		}
	}
	
	var aInpt, sTitleInpt;
	var inpt = document.getElementsByTagName (qTipInput);

	for (var i = 0; i < inpt.length; i ++) {
		aInpt = inpt[i];
		sTitleInpt = aInpt.getAttribute("title");
		if(sTitleInpt) {
			aInpt.setAttribute("tiptitle", sTitleInpt);
			aInpt.removeAttribute("title");
			aInpt.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			aInpt.onmouseout = function() {tooltip.hide()};
		}
	}
	
	var aArea, sTitleArea;
	var inpt = document.getElementsByTagName (qTipArea);

	for (var i = 0; i < inpt.length; i ++) {
		aArea = inpt[i];
		sTitleArea = aArea.getAttribute("tip");
		if(sTitleArea) {
			aArea.setAttribute("tiptitle", sTitleArea);
			aArea.removeAttribute("tip");
			aArea.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			aArea.onmouseout = function() {tooltip.hide()};
		}
	}
	
	var aDiv, sTitleDiv;
	var inpt = document.getElementsByTagName (qTipDiv);

	for (var i = 0; i < inpt.length; i ++) {
		aDiv = inpt[i];
		sTitleDiv = aDiv.getAttribute("tip");
		if(sTitleDiv) {
			aDiv.setAttribute("tiptitle", sTitleDiv);
			aDiv.removeAttribute("tip");
			aDiv.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
			aDiv.onmouseout = function() {tooltip.hide()};
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	var div = document.getElementById("qTip");
	if (document.all) {//IE
		var evnt = window.event;
	} else {//Good Browsers
		var evnt = evt;
	}
	var ex = evnt.clientX + document.body.scrollLeft;
	var ey = evnt.clientY + document.body.scrollTop;
	var x = evnt.clientX + div.offsetWidth > document.body.clientWidth - 7 ? ex - div.offsetWidth + 30 : ex - 30;
	var y = evnt.clientY + div.offsetHeight > document.body.clientHeight - 7 ? ey - div.offsetHeight - 10 : ey + 22;
	if (x < 0 ) {
		x = ex - div.offsetWidth/2
	}
	if (x < 7 ) {
		x = 7
	}
	if (x > document.body.clientWidth - div.offsetWidth - 7) {
		x= document.body.clientWidth - div.offsetWidth - 7
	}

	div.style.left = x;
	div.style.top = y;
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}
