(function($){
	$.fn.countClock = function(options){
		// extend our default options with those provided
		var $options = $.extend({}, $.fn.countClock.defaults, options);
		var $days,$hrs,$mins,$secs;
		var nDays = nHrs = nMins = nSecs = 0;
		var nEndZero = ((60*(24*-1))+4);
		
		return this.each(function() {
			var $this = $(this);
			// ~~~~~ cache elements ~~~~~
			$days = $this.find('.days span');
			$hrs = $this.find('.hours span');
			$mins = $this.find('.minutes span');
			$secs = $this.find('.seconds span');
			
			switch($options.data.TimeZone.toUpperCase()){
				case "ET": $options.nZone = -5; break;
				case "CT": $options.nZone = -6; break;
				case "MT": $options.nZone = -7; break;
				case "PT": $options.nZone = -8; break;
			};
			
			// ~~~~~ init ~~~~~
			display();
			countdown();
		});
		
		function display(){
			switch($options.step++){
				case 1:
					$options.date = $options.data["Date"+$options.data.Count].StartDate;
					$options.type = "onClock";
					$options.text = "";
					break;
				case 2:
					$options.date = $options.data["Date"+$options.data.Count].EndDate;
					$options.type = "onNow";
					$options.text = $options.data["Date"+$options.data.Count].OnNowMsg;
					break;
				case 3:
					$options.type = "onFinish"
					$options.text = $options.data["Date"+$options.data.Count].OnFinish;
					if($options.data.Count < $options.data.Total){
						$options.data.Count++;
						$options.step = 1;
						$.fn.countClock.display();
					}
					break;
			};
			
			// ~~~~~ position ~~~~~
			//$days.css({backgroundPosition:'4px '+ ((60*(24*-1))+4) +'px'});
			
			// ~~~~~ make it tick ~~~~~
			$options.periodical = window.setInterval(countdown, 1000);
		};
		
		function countdown(){
			$options.today = new Date();
			// ===== Reset Timezone ============================
			//var utc = this.today.getTime() + (this.today.getTimezoneOffset() * (60*1000));
			var utc = $options.today.getTime() + (300 * (60*1000));
			var now = new Date(utc+((60*60*1000)*$.offset($options)));
			// ===== Format Date ===============================           
			//Find the difference, and convert that into seconds.                  
			var nTime = Math.round(($options.date.getTime() - now.getTime()) / 1000);
			if(nTime > -1) { // still counting
				if($options.type == "onClock"){
					var days = Math.floor(nTime / (60 * 60 * 24));
					nTime %= (60 * 60 * 24);
					var hours = Math.floor(nTime / (60 * 60));
					nTime %= (60 * 60);
					var minutes = Math.floor(nTime / 60);
					nTime %= 60;
					var seconds = nTime;
					// ~~~~~ position numbers ~~~~~
					if(nDays != days){
						$days.animate({backgroundPosition:'(4px '+ ((days*(24*-1))+4) +'px)'});
					};
					if(nHrs != hours){
						$hrs.animate({backgroundPosition:'(4px '+ ((hours*(24*-1))+4) +'px)'},
							function(){ if(hours == 0) $hrs.css({backgroundPosition:'4px '+ nEndZero +'px'}) }
						);
					};
					if(nMins != minutes){
						$mins.animate({backgroundPosition:'(4px '+ ((minutes*(24*-1))+4) +'px)'},
							function(){ if(minutes == 0) $mins.css({backgroundPosition:'4px '+ nEndZero +'px'}) }
						);
					};
					if(nSecs != seconds){
						$secs.animate({backgroundPosition:'(4px '+ ((seconds*(24*-1))+4) +'px)'},
							function(){ if(seconds == 0) $secs.css({backgroundPosition:'4px '+ nEndZero +'px'}) }
						);
					};
					nDays = days;
					nHrs = hours;
					nMins = minutes;
					nSecs = seconds;
				} else if(this.type == "onNow"){
					//this.el.setHTML(this.text);
				};
			} else {
				clearInterval($options.periodical);
				if($options.type != "onFinish"){
					display();
				} else {
					if($options.text != ""){
						//this.el.setHTML('<em>'+this.text+'<em>');
					}
				}
			}
		};
	};
	
	$.offset = function($option){
		var thisYear = $option.today.getFullYear();
		var MarchDate = 14 - (Math.floor (1 + thisYear * 5 / 4) % 7);
		var NovemberDate = 7 - (Math.floor (1 + thisYear * 5 / 4) % 7);
		var startDate = new Date(thisYear,2,MarchDate);
		var endDate = new Date(thisYear,10,NovemberDate);
		return ($option.today.getTime() > startDate.getTime() && $option.today.getTime() < endDate.getTime())? $option.nZone+1 : $option.nZone;
	};
	
	$.addZeros = function(digits){
		return (Number(digits) < 10) ? "0"+digits : digits;
	};
	
	// default option
	$.fn.countClock.defaults = {
		data: {},
		step: 1,
		count: 0,
		text: '',
		date: '',
		nZone: -6,
		today: new Date(),
		type: 'onClock',
		periodical: null
	};
})(jQuery);

jQuery(function($){
	if($('#RolexCountdown').length){
		var Event = {
			Total : 1,
			Count : 1,
			TimeZone : "CT",
			Date1 : {
				StartDate	: new Date("11/19/2009 07:00"),
				EndDate		: new Date("11/19/2009 07:00"),
				OnNowMsg 	: "",
				OnFinish	: ""
			}
		};
		$('#RolexCountdown').countClock({data:Event});
	}
});