/* $Id: mod_ahcountdown.js,v 1.4 2009/02/16 23:42:10 sqrt Exp $ */

/*
 * mod_ahcountdown.js
 *
 * Copyright (c) 2009 Andreas Hein <andreas@h-ein.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *
 */

/* some global vars */
var ts_to        = 0;  /* future timestamp (utc) */
var ts_from      = 0;  /* current timestamp (utc) */
var diff_days    = 0;
var diff_hours   = 0;
var diff_minutes = 0;
var diff_seconds = 0;

var showDays    = 0;
var showHours   = 0;
var showMinutes = 0;
var showSeconds = 0;


/* 
 * call initCountdown() before showCountdown() 
 *
 * _ts: timestamp
 * _sd: show days (0|1)
 * _sh: show hours (0|1)
 * _sm: show minutes (0|1)
 * _ss: show seconds (0|1)
 *
 * example:   initCountdown(1234567890, 1, 1, 1, 0);
 *
 */
function initCountdown(_ts, _sd, _sh, _sm, _ss)
{
  /* initialize global vars */
  ts_to       = _ts;
  showDays    = _sd;
  showHours   = _sh;
  showMinutes = _sm;
  showSeconds = _ss;

  /* show countdown */
  setTimeout('showCountdown()', 300);
}


/* show Countdown */
function showCountdown()
{
  var update_days    = -99999999;
  var update_hours   = -99999999;
  var update_minutes = -99999999;
  var update_seconds = -99999999;

  /* get current timestamp in seconds (utc) */
  var now = new Date(); 
  ts_from = Math.floor(now.getTime()/1000);

  diff_seconds = ts_to-ts_from;
  if (diff_seconds > 0) {

    /* days */
    diff_days = Math.floor(diff_seconds/86400);
    diff_seconds -= diff_days*86400;

    /* hours */
    diff_hours = Math.floor(diff_seconds/3600);
    diff_seconds -= diff_hours*3600;
    
    /*minutes */
    diff_minutes = Math.floor(diff_seconds/60);
    diff_seconds -= diff_minutes*60;

  } else {
    diff_seconds = "00";
    diff_minutes = "00";
    diff_hours   = "00";
    diff_days    = "00";
  }
  
  /* put countdown values into html document */
  /* days */
  if (showDays && diff_days != update_days) {
    update_days = diff_days;
    try {
      if (diff_days > 0) {
		if (update_days < 10 && update_days > 1) {
			diff_days = "0" + diff_days;// + " Tage";
		//} else if (update_days == 1) {
		//	diff_days = "01 Tag"; 
		}
		else {
			diff_days = diff_days;// + " Tage";
		}
		document.getElementById("countdownDays").firstChild.nodeValue = diff_days;
      } else {
		document.getElementById("countdownDays").firstChild.nodeValue = "00"; // Tage";
      }
    } catch(e) { }
  }
  
  /* hours */
  if (showHours && diff_hours != update_hours) {
    update_hours = diff_hours;
    try {
    	if (diff_hours > 0 || diff_days > 0) {
            if (showDays == 0)
            {
                diff_hours = diff_hours + diff_days*24;
                update_hours = diff_hours;
            }
			if (update_hours < 10 && update_hours > 1) {
				diff_hours = "0" + diff_hours + " Stunden";
			}
			else if (update_hours == 1) { 
				diff_hours = "01 Stunde";
			}
			else {
				diff_hours = diff_hours + " Stunden";
			}
			document.getElementById("countdownHours").firstChild.nodeValue = diff_hours;
    	} else {
			document.getElementById("countdownHours").firstChild.nodeValue = "00 Stunden";
      	}
    } catch (e) { }
  }

  /* minutes */
  if (showMinutes > 0 && diff_minutes != update_minutes) {
    update_minutes = diff_minutes;
    try {
      if (diff_minutes > 0 || diff_hours > 0 || diff_days > 0) {
		if (update_minutes < 10  && update_minutes > 1) {
			diff_minutes = "0" + diff_minutes + " Minuten";
		} else if (update_minutes == 1) {
			diff_minutes = "01 Minute";
		}
		else  {
			diff_minutes = diff_minutes + " Minuten";
		}
		
		document.getElementById("countdownMinutes").firstChild.nodeValue = diff_minutes;
      } else {
		document.getElementById("countdownMinutes").firstChild.nodeValue = "00 Minuten";
      }
    } catch(e) { }
  }
  
  /* seconds */
  if (showSeconds && diff_seconds != update_seconds) {
    update_seconds = diff_seconds;
    try {
      if (diff_seconds > 0 && diff_seconds < 10) { 
      	diff_seconds ="0" + diff_seconds + " Sekunden";
      }
      else {
      	diff_seconds = diff_seconds + " Sekunden";
      }
      
      if(update_seconds == 1) diff_seconds = "01 Sekunde";
      if (update_seconds <= 0) diff_seconds = "00 Sekunden";
      document.getElementById("countdownSeconds").firstChild.nodeValue = diff_seconds;
    } catch(e) { }
  }
  
  /* recall countdown() */
  if (showSeconds > 0) {
    setTimeout('showCountdown()', 500);
  } else {
    setTimeout('showCountdown()', 59000);
  }
}




// Ziel-Datum in MEZ
/*var jahr=2010, monat=3, tag=14, stunde=22, minute=00, sekunde=00;
var zielDatum=new Date(jahr,monat-1,tag,stunde,minute,sekunde);

function countdown() {
	startDatum=new Date(); // Aktuelles Datum

	// Countdown berechnen und anzeigen, bis Ziel-Datum erreicht ist
	if(startDatum<zielDatum)  {

		var jahre=0, monate=0, tage=0, stunden=0, minuten=0, sekunden=0;

		// Jahre
		while(startDatum<zielDatum) {
			jahre++;
			startDatum.setFullYear(startDatum.getFullYear()+1);
		}
		startDatum.setFullYear(startDatum.getFullYear()-1);
		jahre--;

		// Monate
		while(startDatum<zielDatum) {
			monate++;
			startDatum.setMonth(startDatum.getMonth()+1);
		}
		startDatum.setMonth(startDatum.getMonth()-1);
		monate--;

		// Tage
		while(startDatum.getTime()+(24*60*60*1000)<zielDatum) {
			tage++;
			startDatum.setTime(startDatum.getTime()+(24*60*60*1000));
		}

		// Stunden
		stunden=Math.floor((zielDatum-startDatum)/(60*60*1000));
		startDatum.setTime(startDatum.getTime()+stunden*60*60*1000);
		
		// Minuten
		minuten=Math.floor((zielDatum-startDatum)/(60*1000));
		startDatum.setTime(startDatum.getTime()+minuten*60*1000);
		
		// Sekunden
		sekunden=Math.floor((zielDatum-startDatum)/1000);
		
		// Anzeige formatieren
		if(jahre != 0) {
			(jahre!=1)?jahre=jahre+" Jahre,  ":jahre=jahre+" Jahr,  ";
		}
		if(monate != 0) {
			(monate!=1)?monate=monate+" Monate,  ":monate=monate+" Monat,  ";
		}
		
		(tage!=1)?tage=tage+" Tage,  ":tage=tage+" Tag,  ";
		(stunden!=1)?stunden=stunden+" Stunden,  ":stunden=stunden+" Stunde,  ";
		(minuten!=1)?minuten=minuten+" Minuten  und  ":minuten=minuten+" Minute  und  ";
		if(sekunden<10) {
			sekunden="0"+sekunden;
		}
		
		(sekunden!=1)?sekunden=sekunden+" Sekunden":sekunden=sekunden+" Sekunde";
			
		document.countdownform.countdowninput.value=
		jahre+monate+tage+stunden+minuten+sekunden;
		
		setTimeout('countdown()',200);
	}
	// Anderenfalls alles auf Null setzen
	else {
		document.countdownform.countdowninput.value="0 Jahre,  0 Monate,  0 Tage,  0 Stunden,  0 Minuten  und  00 Sekunden";
	}
}*/
