/*
Tooltip Ajax - non intrusive - Cross browser - Optimal layout- Shadow Drop.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


vuDAT_glossary_css = window.vuDAT_glossary_css || "/css/glossary.css";
vuDAT_glossary = "/translate/default.asp";
section = window.section || "";  
var timer;
var tmp_word = "";
var c_left;
arrow = new Image(); 
arrow.src = "/img/arrow.gif"; 
arrow_2 = new Image(); 
arrow_2.src = "img/arrow_2.gif";

String.prototype.escapeMyHTML = function () {                                        
        return                                                                 
            /*(this.replace('á','&aacute;').
				replace(/Á/g,'&Aacute;').
 				replace(/í/g,'&iacute;').
				replace(/Í/g,'&Iacute;').   
				replace(/ó/g,'&oacute;').
				replace(/Ó/g,'&Oacute;'). 
				replace(/é/g,'&eacute;').
				replace(/É/g,'&Eacute;').
				replace(/ú/g,'&uacute;').
				replace(/Ú/g,'&Uacute;').  
				replace(/ñ/g,'&ntilde;').
				replace(/Ñ/g,'&Ntilde;'))*/this;
};
/*String.prototype.escapeMyHTML = function () {                                        
        return(                                                                 
            this.replace('&','&amp;').replace('>','&gt;').replace('<','&lt;').replace('"','&quot;').replace('á','&aacute;'));
           //      .replace('Á','&Aacute;').replace('í','&iacute;').replace('Í','&Iacute;').replace('ó','&oacute;').replace('Ó','&Oacute;').replace('é','&eacute;').replace('É','&Eacute;').replace('ú','&uacute;').replace('Ú','&Uacute;').replace('ñ','&ntilde;').replace('Ñ','&Ntilde;'));
};*/
var ajaxHandlers = {
	getDefinition : function(key) {
		clickPosText(tmp_word,"");
		var self = ajaxHandlers;
		var value = Math.random();
		value += "&key=" + key;
		//alert(value);
		var ajax = new Ajax.Request( vuDAT_glossary, {
                                parameters: value,
                                method:"get",
                                onComplete: self.handleDefinition
                                }
        ); 
		//self.ajax.doGet(,self.handleDefinition);
		},
	handleDefinition : function(st) {
	    var str = st.responseText;
	    var the_cont = document.getElementById("clickPosText");
		the_cont.innerHTML = str;
//Comprueba si la ventana puede verse entera y aplica corrección

//the_cont.style.left;
var _top = parseInt(the_cont.style.top,10);
client_h=document.documentElement.clientHeight;
scroll_t=document.documentElement.scrollTop;
client_w=document.documentElement.clientWidth;
if ( parseInt(client_h+scroll_t-100) < parseInt(the_cont.style.top)+parseInt(the_cont.offsetHeight))
{
			the_cont.style.top = parseInt(client_h+scroll_t-50)-parseInt(the_cont.offsetHeight)+'px';	
}
if ( parseInt(client_w+document.documentElement.scrollLeft) < parseInt(c_left)+parseInt(the_cont.offsetWidth))
{
			if (parseInt(the_cont.style.top,10)< 0)
			{
			    document.getElementById("arrow").style.top = _top + "px";
			    the_cont.style.top = "0px";
			}
			the_cont.style.left = parseInt(c_left)-32-34-parseInt(the_cont.offsetWidth)+'px';
			the_cont.style.marginLeft="1em";
			document.getElementById("arrow").style.left=parseInt(parseInt(document.getElementById("arrow").style.left)-34)+"px";			
			document.getElementById("arrow").innerHTML = "<img src='/img/arrow_2.gif' width='34' height='35' class='arrow' />";
}
else
{
the_cont.style.left = parseInt(c_left)+ "px";
}


var contenido=the_cont.innerHTML;

the_cont.innerHTML= "<div class='mod'><div class='cont'><div class='hd'></div><div class='bd'>"+contenido+"</div><div class='ft'></div></div></div>"
the_cont.style.visibility="visible";
document.getElementById("arrow").style.visibility="visible";
return true;
the_cont.innerHTML = "<span class='red'>No hay información sobre este tema.</span>";
	},
	getGlossary : function() { 
		var the_cont = document.getElementById("glossary");
		if(the_cont) {
			var self = ajaxHandlers;
			var ajax = new Ajax.Request( vuDAT_glossary, {
                                parameters: "",
                                method:"post",
                                onComplete: self.handleGlossary
                                }
            );}}}

			
Event.observe(window,"load",function () {
// Este código se ejecuta tras cargarse la página
		//<![CDATA[
		// loading the stylesheet into the document header
		if(document.createStyleSheet) { // non-standard IE way of doing it
			document.createStyleSheet(vuDAT_glossary_css);
		} else { // do it the W3C DOM way
			var newCSS=document.createElement('link');
			newCSS.rel='stylesheet';
			newCSS.href=vuDAT_glossary_css;
			newCSS.type='text/css';
			document.getElementsByTagName("head")[0].appendChild(newCSS);
		}
		//]]>
		var self = ajaxHandlers;
		var the_words = $("container").getElementsBySelector(".highlightSpan");
		if(the_words) {
			var cur_word = "";
			for (i = 0; i < the_words.length; i++) {
				cur_word = the_words[i];
				cur_word.onmouseout = function() {
					var the_div = document.getElementById("clickPosText");
					if(the_div) {
						timer = setTimeout("clickPosTextKill()",1200);
										}
				}
				cur_word.onclick = function() {
					var me = this;
					var the_div = document.getElementById("clickPosText");
					tmp_word = me;
					self.getDefinition(me.id);
				}

			}
		}
		ajaxHandlers.getGlossary();
		// code in here gets executed once the page loads // end section
	}
);

function clickPosText(obj,txt) {

	clickPosTextKill();
	var diff = 88;
	var container = document.createElement('div');
	var pos = findPos(obj);
	container.id = "clickPosText";
	container.style.visibility="hidden";
	container.style.top = pos[1] < 0 ? "0px" : (pos[1]+18) + "px";
	c_left=(pos[0]+34-1)+ "px";
	//container.style.left = (pos[0]+34-1)+ "px";
	container.innerHTML = txt;
	var image_container= document.createElement('div');
	image_container.id = "arrow";
	image_container.style.visibility="hidden";
	if ( navigator.userAgent.indexOf("MSIE 6") != -1 )
	    diff = 78;
	if (pos[1] + diff < document.documentElement.clientHeight)
	{
	    image_container.style.top = (pos[1]+18) + "px";
	}
    else
    {
        var difference = pos[1] + 78 - document.documentElement.clientHeight;
        image_container.style.top= (pos[1]  - difference ) + "px";
    }
	image_container.style.left = (pos[0])+34+10+ "px";
	image_container.innerHTML = "<img src='/img/arrow.gif' width='34' height='35' class='arrow' />";
	document.body.appendChild(image_container);
	document.body.appendChild(container);

	container.onclick = function() {
		killMe(this);
		killMe(image_container);
	}
	container.onmouseover = function() {
		stopper();
	}
	container.onmouseout = function() {
		timer = setTimeout("clickPosTextKill()",1200);
	}
}

function clickPosTextKill() {
	var att = document.getElementById("clickPosText");
	if(att) {
		att.parentNode.removeChild(att);
	}
var att = document.getElementById("arrow");
	if(att) {
		att.parentNode.removeChild(att);
	}
}

function stopper() {
	clearTimeout(timer);
}
function killMe(obj) {
	obj.parentNode.removeChild(obj);
}
/* 
Encontrar la posición de un objeto
http://www.quirksmode.org/js/findpos.html
*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft+(obj.offsetWidth/2)
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}