/***** 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" *****/

// alias para document.getElementById
function $(id){
    return document.getElementById(id);
}

/****** Calcular cuanto se ha desplazado el scroll de la pantalla ******/

function calcularScroll(){
    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
    	x = self.pageXOffset;
    	y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    	// Explorer 6 Strict
    {
    	x = document.documentElement.scrollLeft;
    	y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
    	x = document.body.scrollLeft;
    	y = document.body.scrollTop;
    }
    
    resultado=new Array(x,y);
    
    return resultado;

}

/****** Comprueba si el navegador es Internet Explorer ******/
function esIE(){
    var agt=navigator.userAgent.toLowerCase();
    return ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
}

/****** Registra un evento sobre un objeto (multi-plataforma) ******/

function anyadirEvento(objeto,evento,funcion){
	try{
		objeto.addEventListener(evento,funcion,false);
		return true;
	}catch(e){
		try{
			objeto.attachEvent("on"+evento,funcion);
			return true;
		}catch(e){
			return false;
		}
	}
}

/***** Altera la opacidad de un elemento ******/

function cambiarOpacidad(objeto,op){    
	objeto.style.filter="alpha(opacity="+op+")";
	objeto.style.MozOpacity=op/100;
	objeto.style.opacity=op/100;
}

/********ventanaEmergente**********
texto_id = id del div que contiene el HTML a mostrar en la ventana
x,y posicion de la pantalla donde se muestra la ventana
minancho, minalto: dimensiones minimas de la ventana (se alarga para  que quepa el texto)
****************************************/
    function ventanaEmergente(texto_id,x,y,minancho,minalto,titulo){
        if (document.getElementById('emergente')){
            quitarDiv(document.getElementById('emergente'));
        }
        this.IE=esIE();
        this.minancho=minancho;
        this.minalto=minalto;
        this.sumaX=minancho/minalto*12;
        this.sumaY=12;
        this.ancho=0;
        this.alto=0;
        this.interv=null;
        this.desplegado=false;
        this.detalle=document.createElement("div");
        this.detalle.setAttribute("class","detalle");
        this.detalle.setAttribute("className","detalle");
        this.detalle.setAttribute("id","emergente");
        this.detalle.style.width="0px";
        this.detalle.style.minHeight="0px";
        if (IE){
            this.detalle.style.height="0px";
        }

        this.despl=calcularScroll();
        
        this.detalle.style.top=y+this.despl[1]+"px";
        this.detalle.style.left=x+this.despl[0]+"px";
        
        //creamos la barra del título;
        this.barratitulo=document.createElement("div");
        this.barratitulo.setAttribute("class","barradesc");
        this.barratitulo.setAttribute("className","barradesc");
        
        //creamos el aspa y la añadimos
        this.aspa=document.createElement("img");
        this.aspa.setAttribute("src","/images/iconos/aspa.gif");
        this.aspa.setAttribute("class","aspa");
        this.aspa.setAttribute("className","aspa");
        this.aspa.setAttribute("onclick","quitarDiv(this.parentNode.parentNode)");
        if (IE){
            this.aspa.onclick=quitarDiv;
        }
        
        this.barratitulo.appendChild(this.aspa);
        
        //creamos el titulo y lo añadimos al agrandar
        this.etiquetatitulo=document.createElement("span");
        this.etiquetatitulo.style.maxWidth=(minancho-50)+"px";
        this.titulov=document.createTextNode(document.getElementById(texto_id).title);

        
        this.barratitulo.appendChild(this.etiquetatitulo);
        
        

        
        //creamos el contenedor del texto
        this.contenido=document.createElement("div");
        this.contenido.setAttribute("class","contenidotexto desarrollo");
        this.contenido.setAttribute("className","contenidotexto desarrollo");
        this.contenido.style.width=(this.minancho-50)+'px';
        this.contenido.innerHTML=document.getElementById(texto_id).innerHTML;
        
        this.detalle.appendChild(this.barratitulo);
                
        this.id_texto=texto_id;
        document.getElementsByTagName('body')[0].appendChild(this.detalle);  
        this.interv=window.setInterval("agrandarDiv()",1);

        this.agrandarDiv=function(){
            if (this.ancho< this.minancho){
                this.detalle.style.width=this.ancho+"px";
                this.detalle.style.minHeight=this.alto+"px";
                if (IE){
                    this.detalle.style.height=this.alto+"px";
                }
                this.ancho+=sumaX;
                this.alto+=sumaY;
                this.detalle.style.top=y+this.despl[1]-this.alto+"px";
                this.detalle.style.left=x+this.despl[0]-this.ancho+"px";
            }else{
                clearInterval(this.interv);
                desplegado=true;
              //  detalle.innerHTML=document.getElementById(id_texto).innerHTML;
                      this.etiquetatitulo.appendChild(this.titulov);
                this.detalle.appendChild(this.contenido);
            }
        }
        
}        
    
function quitarDiv(detalle){
    if (IE && !detalle) detalle=this.parentNode.parentNode;
    if (detalle.parentNode){
          detalle.style.display='none';
          detalle.innerHTML='';
          detalle.parentNode.removeChild(detalle);
          desplegado=false;
          ancho=0;
          alto=0;
    }
}

//crea un objeto del tipo XMLHttpRequest según el navegador
function XML(){ //no quitar los comentarios condicionales
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}


//rellena un select con el contenido de una array con la estructura [texto: "eltexto", valor: "elvalor,....]
function rellenarSelect(obj,contenido){
 /*   tamanyo=obj.length; 
    for(i=0;i<tamanyo;i++){
        obj.options[0]=null;
    }
    */
    obj.innerHTML="";

    tamanyo=contenido.length;
    for(i=0;i<tamanyo;i++){               
        obj.options[i]=new Option(contenido[i].texto,contenido[i].valor);
    }
}


//selecciona la opción de un select que tenga como valor el parametro
function seleccionarSelect(obj,valor){
    tamanyo=obj.options.length;
    for(var i=0;i<tamanyo;i++){
          if(obj.options[i].value==valor){
            obj.options[i].selected=true;  
          } 
    }
}



function Ayuda(){	
	var etiquetas=new Array('A','IMG','DIV');
	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=="") tit=enlaces[k].alt;
			if (tit && enlaces[k].id!='resencuesta'){
				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){}
				contenido[k]=tit;
				//eval("anyadirEvento(enlaces[k],'mouseover',function(e){generarAyuda('"+tit.replace("'","´")+"',e)});");
				eval("anyadirEvento(enlaces[k],'mouseover',function(e){generarAyuda(contenido["+k+"],e)});");
				anyadirEvento(enlaces[k],'mouseout',function(e){destruirAyuda(e)});
				anyadirEvento(enlaces[k],'mousemove',function(e){if (!document.getElementById('ayuda_contextual')) generarAyuda(contenido["+k+"],e);moverAyuda(e)});
			}
		}
	}
    /*Parche para solucinar un espacion en blanco en IE*/	
 /*   generarAyuda("",{clientX:0,clientY:0});
    setTimeout(destruirAyuda,100);*/
    /******************************************/
}

function generarAyuda(titulo,e){
          if (titulo==undefined) return;
        
          var relTarg;
/*try{
	if (!e) var e = window.event;
	if (e.relatedTarget) relTarg = e.relatedTarget;
	else if (e.fromElement) relTarg = e.fromElement;
	var reltg=relTarg;
	while (reltg.nodeName != 'BODY'){
              	if(reltg.longdesc==titulo){ return;}
            	reltg= reltg.parentNode;
            }
}catch(ex){}*/

        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]+"px";
    this.caja.style.left=e.clientX+this.despl[0]+"px";
    this.frecuencia=30;
    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<95){	
	        this.opacidad+=this.aumento;
	           if (this.opacidad<1){
	                this.aumento+=0.05;
	           }else{
	        	  this.aumento+=1;
	           }
	        cambiarOpacidad(this.caja,this.opacidad);
	    }else{
	        this.opacidad=95;
	        window.clearInterval(this.tempo);
	    }
	}
	
	this.setTimeout('this.tempo=this.setInterval("this.aparecer()",this.frecuencia)',200);
	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;
            	
            //	if (reltg.nodeName=="IMG") return;

            	
            	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){
	try{
		this.despl=calcularScroll();  	
	    document.getElementById('ayuda_contextual').style.top=e.clientY+this.despl[1]+"px";
	    document.getElementById('ayuda_contextual').style.left=e.clientX+this.despl[0]+"px";
	}catch(e){}
}

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

