var componentOnLoad = function()
{
	updateOrderDeadlineCountdown();
}

function updateOrderDeadlineCountdown(){
	if(typeof(orderDeadlineData) != "undefined" ){
		var d = new Date();
		var newTimeout = 1000;

		var currentTimeDifference = orderDeadlineData.cutoffEpochTime - Math.round(d.getTime() / 1000) + orderDeadlineData.correction;

		if(currentTimeDifference < 0){
			// Currently just show 00 00 00 to the user, could show a message.
		} else {
			var days    = '0' + Math.floor((currentTimeDifference        ) / 86400);
			days = days.substring(days.length-2);

			var hours   = '0' + Math.floor((currentTimeDifference % 86400) / 3600);
			hours = hours.substring(hours.length-2);

			var minutes = '0' + Math.floor((currentTimeDifference % 3600 ) / 60);
			minutes = minutes.substring(minutes.length-2);

			var seconds = '0' + currentTimeDifference % 60;
			seconds = seconds.substring(seconds.length-2);

			var delClock = document.getElementById(orderDeadlineData.counterId);

			var pos1 = '',  pos2 = '',  pos3 = '';
			var pos1Label = '',  pos2Label = '',  pos3Label = '';

			if(days > 0){
				/* Refresh every minute */
				newTimeout = (seconds > 0 ? seconds : 60) * 1000;

				pos1 = days;
				pos2 = hours;
				pos3 = minutes;

				pos1Label = ' days ';
				pos2Label = ' hours ';
				pos3Label = ' minutes ';

				if(delClock.className.indexOf('delClockDays') == -1){
					delClock.className += ' delClockDays';
				}
			} else {
				pos1 = hours;
				pos2 = minutes;
				pos3 = seconds;

				pos1Label = ' hours ';
				pos2Label = ' minutes ';
				pos3Label = ' seconds ';

				if(delClock.className.indexOf('delClockDays') != -1){
					delClock.className = delClock.className.replace('delClockDays', '');
				}
			}

			for(var i=0; i<delClock.childNodes.length; i++){
				switch(delClock.childNodes[i].className){
					case 'pos1':
						if(delClock.childNodes[i].firstChild.nodeValue != pos1){
							delClock.childNodes[i].firstChild.nodeValue = pos1;
						}

						if(delClock.childNodes[i].nextSibling.firstChild.nodeValue != pos1Label){
							delClock.childNodes[i].nextSibling.firstChild.nodeValue = pos1Label;
						}
						break;
					case 'pos2':
						if(delClock.childNodes[i].firstChild.nodeValue != pos2){
							delClock.childNodes[i].firstChild.nodeValue = pos2;
						}

						if(delClock.childNodes[i].nextSibling.firstChild.nodeValue != pos2Label){
							delClock.childNodes[i].nextSibling.firstChild.nodeValue = pos2Label;
						}
						break;
					case 'pos3':
						if(delClock.childNodes[i].firstChild.nodeValue != pos3){
							delClock.childNodes[i].firstChild.nodeValue = pos3;
						}

						if(delClock.childNodes[i].nextSibling.firstChild.nodeValue != pos3Label){
							delClock.childNodes[i].nextSibling.firstChild.nodeValue = pos3Label;
						}
						break;
				}
			}

			setTimeout('updateOrderDeadlineCountdown()', newTimeout);
		}
	}
}
