var starttime = 0;

function toSt2(n) {
  s = "";
  if (n < 10) s += "0";
  return (s + n).toString();
}

function toSt3(n) {
  s = "";
  if (n < 10) s += "00";
  else if (n < 100) s += "0";
  return (s + n).toString();
}

function preloadImages() {
	var image = document.createElement("img");
	image.src = imagepath + "colon.gif";
	
	for(i = 0; i <= 9; i++) {
		var src = imagepath + i + ".gif";
		image.src = src;
	}
}

function initCountdown() {
	var now  = new Date();
	var year = now.getYear();
	if (year < 1900) {
		year += 1900;
	}
	
	var dateStr = starttime.monat + " " + starttime.tag + ", " + starttime.jahr;
	var end = new Date(dateStr);

	d = new Date();
	count = Math.floor(end.getTime() - d.getTime());

	if(count > 0) {
	    miliseconds = toSt3(count%1000);
	    
	    count = Math.floor(count/1000);
	    seconds = new String(toSt2(count%60));
	    
	    count = Math.floor(count/60);
	    minutes = new String(toSt2(count%60));
	    
	    count = Math.floor(count/60);
	    hours = new String(toSt2(count%24));
	    
	    count = Math.floor(count/24);
	    days = new String(count);
	    
	    var dayimages = new Array();
	    for(i = 0; i < days.length; i++ ) {
	    	var d = String(days.charAt(i));
	    	dayimages.push('<img src="'+imagepath+d+'.gif" alt="'+d+'" width="15" height="20"/>');
	    }
	    days = dayimages.join('');
	    
	    var hourimages = new Array();
	    for(i = 0; i < hours.length; i++ ) {
	    	var d = String(hours.charAt(i));
	    	hourimages.push('<img src="'+imagepath+d+'.gif" alt="'+d+'" width="15" height="20"/>');
	    }
	    hours = hourimages.join('');
	    
	    var minutesimages = new Array();
	    for(i = 0; i < minutes.length; i++ ) {
	    	var d = String(minutes.charAt(i));
	    	minutesimages.push('<img src="'+imagepath+d+'.gif" alt="'+d+'" width="15" height="20"/>');
	    }
	    minutes = minutesimages.join('');
	    
	    var secondsimages = new Array();
	    for(i = 0; i < seconds.length; i++ ) {
	    	var d = String(seconds.charAt(i));
	    	secondsimages.push('<img src="'+imagepath+d+'.gif" alt="'+d+'" width="15" height="20"/>');
	    }
	    seconds = secondsimages.join('');
	    
	    var countdown = document.getElementById("countdown");
	    countdown.innerHTML  = days;
	    countdown.innerHTML += '<img src="'+imagepath+'colon.gif" alt=":" width="10" height="20"/>';
	    countdown.innerHTML += hours;
	    countdown.innerHTML += '<img src="'+imagepath+'colon.gif" alt=":" width="15" height="20"/>';
	    countdown.innerHTML += minutes;
	    countdown.innerHTML += '<img src="'+imagepath+'colon.gif" alt=":" width="15" height="20"/>';
	    countdown.innerHTML += seconds;
	
	    setTimeout("initCountdown()", 1000);
	}
}