
// Constants
	var SECOND = 1;
	var MINUTE = 60 * SECOND;
	var HOUR = 60 * MINUTE;
	var DAY = 24 * HOUR;

	//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 dateString;
	
	var hit_date;

	var hit;
	var now;
	var ticky;

	var imgPath = '/img/flipclock/';
    
    function flip(upperId, lowerId, changeNumber, pathUpper, pathLower){
        
        pathUpper = imgPath + pathUpper;
        pathLower = imgPath + pathLower;

        var upperBackId = upperId+"Back";
        $(upperId)
            .attr('src', $(upperBackId).attr('src'))
            .height("25px")
            .css({"visibility": "visible", 'display': 'inline-block' });
        
        $(upperBackId).attr('src', pathUpper + parseInt(changeNumber) + ".png");
        
        $(lowerId)
            .attr('src', pathLower + parseInt(changeNumber) + ".png")
            .height('0px')
            .css({"visibility": "visible", 'display': 'inline-block'});
        
        $(upperId).animate({'height': 0}, { 'duration': 50, defaultEasing: 'easeinoutsine', 'complete': function(){
            $(lowerId).animate({'height': 25}, { 'duration': 50, defaultEasing: 'easeinoutsine', 'complete': function(){
                $(lowerId + "Back").attr('src', $(lowerId).attr('src') );
                $(lowerId).add(upperId).css({"visibility": "hidden",
                                             "display": 'inline-block' }).height('0');
            } } )
        } })
    }

    
    function tick(){
        
        var now = new Date();
		difference = ((hit - now.getTime()) / 1000);
		
		if( difference >= 0 ) {
			diff_days = Math.floor(difference / DAY);
			diff_days_left = Math.floor((diff_days % 100) / 10);
			diff_days_right = Math.floor((diff_days % 100) % 10);
			diff_hours = Math.floor((difference - (diff_days * DAY)) / HOUR);
			diff_hours_left = Math.floor(diff_hours / 10);
			diff_hours_right = Math.floor(diff_hours % 10);
			diff_mins = Math.floor((difference - (diff_days * DAY) - (diff_hours * HOUR)) / MINUTE);
			diff_mins_left = Math.floor(diff_mins / 10);
			diff_mins_right = Math.floor(diff_mins % 10);
			diff_secs = Math.floor((difference - (diff_days * DAY) - (diff_hours * HOUR) - (diff_mins * MINUTE)) / SECOND);
			diff_secs_left = Math.floor(diff_secs / 10);
			diff_secs_right = Math.floor(diff_secs % 10);
			
			//change pads

			if( diff_days_right != d2_current ){
				flip('#daysUpRight', '#daysDownRight', diff_days_right, 'up/right/', 'down/right/');
				d2_current = diff_days_right;

				flip('#daysUpLeft', '#daysDownLeft', diff_days_left, 'up/left/', 'down/left/');
				d1_current = diff_days_left;
			}

			if( diff_hours_right != h2_current ){
				flip('#hoursUpRight', '#hoursDownRight', diff_hours_right, 'up/right/', 'down/right/');
				h2_current = diff_hours_right;

				flip('#hoursUpLeft', '#hoursDownLeft', diff_hours_left, 'up/left/', 'down/left/');
				h1_current = diff_hours_right;
			}

			if( diff_mins_right != m2_current ){
				flip('#minutesUpRight', '#minutesDownRight', diff_mins_right, 'up/right/', 'down/right/');
				m2_current = diff_mins_right;

				flip('#minutesUpLeft', '#minutesDownLeft', diff_mins_left, 'up/left/', 'down/left/');
				m1_current = diff_mins_left;
			}

			if (diff_secs_right != s2_current ){
				flip('#secondsUpRight', '#secondsDownRight', diff_secs_right, 'up/right/', 'down/right/');
				s2_current = diff_secs_right;

				flip('#secondsUpLeft', '#secondsDownLeft', diff_secs_left, 'up/left/', 'down/left/');
				s1_current = diff_secs_left;
			}
		} else {
			clearInterval(ticky);
			$('#wrapperClock').fadeOut('slow', function(){
				$('#livenow').fadeIn('slow');
			})
		}

    }
    

    function retroClock(){
        tick();
        ticky = setInterval('tick()', 1000);
    }
    

    $(function(){
		dateString = $("#dateClock").val();
		hit_date = new Date(dateString);
		hit = hit_date.getTime();
		retroClock();
    })
