/*
 * eClock
 * Examples and documentation at: http://
 * Version: 1.0.0 (04/07/2011)
 * Copyright © 2011 
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.3+
*/
;(function($) {
	$.fn.eClock = function( options ){
	
		var _this = $(this);
		
		var defaults = {
			total:""
		};
		
		var options = $.extend( {}, defaults, options );
		
		var timer_date = options.total;
		
		$("#daytime_cap").html("<span class='on'>DEAL IS ON</span>");
		
		var different_tostart = ( new Date( options.startson )  ).getTime() - (new Date()).getTime();
		var different_tostart_trigger = different_tostart > 0;
		if( different_tostart_trigger ) {
			timer_date = options.startson;
			_this.addClass("DEAL_STARTS_IN");
			$("#daytime_cap").html("<span>DEAL STARTS IN</span>");
			$("#additems").hide();
			$("#shipsubbt").hide();
		}
		
		var arg = timer_date.split("/");
		var todayDate = new Date();
		var expiresDate = new Date((arg[0]*1), (arg[1]*1-1), (arg[2]*1) );//year, month, date
		var expiresMilliseconds = expiresDate.getTime();
		
		var deltaExpMilliseconds = expiresMilliseconds - todayDate.getTime();
			
		function eClock(){
			
			if( deltaExpMilliseconds < 0 )
			{
				if( different_tostart_trigger )
				{
					$("#additems").fadeIn();
					$("#shipsubbt").fadeIn();
					$("#daytime_cap").html("<span class='on'>DEAL IS ON</span>");
					_this.removeClass("DEAL_STARTS_IN");
			
					timer_date = options.total;
					
					arg = timer_date.split("/");
					todayDate = new Date();
					expiresDate =  new Date((arg[0]*1), (arg[1]*1-1), (arg[2]*1) );
					
					expiresMilliseconds = expiresDate.getTime();
					
					deltaExpMilliseconds = expiresMilliseconds - todayDate.getTime();
					
				
					
					eClock();
				}
				else
				{
					//window.open("http://"+document.domain+"/templates/refresh_deal.asp",'_self');
				}
				
			}
			else
			{
				days=0;hours=0;mins=0;secs=0;out="";
				var tempTotal = Math.floor( deltaExpMilliseconds / 1000 );
				days = Math.floor( tempTotal / 86400 );
				tempTotal = tempTotal % 86400;
				hours = Math.floor( tempTotal / 3600 );
				tempTotal = tempTotal % 3600;
				mins = Math.floor( tempTotal / 60 );
				tempTotal = tempTotal % 60;
				secs = Math.floor( tempTotal );
				if( days != 0 )
				{
					var d = pad(days.toString(),'0').toString();
				}
				if( days != 0 || hours != 0 )
				{
					var h = pad( hours.toString(), '0' ).toString();
				}
				if( days != 0 || hours != 0 || mins != 0 )
				{
					var m = pad( mins.toString(), '0' ).toString();
				}
				
				var s = pad( secs.toString(), '0' ).toString();
				
				if( h == undefined ) h = "00";
				if( m == undefined ) m = "00";
				
				if( days == 0 )
				{
					_this.html("<span>"+h+"</span>:<span>"+m+"</span>:<span>"+s+"</span>");
				}
				else if( days == 1)
				{
					_this.html("<span>"+days+"</span>/<span>"+h+"</span>:<span>"+m+"</span>:<span>"+s+"</span>");
				}
				else
				{
					_this.html("<span>"+days+"</span>/<span>"+h+"</span>:<span>"+m+"</span>:<span>"+s+"</span>");
				}
				
				deltaExpMilliseconds = deltaExpMilliseconds - 1000;
				setTimeout(eClock, 1000);
			}
		}
		
		function pad( val, ch )
		{
			if ( val < 10 )
			return ch + val;
			return val;
		}
		
		eClock();

	};
})(jQuery);

