/***** Añade eventos a los elementos de la página definidos en el vector "etiquetas" de la página de modo que al pasar por encima aparezca una descripción según su atributo "title" *****/
//Requiere funciones del archivo dom.js

function Ayuda(){
  var etiquetas=new Array('A','IMG','SPAN');
  var contenido={};
  for (q=0;q<etiquetas.length;q++){
    var enlaces=document.getElementsByTagName(etiquetas[q]);
    for(k=0;k<enlaces.length;k++){
      tit=enlaces[k].title;
      if (tit){
        enlaces[k].removeAttribute('title');
        enlaces[k].removeAttribute('alt');
        enlaces[k].longdesc=tit;
        try{
          enlaces[k].firstChild.removeAttribute('title');
          enlaces[k].firstChild.removeAttribute('alt');
          enlaces[k].firstChild.longdesc=tit;
        }catch(ex){}
        enlaces[k].onmouseover=function(e){generarAyuda(this.longdesc,e)};
        enlaces[k].onmouseout=function(e){destruirAyuda(e)};
        enlaces[k].onmousemove=function(e){if (!document.getElementById('ayuda_contextual')) generarAyuda(this.longdesc,e);moverAyuda(e)};
      }
    }
  }
}

function generarAyuda(titulo,e){
  if (titulo==undefined) return;
  if (!e) var e = window.event;
  var relTarg;

  if(document.getElementById('ayuda_contextual')) document.getElementById('ayuda_contextual').parentNode.removeChild(document.getElementById('ayuda_contextual'));

  this.caja=document.createElement("div");
  this.caja.setAttribute("id","ayuda_contextual");
  this.despl=calcularScroll();
  this.caja.style.top=e.clientY+this.despl[1]+40+"px";//40=alto del puntero
  this.caja.style.left=e.clientX+this.despl[0]+"px";
  this.frecuencia=40;
  this.opacidad=0;
  this.aumento=0;
  this.caja.innerHTML=titulo;
  cambiarOpacidad(this.caja,this.opacidad);
  document.getElementsByTagName('BODY')[0].appendChild(this.caja);

  try{
    if(document.getElementById('ayuda_contextual').clientWidth>250){
      document.getElementById('ayuda_contextual').style.width="250px";
    }
  }catch(ex){}

  this.aparecer=function(){
    if (this.opacidad<85){
      this.opacidad+=this.aumento;
      if (this.opacidad<1){
        this.aumento+=0.05;
      }else{
        this.aumento+=1;
      }
      cambiarOpacidad(this.caja,this.opacidad);
    }else{
      this.opacidad=85;
      window.clearInterval(this.tempo);
    }
  }

  this.setTimeout('this.tempo=this.setInterval("this.aparecer()",this.frecuencia)',10);
  return false;
}

function destruirAyuda(e){
  try{
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    var reltg1=reltg;
    while (reltg1 != tg && reltg1.nodeName != 'BODY'){
      reltg1= reltg1.parentNode;
    }
    if (reltg1== tg) return;
    while (reltg != tg && tg.nodeName != 'BODY'){
      tg= tg.parentNode;
    }
    if (reltg== tg && tg.longdesc) return;
    document.getElementById('ayuda_contextual').parentNode.removeChild(document.getElementById('ayuda_contextual'));
  }catch(e){}
}

function moverAyuda(e){
  if (!e) var e = window.event;
  try{
    this.despl=calcularScroll();
    document.getElementById('ayuda_contextual').style.top=e.clientY+40+this.despl[1]+"px";//40=alto del puntero
    document.getElementById('ayuda_contextual').style.left=e.clientX+this.despl[0]+"px";
  }catch(e){}
}

generarAyuda.prototype=window;
anyadirEvento(window,"load",Ayuda);