/*
Zona de tooltips
*/
/*
	Gestionador de eventos para JavaScript.
	Ejemplo:
		- Suscribirse a un evento: AddEventListener("Click", this.CreateMenu);
		- Eliminar suscripción: RemoveEventListener("Click", this.CreateMenu);
		- Lanzar evento: DispatchEvent('Click', this);
	Tested:
		- IE, Firefox, Safari.
*/
function addEvent(elm, evType, fn, useCapture){
if (typeof(fn)=="string")
{
	if (elm.addEventListener) 
			{	
				elm.addEventListener(evType, function (){DispatchEvent(fn,elm)}, useCapture);
				return true;
			} else if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, function (){DispatchEvent(fn,elm);});
				return r;
			} else {
				elm['on' + evType] = function (){DispatchEvent(fn,elm)};
			}
	}else{
			if (elm.addEventListener) 
			{	
				elm.addEventListener(evType, fn, useCapture);
				return true;
			} else if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, fn);
				return r;
			}else{
				elm['on' + evType] = fn;
			}
	}
}
function CreateEventObject (e, fn)
{
	this.e = e;
	this.fn = fn;
}
function AddEventListener (e, fn)
{
	arrEvents.push(new CreateEventObject(e, fn));
}
function RemoveEventListener (e, fn)
{
	for (var i in arrEvents)
		if (arrEvents[i].e == e && arrEvents[i].fn == fn)
			arrEvents.splice(i, 1);
}
function DispatchEvent (e, obj)
{
	
	for (var i in arrEvents)
		if (arrEvents[i].e == e)
			arrEvents[i].fn(obj);
}

var arrEvents = new Array();


var div_movil;


function eliminaToolTip(obj)
{
var body=document.getElementsByTagName("body")[0];
body.removeChild(div_movil);
}
function creaToolTip(obj)
{
imagenhref=obj.href;
if(obj.firstChild.tagName=="STRONG")
{

imagenalt = obj.previousSibling.firstChild.firstChild.attributes.alt.value;
}else
{
imagenalt = obj.firstChild.attributes.alt.value;
}
div_movil=document.createElement("div");
div_movil.setAttribute("id","tooltip_capa");
div_movil.style.display="none";
var imagen=document.createElement("img");
//imagenhref = imagenhref.split("/");
//imagenhref=imagenhref[imagenhref.length-1]
//alert ("http://oficina2.kaplan.di:81/frai/utils/imagen.php?ancho=335&alto=255&archivo="+imagenhref);
imagen.setAttribute("src",imagenhref);
var texto=document.createElement("p");
var alt= document.createTextNode(imagenalt);
texto.appendChild(alt);
div_movil.appendChild(imagen);
div_movil.appendChild(texto);
document.getElementsByTagName('body')[0].appendChild(div_movil);
}
var offsetfrommouse=[15,15];
var displayduration=0;
var currentimageheight = 100;
function truebody(){return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;}
function controlaventana(e)
{
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)
if (typeof e != "undefined"){
		if (docwidth - e.pageX < 300){
			xcoord = e.pageX - xcoord - 286; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + 110)){
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < 300){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 286; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	if (div_movil.style.display=="none")div_movil.style.display="block";
	div_movil.style.left=xcoord+"px";
	div_movil.style.top=ycoord+"px";
}
function manejaToolTip(obj)
{

window.onmousemove=controlaventana;
document.onmousemove=controlaventana;
}
function abreToolTip(obj)
{
	imagenhref=obj.href;
	GTK_openImage(imagenhref);
}

function gestiona(enlaces)
{
	var enlace;
	AddEventListener("muestraToolTip",creaToolTip);
  	AddEventListener("muestraToolTip",manejaToolTip);
  	AddEventListener("ocultaToolTip",eliminaToolTip);
  	AddEventListener("abreToolTip",abreToolTip);
  	AddEventListener("mueve",controlaventana);
  
	for (i=0;i<enlaces.length;i++)
	{
	enlace = enlaces[i];
	addEvent(enlace, "mouseover",'muestraToolTip',false);
	addEvent(enlace, "mouseout",'ocultaToolTip',false);	
	addEvent(enlace, "click",'abreToolTip',false);
	}	
}
/*
Zona de tooltips
*/
function init_tooltips()
{
  	var enlaces=new Array();
  	var todos=document.getElementsByTagName("a");
  	var j=0;
  	
  	for (i=0;i<todos.length;i++)
  	{
  	if (todos[i].className=="tooltip")enlaces[j++] =todos[i];
  	}
  	gestiona(enlaces);
}

window.onload=init_tooltips;