/*

// JQuery Skinnable Countdown plugin by Ignacio Segura, "nachenko" (www.isegura.es)
// version: 1.0
// ----------------

What it does: builds a countdown to zero, showing hours, minutes and seconds. If hours is bigger than 24, it also shows days.

The difference with other plugins is that it does not only returns the countdown, it also creates an object with the timer, properly structured, and an event that fires every second, so you can theme the timer your own way. besides that, it is able to execute a callback (function) when timer reaches zero.

Usage (simple - default timer):
// Language string. Change this to your own language
dayString = { 
	single : 'day',
	plural : 'days'
}
// "target" is the place where you want to put the timer
$(target).countdown(true, deadline, [now, callback]);

Usage (advanced - the plugin will not print a countdown. Instead, the plugin will give you an object containing days, hours, minutes and seconds for you to format it your style).

$('').bind('countdownClick', function(e, timerObj) {
	--- put your own code here ---
	--- parameter 'e' is the event info ---
	--- parameter 'timerObj' is the timer object ---
} )
.countdown(false, 10, 0, function() { 
	$(this).css('color', 'red');
});

Parameters:
	- visible (required): true/false - if you don't want the timer to be print on screen, set this to false.
			Sounds weird, but maybe you don't want the countdown to be formatted by the plugin, you want
			to give it a different look, or you just want something to happen when time reaches zero.
	- deadline (required): time to zero. If "now" is specified, "deadline" should be a UNIX timestamp. Otherwise,
			it should be the number of seconds to zero. This parameter is required.
	- now (optional): UNIX timestamp for now, for this exact moment. Used in combination with "deadline", you can,
			for example, establish a countdown to the next Independence Day. If you don't want to use this parameter,
			but you need to specify the next one, set this to 0 (zero).
	- callback (optional): A function to be run when countdown reaches zero.

Returns: An object containing the days, hours, minutes and seconds to zero. Useful if you want to format the countdown your own way.

timerObj structure (for advanced usage):

	this.days
	this.hours
	this.minutes
	this.seconds

*/

(function($) {
	$.fn.countdown = function (visible, deadline, now, callback) {
		
		countdown = new Object();

		if (typeof(now) === 'number') {
			countdown.timeDue = deadline - now;
		} else {
			countdown.timeDue = deadline;
		}
		
		if(typeof(callback) === 'function'){
			countdown.callback = callback;
		}
		
		target = this;
		
		countdownTimer = setInterval('countdown.setTimer()', 1000);
		
		// HELPER FUNCTIONS, build inside countdown object
		countdown.setTimer = function() {
			if (countdown.timeDue > 0 ) {
				countdown.timeDue--;
			} else {
				// When timer reaches Zero, if callback exists, run it. Besides that, stop timer.
				countdown.timeDue = 0;
				clearInterval(countdownTimer);
					// now we are calling our own callback function
					if(typeof(countdown.callback) == 'function'){
						countdown.callback.call(target);
					}
				
			}
			countdown.timeObj = countdown.humanizeTimer();
			if (visible == true) {
				countdown.timeString = countdown.renderTimer(countdown.timeObj);
				$(target).text( countdown.timeString );
			}
			$(target).trigger('countdownClick', countdown.timeObj);
		
		}
		
		// Divide timestamp in hours, minutes and seconds, and returns an object containing all this.
		countdown.humanizeTimer = function() {
			t = countdown.timeDue;
			timeObj = new Object;
			timeObj.days = Math.floor(t / 86400);
			t = t%86400;
			timeObj.hours = Math.floor(t / 3600);
			t = t%3600;
			timeObj.minutes = Math.floor( t / 60);
			t = t%60;
			timeObj.seconds = t;
		
			return timeObj;
		}
		
		// Creates the timer string from the time object.
		countdown.renderTimer = function(timeObj) {
			var timeString = 'Time-out: '; // Je moet nog 
			if (timeObj.days == 1) {
				timeString = timeObj.days + ' ' + dayString.single + ' ';
			} else if (timeObj.days > 1) {
				timeString = timeObj.days + ' ' + dayString.plural + ' ';
			}
		
			//timeString += countdown.checkZero(timeObj.hours);
			timeString += countdown.checkZero(timeObj.minutes); // ':' + 
			timeString += ':' + countdown.checkZero(timeObj.seconds);
			// timeString += ' wachten';
			 
			return (timeString);
		}
		
		// Adds a zero if value under 10
		countdown.checkZero = function(i) {
			if (i<10)	{
				i="0" + i;
			}
			return i;
		}
		
	}

					
})(jQuery);

