/**************************************************************************
JTip
Permie representar un tooltip al pasar el raton por encima de un enlace 
El enlace contendra el codigo html en el title empezando por la marca <tip>
***************************************************************************/
// Configuracion
// debe definirse el estilo css #jTip 
tooltip = {
	name : "jTip",
	offsetX : 20,
	offsetY : 40,
	tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "jTip";}
	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 = new Array();
	var anchors2;

    anchors2 = document.getElementsByTagName("td");

	for (var i = 0; i < anchors2.length; i ++) {
		anchors.push(anchors2[i]);
	}

	anchors2 = document.getElementsByTagName("li");

	for (var i = 0; i < anchors2.length; i ++) {
		anchors.push(anchors2[i]);
	}

	
	for (var i = 0; i < anchors.length; i ++) {
		a = anchors[i]; // <-- Solo enlaces
		sTitle = unescapeHTML(a.getAttribute("title")); //<-- Solo titles (recuerda que los atributos no son extensibles)
		if(sTitle && sTitle.substring(0,5).toLowerCase()=="<tip>" ) {
			sTitle=sTitle.replace("<tip>","")
			a.setAttribute("tiptitle", sTitle);
			a.removeAttribute("title");
			
			if (a.tagName!='LI') {
					a.onmouseover = function() {
						tooltip.show(this.getAttribute('tiptitle')); 
						this.className = this.className + '_over';
					};
			}
			else {
					a.onmouseover = function() {
						tooltip.show(this.getAttribute('tiptitle')); 
					};
			}
			
		
			if (a.tagName!='LI') {
					a.onmouseout = function() {
						tooltip.hide();
						this.className= this.className.replace('_over','');
				    };
			}
			else {
					a.onmouseout = 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 {// Mozilla
		x = evt.pageX;
		y = evt.pageY;
	}

	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";

	
	// Posicion del elemento en la ventana
	coorY =y - get_scroll_client_height();
	coorX =x - get_scroll_client_width();
   
  	
	// Tamaņo de la ventana
	width_window = get_client_width();
	height_window = get_client_height();
	
	// Tamaņo del elemento
	ElemWidth = this.tip.offsetWidth; 
  ElemHeight = this.tip.offsetHeight;
 
	//Y
	if ((coorY + this.offsetY + ElemHeight) < height_window) {
 		 this.tip.style.left = (y + this.offsetY) + "px";
     }
     else {
  		this.tip.style.top = (y - this.offsetY - ElemHeight) + "px";
     }

    //X
	if ((coorX + this.offsetX + ElemWidth + 20 ) < width_window) {
		this.tip.style.left = (x + this.offsetX) + "px";
	}   
	else {
 		this.tip.style.left = (get_scroll_client_width()  + width_window - ElemWidth - 20 ) + "px";
	}

	//Posicion en la pagina
	ElemTop = parseInt(this.tip.style.top.replace("px",""));
	ElemLeft= parseInt(this.tip.style.left.replace("px",""));


}

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 ();
}
/* Tamaņo de la ventana */ 
function get_client_width() { 
    if (window.innerWidth != null) 
        return window.innerWidth; 
    if (document.documentElement.clientWidth)
        return document.documentElement.clientWidth;    
    if (document.body.clientWidth != null) 
        return document.body.clientWidth; 
    return (null); 
} 
function get_client_height() { 
    if (window.innerHeight != null) 
        return window.innerHeight; 
    if (document.documentElement.clientHeight)
        return document.documentElement.clientHeight;    
    if (document.body.clientHeight != null) 
        return document.body.clientHeight; 
    return (null); 
} 

function get_scroll_client_width(){ 
  if(window.pageXOffset != null) 
    return window.pageXOffset; 
  if (document.documentElement.scrollLeft)
    return document.documentElement.scrollLeft;
  if(document.body.scrollLeft != null) 
    return document.body.scrollLeft; 
  return (null) 

} 
function get_scroll_client_height(){ 
  if(window.pageYOffset != null) 
    return window.pageYOffset;
  if(document.documentElement.scrollTop)
    return document.documentElement.scrollTop; 
  if(document.body.scrollTop != null) 
    return document.body.scrollTop; 
  return (null) 

} 

function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}; 

function unescapeHTML(html) {
   var htmlNode = document.createElement("DIV");
   htmlNode.innerHTML = html;
   if(htmlNode.innerText)
      return htmlNode.innerText; // IE
   return htmlNode.textContent; // FF
}
