/**
 * Countdown.
 *
 * @author Peter Suschlik
 * @copyright neopoly GmbH
 *
 * Usage:
 * var start = new Date(2006, 5, 9, 18, 0, 0);
 * new Countdown("cd_").start(start.getTime());
 * @modified Rolf Frankenberg
 */

/*--------------------------------------------------------------------------*/
var Prototype = {
  Version: '1.2.0'
}
var Class = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}

var Abstract = new Object();
Object.prototype.extend = function(object) {
  for (property in object) {
    this[property] = object[property];
  }
  return this;
}

Function.prototype.bind = function(object) {
  var method = this;
  return function() {
    method.apply(object, arguments);
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var method = this;
  return function(event) {
    method.call(object, event || window.event);
  }
}

Number.prototype.toColorPart = function() {
  var digits = this.toString(16);
  if (this < 16) return '0' + digits;
  return digits;
}

var Try = {
  these: function() {
    var returnValue;
    
    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }
    return returnValue;
  }
}
/*--------------------------------------------------------------------------*/
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1) 
      return element;
    elements.push(element);
  }
  return elements;
}
/*--------------------------------------------------------------------------*/
var Countdown = Class.create();
/*--------------------------------------------------------------------------*/
Countdown.prototype = {
    initialize: function(element_prefix) {
        this.element_prefix = element_prefix;
        this.repeat = 990, // TWEAKME
        this.interval = null;
        this.count = 0;
    },
    start: function(to, now) {
        this.to = to;
        this.now = now || new Date().getTime();
        this.tick();
        this.interval = window.setInterval(this.tick.bind(this), this.repeat);
    },
    stop: function() {
        if (this.interval != null) {
            window.clearInterval(this.interval);
            this.interval = null;
        }
    },
    tick: function() {
        var now = this.now + (this.count++ * this.repeat);
        var diff = Math.floor((this.to - now) / 1000);

        if (diff <= 0)
        {
            this.stop();
            if (diff < 0) return;
        }
        var seconds = diff % 60;
        diff = Math.floor(diff / 60);
        var minutes = diff % 60;
        diff = Math.floor(diff / 60);
        var hours =   diff % 24;
        diff = Math.floor(diff / 24);

        this.change("days", diff + (diff == 1 ? " Tag" : " Tage"));
        this.change("hours", hours + (hours == 1 ? " Std." : " Std."));
        this.change("minutes", minutes + " Min.");
        this.change("seconds", seconds + " Sek.");
    },
    change: function(name, value) {
        var element = $(this.element_prefix + name);
        if (element != null) {
            var node = element.firstChild;
            node.data = value;
        }
    }
}

