(function () {
$.fn.SpecTimer = function (options) {
return this.each(function () {
new $.SpecTimer(this, options);
});
}
$.SpecTimer = function (obj, options) {
this.options = $.extend({
formatStyle: '1',
autoStart: false,
LeadingZero: true,
OutputHTML: " ",
TextEnded: " ",
CountActive: true,
DateEnd: null
}, options || {});
if (this.options.DateEnd == null || this.options.DateEnd == '') {
return;
}
this.timer = null;
this.element = obj;
this.CountStepper = -1;
this.CountStepper = Math.ceil(this.CountStepper);
this.SetTimeOutPeriod = (Math.abs(this.CountStepper) - 1) * 1000 + 990;
var dthen = new Date(this.options.DateEnd);
var dnow = new Date();
if (this.CountStepper > 0) {
ddiff = new Date(dnow - dthen);
} else {
ddiff = new Date(dthen - dnow);
}
gsecs = Math.floor(ddiff.valueOf() / 1000);
this.CountBack(gsecs, this);
};
$.SpecTimer.fn = $.SpecTimer.prototype;
$.SpecTimer.fn.extend = $.SpecTimer.extend = $.extend;
$.SpecTimer.fn.extend({
calculateDate: function (secs, num1, num2) {
var s = ((Math.floor(secs / num1)) % num2).toString();
if (this.options.LeadingZero && s.length < 2) {
s = "" + s;
}
if (this.options.formatStyle == 1)return "" + s + ""; else if (this.options.formatStyle == 2) {
var n = s.split("");
return "<div class=\"timer_num\"><span>" + n[0] + "</span><span>" + n[1] + "</span></div>";
}
}, CountBack: function (secs, self) {
if (secs < 0) {
$elem = $(self.element).parent();
$elem.addClass('ended');
$elem.empty();
$elem.prepend('<div>' + self.options.TextEnded + '</div>');
OutputStr = self.options.OutputHTML.replace(/%%D%%/g, '00');
OutputStr = OutputStr.replace(/%%H%%/g, '00');
OutputStr = OutputStr.replace(/%%M%%/g, '00');
OutputStr = OutputStr.replace(/%%S%%/g, '00');
self.element.innerHTML = OutputStr;
return;
} else if (isNaN(secs) == true) {
$elem = $(self.element).parent();
$elem.addClass('ended');
$elem.empty();
$elem.prepend('<div>' + self.options.TextEnded + '</div>');
OutputStr = self.options.OutputHTML.replace(/%%D%%/g, '00');
OutputStr = OutputStr.replace(/%%H%%/g, '00');
OutputStr = OutputStr.replace(/%%M%%/g, '00');
OutputStr = OutputStr.replace(/%%S%%/g, '00');
self.element.innerHTML = OutputStr;
return;
}
clearInterval(self.timer);
OutputStr = self.options.OutputHTML.replace(/%%D%%/g, self.calculateDate(secs, 86400, 100000));
OutputStr = OutputStr.replace(/%%H%%/g, self.calculateDate(secs, 3600, 24));
OutputStr = OutputStr.replace(/%%M%%/g, self.calculateDate(secs, 60, 60));
OutputStr = OutputStr.replace(/%%S%%/g, self.calculateDate(secs, 1, 60));
self.element.innerHTML = OutputStr;
if (self.options.CountActive) {
self.timer = null;
self.timer = setTimeout(function () {
self.CountBack((secs + self.CountStepper), self);
}, (self.SetTimeOutPeriod));
}
}
})
})(jQuery)