/* =============================================================================
* countdown.js
*
* Class definition for a CountDown class
* Constructor takes two arguments, a date to count down to, and an object of
*   time length/input id pairs
*
* Time lengths supported are:
*   year      calculated as
*               365.25 days
*               1000 * 60 * 60 * 24 * 365.25 milliseconds
*   month     calculated as
*               (365.25 / 12) days
*               (1000 * 60 * 60 * 24 * 365.25) / 12 milliseconds
*   week      calculated as
*               7 days
*               1000 * 60 * 60 * 24 * 7 milliseconds
*   day       calculated as
*               24 hours
*               1000 * 60 * 60 * 24 milliseconds
*   hour      calculated as
*               60 minutes
*               60 * 60 * 1000 milliseconds
*   minute    calculated as
*               60 seconds
*               60 * 1000 milliseconds
*   second    calculated as
*               1000 milliseconds
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006
*
* Last updated 2006.05.09
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */

function CountDown(dt, input, update) {
  /* ***************************************************************************
  * PRIVATE VARIABLES */

  // storage for setTimeout calls
  var _timer;

  /* ***************************************************************************
  * PUBLIC VARIABLES */

  this.updateTime = (update ? update : 1000);

  this.timeLeft = {
    year : 0,
    month : 0,
    week : 0,
    day : 0,
    hour : 0,
    minute : 0,
    second : 0
  };

  this.input = input;

  this.date = dt;

  /* ***************************************************************************
  * INSTANCE CONSTRUCTION */

  this.init = function() {
    for (var i in this.input) {
      if (typeof(this.input[i]) == 'string'
          && document.getElementById) {
        this.input[i] = document.getElementById(this.input[i]);
      }
    }

    if (typeof(this.date) == 'string') {
      this.date = new Date(this.date);
    }

    this.start();
  }

  /* ***************************************************************************
  * PRIVATE METHODS */

  /* ***************************************************************************
  * PUBLIC METHODS */

  this.start = function() {
    if(document.getElementById) {
      this.calculate();
      this.update();

      var self = this;
      window.onunload = function() {
        self.clear();
      };
    }
  }

  this.stop = function() {
    clearTimeout(_timer);
  }

  this.clear = function() {
    this.stop();
  }

  this.loop = function() {
    var self = this;
    _timer = setTimeout(function() {
      self.calculate();
      self.update();
    }, self.updateTime);
  }

  this.calculate = function() {
    this.clear();

    var now = new Date();

    if (!this.date
        || !this.date.getTime
        || now.getTime() >= this.date.getTime()) {
      return this.timeLeft;
    }

    // check again in 1 second
    this.loop();

    var remaining = {
      'second' : Math.round((this.date.getTime() - now.getTime()) / 1000)
    };

    if (this.input.year) {
      var year_s = 365.25 * 24 * 60 * 60;
      remaining.year = Math.floor(remaining.second / year_s);
      remaining.second -= (remaining.year * year_s);
    }

    if (this.input.month) {
      var month_s = 365.25 / 12 * 24 * 60 * 60;
      remaining.month = Math.floor(remaining.second / month_s);
      remaining.second -= (remaining.month * month_s);
    }

    if (this.input.week) {
      var week_s = 7 * 24 * 60 * 60;
      remaining.week = Math.floor(remaining.second / week_s);
      remaining.second -= (remaining.week * week_s);
    }

    if (this.input.day) {
      var day_s = 24 * 60 * 60;
      remaining.day = Math.floor(remaining.second / day_s);
      remaining.second -= (remaining.day * day_s);
    }

    if (this.input.hour) {
      var hour_s = 60 * 60;
      remaining.hour = Math.floor(remaining.second / hour_s);
      remaining.second -= (remaining.hour * hour_s);
    }

    if (this.input.minute) {
      var minute_s = 60;
      remaining.minute = Math.floor(remaining.second / minute_s);
      remaining.second -= (remaining.minute * minute_s);
    }

    this.timeLeft = remaining;
  }

  this.update = function() {
    for (var time in this.timeLeft) {
      if (typeof(this.timeLeft[time]) != 'undefined'
          && this.input[time]) {
        this.input[time].value = this.timeLeft[time];
      }
    }
  }
}

var in_array = (in_array ? in_array : function(val, ar) {
  for (var i = 0; i < ar.length; i++) {
    if (ar[i] == val) {
      return true;
    }
  }

  return false;
});