/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.1.1.
   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])}if(b._now.getTime()>=b._getUntil(b._now).getTime()){if(b._timer){var d=b._get('onExpiry');if(d){d.apply(b._target[0],[])}var e=b._get('expiryUrl');if(e){window.location=e}}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>'+h[a]+'</div>':'')};return(g?'<div class="countdown_row clearfix 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 clearfix countdown_show'+e+'">'+m(Y)+m(O)+m(W)+m(D)+m(H)+m(M)+m(S))+'</div>'+(j?'<div class="countdown_row clearfix countdown_descr">'+j+'</div>':'')},_calculatePeriods:function(c,d){this._now=d;this._now.setMilliseconds(0);var e=this._getUntil(this._now);if(this._now.getTime()>e.getTime()){this._now=d=e}var f=[0,0,0,0,0,0,0];if(c[Y]||c[O]){var g=Math.max(0,(e.getFullYear()-this._now.getFullYear())*12+e.getMonth()-this._now.getMonth()+(e.getDate()<this._now.getDate()?-1:0));f[Y]=(c[Y]?Math.floor(g/12):0);f[O]=(c[O]?g-f[Y]*12:0);d=new Date(d.getTime());d.setFullYear(d.getFullYear()+f[Y]);d.setMonth(d.getMonth()+f[O])}var h=Math.floor((e.getTime()-d.getTime())/1000);var i=function(a,b){f[a]=(c[a]?Math.floor(h/b):0);h-=f[a]*b};i(W,604800);i(D,86400);i(H,3600);i(M,60);i(S,1);return f},_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))}})};$(document).ready(function(){$.countdown=new Countdown()})})(jQuery);
