/**
 * Perform Group Built on top of the jQuery library http://jquery.com
 */
(function($) {

	/**
	 * Creates a match countdown for all matched elements.
	 */

	$.fn.countdown = function(o) {
		return this.each(function() {
			new $cd(this, o);
		});
	};

	// Default configuration properties.
	var defaults = {
		type : "url",
		days : "days",
		hours : "hrs",
		mins : "mins",
		secs : "secs"
	};
	function performTSToDate(timestamp) {
		// timestamp = "20100501150000";
		// functino to convert wierdy datestamp to epoch using regexp
		// input has to be in this format: 20100501150000
		var regex = /^([0-9]{4})([0-1][0-9])([0-3][0-9])([0-2][0-9])([0-5][0-9])([0-5][0-9])?$/;
		var results = regex.exec(timestamp);
		return new Date(results[1], results[2] - 1, results[3], results[4],
				results[5], results[6]);
	}
	$.countdown = function(e, o) {
		this.options = $.extend({}, defaults, o || {});
		options = this.options;
		var matchTimeDate;
		var endDate;
		var endDateVal;

		var localDateTime = new Date();
		var timeZoneOffset = localDateTime.getTimezoneOffset() * 60000;

		if (this.options.endDate == "HTML") {
			endDateVal = $(e).html();
			endDate = performTSToDate($(e).html());
		} else {
			endDateVal = this.options.endDate;
			endDate = performTSToDate(this.options.endDate);
		}

		function createTimer(endDate) {
			var serverTimeDate = new Date();

			mssec = 1000;
			msmin = 60 * mssec;
			mshour = 60 * msmin;
			msday = 24 * mshour;
			msmonth = 24 * msday;

			var timeleft = endDate.getTime()
					- (serverTimeDate.getTime() + timeZoneOffset);
			dayremaining = (timeleft / msday);
			hourremaining = ((timeleft / mshour) % 24);
			minremaining = ((timeleft / msmin) % (60));
			secremaining = ((timeleft / mssec) % (60));

			// countDownTimeDate matchTimeDate serverTimeDate;
			// $(e).html('<div
			// class="days"><span>'+Math.floor(dayremaining)+'</span><p>'+options.days+'</p></div><div
			// class="hours"><span>'+Math.floor(hourremaining)+'</span><p>'+options.hours+'</p></div><div
			// class="minutes"><span>'+Math.floor(minremaining)+'</span><p>'+options.mins+'</p></div><div
			// class="seconds"><span>'+Math.floor(secremaining)+'</span><p>'+options.secs+'</p></div>');
			if (secremaining >= 0) {
				// tweak labels for singular - removed because we can duplicate
				// for arabic content
				/*
				 * if(Math.floor(dayremaining) == 1){ options.days = "day";
				 * }else{ options.days = "days"; } if(Math.floor(hourremaining) ==
				 * 1){ options.hours = "hour"; }else{ options.hours = "hours"; }
				 * if(Math.floor(minremaining) == 1){ options.mins = "min";
				 * }else{ options.mins = "mins"; } if(Math.floor(secremaining) ==
				 * 1){ options.secs = "sec"; }else{ options.secs = "secs"; }
				 */
				
				if(this.options.showDays == 0) {
				$(e).html(
						'<ul class="' + endDateVal
								+ '"><li class="countElmt"><span>'
								+ Math.floor(hourremaining) + '</span> '
								+ options.hours
								+ '</li><li class="countElmt"><span>'
								+ Math.floor(minremaining) + '</span> '
								+ options.mins
								+ '</li><li class="countElmt"><span>'
								+ Math.floor(secremaining) + '</span> '
								+ options.secs + '</li></ul>');
				} else {
					$(e).html(
							'<ul class="' + endDateVal
									+ '"><li class="countElmt"><span>'
									+ Math.floor(dayremaining) + '</span> '
									+ options.days
									+ '</li><li class="countElmt"><span>'
									+ Math.floor(hourremaining) + '</span> '
									+ options.hours
									+ '</li><li class="countElmt"><span>'
									+ Math.floor(minremaining) + '</span> '
									+ options.mins
									+ '</li><li class="countElmt"><span>'
									+ Math.floor(secremaining) + '</span> '
									+ options.secs + '</li></ul>');
				}
			} else {
			if(this.options.showDays == 0) {
				$(e).html(
						'<ul><li class="countElmt"><span>0</span> '
								+ options.hours
								+ '</li><li class="countElmt"><span>0</span> '
								+ options.mins
								+ '</li><li class="countElmt"><span>0</span> '
								+ options.secs + '</li></ul>');
			} else {
				$(e).html(
						'<ul><li class="countElmt"><span>0</span> '
								+ options.days
								+ '</li><li class="countElmt"><span>0</span> '
								+ options.hours
								+ '</li><li class="countElmt"><span>0</span> '
								+ options.mins
								+ '</li><li class="countElmt"><span>0</span> '
								+ options.secs + '</li></ul>');
			}
				// clear interval
				clearInterval(counterid);
				// process callback function
				if (typeof (this.options.zeroCallBack) == 'function') {
					this.options.zeroCallBack.call(this);
				}
			}
		}

		createTimer(endDate);
		var counterid = setInterval(function() {
			createTimer(endDate);
		}, 1000);
	};

	var $cd = $.countdown;

})(jQuery);
