function set_countdown(to_date) {
	var from_date = new Date();
	return Math.floor((to_date - from_date)/1000);
}
   
function display_countdown(outformat,countdn,spanid) {
	if (countdn < 2) {
		$("#"+spanid).html('Deze aanbieding is verlopen');
	} else {
		var secs = countdn % 60;
		if (secs < 10) {
			secs = '0'+secs;
		}
		var countdn1 = (countdn - secs) / 60;
		var mins = countdn1 % 60;
		if (mins < 10) {
			mins = '0'+mins;
		}
		countdn1 = (countdn1 - mins) / 60;
		var hours = (countdn1 % 24);
		if (hours < 10) {
			hours = '0'+hours;
		}
		var days = (countdn1 - hours) / 24;
		var outhtml = new String(outformat);
		outhtml = outhtml.replace(/%days%/,days);
		outhtml = outhtml.replace(/%hours%/,hours);
		outhtml = outhtml.replace(/%mins%/,mins);
		outhtml = outhtml.replace(/%secs%/,secs);
		if (days == 1) {
			outhtml = outhtml.replace(/dagen/,'dag');
		}
		$("#"+spanid).html(outhtml);
		setTimeout ('display_countdown(\''+ outformat + '\','+(countdn-1)+',\''+spanid+'\');',999);
	}
}

function start () {
	display_countdown(this.outformat,set_countdown(this.to_date),this.timer_span.id);
}

function jstimer (timer_span) {
	year="2068"; month="1"; day="1"; hour="00"; min="00"; sec="00";
	
	var rightNow = new Date();
	var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
	var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
	var temp = date1.toGMTString();
	var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
		temp = date2.toGMTString();
	var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
	var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
	if (hoursDiffDaylightTime == hoursDiffStdTime) { 
		tz_hours = 1;
	} else {
		tz_hours = 2;
	}
	
	this.timer_span = timer_span;
	strdate = timer_span.innerHTML;
	timer_content = strdate.split(', geldig tot ');
	date_and_time = timer_content[1].split(' ');
	
	if (typeof(date_and_time[0]) != 'undefined' && date_and_time[0] != '') {
		date_only	= date_and_time[0].split('-');
		year		= date_only[0] || year;
		month		= date_only[1] || month ;
		day			= new String(date_only[2] || day) ;
	} 
	if (typeof(date_and_time[1]) != 'undefined' && date_and_time[1] != '') {
		time_only	= date_and_time[1].split(':');
		hour		= time_only[0] || hour;
		min			= time_only[1] || min;
		sec			= time_only[2] || sec;
	} 
	if (typeof(date_and_time[2]) != 'undefined' && date_and_time[2] != '') {
		tz_hours = date_and_time[2];
	} 
      
	this.to_date = new Date();
	this.to_date.setFullYear(year,month-1,day);
	this.to_date.setHours(hour);
	this.to_date.setMinutes(min);
	this.to_date.setSeconds(sec);
	
      
	var tz_offset_client = -(this.to_date.getTimezoneOffset()*60*1000);
	var tz_offset = (tz_hours*60*60*1000);
	var msecs =  this.to_date.getTime();
	this.to_date.setTime(msecs - tz_offset + tz_offset_client);    
	this.outformat = 'Nog %days% dagen, %hours%:%mins%:%secs% geldig';
	this.start = start;

	timer_span.style.fontSize='10px';
	timer_span.style.color='#333333';
}

$(document).ready(function() {
	$('.countdowntimer').each(
		function (i) {
			this.id = 'timer_'+i;
			var thelink = this.parentNode.href;
			this.parentNode.parentNode.onclick = function(){window.location.href=thelink;};
			var timer = new jstimer(this);
			timer.start();
		}
	);
});
