/* Mainscript pro načtení aktualní písničky podle ID */

function GetActualSong() {
         var self = this;
         var interval = false;
         var casovac;
         var pisnicka;
         var conf; 
         var json;
         var style;
         
         this.load = function() {
              // vymaže časovač
              clearInterval(casovac);
              // vytvoří dynamický script
              json = new ScriptRequest('http://my.abradio.cz/external/aktualni_sklatba.php?id_radia='+conf.uid+'&src_type=jsc&encode='+conf.enc+'&hash='+self.uniqid());     
              json.buildScriptTag();
              json.addScriptTag();
              
              return true;
         }
         
         this.prepare = function() {
              // první internace
              if(!interval) {
                  this.load();
                  interval = true;
                  //loopback
                  this.prepare();
              }
              // spustí load každýh 10 vteřin 
              casovac = setInterval(this.load,10000);
         } 
                  
         this.init = function(options,callback) {
              // init
              conf = options;
              style = callback;
              window.onload = function() {
                     self.prepare();
                     return true;
              }
              return false;      
         }
         
         this.Data = function(data) {
              json.removeScriptTag();
              pisnicka = style(data);

              document.getElementById(conf.obj).innerHTML = pisnicka;
         }
         
         this.uniqid = function() {
              var newDate = new Date;
           return newDate.getTime();
         }
}

/* template default */
function StyleDefault(data) {
    this.txt  = '<strong>';
    if(data['actual']['odkaz']) {
       this.txt += '<a href="'+data['actual']['odkaz']+'">'+data['actual']['interpret']+'</a>';
    } else {
       this.txt += data['actual']['interpret'];
    }
    this.txt += '</strong><span> - '+data['actual']['skladba']+'</span><br />';
    //this.txt += '<strong>'+data['radio']['jmeno']+'</strong>';

    return this.txt;
}

/* Vytváří a uzavírá SCRIPT se zdrojem informací */
function ScriptRequest(fullUrl) {

    var fullUrl = fullUrl; 
    var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    var headLoc = document.getElementsByTagName("head").item(0);
    var scriptObj;
    
    this.buildScriptTag = function () {
         scriptObj = document.createElement("script");
         scriptObj.setAttribute("type", "text/javascript");
         scriptObj.setAttribute("charset", "utf-8");
         scriptObj.setAttribute("src", fullUrl + noCacheIE);
         return this.scriptObj;
    }
    this.removeScriptTag = function () {
         headLoc.removeChild(scriptObj);  
    }
    this.addScriptTag = function () {
         headLoc.appendChild(scriptObj);
    }
}