// JavaScript Document

	//initial time
	var d1_current = -1;
	var d2_current = -1;
	var h1_current = -1;
	var h2_current = -1;
	var m1_current = -1;
	var m2_current = -1;
	var s1_current = -1;
	var s2_current= -1;
	
	var NewMatchDate = new Date();

	
	function flip (upperId, lowerId, changeNumber, pathUpper, pathLower){


if (isNaN(changeNumber)) {
  changeNumber = 0;
}


		var upperBackId = upperId+"Back";
		$(upperId).src = $(upperBackId).src;
		$(upperId).setStyle("height", "25px");
		$(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', 25);
					
			}
							});
		flipUpper.start('height', 0);
		
		
	}//flip
				
	
	function retroClock()
	{
		// get new time
		now = new Date();
		MatchDate = NewMatchDate;
		
		var difference = MatchDate.getTime() - now.getTime();
		if(difference < 0) {
			difference = 0;
		}
 
    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
 
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
 
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
 
    var secondsDifference = Math.floor(difference/1000);

		d1 = Math.floor(daysDifference / 10);
		d2 = daysDifference % 10;
		h1 = Math.floor(hoursDifference / 10);
		h2 = hoursDifference % 10;
		m1 = Math.floor(minutesDifference / 10);
		m2 = minutesDifference % 10;
		s1 = Math.floor(secondsDifference / 10); //(MatchDate.getSeconds() - now.getSeconds()) / 10;
		s2 = secondsDifference % 10; //(MatchDate.getSeconds() - now.getSeconds()) % 10;

		//change pads
		
		if (d2 != d2_current){
		flip('daysUpRight', 'daysDownRight', d2, 'pics/Double/up/right/day/', 'pics/Double/down/right/');
		d2_current = d2;
		
		flip('daysUpLeft', 'daysDownLeft', d1, 'pics/Double/up/left/', 'pics/Double/down/left/');
		d1_current = d1;
		}
		
		if (h2 != h2_current){
		flip('hoursUpRight', 'hoursDownRight', h2, 'pics/Double/up/right/hour/', 'pics/Double/down/right/');
		h2_current = h2;
		
		flip('hoursUpLeft', 'hoursDownLeft', h1, 'pics/Double/up/left/', 'pics/Double/down/left/');
		h1_current = h1;
		}
		
		if( m2 != m2_current){
		flip('minutesUpRight', 'minutesDownRight', m2, 'pics/Double/up/right/minute/', 'pics/Double/down/right/');
		m2_current = m2;
		
		flip('minutesUpLeft', 'minutesDownLeft', m1, 'pics/Double/up/left/', 'pics/Double/down/left/');
		m1_current = m1;
		}
		
		if (s2 != s2_current){
		flip('secondsUpRight', 'secondsDownRight', s2, 'pics/Double/up/right/second/', 'pics/Double/down/right/');
		s2_current = s2;
		
		flip('secondsUpLeft', 'secondsDownLeft', s1, 'pics/Double/up/left/', 'pics/Double/down/left/');
		s1_current = s1;
		}
		
	}
	
	function SetNewMatchDate(MatchYear,MatchMonth,MatchDay,MatchHour,MatchMinute,MatchSecond) {
		NewMatchDate = new Date(MatchYear,MatchMonth,MatchDay,MatchHour,MatchMinute,MatchSecond);
	}
	

	
	
				
	
