var ticker = {
  debug : true,
  spielBeendet : false,
  timeout : null,
  urlPrefix : "",
  
  init : function(urlPrefix) {
    this.urlPrefix = urlPrefix;

    // Ticker stoppen, wenn Facebox geschlossen wird
    var $ = jQuery;
    var me = this;
    $(document).bind('close.facebox', function() {
      window.clearTimeout(me.timeout);
      
      if (me.debug && 'undefined' != typeof console) {
        console.log('Ticker gestoppt.');
      };
    });
    
    this.run();
    
    $("#ladestatus").click(function() {
      $(this).stop({ clearQueue: true });
      me.ladestatus('aktiv');
      me.ladestatus('warte');
    });
    
    $("#ticker_scroll_up").click(function() {
      $('#ticker_nachrichten_bereich').scrollTo('-=50px', 300);
    });
    $("#ticker_scroll_down").click(function() {
      $('#ticker_nachrichten_bereich').scrollTo('+=50px', 300);
    });
  },
  
  ladestatus : function(typ, stopQueue) {
    var ladestatus = $("#ladestatus");
    var ladestatusText = $("#ladestatus_text");
    
    if ('aktiv' == typ) {
      ladestatusText.html("Daten werden geladen&hellip;");
      ladestatus
        .html('<img src="' + this.urlPrefix + 'ladestatus-loop.gif" alt="Daten werden geladen&hellip;" />')
        .css({ width: '298px' })
        .animate({ width: '298px' }, 2500);
    } else if ('warte' == typ) {
      var me = this;
      $('#ladestatus')
        .queue(function() {
          ladestatusText.html("Status bis zur n&auml;chsten Aktualisierung");
          $('#ladestatus')
            .css({ width: '0px' })
            .html('<img src="' + me.urlPrefix + 'ladestatus.gif" alt="Status bis zur n&auml;chsten Aktualisierung" />')
            .animate({ width: '298px' }, 27500, 'linear', function() { me.ladestatus('aktiv', false); me.ladestatus('warte'); });
          $(this).dequeue();
        });
    } else {
      $('#ladestatus').html('');
      $('#ladestatus_text').html('Das Spiel ist beendet.');
    }
  },
  
  formatTime : function(time) {
    if (null == time) return '';
    var mins = Math.floor(time / 60);
    var secs = time % 60;
    return mins + "&prime; " + (secs < 10 ? "0" : "") + secs + "&Prime;";
  },
  
  stop : function() {
    if (this.timeout != null) {
      window.clearTimeout(this.timeout);
    };
  },
  
  run : function() {
    var d = new Date();
    var utc = Date.UTC(
      d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
      d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()
    );
    
    var me = this;
    $("#ladestatus").stop({ clearQueue: true });
    me.ladestatus('aktiv');
    jQuery.getJSON(this.urlPrefix + 'ticker-json.js?i=' + utc, function(data) {
      var nachrichten = $("<ul></ul>");
      $.each(data.nachrichts, function(i, item) {
        var zeit = me.formatTime(item.zeit);
        nachrichten.append($('<li class="ticker_nachricht_' + item.typ + '">' + '<span class="zeit">' + zeit + ' </span>' + item.nachricht + '</li>'));
      });
      $('#ticker_nachrichten').html(nachrichten);
      
      if (data.spielBeendet) {
        $('#ticker_spielstand_text').html('Ergebnis');
        me.spielBeendet = true;
        me.ladestatus('ende');
      } else {
        me.ladestatus('warte');
      }
      
      var spielstandAktuell = $('#ticker_spielstand');
      spielstandAktuell.html(data.stand);

      $('#ticker_pointstreak_url').attr('href', data.pointstreakUrl);
    });

    if (!this.spielBeendet) {
      var waitForNextRun = Math.round(Math.random() * 60000);
      if (this.debug && 'undefined' != typeof console) console.log('Warten bis zum nächsten Request: ' + waitForNextRun + ' ms'); 
      if (this.timeout) window.clearTimeout(this.timeout);
      this.timeout = window.setTimeout(function() { me.run(); }, waitForNextRun);
    };
  }
};