Advertisement
aligatro

js countdown

Dec 28th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* http://keith-wood.name/countdown.html
  2.    Countdown for jQuery v1.6.2.
  3.    Written by Keith Wood (kbwood{at}iinet.com.au) January 2008.
  4.    Available under the MIT (https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt) license.
  5.    Please attribute the author if you use it. */
  6. (function ($) {
  7.     function Countdown() {
  8.         this.regional = [];
  9.         this.regional[''] = {
  10.             labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'],
  11.             labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'],
  12.             compactLabels: ['y', 'm', 'w', 'd'],
  13.             whichLabels: null,
  14.             digits: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
  15.             timeSeparator: ':',
  16.             isRTL: false
  17.         };
  18.         this._defaults = {
  19.             until: null,
  20.             since: null,
  21.             timezone: null,
  22.             serverSync: null,
  23.             format: 'dHMS',
  24.             layout: '',
  25.             compact: false,
  26.             significant: 0,
  27.             description: '',
  28.             expiryUrl: '',
  29.             expiryText: '',
  30.             alwaysExpire: false,
  31.             onExpiry: null,
  32.             onTick: null,
  33.             tickInterval: 1
  34.         };
  35.         $.extend(this._defaults, this.regional['']);
  36.         this._serverSyncs = [];
  37.  
  38.         function timerCallBack(a) {
  39.             var b = (a < 1e12 ? (b = performance.now ? (performance.now() + performance.timing.navigationStart) : Date.now()) : a || new Date().getTime());
  40.             if (b - d >= 1000) {
  41.                 x._updateTargets();
  42.                 d = b
  43.             }
  44.             c(timerCallBack)
  45.         }
  46.         var c = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || null;
  47.         var d = 0;
  48.         if (!c || $.noRequestAnimationFrame) {
  49.             $.noRequestAnimationFrame = null;
  50.             setInterval(function () {
  51.                 x._updateTargets()
  52.             }, 980)
  53.         } else {
  54.             d = window.animationStartTime || window.webkitAnimationStartTime || window.mozAnimationStartTime || window.oAnimationStartTime || window.msAnimationStartTime || new Date().getTime();
  55.             c(timerCallBack)
  56.         }
  57.     }
  58.     var Y = 0;
  59.     var O = 1;
  60.     var W = 2;
  61.     var D = 3;
  62.     var H = 4;
  63.     var M = 5;
  64.     var S = 6;
  65.     $.extend(Countdown.prototype, {
  66.         markerClassName: 'hasCountdown',
  67.         propertyName: 'countdown',
  68.         _rtlClass: 'countdown_rtl',
  69.         _sectionClass: 'countdown_section',
  70.         _amountClass: 'countdown_amount',
  71.         _rowClass: 'countdown_row',
  72.         _holdingClass: 'countdown_holding',
  73.         _showClass: 'countdown_show',
  74.         _descrClass: 'countdown_descr',
  75.         _timerTargets: [],
  76.         setDefaults: function (a) {
  77.             this._resetExtraLabels(this._defaults, a);
  78.             $.extend(this._defaults, a || {})
  79.         },
  80.         UTCDate: function (a, b, c, e, f, g, h, i) {
  81.             if (typeof b == 'object' && b.constructor == Date) {
  82.                 i = b.getMilliseconds();
  83.                 h = b.getSeconds();
  84.                 g = b.getMinutes();
  85.                 f = b.getHours();
  86.                 e = b.getDate();
  87.                 c = b.getMonth();
  88.                 b = b.getFullYear()
  89.             }
  90.             var d = new Date();
  91.             d.setUTCFullYear(b);
  92.             d.setUTCDate(1);
  93.             d.setUTCMonth(c || 0);
  94.             d.setUTCDate(e || 1);
  95.             d.setUTCHours(f || 0);
  96.             d.setUTCMinutes((g || 0) - (Math.abs(a) < 30 ? a * 60 : a));
  97.             d.setUTCSeconds(h || 0);
  98.             d.setUTCMilliseconds(i || 0);
  99.             return d
  100.         },
  101.         periodsToSeconds: function (a) {
  102.             return a[0] * 31557600 + a[1] * 2629800 + a[2] * 604800 + a[3] * 86400 + a[4] * 3600 + a[5] * 60 + a[6]
  103.         },
  104.         _attachPlugin: function (a, b) {
  105.             a = $(a);
  106.             if (a.hasClass(this.markerClassName)) {
  107.                 return
  108.             }
  109.             var c = {
  110.                 options: $.extend({}, this._defaults),
  111.                 _periods: [0, 0, 0, 0, 0, 0, 0]
  112.             };
  113.             a.addClass(this.markerClassName).data(this.propertyName, c);
  114.             this._optionPlugin(a, b)
  115.         },
  116.         _addTarget: function (a) {
  117.             if (!this._hasTarget(a)) {
  118.                 this._timerTargets.push(a)
  119.             }
  120.         },
  121.         _hasTarget: function (a) {
  122.             return ($.inArray(a, this._timerTargets) > -1)
  123.         },
  124.         _removeTarget: function (b) {
  125.             this._timerTargets = $.map(this._timerTargets, function (a) {
  126.                 return (a == b ? null : a)
  127.             })
  128.         },
  129.         _updateTargets: function () {
  130.             for (var i = this._timerTargets.length - 1; i >= 0; i--) {
  131.                 this._updateCountdown(this._timerTargets[i])
  132.             }
  133.         },
  134.         _optionPlugin: function (a, b, c) {
  135.             a = $(a);
  136.             var d = a.data(this.propertyName);
  137.             if (!b || (typeof b == 'string' && c == null)) {
  138.                 var e = b;
  139.                 b = (d || {}).options;
  140.                 return (b && e ? b[e] : b)
  141.             }
  142.             if (!a.hasClass(this.markerClassName)) {
  143.                 return
  144.             }
  145.             b = b || {};
  146.             if (typeof b == 'string') {
  147.                 var e = b;
  148.                 b = {};
  149.                 b[e] = c
  150.             }
  151.             this._resetExtraLabels(d.options, b);
  152.             var f = (d.options.timezone != b.timezone);
  153.             $.extend(d.options, b);
  154.             this._adjustSettings(a, d, b.until != null || b.since != null || f);
  155.             var g = new Date();
  156.             if ((d._since && d._since < g) || (d._until && d._until > g)) {
  157.                 this._addTarget(a[0])
  158.             }
  159.             this._updateCountdown(a, d)
  160.         },
  161.         _updateCountdown: function (a, b) {
  162.             var c = $(a);
  163.             b = b || c.data(this.propertyName);
  164.             if (!b) {
  165.                 return
  166.             }
  167.             c.html(this._generateHTML(b)).toggleClass(this._rtlClass, b.options.isRTL);
  168.             if ($.isFunction(b.options.onTick)) {
  169.                 var d = b._hold != 'lap' ? b._periods : this._calculatePeriods(b, b._show, b.options.significant, new Date());
  170.                 if (b.options.tickInterval == 1 || this.periodsToSeconds(d) % b.options.tickInterval == 0) {
  171.                     b.options.onTick.apply(a, [d])
  172.                 }
  173.             }
  174.             var e = b._hold != 'pause' && (b._since ? b._now.getTime() < b._since.getTime() : b._now.getTime() >= b._until.getTime());
  175.             if (e && !b._expiring) {
  176.                 b._expiring = true;
  177.                 if (this._hasTarget(a) || b.options.alwaysExpire) {
  178.                     this._removeTarget(a);
  179.                     if ($.isFunction(b.options.onExpiry)) {
  180.                         b.options.onExpiry.apply(a, [])
  181.                     }
  182.                     if (b.options.expiryText) {
  183.                         var f = b.options.layout;
  184.                         b.options.layout = b.options.expiryText;
  185.                         this._updateCountdown(a, b);
  186.                         b.options.layout = f
  187.                     }
  188.                     if (b.options.expiryUrl) {
  189.                         window.location = b.options.expiryUrl
  190.                     }
  191.                 }
  192.                 b._expiring = false
  193.             } else if (b._hold == 'pause') {
  194.                 this._removeTarget(a)
  195.             }
  196.             c.data(this.propertyName, b)
  197.         },
  198.         _resetExtraLabels: function (a, b) {
  199.             var c = false;
  200.             for (var n in b) {
  201.                 if (n != 'whichLabels' && n.match(/[Ll]abels/)) {
  202.                     c = true;
  203.                     break
  204.                 }
  205.             }
  206.             if (c) {
  207.                 for (var n in a) {
  208.                     if (n.match(/[Ll]abels[02-9]|compactLabels1/)) {
  209.                         a[n] = null
  210.                     }
  211.                 }
  212.             }
  213.         },
  214.         _adjustSettings: function (a, b, c) {
  215.             var d;
  216.             var e = 0;
  217.             var f = null;
  218.             for (var i = 0; i < this._serverSyncs.length; i++) {
  219.                 if (this._serverSyncs[i][0] == b.options.serverSync) {
  220.                     f = this._serverSyncs[i][1];
  221.                     break
  222.                 }
  223.             }
  224.             if (f != null) {
  225.                 e = (b.options.serverSync ? f : 0);
  226.                 d = new Date()
  227.             } else {
  228.                 var g = ($.isFunction(b.options.serverSync) ? b.options.serverSync.apply(a, []) : null);
  229.                 d = new Date();
  230.                 e = (g ? d.getTime() - g.getTime() : 0);
  231.                 this._serverSyncs.push([b.options.serverSync, e])
  232.             }
  233.             var h = b.options.timezone;
  234.             h = (h == null ? -d.getTimezoneOffset() : h);
  235.             if (c || (!c && b._until == null && b._since == null)) {
  236.                 b._since = b.options.since;
  237.                 if (b._since != null) {
  238.                     b._since = this.UTCDate(h, this._determineTime(b._since, null));
  239.                     if (b._since && e) {
  240.                         b._since.setMilliseconds(b._since.getMilliseconds() + e)
  241.                     }
  242.                 }
  243.                 b._until = this.UTCDate(h, this._determineTime(b.options.until, d));
  244.                 if (e) {
  245.                     b._until.setMilliseconds(b._until.getMilliseconds() + e)
  246.                 }
  247.             }
  248.             b._show = this._determineShow(b)
  249.         },
  250.         _destroyPlugin: function (a) {
  251.             a = $(a);
  252.             if (!a.hasClass(this.markerClassName)) {
  253.                 return
  254.             }
  255.             this._removeTarget(a[0]);
  256.             a.removeClass(this.markerClassName).empty().removeData(this.propertyName)
  257.         },
  258.         _pausePlugin: function (a) {
  259.             this._hold(a, 'pause')
  260.         },
  261.         _lapPlugin: function (a) {
  262.             this._hold(a, 'lap')
  263.         },
  264.         _resumePlugin: function (a) {
  265.             this._hold(a, null)
  266.         },
  267.         _hold: function (a, b) {
  268.             var c = $.data(a, this.propertyName);
  269.             if (c) {
  270.                 if (c._hold == 'pause' && !b) {
  271.                     c._periods = c._savePeriods;
  272.                     var d = (c._since ? '-' : '+');
  273.                     c[c._since ? '_since' : '_until'] = this._determineTime(d + c._periods[0] + 'y' + d + c._periods[1] + 'o' + d + c._periods[2] + 'w' + d + c._periods[3] + 'd' + d + c._periods[4] + 'h' + d + c._periods[5] + 'm' + d + c._periods[6] + 's');
  274.                     this._addTarget(a)
  275.                 }
  276.                 c._hold = b;
  277.                 c._savePeriods = (b == 'pause' ? c._periods : null);
  278.                 $.data(a, this.propertyName, c);
  279.                 this._updateCountdown(a, c)
  280.             }
  281.         },
  282.         _getTimesPlugin: function (a) {
  283.             var b = $.data(a, this.propertyName);
  284.             return (!b ? null : (b._hold == 'pause' ? b._savePeriods : (!b._hold ? b._periods : this._calculatePeriods(b, b._show, b.options.significant, new Date()))))
  285.         },
  286.         _determineTime: function (k, l) {
  287.             var m = function (a) {
  288.                 var b = new Date();
  289.                 b.setTime(b.getTime() + a * 1000);
  290.                 return b
  291.             };
  292.             var n = function (a) {
  293.                 a = a.toLowerCase();
  294.                 var b = new Date();
  295.                 var c = b.getFullYear();
  296.                 var d = b.getMonth();
  297.                 var e = b.getDate();
  298.                 var f = b.getHours();
  299.                 var g = b.getMinutes();
  300.                 var h = b.getSeconds();
  301.                 var i = /([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g;
  302.                 var j = i.exec(a);
  303.                 while (j) {
  304.                     switch (j[2] || 's') {
  305.                     case 's':
  306.                         h += parseInt(j[1], 10);
  307.                         break;
  308.                     case 'm':
  309.                         g += parseInt(j[1], 10);
  310.                         break;
  311.                     case 'h':
  312.                         f += parseInt(j[1], 10);
  313.                         break;
  314.                     case 'd':
  315.                         e += parseInt(j[1], 10);
  316.                         break;
  317.                     case 'w':
  318.                         e += parseInt(j[1], 10) * 7;
  319.                         break;
  320.                     case 'o':
  321.                         d += parseInt(j[1], 10);
  322.                         e = Math.min(e, x._getDaysInMonth(c, d));
  323.                         break;
  324.                     case 'y':
  325.                         c += parseInt(j[1], 10);
  326.                         e = Math.min(e, x._getDaysInMonth(c, d));
  327.                         break
  328.                     }
  329.                     j = i.exec(a)
  330.                 }
  331.                 return new Date(c, d, e, f, g, h, 0)
  332.             };
  333.             var o = (k == null ? l : (typeof k == 'string' ? n(k) : (typeof k == 'number' ? m(k) : k)));
  334.             if (o) o.setMilliseconds(0);
  335.             return o
  336.         },
  337.         _getDaysInMonth: function (a, b) {
  338.             return 32 - new Date(a, b, 32).getDate()
  339.         },
  340.         _normalLabels: function (a) {
  341.             return a
  342.         },
  343.         _generateHTML: function (c) {
  344.             var d = this;
  345.             c._periods = (c._hold ? c._periods : this._calculatePeriods(c, c._show, c.options.significant, new Date()));
  346.             var e = false;
  347.             var f = 0;
  348.             var g = c.options.significant;
  349.             var h = $.extend({}, c._show);
  350.             for (var i = Y; i <= S; i++) {
  351.                 e |= (c._show[i] == '?' && c._periods[i] > 0);
  352.                 h[i] = (c._show[i] == '?' && !e ? null : c._show[i]);
  353.                 f += (h[i] ? 1 : 0);
  354.                 g -= (c._periods[i] > 0 ? 1 : 0)
  355.             }
  356.             var j = [false, false, false, false, false, false, false];
  357.             for (var i = S; i >= Y; i--) {
  358.                 if (c._show[i]) {
  359.                     if (c._periods[i]) {
  360.                         j[i] = true
  361.                     } else {
  362.                         j[i] = g > 0;
  363.                         g--
  364.                     }
  365.                 }
  366.             }
  367.             var k = (c.options.compact ? c.options.compactLabels : c.options.labels);
  368.             var l = c.options.whichLabels || this._normalLabels;
  369.             var m = function (a) {
  370.                 var b = c.options['compactLabels' + l(c._periods[a])];
  371.                 return (h[a] ? d._translateDigits(c, c._periods[a]) + (b ? b[a] : k[a]) + ' ' : '')
  372.             };
  373.             var n = function (a) {
  374.                 var b = c.options['labels' + l(c._periods[a])];
  375.                 return ((!c.options.significant && h[a]) || (c.options.significant && j[a]) ? '<span class="' + x._sectionClass + '">' + '<span class="' + x._amountClass + '">' + d._translateDigits(c, c._periods[a]) + '</span><br/>' + (b ? b[a] : k[a]) + '</span>' : '')
  376.             };
  377.             return (c.options.layout ? this._buildLayout(c, h, c.options.layout, c.options.compact, c.options.significant, j) : ((c.options.compact ? '<span class="' + this._rowClass + ' ' + this._amountClass + (c._hold ? ' ' + this._holdingClass : '') + '">' + m(Y) + m(O) + m(W) + m(D) + (h[H] ? this._minDigits(c, c._periods[H], 2) : '') + (h[M] ? (h[H] ? c.options.timeSeparator : '') + this._minDigits(c, c._periods[M], 2) : '') + (h[S] ? (h[H] || h[M] ? c.options.timeSeparator : '') + this._minDigits(c, c._periods[S], 2) : '') : '<span class="' + this._rowClass + ' ' + this._showClass + (c.options.significant || f) + (c._hold ? ' ' + this._holdingClass : '') + '">' + n(Y) + n(O) + n(W) + n(D) + n(H) + n(M) + n(S)) + '</span>' + (c.options.description ? '<span class="' + this._rowClass + ' ' + this._descrClass + '">' + c.options.description + '</span>' : '')))
  378.         },
  379.         _buildLayout: function (c, d, e, f, g, h) {
  380.             var j = c.options[f ? 'compactLabels' : 'labels'];
  381.             var k = c.options.whichLabels || this._normalLabels;
  382.             var l = function (a) {
  383.                 return (c.options[(f ? 'compactLabels' : 'labels') + k(c._periods[a])] || j)[a]
  384.             };
  385.             var m = function (a, b) {
  386.                 return c.options.digits[Math.floor(a / b) % 10]
  387.             };
  388.             var o = {
  389.                 desc: c.options.description,
  390.                 sep: c.options.timeSeparator,
  391.                 yl: l(Y),
  392.                 yn: this._minDigits(c, c._periods[Y], 1),
  393.                 ynn: this._minDigits(c, c._periods[Y], 2),
  394.                 ynnn: this._minDigits(c, c._periods[Y], 3),
  395.                 y1: m(c._periods[Y], 1),
  396.                 y10: m(c._periods[Y], 10),
  397.                 y100: m(c._periods[Y], 100),
  398.                 y1000: m(c._periods[Y], 1000),
  399.                 ol: l(O),
  400.                 on: this._minDigits(c, c._periods[O], 1),
  401.                 onn: this._minDigits(c, c._periods[O], 2),
  402.                 onnn: this._minDigits(c, c._periods[O], 3),
  403.                 o1: m(c._periods[O], 1),
  404.                 o10: m(c._periods[O], 10),
  405.                 o100: m(c._periods[O], 100),
  406.                 o1000: m(c._periods[O], 1000),
  407.                 wl: l(W),
  408.                 wn: this._minDigits(c, c._periods[W], 1),
  409.                 wnn: this._minDigits(c, c._periods[W], 2),
  410.                 wnnn: this._minDigits(c, c._periods[W], 3),
  411.                 w1: m(c._periods[W], 1),
  412.                 w10: m(c._periods[W], 10),
  413.                 w100: m(c._periods[W], 100),
  414.                 w1000: m(c._periods[W], 1000),
  415.                 dl: l(D),
  416.                 dn: this._minDigits(c, c._periods[D], 1),
  417.                 dnn: this._minDigits(c, c._periods[D], 2),
  418.                 dnnn: this._minDigits(c, c._periods[D], 3),
  419.                 d1: m(c._periods[D], 1),
  420.                 d10: m(c._periods[D], 10),
  421.                 d100: m(c._periods[D], 100),
  422.                 d1000: m(c._periods[D], 1000),
  423.                 hl: l(H),
  424.                 hn: this._minDigits(c, c._periods[H], 1),
  425.                 hnn: this._minDigits(c, c._periods[H], 2),
  426.                 hnnn: this._minDigits(c, c._periods[H], 3),
  427.                 h1: m(c._periods[H], 1),
  428.                 h10: m(c._periods[H], 10),
  429.                 h100: m(c._periods[H], 100),
  430.                 h1000: m(c._periods[H], 1000),
  431.                 ml: l(M),
  432.                 mn: this._minDigits(c, c._periods[M], 1),
  433.                 mnn: this._minDigits(c, c._periods[M], 2),
  434.                 mnnn: this._minDigits(c, c._periods[M], 3),
  435.                 m1: m(c._periods[M], 1),
  436.                 m10: m(c._periods[M], 10),
  437.                 m100: m(c._periods[M], 100),
  438.                 m1000: m(c._periods[M], 1000),
  439.                 sl: l(S),
  440.                 sn: this._minDigits(c, c._periods[S], 1),
  441.                 snn: this._minDigits(c, c._periods[S], 2),
  442.                 snnn: this._minDigits(c, c._periods[S], 3),
  443.                 s1: m(c._periods[S], 1),
  444.                 s10: m(c._periods[S], 10),
  445.                 s100: m(c._periods[S], 100),
  446.                 s1000: m(c._periods[S], 1000)
  447.             };
  448.             var p = e;
  449.             for (var i = Y; i <= S; i++) {
  450.                 var q = 'yowdhms'.charAt(i);
  451.                 var r = new RegExp('\\{' + q + '<\\}(.*)\\{' + q + '>\\}', 'g');
  452.                 p = p.replace(r, ((!g && d[i]) || (g && h[i]) ? '$1' : ''))
  453.             }
  454.             $.each(o, function (n, v) {
  455.                 var a = new RegExp('\\{' + n + '\\}', 'g');
  456.                 p = p.replace(a, v)
  457.             });
  458.             return p
  459.         },
  460.         _minDigits: function (a, b, c) {
  461.             b = '' + b;
  462.             if (b.length >= c) {
  463.                 return this._translateDigits(a, b)
  464.             }
  465.             b = '0000000000' + b;
  466.             return this._translateDigits(a, b.substr(b.length - c))
  467.         },
  468.         _translateDigits: function (b, c) {
  469.             return ('' + c).replace(/[0-9]/g, function (a) {
  470.                 return b.options.digits[a]
  471.             })
  472.         },
  473.         _determineShow: function (a) {
  474.             var b = a.options.format;
  475.             var c = [];
  476.             c[Y] = (b.match('y') ? '?' : (b.match('Y') ? '!' : null));
  477.             c[O] = (b.match('o') ? '?' : (b.match('O') ? '!' : null));
  478.             c[W] = (b.match('w') ? '?' : (b.match('W') ? '!' : null));
  479.             c[D] = (b.match('d') ? '?' : (b.match('D') ? '!' : null));
  480.             c[H] = (b.match('h') ? '?' : (b.match('H') ? '!' : null));
  481.             c[M] = (b.match('m') ? '?' : (b.match('M') ? '!' : null));
  482.             c[S] = (b.match('s') ? '?' : (b.match('S') ? '!' : null));
  483.             return c
  484.         },
  485.         _calculatePeriods: function (c, d, e, f) {
  486.             c._now = f;
  487.             c._now.setMilliseconds(0);
  488.             var g = new Date(c._now.getTime());
  489.             if (c._since) {
  490.                 if (f.getTime() < c._since.getTime()) {
  491.                     c._now = f = g
  492.                 } else {
  493.                     f = c._since
  494.                 }
  495.             } else {
  496.                 g.setTime(c._until.getTime());
  497.                 if (f.getTime() > c._until.getTime()) {
  498.                     c._now = f = g
  499.                 }
  500.             }
  501.             var h = [0, 0, 0, 0, 0, 0, 0];
  502.             if (d[Y] || d[O]) {
  503.                 var i = x._getDaysInMonth(f.getFullYear(), f.getMonth());
  504.                 var j = x._getDaysInMonth(g.getFullYear(), g.getMonth());
  505.                 var k = (g.getDate() == f.getDate() || (g.getDate() >= Math.min(i, j) && f.getDate() >= Math.min(i, j)));
  506.                 var l = function (a) {
  507.                     return (a.getHours() * 60 + a.getMinutes()) * 60 + a.getSeconds()
  508.                 };
  509.                 var m = Math.max(0, (g.getFullYear() - f.getFullYear()) * 12 + g.getMonth() - f.getMonth() + ((g.getDate() < f.getDate() && !k) || (k && l(g) < l(f)) ? -1 : 0));
  510.                 h[Y] = (d[Y] ? Math.floor(m / 12) : 0);
  511.                 h[O] = (d[O] ? m - h[Y] * 12 : 0);
  512.                 f = new Date(f.getTime());
  513.                 var n = (f.getDate() == i);
  514.                 var o = x._getDaysInMonth(f.getFullYear() + h[Y], f.getMonth() + h[O]);
  515.                 if (f.getDate() > o) {
  516.                     f.setDate(o)
  517.                 }
  518.                 f.setFullYear(f.getFullYear() + h[Y]);
  519.                 f.setMonth(f.getMonth() + h[O]);
  520.                 if (n) {
  521.                     f.setDate(o)
  522.                 }
  523.             }
  524.             var p = Math.floor((g.getTime() - f.getTime()) / 1000);
  525.             var q = function (a, b) {
  526.                 h[a] = (d[a] ? Math.floor(p / b) : 0);
  527.                 p -= h[a] * b
  528.             };
  529.             q(W, 604800);
  530.             q(D, 86400);
  531.             q(H, 3600);
  532.             q(M, 60);
  533.             q(S, 1);
  534.             if (p > 0 && !c._since) {
  535.                 var r = [1, 12, 4.3482, 7, 24, 60, 60];
  536.                 var s = S;
  537.                 var t = 1;
  538.                 for (var u = S; u >= Y; u--) {
  539.                     if (d[u]) {
  540.                         if (h[s] >= t) {
  541.                             h[s] = 0;
  542.                             p = 1
  543.                         }
  544.                         if (p > 0) {
  545.                             h[u]++;
  546.                             p = 0;
  547.                             s = u;
  548.                             t = 1
  549.                         }
  550.                     }
  551.                     t *= r[u]
  552.                 }
  553.             }
  554.             if (e) {
  555.                 for (var u = Y; u <= S; u++) {
  556.                     if (e && h[u]) {
  557.                         e--
  558.                     } else if (!e) {
  559.                         h[u] = 0
  560.                     }
  561.                 }
  562.             }
  563.             return h
  564.         }
  565.     });
  566.     var w = ['getTimes'];
  567.  
  568.     function isNotChained(a, b) {
  569.         if (a == 'option' && (b.length == 0 || (b.length == 1 && typeof b[0] == 'string'))) {
  570.             return true
  571.         }
  572.         return $.inArray(a, w) > -1
  573.     }
  574.     $.fn.countdown = function (a) {
  575.         var b = Array.prototype.slice.call(arguments, 1);
  576.         if (isNotChained(a, b)) {
  577.             return x['_' + a + 'Plugin'].apply(x, [this[0]].concat(b))
  578.         }
  579.         return this.each(function () {
  580.             if (typeof a == 'string') {
  581.                 if (!x['_' + a + 'Plugin']) {
  582.                     throw 'Unknown command: ' + a;
  583.                 }
  584.                 x['_' + a + 'Plugin'].apply(x, [this].concat(b))
  585.             } else {
  586.                 x._attachPlugin(this, a || {})
  587.             }
  588.         })
  589.     };
  590.     var x = $.countdown = new Countdown()
  591. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement