var myCountdown = new Array();
var repeat = false;

function checkPluralDay(noun, value) {
  if(value == 1) {noun = "dzien"}
  return noun;
}
function checkPluralHour(noun, value) {
  if(value == 1) {noun = "godzina"}
  if(value == 52 || value == 53 || value == 54 || value == 44 || value == 43 || value == 42 || value == 34 || value == 33 || value == 32 || value == 24 || value == 23 || value == 22 || value == 4 || value == 3 || value == 2) {noun = "godziny"};
  return noun;
}
function checkPluralMin(noun, value) {
  if(value == 1) {noun = "minuta"}
  if(value == 52 || value == 53 || value == 54 || value == 44 || value == 43 || value == 42 || value == 34 || value == 33 || value == 32 || value == 24 || value == 23 || value == 22 || value == 4 || value == 3 || value == 2) {noun = "minuty"};
  return noun;
}
function checkPluralSek(noun, value) {
  if(value == 1) {noun = "sekunda"}
  if(value == 52 || value == 53 || value == 54 || value == 44 || value == 43 || value == 42 || value == 34 || value == 33 || value == 32 || value == 24 || value == 23 || value == 22 || value == 4 || value == 3 || value == 2) {noun = "sekundy"};
  return noun;
}

function updateDisplay(text, id) {
  var tag = document.getElementById(id);
  if (tag.firstChild) {
    tag.firstChild.nodeValue = text;
  }
  else {
    textNode = document.createTextNode(text);
    tag.appendChild(textNode);
  }
  return;
}

var d = 0;
function doCountdown() {
  for (i = 0; i < myCountdown.length; i++) {
	if(d == i){
	    if (!myCountdown[d].expired) {
	      var currentDate = new Date();
	      var eventDate = myCountdown[d].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 = checkPluralDay("dni", daysLeft);
	      hour = checkPluralHour("godzin", hoursLeft);
	      minute = checkPluralMin("minut", minsLeft);
	      second = checkPluralSek("sekund", secsLeft);

	        if (daysLeft <= -1) {
	          updateDisplay("", myCountdown[d].tagID);
	          myCountdown[d].expired = true;
			  d++;
	          repeat = true;
	        }
	        else {
	          updateDisplay(daysLeft + " " + day + " " + hoursLeft + " " + hour +
	                        " " + minsLeft + " " + minute + ", i " +
	                        secsLeft + " " + second + " do " +
	                        myCountdown[d].event, myCountdown[d].tagID);
	          repeat = true;
	        }
	    }
	}
  }
  if (repeat) {
    repeat = false;
    window.setTimeout("doCountdown()", 1000);
  }
  else {
    return;
  }
}

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.event = "";
  this.onevent = "";
  this.afterevent = "";
  this.expired = false;
}