/**
 * jquery-flipclock
 *  jQuery Plugin to Create a flipclock.
 *  http://bitbucket.org/johnhamelink/jquery-flipclock
 *
 * Copyright (c) 2010 John Hamelink
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since  : 0.1.0 - 01/11/2010
 *          0.2.0 - 02/11/2010
 */
// remap jQuery to $
(function ($) {

	// Function to change the clock's value when needed.
	$.clock_change = function (element, value, flipclockelement, pushvalue, clockheight, easingIn, easingOut) {
		//If weeks/days/hours/minutes/seconds is not equal to what's on screen
		if ($(flipclockelement + " #" + element + " div.current").text() != value) {
			
			//if jQuery can't detect the easing option, then disable it.
			if (!$.easing[easingIn]){
				easingIn = '';
			}
			if (!$.easing[easingOut]){
				easingOut = '';
			}
			
			//Push the number off of the clock, delete it, replace it, and push the new number in.
			$(flipclockelement + " #" + element + " div.current").animate(
				{
					'margin-top': '+=' + pushvalue + 'px',
					"opacity": "hide"
				}, 200, easingIn, function () {
					$(this).remove();
					
					$(flipclockelement + ' #' + element + ' div.notcurrent')
						.hide()
						.addClass('current')
						.removeClass('notcurrent')
						.css('margin-top', '-' + (pushvalue + clockheight) + 'px')
						.text(value)
						.animate(
							{
								'margin-top': pushvalue + 'px',
								"opacity": "show"
							}, 200, easingOut, function () {
								$(flipclockelement + ' #' + element).append('<div class="notcurrent">' + (value - 1) + '</div>');
							}
						);
				}
			);
		} else {

		}
	}

	//Main function
	$.fn.flipclock = function (options) {
		// plugin defaults
		$.fn.flipclock.defaults = {
			flipclockelement: '.flipclock',
			target_year:      '2011',
			target_month:     '04',
			target_day:       '26',
			target_hour:      '14',
			target_minute:    '15',
			target_second:    '00',
			pushvalue:        '0',
			clockheight:      '0',
			easing_in:        'easeInElastic',
			easing_out:       'easeOutElastic',
			skel:             ''
		};
		// build main options before element iteration by extending the default ones
		var opts = $.extend({}, $.fn.flipclock.defaults, options);

		if (opts.skel != ''){
			//Add standard structure to flipclock element:
			$(opts.flipclockelement).load(opts.skel);
		}
		
		//Add standard CSS to flipclock:
		$(opts.flipclockelement).css({
			'overflow': 'hidden',
			'text-align': 'center'
		});

		//Find the target date, the current date, and the time between.
		// NOTE: the month entered must be one less than current month. ie; 0=January, 11=December
		// NOTE: the hour is in 24 hour format. 0=12am, 15=3pm etc
		// format: dateFuture = new Date(year,month-1,day,hour,min,sec)
		// example: dateFuture = new Date(2003,03,26,14,15,00) = April 26, 2003 - 2:15:00 pm
		dateFuture = new Date(opts.target_year, (opts.target_month - 1), opts.target_day, opts.target_hour, opts.target_minute, opts.target_second);
		dateNow = new Date(); //grab current date
		var amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates
		//delete dateNow to keep minimise overhead
		delete dateNow;

		// time is already past
		if (amount < 0) {
			$(opts.flipclockelement + ' .number div').detach();
			$(opts.flipclockelement + ' .number').append('<div>0</div>');
		} else {

			setInterval(function () {
				dateNow = new Date(); //grab current date
				var amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates
				delete dateNow;

				var weeks = 0;
				var days = 0;
				var hours = 0;
				var mins = 0;
				var secs = 0;

				amount = Math.floor(amount / 1000); //kill the "milliseconds" so just secs
				weeks = Math.floor(amount / 604800); //weeks
				amount = amount % 604800;

				days = Math.floor(amount / 86400); //days
				amount = amount % 86400;

				hours = Math.floor(amount / 3600); //hours
				amount = amount % 3600;

				minutes = Math.floor(amount / 60); //minutes
				amount = amount % 60;

				seconds = Math.floor(amount); //seconds
				//Change the clock if it's different, then delete the variable to minimise overhead
				$.clock_change("weeks", weeks, opts.flipclockelement, opts.pushvalue, opts.clockheight, opts.easing_in, opts.easing_out);
				delete weeks;

				$.clock_change("days", days, opts.flipclockelement, opts.pushvalue, opts.clockheight, opts.easing_in, opts.easing_out);
				delete days;

				$.clock_change("hours", hours, opts.flipclockelement, opts.pushvalue, opts.clockheight, opts.easing_in, opts.easing_out);
				delete hours;

				$.clock_change("minutes", minutes, opts.flipclockelement, opts.pushvalue, opts.clockheight, opts.easing_in, opts.easing_out);
				delete minutes;

				$.clock_change("seconds", seconds, opts.flipclockelement, opts.pushvalue, opts.clockheight, opts.easing_in, opts.easing_out);
				delete seconds;
				delete amount;

			}, 500);

		}
	}

})(jQuery);

