/**
* Date/Clock: a jQuery Plugin
* @author: Trevor Morris (trovster)
* @url: http://www.trovster.com/lab/code/plugins/jquery.date-clock.js
* @documentation: http://www.trovster.com/lab/plugins/date-clock/
* @published: 11/09/2008
* @updated: 29/09/2008
*
* Takes the date format 09.01.2009 09:45
* Separates out in to separate parts
* Then ticks the clock up
*
*/
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			dateClock: function(options) {
				var settings = $.extend({}, $.fn.dateClock.defaults, options);
			
				return this.each(
					function() {
						if($.fn.jquery < '1.2.6') {return;}
						var $date = $(this);
						var o = $.metadata ? $.extend({}, settings, $date.metadata()) : settings;
				
						/**
						* Split the Date in to component parts
						*/
						var date_value = $date.text();
						var date_value_parts = date_value.split(' ');
						var date_parts = date_value_parts[0].split('.');
						var time_parts = date_value_parts[1].split(':');
						var hourAMPM = date_value_parts[2];
			
						/**
						* Create HTML for individual date parts
						*/
						var $date_day 	 = $('<span></span>').addClass('day').text(date_parts[0]);
						var $date_month  = $('<span></span>').addClass('month').text(date_parts[1]);
						var $date_year 	 = $('<span></span>').addClass('year').text(date_parts[2]);
            var hour = time_parts[0];
						var $date_hour 	 = $('<span></span>').addClass('hour').text(hour);
						var $date_minute = $('<span></span>').addClass('minute').text(time_parts[1]);
						var $date_second = $('<span></span>').addClass('second').text('00');
						var $date_AMPM = $('<span></span>').addClass('AMPM').text(hourAMPM);
						
						/**
						* Append the Date/Time Parts to the HTML
						*/

            var $datePart = $('<span></span>').addClass('date');
            var $timePart = $('<span></span>').addClass('time');
						$date.empty();


						$datePart.append($date_day).append('.').append($date_month).append('.').append($date_year);
						$timePart.append(' ').append($date_hour).append(':').append($date_minute);
						$date.append($datePart).append($timePart);
						if(o.second===true) {
							$timePart.append(':').append($date_second);
						}	
						$timePart.append($date_AMPM);
						/**
						* Analogue Clock
						* Create a UL with List Items
						*/
						if(o.type === 'analogue') {
							var $clock_ul 	= $('<ul></ul>').addClass('clock');
							
							$date_hour	 	= $('<li></li>').addClass('hour').attr('title',time_parts[0]);
							$date_minute 	= $('<li></li>').addClass('minute').attr('title',time_parts[1]);
							$date_second 	= $('<li></li>').addClass('second').attr('title','00');
							
							$clock_ul.append($date_hour).append($date_minute).append($date_second);
							$date.replaceWith($clock_ul);
						}
						
						/**
						* Set the Data
						*/
						$date.data('day', date_parts[0]).data('month', date_parts[1]).data('year', date_parts[2]);
						$date.data('hour', hour).data('minute', time_parts[1]).data('second', 0).data('AMPM', hourAMPM);
						/**
						* Set the Timer to start the clock
						*/
						var intervalID = setInterval(function() {
							$.fn.dateClock.start(o, $date, $date_day, $date_month, $date_year, $date_hour, $date_minute, $date_second, $date_AMPM);
						}, o.timeout);
					}
				);
			}
		});
		
		/**
		* Set your Plugin Defaults Here…
		*/
		$.fn.dateClock.defaults = {
			timeout : 1000,
			type : 'digital',
			second : true
		};
		
		/**
		* Update the Date/Time
		*/
		$.fn.dateClock.start = function(o, $date, $day, $month, $year, $hour, $minute, $second, $hourAMPM) {
			/**
			* Get the Values from the Elements
			*/
			var day = parseInt($date.data('day'), 10);
			var month = parseInt($date.data('month'), 10);
			var year = parseInt($date.data('year'), 10);
			
			var hour = parseInt($date.data('hour'), 10);
			var minute = parseInt($date.data('minute'), 10);
			var second = parseInt($date.data('second'), 10);
			var hourAMPM = $date.data('AMPM');

			/**
			* Set the Seconds going
			*/
			second++;
			
			/**
			* Is it a leap year?
			*/
			var isALeapYear = (year % 4 === 0) ? (year % 100 === 0) ? (year % 400 === 0) ? true : false : true : false;
			
			/**
			* Increase the Minute
			*/
			if(second > 59) {
				minute++;
				second = 0;
			}
			
			/**
			* Increase the Hour
			*/
			if(minute > 59) {
				hour++;
				minute = 0;
			}
			
			/**
			* Increase the Day
			*/
			if(hour == 12 && minute == 0 && second == 0 && hourAMPM == 'PM') {
				day++;
        hourAMPM = 'AM';
			}
			else if(hour == 12 && minute == 0 && second == 0 && hourAMPM == 'AM') {
        hourAMPM = 'PM';
			}
      
      if(hour > 12) {
        hour = 1;
      }
			
			/**
			* Check whether we need to update the month
			* Check the number of days in this month
			* Also, leap year if the month is February
			*/
			var m31 = [1,3,5,7,8,10,12];
			var m30 = [4,6,9,11];
			var updateMonth = false;
			
			if(day > 31 && $.inArray(month, m31) !== -1) {
				updateMonth = true;
			}
			else if(day > 30 && $.inArray(month, m30) !== -1) {
				updateMonth = true;
			}
			else if(month === 2) {
				if(isALeapYear && day > 29) {
					updateMonth = true;
				}
				else if(day > 28) {
					updateMonth = true;
				}
			}

			/**
			* Increase the Month
			*/
			if(updateMonth === true) {
				month++;
				day = 1;
			}
			
			/**
			* Increase the Year
			*/
			if(month > 12) {
				year++;
				month = 1;
			}
			
			/**
			* Update the HTML
			*/			
			if(o.type === 'digital') {
				$day.text(((day < 10) ? '0' : '') + day);
				$month.text(((month < 10) ? '0' : '') + month);
				$year.text(((year < 10) ? '0' : '') + year);
				
				$hour.text(((hour < 10) ? '0' : '') + hour);
				$minute.text(((minute < 10) ? '0' : '') + minute);
				$second.text(((second < 10) ? '0' : '') + second);
        $hourAMPM.text(hourAMPM);
			}
			else if(o.type === 'analogue') {
				hour_css = 'rotate(' + (hour * 30 + (minute / 2)) + 'deg)';
				minute_css = 'rotate(' + (minute * 6) + 'deg)';
				second_css = 'rotate(' + (second * 6) + 'deg)';
				
				$hour.css('-webkit-transform', hour_css).css('-moz-transform', hour_css).attr('title', ((hour < 10) ? '0' : '') + hour);
				$minute.css('-webkit-transform', minute_css).css('-moz-transform', minute_css).attr('title', ((minute < 10) ? '0' : '') + minute);
				$second.css('-webkit-transform', second_css).css('-moz-transform', second_css).attr('title', ((second < 10) ? '0' : '') + second);
			}

			/**
			* Update the data
			*/
			$date.data('day', day).data('month', month).data('year', year);
			$date.data('hour', hour).data('minute', minute).data('second', second).data('AMPM', hourAMPM);
		};
	});
}
