function Time(title, time, repeat) {
  this.type = null;
  this.title = title;
  this.daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  this.time = time;
  this.repeat = repeat;
  
  this.setTitle = function(title) {
    this.title = title;
  }
  
  this.getTime = function() {
    return this.time;
  }
  
  this.setTime = function(time) {
    this.time = time;
    if (this.hasNextTime()) {
      this.nextTime();
    }
  }
  
  this.setRepeat = function(repeat) {
    this.repeat = repeat;
    if (this.hasNextTime()) {
      this.nextTime();
    }
    if (this.onload) {
      this.onload();
    }
  }
  
  this.hasNextTime = function() {
    return repeat != 0;
  }
  
  this.nextTime = function() {
    var now = new Date();
    switch (this.repeat) {
      case 1: //yearly;
        var year = now.getFullYear();
        this.time.setFullYear(year);
        if (this.time > now) {
          return;
        }
        this.time.setFullYear(year + 1);
        break;
      case 2: //monthly;  
        var year = now.getFullYear();
        var month = now.getMonth();
        this.time.setFullYear(year);
        this.time.setMonth(month);
        if (this.time > now) {
          return;
        }
        month += 1;
        if (month > 11) {
          month = 0;
          year += 1;
        }
        this.time.setFullYear(year);
        this.time.setMonth(month);          
        break;
      case 3: //daily;
        var year = now.getFullYear();
        var month = now.getMonth();
        var day = now.getDate();
        this.time.setFullYear(year);
        this.time.setMonth(month);          
        this.time.setDate(day);
        if (this.time > now) {
          return;
        }
        day += 1;          
        if (day > this.daysInMonth[month]) {
          day = 0;
          month += 1;
          if (month > 11) {
            month = 0;
            year += 1;
          }  
        }
        this.time.setFullYear(year);
        this.time.setMonth(month);          
        this.time.setDate(day);          
        break;
      case 4: //weekly;
        var year = now.getFullYear();
        var month = now.getMonth();
        var date = now.getDate();
        var today = now.getDay();
        var day = this.time.getDay();
        var diff = day - today;
        if (diff < 0) {
          diff += 7;
        }
        this.time.setFullYear(year);
        this.time.setMonth(month);          
        this.time.setDate(date + diff);
        if (this.time > now) {
          return;
        }
        this.time.setFullYear(year);
        this.time.setMonth(month);          
        this.time.setDate(date + diff + 7);
        break;
    }
    if (this.onload) {
      this.onload();
    }
    return this.time;
  }

  if (this.hasNextTime()) {
    this.nextTime();
  }
}

function Countdown(time, element, id) {
  this.time = time;
  this.element = element;
  this.element.object = this;
  this.id = id;
  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;
    if (this.time.icon) {
      title = "<img src='" + this.time.icon + "' alt='icon' width='16' height='16'/> " + title;
    }
    $("Header" + this.id).innerHTML = title;
    $("Header" + this.id).title = this.time.title;
    $("InnerContent" + this.id).title = this.time.type;
    if (this.time.getTime()) {
      $("CountdownText" + this.id).title = this.time.getTime().toLocaleString();
    }
    if (window.widget && widget.mobile) {
      if (title.length > 0) {
        widget.mobile.setTitle(title);
      }
      else {
        widget.mobile.setTitle("Countdown");        
      }  
    }
  }
  
  this.updateHeader();    

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

  this.time.onerror = 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();
    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.icon) {
        title = "<img src='" + this.time.icon + "' alt='icon' width='16' height='16'/> " + title;
      }
      $("AlertHeader" + this.id).innerHTML = title;
      $("AlertHeader" + this.id).title = this.time.title;
      this.alertShown = true;
      if (!window.hideAlerts) {
        top.alert(this.time.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'/>";
  }  
}


