
///////////////////////////////////////////FUNCIONES AUXILIARES//////////////////////////////////////////////

//función q limita los carácteres introducidos en un text area y decrementa el contador 
function calcLong(txt,dst,id,maximo){

        //alert("Origen: "+txt+" Destino: "+dst+" Id: "+id+ " Maximo: "+maximo);

        var largo = document.getElementById(id).value.length;
        
       // alert("Largo: "+largo+" \n Maximo: "+maximo);
        
        if (largo > maximo){

                document.getElementById(id).value = document.getElementById(id).value.substring(0,maximo)

        }else{

                document.getElementById(dst).innerHTML ="("+(maximo-largo)+" m&aacute;x)";
        }
    }


//función q espera q se carguen las imagenes de la página para cargar el video
function wait_images(){

    var cargadas=true;
	
    for (i=0;i<document.images.length-1;i++){ if(document.images[i].complete == false){  cargadas=false; } }
    
    if(cargadas == true || load_images == 5){ //imagenes ya cargadas
        
        //alert("Playing playlist: "+playing_playlist+" current playlist:"+current_playlist[0]+" playing_pos: "+playing_pos);
        
        //hemos vuelto a la playlist activa, marcamos video actual
        if(playing_playlist == current_playlist){
            
            //actualizamos elemento activo
            current=playing_pos;
            
            //fondo negro
            document.getElementById("playlist_table").rows[playing_pos*3].style.backgroundColor="#000000";
            document.getElementById("playlist_table").rows[(playing_pos*3)+1].style.backgroundColor="#000000";
            
            //ponemos imagen estrellas activa
            document.getElementById("playlist_table").rows[(playing_pos*3)+1].cells[0].innerHTML="<img src='http://media.directa.tv/images_web/estrellas"+valorations[playing_pos]+"activo.gif'>";
            
            //imagen link activa
            document.getElementById("playlist_links").rows[playing_pos].cells[0].innerHTML="<a href='javascript:manage_video("+playing_pos+");'><img border=0 src='http://media.directa.tv/images_web/activo.gif'></a>";
            
        } 
        
        //primera carga del canal
        if(first_load == 1){ 
            
            first_load=0; 
            
            //actualizamos posicion
            var pos=0;
            if(direct_position != null) pos=direct_position-1; 
            
            //activamos links 
            document.getElementById("link_envia").href="javascript:enviar_amigo();";
            document.getElementById("link_valoracion").href="javascript:valoracion();";
            document.getElementById("link_comentarios").href="javascript:ver_comentarios(\"video\");";
            document.getElementById("link_destacado").href=link_destacado;
            document.getElementById("link_mini").href=link_mini;
            
            //cargamos vídeo
            manage_video(pos); 
        }
        
    }else{ load_images++; setTimeout("wait_images();",500); } //esperamos imagenes
    
}


//función q aplica la transparencia a las imagenes PNG
function correctPNG(channel){
    
    //guardamos canal en el q estamos
    current_channel=channel;
    
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)){
      
       for(var i=0; i<document.images.length; i++){
           
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
             
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

//función que codifica el texto
function encode_text(text){
	
        //eliminamos caracteres terminacion cadena ('," --> ´) 
	while (text.split("\'").length > 1) text=text.replace("\'","^^");
        while (text.split("\"").length > 1) text=text.replace("\"","^^^^");
	
        //encoding nativo de javascript
        text=escape(text);
        
        //encoding "casero"
       // while(text.split("%3F").length > 1) text=text.replace("%3F","^ ^"); //´ -> ^ ^
        while(text.split("%A1").length > 1) text=text.replace("%A1","^!^"); //¡ -> -!-
        while(text.split("%BF").length > 1) text=text.replace("%BF","^?^"); //¿ -> -?-
        while(text.split("%C0").length > 1) text=text.replace("%C0","*A*"); //À -> *A*
        while(text.split("%C8").length > 1) text=text.replace("%C8","*E*"); //È -> *E*
        while(text.split("%D2").length > 1) text=text.replace("%D2","*O*"); //Ò -> *O*
        while(text.split("%C1").length > 1) text=text.replace("%C1","^A^"); //Á -> -A-
        while(text.split("%C9").length > 1) text=text.replace("%C9","^E^"); //É -> -E-
        while(text.split("%CD").length > 1) text=text.replace("%CD","^I^"); //Í -> -I-
        while(text.split("%D3").length > 1) text=text.replace("%D3","^O^"); //Ó -> -O-
        while(text.split("%DA").length > 1) text=text.replace("%DA","^U^"); //Ú -> -U-
        while(text.split("%E0").length > 1) text=text.replace("%E0","*a*"); //à -> *a*
        while(text.split("%E8").length > 1) text=text.replace("%E8","*e*"); //è -> *e*
        while(text.split("%F2").length > 1) text=text.replace("%F2","*o*"); //ò -> *o*
        while(text.split("%E1").length > 1) text=text.replace("%E1","^a^"); //á -> -a-
        while(text.split("%E9").length > 1) text=text.replace("%E9","^e^"); //é -> -e-
        while(text.split("%ED").length > 1) text=text.replace("%ED","^i^"); //í -> -i-
        while(text.split("%F3").length > 1) text=text.replace("%F3","^o^"); //ó -> -o-
        while(text.split("%FA").length > 1) text=text.replace("%FA","^u^"); //ú -> -u-
        while(text.split("%F1").length > 1) text=text.replace("%F1","^n^"); //ñ -> -n-
        while(text.split("%E7").length > 1) text=text.replace("%E7","^c^"); //ç -> -c-
        while(text.split("%0D").length > 1) text=text.replace("%0D"," "); //saltos de linea
	while(text.split("%0A").length > 1) text=text.replace("%0A"," ");
        while(text.split("%20").length > 1) text=text.replace("%20"," "); //espacios en blanco
        while(text.split("%60").length > 1) text=text.replace("%60","* *"); //` -> * * 
        while(text.split("%B4").length > 1) text=text.replace("%B4","^^"); //´ -> ^ ^
        
        return text;
        
}

//////////////////////////////////////////////////////////////////////////////////////////////////
