//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - por Craig Erskine
// http://qrayg.com | http://solardreamstudios.com
//
// Inspirado no código de Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
//
// Adaptado por Robson Junior
// http://www.robsonjunior.com.br
//////////////////////////////////////////////////////////////////

var qTipTag = new Array("a","abbr","acronym","input","textarea"); // Array com as tags que nos implementaremos o Tool-tip
var qTipX = 15; // qTip's X offset//
var qTipY = 15; // qTip's Y offset//

tooltip = {
	name : "qTip",
	offsetX : qTipX,
	offsetY : qTipY,
	qTipW : "auto",
	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.qTipW = this.getStyle('width');

	var a, sTitle, sAccess;	
	for (var j = 0; j < qTipTag.length; j ++) { // loop que vai implementar o tool-tip nas tags escolhidas
		anchors = document.getElementsByTagName ( qTipTag[j] ); // pegamos a tag escolhida
		
		for (var i = 0; i < anchors.length; i ++) { // atribuicao dos tool tips
			a = anchors[i];
			sTitle  = a.getAttribute("title"); // pegamos o atributo title
			sAccess = a.getAttribute("accesskey"); // usado para ajustar largura para auto
			
			if(sTitle) { // se estiver setado
				a.setAttribute("tiptitle", sTitle);
				a.removeAttribute("title");
				
				a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'), this.getAttribute('accesskey'))};
				a.onmouseout = function() {tooltip.hide()};
			}
		}// fim do for
	}
}

tooltip.move = function (evt) {
	var x = 0, y = 0, offsetX, offsetY, winWidth, winHeight, scrollX, scrollY;
	
	scrollX   = tooltip.getScrollX();
	scrollY   = tooltip.getScrollY();
	winWidth  = tooltip.getWindowWidth();
	winHeight = tooltip.getWindowHeight();
	
//	$("debugtxt").value = (window.event.clientY)+','+(this.tip.offsetHeight + this.offsetY)+','+document.documentElement.clientHeight;
	
	if (document.all) {	// IE
		if((window.event.clientX + this.tip.offsetWidth + this.offsetX) > winWidth)
			offsetX = -(this.tip.offsetWidth + this.offsetX);
		else
			offsetX = this.offsetX;
		
		if((window.event.clientY + this.tip.offsetHeight + this.offsetY) > winHeight)
			offsetY = -(this.tip.offsetHeight + this.offsetY);
		else
			offsetY = this.offsetY;
	
		x = window.event.clientX + scrollX;
		y = window.event.clientY + scrollY;
		
	} else {	//Bons Navegadores
		if((evt.pageX - scrollX + this.tip.offsetWidth + this.offsetX) > winWidth)
			offsetX = -(this.tip.offsetWidth + this.offsetX);
		else
			offsetX = this.offsetX;
			
		if((evt.pageY - scrollY + this.tip.offsetHeight + this.offsetY) > winHeight)
			offsetY = -(this.tip.offsetHeight + this.offsetY);
		else
			offsetY = this.offsetY;
	
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + offsetX) + "px";
	this.tip.style.top = (y + offsetY) + "px";
	
	if(this.getStyle('display') != 'none')
		this.tip.style.visibility = 'visible';
	
	if(navigator.userAgent.toLowerCase().indexOf('msie 6')!=-1)
		this.hideShowCovered();
}

tooltip.show = function (text, access) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	if(access)
		this.tip.style.width = 'auto';
	else
		this.tip.style.width = this.qTipW;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
	this.tip.style.visibility = "hidden";
}

tooltip.getStyle = function (cssprop) {
	if (this.tip.currentStyle) // IE
		return this.tip.currentStyle[cssprop];
 	else if (document.defaultView && document.defaultView.getComputedStyle) // Firefox e compatíveis
		return document.defaultView.getComputedStyle(this.tip, "")[cssprop];
 	else {	// Outros
		try { 
			return this.tip.style[cssprop];
		}
		catch(e) {}
	}
}

tooltip.getWindowWidth = function () {
	return 	window.innerWidth != null ?	window.innerWidth : 
			document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth :
			document.body != null ?	document.body.clientWidth : null;
}

tooltip.getWindowHeight = function () {
	return  window.innerHeight != null ? window.innerHeight :
			document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight :
			document.body != null ? document.body.clientHeight : null;
}

tooltip.getScrollX = function () {
	return document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
}

tooltip.getScrollY = function () {
	return document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
}

tooltip.hideShowCovered = function () {
	var tags = new Array("applet", "iframe", "select");
	var el = this.tip;

	var p = tooltip.getAbsolutePos(el);
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;

	for (var k = tags.length; k > 0; ) {
		var ar = document.getElementsByTagName(tags[--k]);
		var cc = null;

		for (var i = ar.length; i > 0;) {
			cc = ar[--i];

			p = tooltip.getAbsolutePos(cc);
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;status=p.x+','+p.y

			if (this.tip.style.visibility == 'hidden') {
				cc.style.visibility = "visible";
				try{
					cc.parentNode.style.visibility = "visible";	
				}
				catch(er){}
			} else {
				if(!((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1))){
					cc.style.visibility = "hidden";
					try{
						cc.parentNode.style.visibility = "hidden";	
					}
					catch(er){}
				}
			}
		}
	}
}

tooltip.getAbsolutePos = function(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = tooltip.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

window.onload = function () {
	tooltip.init();
}