/*
USAGE:

<table cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td id="CD1days"></td>
		<td id="CD1hours"></td>
		<td id="CD1minutes"></td>
		<td id="CD1seconds"></td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td id="CD2days"></td>
		<td id="CD2hours"></td>
		<td id="CD2minutes"></td>
		<td id="CD2seconds"></td>
	</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td id="CD3days"></td>
		<td id="CD3hours"></td>
		<td id="CD3minutes"></td>
		<td id="CD3seconds"></td>
	</tr>
</table>
<script type="text/javascript">
countdownHandler.start('CD1', "http://www.komogvind.dk/images/ticketshop/numbers/small/gold", 86400);
countdownHandler.start('CD2', "http://www.komogvind.dk/images/ticketshop/numbers/small/gold", 11434);
countdownHandler.start('CD3', "http://www.komogvind.dk/images/ticketshop/numbers/small/gold", 321532);
</script>
*/

if (countdownHandler === undefined) {
	var countdownHandler = {
						/* Holds the seconds left for each instance */
							instances : [],
						/* holds preloaded images */
							pics : [],
						/* holds the time for when the timer should reach zero*/
							timers : [],
						/* holds the difference between server and client clock */
							difference : [],
						/* holds the previous time used to calibrate */
							previousTime : [],

						/*	the function that does everything	*/
							start : function (counterId, imgPath, secs, showDays, unixTime, serverTime, callbackFunction) {

							/*	closure functions	*/

								/*	Add leading zero and split the value into 2 and assign proper image to placeholder */
									function setValue(counterId, which, value) {
										value = (((value > 9) ? '' : '0') + value);
										var splitty = value.split('');

										$(document).ready(function() {
											if (imgPath == '') {
												$('#' + counterId + which).html(value);
											} else {
												
												var tensImage = countdownHandler.pics[counterId][splitty.shift()];
												var onesImage = countdownHandler.pics[counterId][splitty.shift()];
												
												if (tensImage !== undefined && onesImage !== undefined) {
													$('#timer_' + counterId + '_' + which + '_0').attr('src', tensImage.src);
													$('#timer_' + counterId + '_' + which + '_1').attr('src', onesImage.src);
												}
											}
										});
									}

								/*	calibrate time	*/
									var calibrate = function (counterId) {

										var time = new Date();
										var unixtime_ms = time.getTime();
										var unixtime = Math.round(parseInt(unixtime_ms / 1000));

										/* check if time differs to much from estimated */
										var diff = 0;
										if(countdownHandler.previousTime[counterId] != undefined) {
											diff = (unixtime_ms - countdownHandler.previousTime[counterId].getTime()) - 1000;
											if (diff > 1000 || diff < -1000) {
												countdownHandler.difference[counterId] += Math.round(diff/1000);
											}
										}

										calSeconds = countdownHandler.timers[counterId] - unixtime;
										calSeconds += countdownHandler.difference[counterId];
										countdownHandler.previousTime[counterId] = time;

										return calSeconds;
									}

								/*	Preload all images of the numbers */
									var preloadImages = function (counterId) {
										countdownHandler.pics[counterId] = new Array();
										for (var i = 0; i < 10; i++) {
											countdownHandler.pics[counterId][i] = new Image();
											countdownHandler.pics[counterId][i].src = imgPath + i + '.png';
										}
									}

								/*	Get image elem	*/
									var getImage = function (value, counterId, which, digit) {
										var image = '<img src="' + imgPath + value + '.png" alt="" id="timer_' + counterId + '_' + which + '_' + digit + '" border="0" align="left" width="21" height="28" />';
										return image;
									}

									var updateTimeLeft = function (counterId, showDays) {

									/*	Seconds left is calculated during this whole function */
										var sec = countdownHandler.instances[counterId];

									/*	do calibration */
										sec = calibrate(counterId);

									/*	Calculate minutes left */
										var mins = Math.floor(sec/60);
										sec = sec%60;

									/*	Calculate hours left */
										var hours = Math.floor(mins/60);
										mins = mins%60;

									/*	Calculate days left */
										var days = Math.floor(hours/24);
										if(showDays !== false) {
											hours = hours%24;
										}

									/*	Add calculated values to */
										if(showDays !== false) {
											setValue(counterId, 'days', days);
										}
										setValue(counterId, 'hours', hours);
										setValue(counterId, 'minutes', mins);
										setValue(counterId, 'seconds', sec);

									/*	Subtract 1 passed second from remaining time */
										countdownHandler.instances[counterId]--;

									/*	clear and overwrite last timeout, to keep redundancy from messing things up	*/
										window.self.clearTimeout(countdownHandler.timers[counterId + 'Timeout']);

									/*	Stop everything when countdown reaches 0 */
										if (countdownHandler.instances[counterId] >= 0) {
											var delay = function() {
												updateTimeLeft(counterId, showDays);
											}

											countdownHandler.timers[counterId + 'Timeout'] = window.self.setTimeout(delay, 1000);
										} else {
											
											setValue(counterId, 'seconds', 0);
											if (typeof(callbackFunction) === 'function') {
												callbackFunction();
											}
										}
									}
							/**/

						/*	flush old stuff, if any...	*/
							countdownHandler.instances[counterId] = null;
							countdownHandler.pics[counterId] = null;
							countdownHandler.timers[counterId] = null;
							window.self.clearTimeout(countdownHandler.timers[counterId + 'Timeout']);
							countdownHandler.difference[counterId] = null;
							countdownHandler.previousTime[counterId] = null;

							/**/
							var serverTimeOk = (serverTime !== undefined && serverTime !== null && serverTime !== false);
							var unixTimeOk = (unixTime !== undefined && unixTime !== null && unixTime !== false);
							/**/

							countdownHandler.instances[counterId] = secs;
							if (serverTimeOk && unixTimeOk) {
								countdownHandler.instances[counterId] = (unixTime - serverTime);
							}

						/*	MAKE SOME SORT OF OFFSET ACCORDING TO SERVERTIME */
							var offset = new Date();
							countdownHandler.timers[counterId] = (unixTimeOk ? unixTime : Math.round(offset.getTime() / 1000) + secs);
							countdownHandler.difference[counterId] = (serverTimeOk ? Math.round(offset.getTime() / 1000) - serverTime : 0);

							if (imgPath != '') {
								if (showDays !== false) {
									$('#' + counterId + 'days').html(getImage(9, counterId, 'days', 0) + getImage(9, counterId, 'days', 1));
								} else {
									$('#' + counterId + 'days').hide();
								}

								$('#' + counterId + 'hours').html(getImage(9, counterId, 'hours', 0) + getImage(9, counterId, 'hours', 1));
								$('#' + counterId + 'minutes').html(getImage(9, counterId, 'minutes', 0) + getImage(9, counterId, 'minutes', 1));
								$('#' + counterId + 'seconds').html(getImage(9, counterId, 'seconds', 0) + getImage(9, counterId, 'seconds', 1));
							}

							preloadImages(counterId);
							updateTimeLeft(counterId, (showDays != null ? showDays : true));
						}
					};
}
function startCountDown (counterId, imgPath, secs, showDays, unixTime, serverTime, callbackFunction) {
	countdownHandler.start(counterId, imgPath, secs, showDays, unixTime, serverTime, callbackFunction);
}