/*
	This plugin modified by Mewsoft.
*/

/* The Great Cornholio Countdown, version: 1.00 (October 14th, 2010)
 * Copyright (c) 2010 Lecho Buszczynski
 * lecho@phatcat.eu
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Requires: jQuery v?.?

 Very simple countdown, all it does is it transforms a sql timestamp (YYYYMMDDHHMMSS) into a countdown timer. I needed something very basic so 
 if you need fancy styling or effects look for other countdown scripts :]

initaite:
  $('#counter').Countdown();

settings (optional):

format: 'html/text [D] = days, [H] = hours, [M] = minutes, [S] = seconds',
warnformat: 'html/text to show on warntime',
expiredmsg: 'html/text to show when the counter has expired',
interval: int microseconds - refresh every X microseconds,
warntime: int seconds (shows warnformat html if less seconds is left to the end of the countdown)
onComplete: code, run this code on counter complete event
ifExpired: bool 1/0 call onComplete code if called on expired counter

example (also these are the default settings):
   $('#counter').Countdown({

       format: '[D] days, [H]:[M]:[S]',
       warnformat: '<span style="color: #F00;">[D] days, [H]:[M]:[S]</span>',
       expiredmsg: '<span style="color: #F00;">expired</span>',
       interval: 1000,
       warntime: 60  
  
   });

Example YYYYMMDDHHMMSS: 
Till X-MASS 2011 countdown:<br />
<div id="countdown-2">20111225000000</div>

<script type="text/javascript">
	jQuery(document).ready(function() {
	    $('#countdown-1').Countdown();
	    $('#countdown-2').Countdown();
	});

</script>


 */
 
(function($) {
	//-------------------------------------------------------------------------
	$.fn.Countdown = function(options) {

		var settings	= {
									format: 'xxxx [D] days, [H]:[M]:[S]',
									warnformat: '', //'<span style="color: #F00;">[D] days, [H]:[M]:[S]</span>',
									expiredmsg: '', //'<span style="color: #ff0000;">Expired</span>',
									interval: 1000,
									warntime: 0,
									timeleft: 0, // time left is seconds
									timestamp: 0, // time in seconds
									timestr: '',
									ifExpired: 0,
									onComplete: null
								};
		
		$.extend(settings, options);

		var target		= $(this);
		var stamp		= $(this).html();
		var date_e		= false;
		var executer	= false;
		//---------------------------------------------------------------------
		var getSecondsDiff = function() {
			var secs	= 0;
			var now	= new Date();
			secs = date_e.getTime() - now.getTime();
			return (secs/1000);
		}
		//---------------------------------------------------------------------
		var getFormattedDiff = function() {
		
			var secs	= getSecondsDiff();
			var counter	= '';
			var val		= '';
			
			if (secs < 0) {
				counterStop();
				target.html(settings.expiredmsg);
				if (settings.onComplete !=null)
				{
					eval(settings.onComplete);
				}
				
				return settings.expiredmsg;

			} else if (settings.warntime > 0 && settings.warnformat && secs < settings.warntime) {
				counter = settings.warnformat;
			} else {
				counter = settings.format;
			}
			
			val = new String(Math.floor(secs/86400));
			
			counter = counter.replace('[D]',val);			

			secs = secs % 86400;
			
			val = new String(Math.floor(secs/3600));
			
			if (val.length == 1) {
				val = '0' + val;
			}
			
			counter = counter.replace('[H]',val);
			
			secs = secs % 3600;

			val = new String(Math.floor(secs/60));

			if (val.length == 1) {
				val = '0' + val;
			}
			
			counter = counter.replace('[M]',val);

			secs = secs % 60;
			
			val = new String(Math.ceil(secs));
			
			if (val.length == 1) {
				val = '0' + val;
			}
			
			counter = counter.replace('[S]',val);
			return counter;
		}
		//---------------------------------------------------------------------
		var counterUpdate = function() {
			target.html(getFormattedDiff());
		}
		//---------------------------------------------------------------------
		var counterStart = function() {
			//counterUpdate();
			if (getSecondsDiff() > 0)
			{
				executer = setInterval(function() {counterUpdate()},settings.interval);
			}
			else {
				if (settings.ifExpired)
				{
					counterUpdate();
				}
				else {
					target.html(settings.expiredmsg);
				}
			}
		}
		//---------------------------------------------------------------------
		var counterStop = function() {
			clearInterval(executer);
		}
		//---------------------------------------------------------------------
		// add the client GMT difference to the count down value.
		var dt = new Date();
		//The getTimezoneOffset() method returns the time difference between Greenwich Mean Time (GMT) and local time, in minutes
		var gmt = dt.getTimezoneOffset() * 60000; //offset in seconds

		if (settings.timeleft > 0)
		{
			//alert(settings.timeleft);
			var today = new Date();
			var timems = today.getTime() + settings.timeleft * 1000;
			date_e = new Date(timems);
			counterStart();
		}
		else if (settings.timestamp > 0)
		{
			date_e = new Date(parseInt(settings.timestamp)*1000 + gmt);
			counterStart();
		}
		else if (settings.timestr > 0)
		{
			stamp = settings.timestr;
			date_e = new Date(stamp.substr(0,4),(stamp.substr(4,2)- 1),stamp.substr(6,2),stamp.substr(8,2),stamp.substr(10,2),stamp.substr(12,2),0);
			date_e = new Date(date_e.getTime() + gmt);
			counterStart();
		}
		else if (/^[0-9]{14}$/.test(stamp)) {
			date_e = new Date(stamp.substr(0,4),(stamp.substr(4,2)- 1),stamp.substr(6,2),stamp.substr(8,2),stamp.substr(10,2),stamp.substr(12,2),0);
			counterStart();
			date_e = new Date(date_e.getTime() + gmt);
		}
		else
		{
			date_e = new Date();
			counterStart();
		}
		//---------------------------------------------------------------------
	}; // $.fn.Countdown

})(jQuery);
