var myCountdown = new Array();
var repeat = false;

function checkPlural(arr, value) {
  noun = (value > 1) ? arr[2] : arr[value];
  return noun;
}

function updateDisplay(text, id) {
  var tag = document.getElementById(id);
  tag.innerHTML = text;
  return;
}

function doCountdown() {
  for (i = 0; i < myCountdown.length; i++) {
    if (!myCountdown[i].expired) {
      var currentDate = new Date();
      var eventDate = myCountdown[i].eventDate;
      var timeLeft = new Date();
      timeLeft = eventDate - currentDate;
      msPerDay = 24 * 60 * 60 * 1000;
      msPerHour = 60 * 60 * 1000;
      msPerMin = 60 * 1000;
      msPerSec = 1000;
      daysLeft = Math.floor(timeLeft / msPerDay);
      hoursLeft = Math.floor((timeLeft % msPerDay) / msPerHour);
      minsLeft = Math.floor(((timeLeft % msPerDay) % msPerHour) / msPerMin);
      secsLeft = Math.floor((((timeLeft % msPerDay) % msPerHour) % msPerMin) / msPerSec);
      day = checkPlural("day", daysLeft);
      hour = checkPlural("hour", hoursLeft);
      minute = checkPlural("minute", minsLeft);
      second = checkPlural("second", secsLeft);
      if ((daysLeft == 0) && (hoursLeft == 0) && (minsLeft == 0) && (secsLeft == 0)) {
        updateDisplay(myCountdown[i].onevent, myCountdown[i].tagID);
      }
      else {
        if (daysLeft <= -1) {
          updateDisplay(myCountdown[i].afterevent, myCountdown[i].tagID);
          myCountdown[i].expired = true;
        }
        else {
          updateDisplay(renderTemplate(daysLeft,hoursLeft,minsLeft,secsLeft,i), myCountdown[i].tagID);
          repeat = true;
        }
      }
    }
  }
  if (repeat) {
    repeat = false;
    window.setTimeout("doCountdown()", 1000);
  }
  else {
    return;
  }
}

function renderTemplate(daysLeft, hoursLeft, minsLeft, secsLeft, i) {
  template = myCountdown[i].template;
  template = template.replace(/###DAYSLEFT###/,daysLeft);
  template = template.replace(/###HOURSLEFT###/,hoursLeft);
  template = template.replace(/###MINUTESLEFT###/,minsLeft);
  template = template.replace(/###SECONDSLEFT###/,secsLeft);

  template = template.replace(/###DAYS###/,checkPlural(myCountdown[i].days,daysLeft));
  template = template.replace(/###HOURS###/,checkPlural(myCountdown[i].hours,hoursLeft));
  template = template.replace(/###MINUTES###/,checkPlural(myCountdown[i].mins,minsLeft));
  template = template.replace(/###SECONDS###/,checkPlural(myCountdown[i].secs,secsLeft));  
	  
  return template;
}  


function setEventDate(year, month, day, hour, minute, second) {
  this.eventDate = new Date(year, month - 1, day, hour, minute, second);
  return;
}

function addCountdown(countdown) {
  myCountdown[myCountdown.length] = countdown;
  return;
}

function Countdown() {
  this.tagID = "";
  this.eventDate = new Date();
  this.setEventDate = setEventDate;
  this.template = "";
  this.onevent = "";
  this.afterevent = "";
  this.days = new Array();
  this.hours = new Array();
  this.mins = new Array();
  this.secs = new Array();
  this.expired = false;
}