

////////////////////////////////////////////GENERAL TOOL FUNCTIONS/////////////////////////////////////////////////


//función q carga los canales de la station especificada
function load_channels(id_stat){
    
    div = 1;
    
    //actualizamos id station actual
    id_station = id_stat
    
    //alert("load_channels();");
    
    refresco = Math.round(Math.random()*10000);
    http.open("GET","./DB_management?refresco=" + refresco 
                                + "&operation=load_channels" 
                                + "&id_station=" + id_station, true); 
    http.onreadystatechange = handleHttpResponse; 
    http.send(null); 
    
}


//función q carga las playlists del canal especificado
function load_playlists(canal,id_ele,id_play,max_play){
    
    //alert("load_playlists");
    
    div = 1;
    
    //guardamos el numero de playlists del canal
    MAX_PLAYLISTS = max_play;
    
    //guardamos id de acceso directo
    if(id_ele != null) direct_element   = id_ele;
    if(id_play != null) direct_playlist = id_play;
    
    //actualizamos id canal actual
    current_channel=canal;
    
    refresco = Math.round(Math.random()*10000); //para burlar caché IE
    http.open("GET","./DB_management?refresco=" + refresco 
                                + "&operation=load_playlists"
                                + "&id_channel=" + current_channel, true); 
    http.onreadystatechange = handleHttpResponse; 
    http.send(null); 
        
}


//función q carga una pagina determinada de la playlist actual
function load_playlist(page,pos){
    
    //alert("load_playlist --> page: "+page+" pos: "+pos);
    
    div = 1;
    
    //reseteamos elemento activo de la playlist
    current = null;
    
    //tenemos varias playlists en el canal -> desmarcamos anterior playlist
    if(playlists_num > 1 && pos != current_playlist){ 
        
        //desmarcamos playlist anterior
        document.getElementById("playlist" + current_playlist).innerHTML = "<a href='javascript:load_playlist(0," + current_playlist + ");'><img border=0 src='http://media.directa.tv/images_web/Botons/botons_playlist/" + playlists_name[current_playlist] + "_inactivo.gif'></a>";
        
        //marcamos playlist actual
        document.getElementById("playlist" + pos).innerHTML = "<a href='javascript:load_playlist(0," + pos + ");'><img border=0 src='http://media.directa.tv/images_web/Botons/botons_playlist/" + playlists_name[pos] + "_activo.gif'></a>";
    }
    
    //guardamos datos playlist
    playlist_page    = page;
    current_playlist = pos;
    
    //reseteamos variables de playlist
    reset_playlist_vars();
    
    refresco = Math.round(Math.random()*10000); //para burlar caché IE
    http.open("GET","./DB_management?refresco=" + refresco 
                                + "&operation=load_playlist"
                                + "&id_playlist=" + playlists_id[pos]
                                + "&page=" + playlist_page
                                + "&length=" + MAX_ELEMENTS, true); 
    http.onreadystatechange = handleHttpResponse; 
    http.send(null); 
    
}

//funcion q gestiona el video
function manage_video(pos){
    
    //alert("Manage video --> "+pos+" num_elements: "+num_elements);
    
    //limpiamos capa player
    document.getElementById("player_t").innerHTML = "";
    
    if(pos >= num_elements){ //final de la playlist
        
        //volvemos al inicio de la playlist
        manage_video(0);
        
        /*playing_playlist++;
          
        if(playing_playlist < playlists_num){ //cambio de playlist
            
            playing_pos=0; 
            
            if(playing_playlist >= MAX_PLAYLISTS) playing_playlist=0;
            first_load=1;
            load_playlist(0,playing_playlist);
            //setTimeout("manage_video(0);",2000);
            
        } else{ //cambio de canal
            
            if(current_channel == 1) location.href="./musica.jsp";
            if(current_channel == 2) location.href="./zapping.jsp";
            if(current_channel == 3) location.href="./index.jsp"; 
            if(current_channel == 5) location.href="./anterior.jsp"; 
            
        } */
        
    }else{ //no final de la playlist
        
        //actualizamos playing playlist
        playing_playlist   = current_playlist;
        playing_pos        = pos;
        playing_element    = id_element[pos];
        playing_valoration = valorations[pos];
        playing_votes      = votes[pos];
        
        //actualizamos elemento seleccionado/anterior
        previous = current;
        current  = pos;
        
        //elemento ANTERIOR como inactivo
        if(previous != -1 && previous != null && previous <= num_elements){
            
            //color fondo de la tabla
            document.getElementById("playlist_table").rows[previous*3].style.backgroundColor     = "#3a3a3a";
            document.getElementById("playlist_table").rows[(previous*3)+1].style.backgroundColor = "#3a3a3a";
            
            //ponemos estrellas valoración inactivas
            document.getElementById("playlist_table").rows[(previous*3)+1].cells[0].innerHTML = "<img src='http://media.directa.tv/images_web/estrellas" + valorations[previous] + "inactivo.gif'>";
            
            //imagen link transparente
            document.getElementById("playlist_links").rows[previous].cells[0].innerHTML = "<a href='javascript:manage_video(" + previous + ");'><img border=0 src='http://media.directa.tv/images_web/transparent.gif'></a>";
        }
        
        //elemento ACTUAL como activo
        //ponemos estrellas valoración activas
        document.getElementById("playlist_table").rows[(pos*3)+1].cells[0].innerHTML = "<img src='http://media.directa.tv/images_web/estrellas" + valorations[pos] + "activo.gif'>";
        
        //marcamos casilla de la tabla como activa
        document.getElementById("playlist_table").rows[pos*3].style.backgroundColor     = "#000000";
        document.getElementById("playlist_table").rows[(pos*3)+1].style.backgroundColor = "#000000";
        
        //imagen link activa
        document.getElementById("playlist_links").rows[pos].cells[0].innerHTML="<a href='javascript:manage_video("+pos+");'><img border=0 src='http://media.directa.tv/images_web/activo.gif'></a>";

        //ponemos contexto de texto
        //lead
        if (current_channel != 2) 
            document.getElementById("titulo").innerHTML = titles[pos];
        else 
            document.getElementById("titulo").innerHTML = titles[pos].replace("<br>"," - "); //PARCHE canal música
        //description
        document.getElementById("texto").innerHTML = descriptions[pos];
        
        //mostramos PUBLICIDAD
        load_advertisements(pos);
        
        //cargamos video o preroll
        if(pre_path[pos] != "") 
            load_video(pos,"preroll"); 
        else 
            load_video(pos,"video"); 
    }
}


//función q carga un vídeo en el player
function load_video(pos,type){
    
    //alert("load video --> pos: "+pos+" type: "+type);
    
    //////////////////////////PARCHE//////////////////////////// --> cuando cambiamos de playlist entre el preroll y el video da error sin esto
    if(pos != null){
    ////////////////////////////////////////////////////////////
        var url      = "";
        var duration = "";

        if(type == "preroll"){

            url = pre_path[pos];
            duration = pre_duration[pos];

            //llamada a google analythics
            urchinTracker("/preroll --> "+url);

            currentDuration = ((duration.split(":")[0]*60) + (duration.split(":")[1]*1));
            currentType = "preroll";
        }

        if(type == "video"){

            url = video_path[pos];
            duration = durations[pos];

            //llamada a google analythics
            urchinTracker("/video --> " + url);

            playing_video_pos = pos+1;
            playing_video_playlist = playlists_id[current_playlist];
            playing_video_page = playlist_page;

            currentDuration = ((duration.split(":")[0]*60) + (duration.split(":")[1]*1));
            currentType = "video";

        }

        //tipo de player (propio o youtube)
        var player = "propio";
        if(url.split("youtube.com").length > 1) 
            player = "youtube";

        //YOUTUBE
        if(player == "youtube"){

            //programamos timer
            clearTimeout(timer);
            var time = ((duration.split(":")[0]*60) + (duration.split(":")[1]*1))*1000 + (60000*1); 
            timer = setTimeout("manage_video("+(pos+1)+");",time);

            document.getElementById("player_t").innerHTML = "<td><object><embed src='" + url + "&autoplay=1' menu='true' type='application/x-shockwave-flash' wmode='transparent' width='366' height='290'></embed></object><br><img src='http://media.directa.tv/images_web/reflejo_youtube.gif'></td>";  
        }

        //PROPIO
        if(player == "propio"){
        
            //alert(url);    
            
            var s1 = new SWFObject("./player/flvplayer.swf","mpl","360","310","7");
            s1.addParam("allowfullscreen","true");
            s1.addVariable("usefullscreen","true");
            s1.addVariable("file",url);
            s1.addVariable("enablejs","true");
            s1.addVariable("logo","./player/mosca_directatv.png");
            if(type == "preroll"){
                s1.addVariable("usecaptions","true");
                s1.addVariable("captions","./player/subtitles_example.xml");
            }
            s1.addVariable("width","360");
            s1.addVariable("height","310");
            s1.addVariable("overstretch","fit");
            s1.addVariable("autostart","true");
            s1.addVariable("backcolor","0x222222");
            s1.addVariable("frontcolor","0xf86200");
            s1.write("player_t"); 

            //var flashvars="file="+url+"&usefullscreen=false&autostart=true&overstretch=fit&backcolor=0x222222&frontcolor=0xf86200&logo=./player/mosca_directatv.png&javascriptid=mpl&enablejs=true&bufferlength=20";
            //if(type == "preroll") flashvars+="&usecaptions=true&captions=./player/subtitles_example.xml";
            //document.getElementById("player_t").innerHTML="<embed src='./player/flvplayer.swf' width='360' height='310' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='"+flashvars+"' />";

        }
    }
}

//función q busca el elemento en la playlist
function search_element(page,pos){
    
    //alert("search_element -> page: "+page+" \n pos: "+pos);
    
    direct_position = pos;
    direct_page     = page;
    
    load_playlist(page,current_playlist);
}

//función q carga la publicidad en los baners
function load_advertisements(pos){
    
    //alert(publi_element[pos]);

    //el vídeo tiene publicidad asociada
    if(publi_element[pos] != null && publi_element[pos] != "null"){

        //alert("Canal: " + current_channel + "\nPlaylist: " + playlists_id[current_playlist] + "\nPublicidad: " + publi_element[current]);
        
        // OPCION 1 
        document.getElementById("jumbo").src = Set_Banner(1,current_channel,playlists_id[current_playlist],publi_element[current]);
        document.getElementById("info_publicidad1").src = Set_Banner(2,current_channel,playlists_id[current_playlist],publi_element[current]);
        document.getElementById("info_publicidad2").src = Set_Banner(3,current_channel,playlists_id[current_playlist],publi_element[current]);
        
        /* OPCION 2 
        var url_jumbo = Set_Banner(1,current_channel,playlists_id[current_playlist],publi_element[current]);
        var url_info1 = Set_Banner(2,current_channel,playlists_id[current_playlist],publi_element[current]);
        var url_info2 = Set_Banner(3,current_channel,playlists_id[current_playlist],publi_element[current]);
        frames[0].window.location.replace(url_jumbo);
        frames[1].window.location.replace(url_info1);
        frames[2].window.location.replace(url_info2);*/
        
        /* OPCION 3
        var url_jumbo = Set_Banner(1,current_channel,playlists_id[current_playlist],publi_element[current]);
        var url_info1 = Set_Banner(2,current_channel,playlists_id[current_playlist],publi_element[current]);
        var url_info2 = Set_Banner(3,current_channel,playlists_id[current_playlist],publi_element[current]);
        frames[0].window.location.replace("./pop-ups/publicidad.jsp?url_publi="+url_jumbo);
        frames[1].window.location.replace("./pop-ups/publicidad.jsp?url_publi="+url_info1);
        frames[2].window.location.replace("./pop-ups/publicidad.jsp?url_publi="+url_info2); */
        
        /*frames[0].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/position1tv.js");
        
        if(current_channel != 1) //publicidad temporal --> cambiar tb tus_videos.jsp
            frames[1].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/top1tv.js");
        
        frames[2].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/top2tv.js");
        
        //frames[0].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://publi.grupocorreo.es/RealMedia/ads/adstream_jx.ads/www.diariodenavarra.es/tvinternet/sanfermin/"+publi_element[pos]+"@Position1?");
        //frames[1].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://publi.grupocorreo.es/RealMedia/ads/adstream_jx.ads/www.diariodenavarra.es/tvinternet/sanfermin/"+publi_element[pos]+"@Top1?");
        //frames[2].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://publi.grupocorreo.es/RealMedia/ads/adstream_jx.ads/www.diariodenavarra.es/tvinternet/sanfermin/"+publi_element[pos]+"@Top2?");
         */
        
    //el vídeo no tiene publicidad asociada, ponemos por defecto    
    }else{
        
        /*
        frames[0].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/position1tv.js");
        
        if(current_channel != 1) //publicidad temporal
            frames[1].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/top1tv.js");
        
        frames[2].window.location.replace("./pop-ups/publicidad.jsp?url_publi=http://www.diariodenavarra.es/cmxxi/top2tv.js");
        */
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
