//<!--

/******************************************************************************
 *
 *	08.10.2010, Colin Appel, FFW Langd 2010 (ff-langd.de)
 *
 *	javascript time functions: each registered date id will be filled
 *	with the current date and the registered countdown ids are filled
 *	with the left time period.
 *
 *	countdown:
 *
 *	<div id="cd1"><span id="cd1_timestamp">???timestamp???</span>
 *	alternative text</div>
 *
 *	countdown date and text:
 *
 *	part of a date:	<span class="cddate">...</span>
 *	text:		<span class="cdtext">...</span>
 *
 *****************************************************************************/

//global variables

/*****************************************************************************/

//name is the id of the div/span container

//date and time names
var id_datetime = new Array('jsdate');

//countdown names
var id_countdown = new Array('cd_normal','cd_page','cd_fest');

/*****************************************************************************/

//the countdowns, will be loaded from source file
var countdowns = new Array(id_countdown.length);

	//event to the local javascript handler
	function clock(servertime) {

		//convert servertimestamp in seconds to javascript timestamp in ms
		var serverts = servertime * 1000;

		//get a timestamp, when the script starts
		if(typeof clock.clientts == 'undefined') {

			//initialize only first time
			clock.clientts = new Date().getTime();

			//the global array with the timestamps from html sourcecode
			for(var i = 0; i < countdowns.length; i++)
				countdowns[i] = null;
		}


		var currentts = serverts + (new Date().getTime() - clock.clientts);


		setdatetime(currentts);

		setcountdown(currentts);

		setTimeout("clock('" + servertime + "')",200);
	}


/******************************************************************************

	date and time.

******************************************************************************/

	//get current client date as a string
	function getDateStr(timestamp) {

		var date = new Date(timestamp);

		var year = date.getYear();
		if(year < 1900) year += 1900;

		var days = date.getDate();
		if(days < 10)
			days = "0" + days;

		var months = date.getMonth() + 1;
		if(months < 10)
			months = "0" + months;

		return days + "." + months + "." + year;
	}


	//get current client time as a string
	function getTimeStr(timestamp) {

		var date = new Date(timestamp);

		var hours = date.getHours();
		if(hours < 10)
			hours = "0" + hours;

		var minutes = date.getMinutes();
		if(minutes < 10)
			minutes = "0" + minutes;

		var seconds = date.getSeconds();
		if(seconds < 10)
			seconds = "0" + seconds;

		return hours + ":" + minutes + ":" + seconds;
	}


	//fill the datetime elements
	function setdatetime(currentts) {

		for(var i = 0; i < id_datetime.length; i++) {

			//go through all datetime ids
			var element_date = document.getElementById(id_datetime[i]);

			if(element_date != null)
				element_date.innerHTML = "" + getDateStr(currentts) + "<br />" + getTimeStr(currentts);
		}
	}


/******************************************************************************

	countdown

******************************************************************************/

	//returns a string with 2 chars
	function twoChars(val) {

		var str = "";

		if(val < 10) {
			str += "0";
		}
		return (str+val).toString();
	}

	//returns a string with 3 chars
	function threeChars(val) {

		var str = "";

		if(val < 10) {
			str += "00";
		} else if(val < 100) {
			str += "0";
		}
		return (str+val).toString();
	}


	//fill the countdown elements
	function setcountdown(currentts) {

		for(var i = 0; i < id_countdown.length; i++) {

			//go through all registered countdown ids
			var element_countdown = document.getElementById(id_countdown[i]);

			var element_timestamp = document.getElementById(id_countdown[i] + "_timestamp");

			//if there is a countdown element with a php timestamp in it
			if(element_countdown != null) {

				if(countdowns[i] == null && element_timestamp != null) {

					//element_timestamp.style.visibility = "visible","hidden";
					//element_timestamp.style.display = "inline","none";

					//we need milliseconds: set the global array
					countdowns[i] = element_timestamp.innerHTML * 1000;
				}

				//retrieve from global array
				element_countdown.innerHTML = "" + getCountdown(currentts,countdowns[i]);
			}
		}
	}

	//actualize the countdown container
	function getCountdown(currentts,dstts) {

		//delta value is in seconds
		var delta = Math.round((dstts - currentts) / 1000);

		//when delta is negative set it to 0, because the time is over
		if(delta < 0) {

			delta = 0;
		}

		var s = twoChars(Math.floor(delta%60));

		delta = delta / 60;
		var m = twoChars(Math.floor(delta%60));

		delta = delta / 60;
		var h = twoChars(Math.floor(delta%24));

		delta = delta / 24;
		var d = threeChars(Math.floor(delta));

		return 	  "<span class='cddate'>" + d + "</span><span class='cdtext'>Tag(e)</span><span class='cdcolon'>:</span>"
			+ "<span class='cddate'>" + h + "</span><span class='cdtext'>Stunde(n)</span><span class='cdcolon'>:</span>"
			+ "<span class='cddate'>" + m + "</span><span class='cdtext'>Minute(n)</span><span class='cdcolon'>:</span>"
			+ "<span class='cddate'>" + s + "</span><span class='cdtext'>Sekunde(n)</span>";
	}

//-->

