// JavaScript Document

	//initial time
	var d1_current = -1,
		d2_current = -1,
		h1_current = -1,
		h2_current = -1,
		m1_current = -1,
		m2_current = -1;
	// What to countdown to
	var countTo = new Date(2011,04,21,08,30);	
	
	function retroClock(){
		now = new Date();
		time_now = now.getTime();
		time_to = countTo.getTime();
		difference = (countTo - now);
		
		days = (difference / 86400000);
		left = (difference % 86400000);
		hours = (left / 3600000);
		left = (left % 3600000);
		minutes = (left / 60000);

		d1 = days / 10;
		d2 = days % 10;
		h1 = hours / 10;
		h2 = hours % 10;
		m1 = minutes / 10;
		m2 = minutes % 10;
		
		if(parseInt(d2) != parseInt(d2_current)){
			flip('daysUpRight', 'daysDownRight', d2, 'countdown/Images/Top/Right/', 'countdown/Images/Bottom/DayRight/');
			d2_current = d2;
			flip('daysUpLeft','daysDownLeft', d1, 'countdown/Images/Top/Left/', 'countdown/Images/Bottom/Left/');
			d1_current = d1;
		}
		
		if(parseInt(h2) != parseInt(h2_current)){
			flip('hoursUpRight', 'hoursDownRight', h2, 'countdown/Images/Top/Right/', 'countdown/Images/Bottom/HourRight/');
			h2_current = h2;
			flip('hoursUpLeft','hoursDownLeft', h1, 'countdown/Images/Top/Left/', 'countdown/Images/Bottom/Left/');
			h1_current = h1;
		}
		
		if(parseInt(m2) != parseInt(m2_current)){
			flip('minutesUpRight', 'minutesDownRight', m2, 'countdown/Images/Top/Right/', 'countdown/Images/Bottom/MinuteRight/');
			m2_current = m2;
			flip('minutesUpLeft','minutesDownLeft', m1, 'countdown/Images/Top/Left/', 'countdown/Images/Bottom/Left/');
			m1_current = m1;
		}
	}
	
	function flip (upperId, lowerId, changeNumber, pathUpper, pathLower){
		var upperBackId = upperId+"Back";
		var x = $(upperId);
		$(upperId).src = $(upperBackId).src;
		$(upperId).setStyle("height", "32px");
		$(upperId).setStyle("visibility", "visible");
		$(upperBackId).src = pathUpper+parseInt(changeNumber)+".png";
		
		$(lowerId).src = pathLower+parseInt(changeNumber)+".png";
		$(lowerId).setStyle("height", "0px");
		$(lowerId).setStyle("visibility", "visible");
		
		var flipUpper = new Fx.Tween(upperId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
		flipUpper.addEvents({
			'complete': function(){
				var flipLower = new Fx.Tween(lowerId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
				flipLower.addEvents({
					'complete': function(){	
						lowerBackId = lowerId+"Back";
						$(lowerBackId).src = $(lowerId).src;
						$(lowerId).setStyle("visibility", "hidden");
						$(upperId).setStyle("visibility", "hidden");
					}
				});					
				flipLower.start('height', 32);
			}
		});
		flipUpper.start('height', 0);
	}

	setInterval('retroClock()', 1000);
