function Countdown(time, element, id, index) {
  this.time = time;
  this.element = element;
  this.element.object = this;
  this.id = id;
  this.index = index;
  this.logo = null;
  this.lastSecond = -1;  
  this.alertShown = true;
  this.alertSound = null;
  this.zeroColor = "orange"; //"orange";//"#E0A060";
  this.beforeColor = "#E0E0E0";
  this.afterColor = "orange";
  var _self = this;
  
  this.updateHeader = function() {
    var title = this.time.title[this.index];
    if (this.time.icon && this.time.icon[this.index]) {
      title = "<img src='" + this.time.icon[this.index] + "' alt='icon' width='16' height='16'/> " + title;
    }
    if (title) {
      $("Header" + this.id).innerHTML = title;
      $("Header" + this.id).title = this.time.title[this.index];
    }  
    $("InnerContent" + this.id).title = this.time.type[this.index];
    if (this.time.getTime(this.index)) {
      $("CountdownText" + this.id).title = this.time.getTime(this.index).toLocaleString();
    }
  }
  
  this.updateHeader();    

  this.time.onload.push(function() {
    _self.updateHeader();
    if (_self.onload) {
      _self.onload();
    }
  });

  this.time.onerror.push(function() {
    $("Header" + _self.id).innerHTML = "Error";
    $("Header" + _self.id).title = "Error - Can't load event.";
    $("InnerContent" + _self.id).title = null;
    if (_self.onerror) {
      _self.onerror();
    }
  })

  this.daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  if (new Date().getYear() % 4 == 0) {
    this.daysInMonth[1] = 29;
  }
  this.daysAfterMonth = new Array(12);
  
  var count = 0;
  for (var i = 0; i < 12; i++) {
  	this.daysAfterMonth[i] = count;
  	count += this.daysInMonth[i];
  }
  
  this.update = function(now) {
    var target = this.time.getTime(this.index);
    if (target == null) {
      return;
    }
    if (this.lastSecond == now.getTime() / 1000) {
      return;
    }
    if (target <= now && !this.alertShown) {
      this.showAlert();
      return;
    }
    if (target > now) {
      this.alertShown = false;
    }  
    this.lastSecond = now.getTime() / 1000;
    
    var diff = target.getTime() - now.getTime();
    var days = parseInt(diff / 86400000);
    diff = diff - days * 86400000;
    $("Days" + this.id).innerHTML = days;
    var hours = parseInt(diff / 3600000);
    diff = diff - hours * 3600000;
    $("Hours" + this.id).innerHTML = this.formatNumber(hours);
    var minutes = parseInt(diff / 60000);
    diff = diff - minutes * 60000;
    $("Minutes" + this.id).innerHTML = this.formatNumber(minutes);
    var seconds = parseInt(diff / 1000);
    $("Seconds" + this.id).innerHTML = this.formatNumber(seconds);

    if (diff < 0) {
      $("Days" + this.id).style.color = this.afterColor;
      $("Hours" + this.id).style.color = this.afterColor;
      $("Minutes" + this.id).style.color = this.afterColor;
      $("Seconds" + this.id).style.color = this.afterColor;
    }
    else {      
      $("Days" + this.id).style.color = this.beforeColor;
      $("Hours" + this.id).style.color = this.beforeColor;
      $("Minutes" + this.id).style.color = this.beforeColor;
      $("Seconds" + this.id).style.color = this.beforeColor;
      if (days == 0) {
        $("Days" + this.id).style.color = this.zeroColor;
        if (hours == 0) {
          $("Hours" + this.id).style.color = this.zeroColor;
          if (minutes == 0) {
            $("Minutes" + this.id).style.color = this.zeroColor;            
          }          
        }        
      }
    }
  }
  
  this.formatNumber = function(number) {
    if (Math.abs(number) < 10) {
      var isNegativ = number < 0;
      number = "0" + Math.abs(number);
      if (isNegativ) {
        number = "-" + number;
      }
    }
    return number;  
  }
  
  this.getDayInYear = function(date) {
    return this.daysAfterMonth[date.getMonth()] + date.getDate();
  }
  
  this.setLogo = function(url) {
    var _self = this;
    var img = new Image();
    img.onerror = function() {
      $("Header" + _self.id).style.left = "0px";
      $("Header" + _self.id).style.width = "280px";
      $("CountdownText" + _self.id).style.left = "0px";
      $("CountdownText" + _self.id).style.width = "280px";
      $("Logo" + _self.id).style.display = "none";
    }
    img.onload = function() {
      $("Header" + _self.id).style.left = null;
      $("Header" + _self.id).style.width = null;
      $("CountdownText" + _self.id).style.left = null;
      $("CountdownText" + _self.id).style.width = null;
      $("Logo" + _self.id).style.display = "block";
      $("Logo" + _self.id).src = img.src;
    }
    img.src = url;
    this.logo = url;
  }
  
  this.setTitle = function(title) {
    this.time.setTitle(title);
    this.updateHeader();
  }
  
  this.showAlert = function() {
    if (!this.alertShown) {
      Countdown.playSound(this.alertSound);
      $("Alert" + this.id).style.display = "block";
      var title = this.time.title;
      if (this.time[this.index].icon) {
        title = "<img src='" + this.time[this.index].icon + "' alt='icon' width='16' height='16'/> " + title;
      }
      $("AlertHeader" + this.id).innerHTML = title;
      $("AlertHeader" + this.id).title = this.time[this.index].title;
      this.alertShown = true;
      if (!window.hideAlerts) {
        top.alert(this.time[this.index].title);
      }  
    }  
  }
  
  this.closeAlert = function() {
    $("Alert" + this.id).style.display = "none";
    if (this.time.hasNextTime) {
      this.time.nextTime();
    }
  }
  
}

Countdown.playSound = function (sound) {
  $("Sound").innerHTML = "";
  if (sound) {
    $("Sound").innerHTML = "<embed src='" + sound + "' autostart='true' hidden='true'/>";
  }  
}

