
window.onload = function(){                        
  radioUpdate();

  function radioUpdate(){                          
  d3.json(radio_server +"/status-json.xsl")
    .timeout(10000)
    .get(function(e,d){                      
      if(e !=undefined){
        console.warn("sin conxion"); 
        offline();}
      else{
        if(d.icestats.source!=undefined){           
          if(d.icestats.source.listenurl == webm_listen_url){
          console.log("transmitiendo video"); 
          onlinev(d); } 
          else if (d.icestats.source.listenurl == radio_listen_url){
          console.log("transmitiendo radio"); 
          onliner(d); } 
          else {
          console.warn("otro canal");
          offline(); }}                                 
        else {
          console.warn("fuera de línea"); 
          offline();
          }
  } }); };
  

  function dataUpdate(){                          
  d3.json(radio_server+"/status-json.xsl")
    .timeout(10000)
    .get(function(e,d){                      
      if(e !=undefined){ console.warn("sin datos radio"); }
      else{
        if(d.icestats.source!=undefined){           
          if(d.icestats.source.listenurl == listen_url ){
          d3.select("#now").text(d.icestats.source.title); 
      }}}

    d3.select("#ra").transition().on("end",dataUpdate).delay(60000);
}); 
};
  

  function offline(){
    d3.selectAll("article.broadcast").style("display","none")
    d3.select("article.broadcast").transition().on("end",radioUpdate).delay(90000);
  };

  function onlinev(d){
    d3.selectAll("article.broadcast")
      .style("display","block")
      .on("click",function(){window.location="bcast";});
    dataUpdate();
}
  function onliner(d){
    d3.selectAll(".broadcast")
      .style("display","block")
      .on("click",function(){window.location="radio";});
    dataUpdate();
}


}
