/**
 * jQuery "Fancy Countdown" Plugin
 * 
 * @name jquery-fancyCountdown-1.0.1.js
 * @author Sarpdoruk TAHMAZ - http://www.sarpdoruktahmaz.com
 * @version 1.0.1
 * @date April 20, 2011
 * @category jQuery Plugin Widget
 * @copyright (c) 2011 Sarpdoruk TAHMAZ (sarpdoruktahmaz.com)
 * @license GNU - GENERAL PUBLIC LICENSE - https://github.com/jquery/jquery/blob/master/GPL-LICENSE.txt
 * @info Visit http://www.sarpdoruktahmaz.com/projects/fancy-countdown for more info
 * @Special thanks to Onat CELMEN http://www.onatcelmen.com for the plugin idea
**/
(function($){
	$.fn.fancyCountdown = function(custom) {
		
		/********** This object's id **********/
		var thisID = $(this).attr("id"),
		
		/********** Default Settings **********/
		settings = {
			'year':2012, //Target year
			'month':12, //Target Month
			'day':12, //Target Day
			'hour':12, //Target Hour
			'minute':12, //Target Minute
			'second':12, //Target Second
			'timeReachedMessage':'Hoorraay!!', //Message after the time has reached
			'timeStamps':['Days', 'Hours', 'Minutes', 'Seconds'], //Timestamps in your own language,
			'sep': ['', '', ''],
			'imgdir': './images/top/'
		},
		
		/********** Table IDs **********/
		tableIds = ['d', 'h', 'm', 's'], 
		
		/********** Table **********/
		tables = "",
		
		/********** Loop Variables *********/
		i,
		j,
		k,
		
		/********** Color Array **********/
		color = {
			'blank':'gray',		
			'seconds':'green',
			'minutes':'lightblue',
			'hours':'red',
			'days':'orange'
		},
		
		/********** Other variables declarations **********/
		countdownTimer, 
		days, 
		hours, 
		minutes, 
		seconds,
		d1, d2, d3,
		h1, h2,
		m1, m2,
		s1, s2,
		
		/********** Number indexes in table **********/
		numberIndex = [
						  [0,1,2,3,4,7,8,11,12,15,16,19,20,23,24,25,26,27],
						  [3,7,11,15,19,23,27],
						  [0,1,2,3,7,11,12,13,14,15,16,20,24,25,26,27],
						  [0,1,2,3,7,11,12,13,14,15,19,23,24,25,26,27],
						  [0,3,4,7,8,11,12,13,14,15,19,23,27],
						  [0,1,2,3,4,8,12,13,14,15,19,23,24,25,26,27],
						  [0,1,2,3,4,8,12,13,14,15,16,19,20,23,24,25,26,27],
						  [0,1,2,3,7,11,15,19,23,27],
						  [0,1,2,3,4,7,8,11,12,13,14,15,16,19,20,23,24,25,26,27],
						  [0,1,2,3,4,7,8,11,12,13,14,15,19,23,24,25,26,27]								
					  ];

		/********** Extend defaults with the custom options **********/
		if(custom) { $.extend(settings, custom); }

		/********** Date objects creation and comparison between two dates **********/
		now = new Date();
		nowMiliseconds = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
		targetDateMiliseconds = Date.UTC(settings['year'], settings['month'] - 1, settings['day'], settings['hour'], settings['minute'], settings['second']);
		differenceMiliseconds = targetDateMiliseconds - nowMiliseconds;
		differenceSeconds = differenceMiliseconds / 1000;

		var imgdir = settings['imgdir'];

		$(this)
		.append($('<span/>').addClass('d1'))
		.append($('<span/>').addClass('d2'))
		.append($('<span/>').addClass('d3'))
		.append($('<span/>').addClass('h1'))
		.append($('<span/>').addClass('h2'))
		.append($('<span/>').addClass('m1'))
		.append($('<span/>').addClass('m2'))
		.append($('<span/>').addClass('s1'))
		.append($('<span/>').addClass('s2'));

		$('.d3', this).after(settings['sep'][0]);
		$('.h2', this).after(settings['sep'][1]);
		$('.m2', this).after(settings['sep'][2]);
		
		$('.d3', this).after($('<span/>').addClass('unit').append($('<img>').attr({'src' : imgdir + 'days.png'})));
		$('.h2', this).after($('<span/>').addClass('unit').append($('<img>').attr({'src' : imgdir + 'hours.png'})));
		$('.m2', this).after($('<span/>').addClass('unit').append($('<img>').attr({'src' : imgdir + 'minutes.png'})));
		$('.s2', this).after($('<span/>').addClass('unit').append($('<img>').attr({'src' : imgdir + 'seconds.png'})));
		$(".d1, .d2, .d3, .h1, .h2, .m1, .m2, .s1, .s2", this).append($('<img/>').attr({'src' : imgdir + '0.png'}));
		
		/********** Check if target date has passed or not **********/
		if(differenceMiliseconds >= 0) {
			countdownTimer = window.setInterval(updateTime, 1000);
		
			/********** INITIALIZE FOR THE FIRST TIME **********/
			/********** Time Calculation **********/
			days = Math.floor(differenceSeconds / 86400);
			differenceSeconds -= (days * 86400);
			hours = Math.floor(differenceSeconds / 3600);			
			differenceSeconds -= (hours * 3600);
			minutes = Math.floor(differenceSeconds / 60);
			differenceSeconds -= (minutes * 60);
			seconds = (differenceSeconds % 60);
			
			/********** SECONDS **********/
			if(parseInt(seconds) < 10) {
				seconds = "0"+seconds;	
			}
			
			seconds = seconds.toString();
			
			s1 = seconds.substr(0,1);
			s2 = seconds.substr(1,1);
			
			drawNumber(thisID, "s1", s1, "seconds", imgdir);
			drawNumber(thisID, "s2", s2, "seconds", imgdir);
			
			/********** MINUTES **********/
			if(parseInt(minutes) < 10) {
				minutes = "0"+minutes;	
			}
			
			minutes = minutes.toString();
			
			m1 = minutes.substr(0,1);
			m2 = minutes.substr(1,1);
			
			drawNumber(thisID, "m1", m1, "minutes", imgdir);
			drawNumber(thisID, "m2", m2, "minutes", imgdir);
			
			/********** HOURS **********/
			if(parseInt(hours) < 10) {
				hours = "0"+hours;	
			}
			
			hours = hours.toString();
			
			h1 = hours.substr(0,1);
			h2 = hours.substr(1,1);
				
			drawNumber(thisID, "h1", h1, "hours", imgdir);
			drawNumber(thisID, "h2", h2, "hours", imgdir);

			/********** DAYS **********/
			if(parseInt(days) < 100 && parseInt(days) > 10) {
				days = "0"+days;	
			} else if(parseInt(days) < 10) {
				days = "00"+days;	
			}
			
			days = days.toString();
			
			d1 = days.substr(0,1);
			d2 = days.substr(1,1);
			d3 = days.substr(2,1);
				
			drawNumber(thisID, "d1", d1, "days", imgdir);
			drawNumber(thisID, "d2", d2, "days", imgdir);
			drawNumber(thisID, "d3", d3, "days", imgdir);
			/********** INITIALIZATION OVER **********/
		}		
		
		/********** Update time every second **********/
		function updateTime() {
			
			/********** Date objects creation and comparison between two dates **********/
			now = new Date();
			nowMiliseconds = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
			differenceMiliseconds = targetDateMiliseconds - nowMiliseconds;
			differenceSeconds = differenceMiliseconds / 1000;
			
			/********** Check if target date has reached **********/
			if(differenceMiliseconds < 0) {				
				$("#"+thisID).html("<span id='timeReached'>"+settings['timeReachedMessage']+"</span>");
				window.clearInterval(countdownTimer);
				return false;
			}
						
			/********** Time Calculation **********/
			days = Math.floor(differenceSeconds / 86400);
			differenceSeconds -= (days * 86400);
			hours = Math.floor(differenceSeconds / 3600);			
			differenceSeconds -= (hours * 3600);
			minutes = Math.floor(differenceSeconds / 60);
			differenceSeconds -= (minutes * 60);
			seconds = (differenceSeconds % 60);
			
			/********** SECONDS **********/
			if(parseInt(seconds) < 10) {
				seconds = "0"+seconds;	
			}
			
			seconds = seconds.toString();
			
			s1 = seconds.substr(0,1);
			s2 = seconds.substr(1,1);
		
			drawNumber(thisID, "s1", s1, "seconds", imgdir);
			drawNumber(thisID, "s2", s2, "seconds", imgdir);
			
			/********** Decrease minutes when seconds reach 59 **********/
			if(s1 == "5" && s2 == "9") {
				/********** MINUTES **********/
				if(parseInt(minutes) < 10) {
					minutes = "0"+minutes;	
				}
				
				minutes = minutes.toString();
				
				m1 = minutes.substr(0,1);
				m2 = minutes.substr(1,1);
					
				drawNumber(thisID, "m1", m1, "minutes", imgdir);
				drawNumber(thisID, "m2", m2, "minutes", imgdir);
				
				/********** Decrease hours when minutes reach 59 **********/
				if(m1 == "5" && m2 == "9") {
					/********** HOURS **********/
					if(parseInt(hours) < 10) {
						hours = "0"+hours;	
					}
					
					hours = hours.toString();
					
					h1 = hours.substr(0,1);
					h2 = hours.substr(1,1);
						
					drawNumber(thisID, "h1", h1, "hours", imgdir);
					drawNumber(thisID, "h2", h2, "hours", imgdir);
					
					/********** Decrease days when hours reach 23 **********/
					if(h1 == "2" && h2 == "3") {
						/********** DAYS **********/
						if(parseInt(days) < 100 && parseInt(days) > 10) {
							days = "0"+days;	
						} else if(parseInt(days) < 10) {
							days = "00"+days;	
						}
						
						days = days.toString();
						
						d1 = days.substr(0,1);
						d2 = days.substr(1,1);
						d3 = days.substr(2,1);
							
						drawNumber(thisID, "d1", d1, "days", imgdir);
						drawNumber(thisID, "d2", d2, "days", imgdir);
						drawNumber(thisID, "d3", d3, "days", imgdir);
						
					}
					
				}
				
			}
						
		}
		
		/********** Draws numbers to the cells **********/
		function drawNumber(targetID, objID, number, timeStamp, imgdir) {
			number = parseInt(number);
			$('#' + targetID + ' .' + objID).find('img').attr({'src' : imgdir + number + '.png'});
		}		
	};
})(jQuery);
