var myCountdown = new Array();
var repeat = false;

// COLOR FUNCTION START

function hsv2rgb (h, s, v)
{
	h /= 360;
	s /= 100;
	v /= 100;

	var m2 = v <= 0.5 ? v * (s + 1) : v + s - v * s;
	var m1 = v * 2 - m2;
    var r = norm2hex (hue2rgb (m1, m2, h + 1/3));
    var g = norm2hex (hue2rgb (m1, m2, h));
    var b = norm2hex (hue2rgb (m1, m2, h - 1/3));
    return r + '' + g + '' + b;
}

function hue2rgb (m1, m2, h)
{
	if (h < 0) h = h + 1;
	if (h > 1) h = h - 1;
	if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
	if (h * 2 < 1) return m2;
	if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
	return m1;
} 

function norm2hex (value)
{
	return dec2hex (Math.floor (255 * value));
}

function dec2hex (dec)
{
	var hexChars = "0123456789ABCDEF";
	var a = dec % 16;
	var b = (dec - a) / 16;
	hex = '' + hexChars.charAt (b) + hexChars.charAt (a);
	return hex;
}

// COLOR FUNCTION ENDE

function checkPluralTag(noun, value) {
  noun = ((value == 1) || (value == 0)) ? noun : (noun += "e");
  return noun;
}

function checkPlural(noun, value) {
  noun = ((value == 1) || (value == 0)) ? noun : (noun += "n");
  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);
  }
  
  // COLOR
  var h = Math.floor (360 * Math.random());
  var s = 30 + Math.floor (70 * Math.random());
  var v = 30 + Math.floor (50 * Math.random());
  tag.style.color = '#' + hsv2rgb (h, s, v); 
  
  return;
}

function doCountdown() {
  for (i = 0; i < myCountdown.length; i++) {
    if (!myCountdown[i].expired) {
      var currentDate = new Date();
      var eventDate = myCountdown[i].eventDate;
	  var difDate = myCountdown[i].difDate;
      var timeLeft = new Date();
      timeLeft = eventDate - currentDate - difDate;
      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 = checkPluralTag("Tag", daysLeft);
      hour = checkPlural("Stunde", hoursLeft);
      minute = checkPlural("Minute", minsLeft);
      second = checkPlural("Sekunde", 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(" " + daysLeft + " " + day + " " + hoursLeft + " " + hour +
                        " " + minsLeft + " " + minute + " und " +
                        secsLeft + " " + second + " bis " +
                        myCountdown[i].event, myCountdown[i].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 setDifDate(year, month, day, hour, minute, second, curdate) {
  this.difDate = new Date(year, month - 1, day, hour, minute, second);
  this.difDate = this.difDate - curdate;
  return;
}

function addCountdown(countdown) {
  myCountdown[myCountdown.length] = countdown;
  return;
}

function Countdown() {
  this.tagID = "";
  this.eventDate = new Date();
  this.setEventDate = setEventDate;
  this.difDate = new Date();
  this.setDifDate = setDifDate;
  this.event = "";
  this.onevent = "";
  this.afterevent = "";
  this.expired = false;
}