


(function($) {
		$.fn.fxcountdown = function(options){
			
			//options 
			var options = $.extend({}, $.fn.fxcountdown.defaultOptions, options);
			return this.each(function(){
			  
			
				// preload the required images
				if(!options.bgImage)
				{
					if(typeof console !== "undefined"){console.log("need to define bgImage for digits")};
					return;
				}
			
				var tickerPreload = [
					options.bgImage
				];
				//preload some images
				var limit = tickerPreload.length;
				for (var i=0; i<limit; i++) {
					var img = new Image();
					img.src = tickerPreload[i];
					tickerPreload[i] = img;
				}
					//end of countdown(i.e start of sale)
					var date = options.endDate;
					//startDate of countDown, normally right now
					var now  = options.startDate;
					
					var secondsLeft = date - now;
					if (!secondsLeft || secondsLeft < 0) {
						return;
					}

					var $timer = $('#timer');
					//$timer.css('background-image', 'url(/images/homepage/thanksgiving2011/countdown-digits-bg.png)');

					displayTimeLeft();
					var timer = window.setInterval(displayTimeLeft, 1000);
									
					function displayTimeLeft()
					{
						if (secondsLeft <= 0) {
							//USE THIS AS AN OPTION TO FILL THE SPACE WHEN THE TIMER HAS FINISHED
							//if no time left, clear the timer, and set the static date
							$timer.html('');
							//$timer.css('background-image', 'url(http://static.fxhome.com/images/thanksgiving2011/countdown-digits-bg.png)');
							window.clearInterval(timer);
							return;
						}

						secondsLeft--;

						var time = secondsLeft;

						// calculate how many whole-days are in the seconds and remove them
						var days = Math.floor(time / 86400);
						time -= days * 86400;

						// calculate how many whole-hours are left and remove them
						var hours = Math.floor(time / 3600);
						time -= hours * 3600;

						// calculate how many whole-minutes are left and remove them
						var minutes = Math.floor(time / 60);
						time -= minutes * 60;

						// whats left should be seconds
						var seconds = time;

						var counter = displayTimeNumber(days, 'days');
										
						counter += displayTimeNumber(hours, 'hours');
										
						counter += displayTimeNumber(minutes, 'minutes');
										
						counter += displayTimeNumber(seconds, 'seconds');

						$timer.html(counter);
					}

					function displayTimeNumber(number, type)
					{
						// work out what colour the digit has to be
						var digitColor = 'grey';
						if (type == 'days') {
							if(secondsLeft <= 432000)
							{    
								digitColor = 'red';
							}
							else
							{
								digitColor = 'white';
							}
						}
						// padd the number to length 2
						var paddedNumber = number+'';
						if (number < 0) {
							paddedNumber =  '00';
						}
						else if (number < 10) {
							paddedNumber = '0' + number;
						}

						// build the html for the time component
						var html = '';

						html += '<div id="'+type+'">';

						var limit = paddedNumber.length;
						for (var i=0; i<limit; i++) {
							var num = parseInt(paddedNumber.charAt(i));
							html += '<div style="background-image:url('+ options.bgImage + ');" class="count-digit count-digit-'+digitColor+'-'+num+'">'+num+'</div>';
						}

						html += '</div>';

						return html;
					}
						
									
				});
		};	
			//defaults to one week countdown from today
			$.fn.fxcountdown.defaultOptions = {
				startDate: Math.round((new Date().getTime())/1000),
				endDate:  Math.round((new Date().getTime())/1000) + 508300
				
			};
			
			
				
		})(jQuery);
