/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Quick Ajax playlist fetcher by Steinhaug AS for Radio Ung */
/* Web: http://www.steinhaug.no/                             */
/*                                                           */
/* Modify the value seconds below, for how often the system  */
/* should refresh.                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var steinhaugAjaxPlaylist = {
  seconds: 10,
  single : false,
  all    : false,
  cache_single : '',
  cache_all : '',
  getsingle : function() {
    this.fetch($('steinhaug-playlist-one'),1,'single');
  },
  getall : function() {
    this.fetch($('steinhaug-playlist-all'),5,'all');
  },
  fetch : function (el){
    var limit = arguments[1] ? arguments[1] : 1;
    var mode = arguments[2] ? arguments[2] : '';
    var currentTime = new Date();
    var opt = {
      method: 'post',
      postBody: 'limit=' + limit,
      onSuccess: function(t) {
        if(mode=='single') 
          var cache = steinhaugAjaxPlaylist.cache_single;
          else 
          var cache = steinhaugAjaxPlaylist.cache_all;
        if(cache != t.responseText){
              new Effect.Fade(el, {
                  duration: 0.5,
                  from: 1,
                  to:0.1,
                  afterFinish: function(){
                    el.innerHTML = t.responseText;
                      new Effect.Appear(el, {
                        duration: 0.5,
                        from: 0.1,
                        to: 1
                      });
                  }
              });
         }
         if(mode=='single')
           steinhaugAjaxPlaylist.cache_single = t.responseText;
           else 
           steinhaugAjaxPlaylist.cache_all = t.responseText;
         if(mode=='single')
           this.single = setTimeout('steinhaugAjaxPlaylist.getsingle()',steinhaugAjaxPlaylist.seconds * 1000);
           else
           this.all = setTimeout('steinhaugAjaxPlaylist.getall()',steinhaugAjaxPlaylist.seconds * 1000);
      },
      on404: function(t) {
          alert('Error 404: location "' + t.statusText + '" was not found.');
      },
      onFailure: function(t) {
          alert('Error ' + t.status + ' -- ' + t.statusText);
      }
    }
    new Ajax.Request('steinhaug.playlist.ajax.php', opt);
  },
  display : function (el){
    el.innerHTML = arguments[1];
  }
}
function SteinhaugEnablePlaylist(){
  if($('steinhaug-playlist-one'))
    steinhaugAjaxPlaylist.getsingle()
  if($('steinhaug-playlist-all'))
    steinhaugAjaxPlaylist.getall()
}
ELO.functionsToCallOnload.push("SteinhaugEnablePlaylist()");

