/*
Script: CountDown Timer
Description: Counts down or up from a date (this is the non-XML version)
Author: Andrew Urquhart
Home: http://andrewu.co.uk/clj/countdown/
History:
20040117 0035UTC	v1		Andrew Urquhart		Created
20040119 1943UTC	v1.1	Andrew Urquhart		Made more accessible/easier to use
20040317 1548UTC	v1.2	Andrew Urquhart		Implemented a less intrusive error message
20040331 1408BST	v1.3	Andrew Urquhart		Attempts to add to the currently window.onload schedule, rather than overriding it
20050210 1558GMT	v1.4	Andrew Urquhart		Saves a short-cut to the each countdown Id element and re-uses that rather than continually calling getElementById - faster. Also reduced all nouns to short-versions to decrease size of script
20050216 0018GMT	v1.41	Andrew Urquhart		Added support for IE4
*/
 
// Update display (strContent, countdownId)
function CD_UD(s, id) {
		CD_OBJS[id].innerHTML = s;
};
 
// Tick (countdownId, eventDate)
function CD_T(id, e) {
	serverdate = serverdate + 1e3;
	var n	= new Date();
	var p	= 1E3 - n.getMilliseconds();
	
	//CD_D(+n, id, e);
	//setTimeout("if(typeof CD_T=='function'){CD_T('" + id + "'," + e + ")}", 1100-n.getMilliseconds()); // We offset from 1100 so that our clock ticks every second (the millisecond correction each loop sees to that), but updates 0.1s after every whole second so that we don't accidentally read the same Date() twice in the same second
	
	if ( (e-n) != 0)
		setTimeout("CD_T('" + id + "', " + e + ")", p);
	CD_D(+n, id, e);
};
 
// Calculate time and invoke draw routine (dateNow, countdownId, eventDate)
function CD_D(n, id, e) {
	var ms = e - n;
	if (ms <= 0) ms *= -1;
	//if (ms >= 0)
	{
  	var d = Math.floor(ms / 864E5);
  	ms -= d * 864E5;
  	var h = Math.floor(ms / 36E5);
  	ms -= h * 36E5;
  	var m = Math.floor(ms / 6E4);
  	ms -= m * 6E4;
  	var s = Math.floor(ms / 1E3);
 
  	if (d > 0)
  		CD_UD("Sluit in:<br><strong>" + d + "&nbsp;</strong>" + "dag" + (d == 1 ? " " : "en ") + "<br /><strong>" + CD_ZP(h) + "</strong>" + "u" + "<strong>" + CD_ZP(m) + "</strong>" + "m" + "<strong>" + CD_ZP(s) + "</strong>" + "s", id);
  	else if (h > 0)
  		CD_UD("<br>Sluit in:<br><strong>" + CD_ZP(h) + "</strong>" + "u" + "<strong>" + CD_ZP(m) + "</strong>" + "m" + "<strong>" + CD_ZP(s) + "</strong>" + "s", id);
  	else
  		CD_UD("<br>Sluit in:<br><strong>" + CD_ZP(m) + "</strong>" + "m" + "<strong>" + CD_ZP(s) + "</strong>" + "s", id);
 
 
	if (n > e)
	{
		CD_UD("<br><br>Gesloten", id);
	}
	
  	if (d == 0 && h == 0 && m == 0 && s == 0)
  	{
  		setTimeout("refreshPage()", 1000);
  	}
  }
 
};
 
// refresh pagina
function refreshPage() {
  location.reload(true);
	return false;
	//document.location = 'veiling.php'
}
 
// Prefix single integers with a zero
function CD_ZP(i) {
	return (i<10 ? "0" + i : i);
};
 
// Initialisation
function CD_Init() {
	var pref = "countdown";
	var objH = 1; // temp boolean true value
	if (document.getElementById || document.all) {
		for (var i=1; objH; ++i) {
			var id	= pref + i;
			objH	= document.getElementById ? document.getElementById(id) : document.all[id];
 
			if (objH && (typeof objH.innerHTML) != 'undefined') {
				var s	= objH.innerHTML;
				var dt	= new Date(s);
				if (!isNaN(dt)) {
					CD_OBJS[id] = objH; // Store global reference to countdown element object
					CD_T(id, dt.valueOf());
					if (objH.style) {
						objH.style.visibility = "visible";
					}
				}
				else {
					objH.innerHTML = s + "<a href=\"http://andrewu.co.uk/clj/countdown/\" title=\"Countdown Error: Invalid date format used, check documentation (see link)\">*</a>";
				}
			}
		}
	}
};
 
//var serverdate = Date.parse('26 May 2010 08:41:02 UTC+0200');
var serverdate = Date.parse('2010-05-26 08:41:02 UTC+0200');

var CD_OBJS = new Object();
 
// Try not to commandeer the default onload handler if possible
if (window.attachEvent) {
	window.attachEvent('onload', CD_Init);
}
else if (window.addEventListener) {
	window.addEventListener("load", CD_Init, false);
}
else {
	window.onload = CD_Init;
}
