function tip_of_the_day() {
  this.tip = '';
  this.seen = new Array();
  this.last_id = 0;
  this.d = new Date();
  this.cnt_all = -1;

  this.do_init = function() {
    this.dt = $('#div_totd');
    this.dl = $('#div_totd_link');
    this.dc = $('#div_totd_container');
    this.db = $('#div_totd_bottom');
    this.du = $('#div_totd_undo');
    this.init = true;
  }
  
  this.toggle = function(val) {
    !this.init ? this.do_init() : 0;
    this.dc.toggle();
    this.db.toggle();
    var obj_w = $('#welcome_links');
    val != -1 && obj_w.is(':visible') ? show_new_stuff(-1) : 0;
    if (this.dc.is(':visible')) {
      this.dl.addClass('btn_bg_on');
      this.dl.removeClass('btn_bg');
    } else {
      this.dl.addClass('btn_bg');
      this.dl.removeClass('btn_bg_on');
    }
  }

  this.next = function(l_id) {
    if (this.b_next)
      return;
    this.b_next = true;
    !this.init ? this.do_init() : 0;
    if (!this.in_array(l_id, this.seen)) {
      this.seen.push(l_id);
      this.last_id = l_id;
    }
    if (this.cnt_all == this.seen.length) {
      this.seen = new Array();
      this.dc.html('Dies war der letzte Tipp. Wenn wir wieder einen neuen Tipp für Sie haben, wird sich der Schriftzug "Wussten Sie schon ...?" grün einfärben.');
      this.b_next = false;
      return;
    }
    this.show_overlay();
    $.post(
      "/include/totd.ajax.php",
      {
        arr_seen: this.seen.join(','),
        last_id: this.last_id,
        t: this.d.getTime()
      },
      function(data) {
        totd.show(data);
      }
    );
  }

  this.show = function(data) {
    window.clearTimeout(this.timeout);
    !this.init ? this.do_init() : 0;
    var l_id = data.match(/^[0-9]+/);
    !this.in_array(l_id, this.seen) ? this.seen.push(l_id) : 0;
    this.last_id = l_id;
    data = data.replace(/^[0-9]+:/, '');
    this.cnt_all = data.match(/^[0-9]+/);
    data = data.replace(/^[0-9]+:/, '');
    this.dc.html(data);
    this.remove_overlay();
  }
  
  this.show_overlay = function() {
    this.timeout = setTimeout('totd.ajax_timeout()', 7000);
    this.tip = this.dc.html();
    this.dc.css('background-color', '#ccc');
    this.dc.fadeTo(1, 0.33);
    $('#div_totd').append('<div id="div_totd_overlay"><img src="/bilder/ajax/ajax-loader.gif" /></div>');
    var pos = this.dc.position();
    this.overlay = $('#div_totd_overlay');
    this.overlay.css('left', pos.left + (parseInt(this.dc.outerWidth()) / 2) - (parseInt(this.overlay.outerWidth()) / 2) + 'px');
    this.overlay.css('top', pos.top + (parseInt(this.dc.outerHeight()) / 2) - (parseInt(this.overlay.outerHeight()) / 2) + 'px');
  }
  
  this.remove_overlay = function() {
    this.b_next = false;
    this.dc.css('background-color', '');
    this.dc.fadeTo(1, 1, function() {
      this.style.removeAttribute("filter");
    });
    this.overlay.remove();
  }

  this.ajax_timeout = function() {
    !this.init ? this.do_init() : 0;
    s_html = !this.do_hide ? 'Es ist ein Problem bei der Anzeige des Tipps aufgetreten - bitte versuchen Sie es später noch einmal.' : 'Es ist ein Problem bei der Deaktivierung aufgetreten - bitte versuchen Sie es später noch einmal.';
    this.dc.html(s_html)
    this.do_hide = false;
    this.remove_overlay();
  }
  
  this.in_array = function(val, arr) {
    var retval = false;
    if (typeof arr == 'object') {
      for (var i in arr) {
        if (arr[i] == val) {
          retval = true;
          break;
        }
      }
    }
    return retval;
  }
  
  this.hide = function() {
    this.do_hide = true;
    this.show_overlay();
    $.post(
      "/include/totd.ajax.php",
      {
        key: 'hide_totd',
        val: 1,
        t: this.d.getTime()
      },
      function(data) {
        window.clearTimeout(totd.timeout);
        var arr_data = data.split(':');
        if (arr_data[2] != '0') {
          totd.dt.hide();
          totd.du.show();
        } else {
          totd.dc.html('Bei der Verarbeitung der Daten trat ein Fehler auf - bitte versuchen Sie es später noch einmal.');
          totd.dt.show();
          totd.du.hide();
        }
        totd.remove_overlay();
      }
    );
  }

  this.undo = function() {
    this.do_hide = true;
    this.show_overlay();
    $.post(
      "/include/totd.ajax.php",
      {
        key: 'hide_totd',
        val: 0,
        t: this.d.getTime()
      },
      function(data) {
        window.clearTimeout(totd.timeout);
        var arr_data = data.split(':');
        arr_data[2] == '0' ? totd.dc.html('Bei der Verarbeitung der Daten trat ein Fehler auf - bitte versuchen Sie es später noch einmal.') : 0;
        totd.dt.show();
        totd.du.hide();
        totd.remove_overlay();
      }
    );
  }
}

var totd = new tip_of_the_day();