home *** CD-ROM | disk | FTP | other *** search
/ familyradio.com / www.familyradio.com.tar / www.familyradio.com / js / jquery.lwtCountdown-0.9.5.js < prev    next >
Text File  |  2011-04-11  |  5KB  |  171 lines

  1. /*!
  2.  * jQuery Countdown plugin v0.9.5
  3.  * http://www.littlewebthings.com/projects/countdown/
  4.  *
  5.  * Copyright 2010, Vassilis Dourdounis
  6.  * 
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  * 
  14.  * The above copyright notice and this permission notice shall be included in
  15.  * all copies or substantial portions of the Software.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23.  * THE SOFTWARE.
  24.  */
  25. (function($){
  26.  
  27.     $.fn.countDown = function (options) {
  28.  
  29.         config = {};
  30.  
  31.         $.extend(config, options);
  32.  
  33.         diffSecs = this.setCountDown(config);
  34.  
  35.         $('#' + $(this).attr('id') + ' .digit').html('<div class="top"></div><div class="bottom"></div>');
  36.         $(this).doCountDown($(this).attr('id'), diffSecs, 500);
  37.  
  38.         if (config.onComplete)
  39.         {
  40.             $.data($(this)[0], 'callback', config.onComplete);
  41.         }
  42.         if (config.omitWeeks)
  43.         {
  44.             $.data($(this)[0], 'omitWeeks', config.omitWeeks);
  45.         }
  46.         return this;
  47.  
  48.     };
  49.  
  50.     $.fn.stopCountDown = function () {
  51.         clearTimeout($.data(this[0], 'timer'));
  52.     };
  53.  
  54.     $.fn.startCountDown = function () {
  55.         this.doCountDown($(this).attr('id'),$.data(this[0], 'diffSecs'), 500);
  56.     };
  57.  
  58.     $.fn.setCountDown = function (options) {
  59.         var targetTime = new Date();
  60.  
  61.         if (options.targetDate)
  62.         {
  63.             targetTime.setDate(options.targetDate.day);
  64.             targetTime.setMonth(options.targetDate.month-1);
  65.             targetTime.setFullYear(options.targetDate.year);
  66.             targetTime.setHours(options.targetDate.hour);
  67.             targetTime.setMinutes(options.targetDate.min);
  68.             targetTime.setSeconds(options.targetDate.sec);
  69.         }
  70.         else if (options.targetOffset)
  71.         {
  72.             targetTime.setDate(options.targetOffset.day + targetTime.getDate());
  73.             targetTime.setMonth(options.targetOffset.month + targetTime.getMonth());
  74.             targetTime.setFullYear(options.targetOffset.year + targetTime.getFullYear());
  75.             targetTime.setHours(options.targetOffset.hour + targetTime.getHours());
  76.             targetTime.setMinutes(options.targetOffset.min + targetTime.getMinutes());
  77.             targetTime.setSeconds(options.targetOffset.sec + targetTime.getSeconds());
  78.         }
  79.  
  80.         var nowTime = new Date();
  81.  
  82.         diffSecs = Math.floor((targetTime.valueOf()-nowTime.valueOf())/1000);
  83.  
  84.         $.data(this[0], 'diffSecs', diffSecs);
  85.  
  86.         return diffSecs;
  87.     };
  88.  
  89.     $.fn.doCountDown = function (id, diffSecs, duration) {
  90.         $this = $('#' + id);
  91.         if (diffSecs <= 0)
  92.         {
  93.             diffSecs = 0;
  94.             if ($.data($this[0], 'timer'))
  95.             {
  96.                 clearTimeout($.data($this[0], 'timer'));
  97.             }
  98.         }
  99.  
  100.         secs = diffSecs % 60;
  101.         mins = Math.floor(diffSecs/60)%60;
  102.         hours = Math.floor(diffSecs/60/60)%24;
  103.         if ($.data($this[0], 'omitWeeks') == true)
  104.         {
  105.             days = Math.floor(diffSecs/60/60/24);
  106.             weeks = Math.floor(diffSecs/60/60/24/7);
  107.         }
  108.         else 
  109.         {
  110.             daysOld = Math.floor(diffSecs/60/60/24)%7;
  111.             weeks = Math.floor(diffSecs/60/60/24/7);
  112.             weeksNew = Math.floor(weeks * 7);
  113.             days = Math.floor(daysOld + weeksNew);
  114.         }
  115.  
  116.         $this.dashChangeTo(id, 'seconds_dash', secs, duration ? duration : 800);
  117.         $this.dashChangeTo(id, 'minutes_dash', mins, duration ? duration : 1200);
  118.         $this.dashChangeTo(id, 'hours_dash', hours, duration ? duration : 1200);
  119.         $this.dashChangeTo(id, 'days_dash', days, duration ? duration : 1200);
  120.         $this.dashChangeTo(id, 'weeks_dash', weeks, duration ? duration : 1200);
  121.  
  122.         $.data($this[0], 'diffSecs', diffSecs);
  123.         if (diffSecs > 0)
  124.         {
  125.             e = $this;
  126.             t = setTimeout(function() { e.doCountDown(id, diffSecs-1) } , 1000);
  127.             $.data(e[0], 'timer', t);
  128.         } 
  129.         else if (cb = $.data($this[0], 'callback')) 
  130.         {
  131.             $.data($this[0], 'callback')();
  132.         }
  133.  
  134.     };
  135.  
  136.     $.fn.dashChangeTo = function(id, dash, n, duration) {
  137.         $this = $('#' + id);
  138.         d2 = n%10;
  139.         d1 = (n - n%10) / 10
  140.  
  141.         if ($('#' + $this.attr('id') + ' .' + dash))
  142.         {
  143.             $this.digitChangeTo('#' + $this.attr('id') + ' .' + dash + ' .digit:first', d1, duration);
  144.             $this.digitChangeTo('#' + $this.attr('id') + ' .' + dash + ' .digit:last', d2, duration);
  145.         }
  146.     };
  147.  
  148.     $.fn.digitChangeTo = function (digit, n, duration) {
  149.         if (!duration)
  150.         {
  151.             duration = 800;
  152.         }
  153.         if ($(digit + ' div.top').html() != n + '')
  154.         {
  155.  
  156.             $(digit + ' div.top').css({'display': 'none'});
  157.             $(digit + ' div.top').html((n ? n : '0')).slideDown(duration);
  158.  
  159.             $(digit + ' div.bottom').animate({'height': ''}, duration, function() {
  160.                 $(digit + ' div.bottom').html($(digit + ' div.top').html());
  161.                 $(digit + ' div.bottom').css({'display': 'block', 'height': ''});
  162.                 $(digit + ' div.top').hide().slideUp(10);
  163.  
  164.             
  165.             });
  166.         }
  167.     };
  168.  
  169. })(jQuery);
  170.  
  171.