/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.2.0.
   Written by Keith Wood (kbwood@virginbroadband.com.au) January 2008.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
(function($){function Countdown(){this._nextId=0;this._inst=[];this.regional=[];this.regional['']={labels:['Years','Months','Weeks','Days','Hours','Minutes','Seconds'],compactLabels:['y','m','w','d'],timeSeparator:':'};this._defaults={format:'dHMS',compact:false,description:'',expiryUrl:null,onExpiry:null,onTick:null};$.extend(this._defaults,this.regional[''])}$.extend(Countdown.prototype,{markerClassName:'hasCountdown',_register:function(a){var b=this._nextId++;this._inst[b]=a;return b},_getInst:function(a){return this._inst[a]||a},setDefaults:function(a){extendRemove(this._defaults,a||{})},_attachCountdown:function(a,b){a=$(a);if(a.is('.'+this.markerClassName)){return}a.addClass(this.markerClassName);a[0]._cdnId=b._id;b._target=a;this._updateCountdown(b._id)},_updateCountdown:function(a){var b=this._getInst(a);b._target.html(b._generateHTML());var c=b._get('onTick');if(c){c.apply(b._target[0],[b._periods])}var d=b._get('since');var e=(d?b._now.getTime()<=d.getTime():b._now.getTime()>=b._getUntil(b._now).getTime());if(e){if(b._timer){var f=b._get('onExpiry');if(f){f.apply(b._target[0],[])}var g=b._get('expiryUrl');if(g){window.location=g}}b._timer=null}else{b._timer=setTimeout('$.countdown._updateCountdown('+b._id+')',(b._get('format').match('s|S')?1:30)*980)}},_changeCountdown:function(a,b){var c=this._getInst(a._cdnId);if(c){extendRemove(c._settings,b||{});this._updateCountdown(c._id)}},_destroyCountdown:function(a){a=$(a);if(!a.is('.'+this.markerClassName)){return}a.removeClass(this.markerClassName);a.empty();clearTimeout(this._inst[a[0]._cdnId]._timer);this._inst[a[0]._cdnId]=null;a[0]._cdnId=undefined}});var Y=0;var O=1;var W=2;var D=3;var H=4;var M=5;var S=6;function CountdownInstance(a){this._id=$.countdown._register(this);this._target=null;this._timer=null;this._now=null;this._periods=[0,0,0,0,0,0,0];this._settings=extendRemove({},a||{})}$.extend(CountdownInstance.prototype,{_get:function(a){return(this._settings[a]!=null?this._settings[a]:$.countdown._defaults[a])},_generateHTML:function(){var b=this._get('format');var c=[];c[Y]=(b.match('y')?'?':(b.match('Y')?'!':null));c[O]=(b.match('o')?'?':(b.match('O')?'!':null));c[W]=(b.match('w')?'?':(b.match('W')?'!':null));c[D]=(b.match('d')?'?':(b.match('D')?'!':null));c[H]=(b.match('h')?'?':(b.match('H')?'!':null));c[M]=(b.match('m')?'?':(b.match('M')?'!':null));c[S]=(b.match('s')?'?':(b.match('S')?'!':null));this._periods=periods=this._calculatePeriods(c,new Date());var d=false;var e=0;for(var f=0;f<c.length;f++){d|=(c[f]=='?'&&periods[f]>0);c[f]=(c[f]=='?'&&!d?null:c[f]);e+=(c[f]?1:0)}var g=this._get('compact');var h=(g?this._get('compactLabels'):this._get('labels'));var i=this._get('timeSeparator');var j=this._get('description')||'';var k=function(a){return(a<10?'0':'')+a};var l=function(a){return(c[a]?periods[a]+h[a]+' ':'')};var m=function(a){return(c[a]?'<div class="countdown_section"><span class="countdown_amount">'+periods[a]+'</span><br/>'+h[a]+'</div>':'')};return(g?'<div class="countdown_row countdown_amount">'+l(Y)+l(O)+l(W)+l(D)+k(this._periods[H])+i+k(this._periods[M])+(c[S]?i+k(this._periods[S]):''):'<div class="countdown_row countdown_show'+e+'">'+m(Y)+m(O)+m(W)+m(D)+m(H)+m(M)+m(S))+'</div>'+(j?'<div class="countdown_row countdown_descr">'+j+'</div>':'')},_calculatePeriods:function(c,d){this._now=d;this._now.setMilliseconds(0);var e=this._getUntil(d);if(d.getTime()>e.getTime()){this._now=d=e}var f=this._get('since');if(f){f.setMilliseconds(0)}if(f&&d.getTime()<f.getTime()){this._now=d=f}if(f){e=d;d=f}var g=[0,0,0,0,0,0,0];if(c[Y]||c[O]){var h=Math.max(0,(e.getFullYear()-d.getFullYear())*12+e.getMonth()-d.getMonth()+(e.getDate()<d.getDate()?-1:0));g[Y]=(c[Y]?Math.floor(h/12):0);g[O]=(c[O]?h-g[Y]*12:0);d=new Date(d.getTime());d.setFullYear(d.getFullYear()+g[Y]);d.setMonth(d.getMonth()+g[O])}var i=Math.floor((e.getTime()-d.getTime())/1000);var j=function(a,b){g[a]=(c[a]?Math.floor(i/b):0);i-=g[a]*b};j(W,604800);j(D,86400);j(H,3600);j(M,60);j(S,1);return g},_getUntil:function(a){var b=this._get('until')||a;b.setMilliseconds(0);return b}});function extendRemove(a,b){$.extend(a,b);for(var c in b){if(b[c]==null){a[c]=null}}return a}$.fn.countdown=function(a){var b=Array.prototype.slice.call(arguments,1);return this.each(function(){if(typeof a=='string'){$.countdown['_'+a+'Countdown'].apply($.countdown,[this].concat(b))}else{$.countdown._attachCountdown(this,new CountdownInstance(a))}})};$(function(){$.countdown=new Countdown()})})(jQuery);