/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.5.9.
   Written by Keith Wood (kbwood{at}iinet.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(g){function o(){this.regional=[];this.regional[""]={labels:"Years,Months,Weeks,Days,Hrs,Min,Sec".split(","),labels1:"Year,Month,Week,Days,Hrs,Min,Sec".split(","),compactLabels:["y","m","w","d"],whichLabels:null,timeSeparator:":",isRTL:!1};this._defaults={until:null,since:null,timezone:null,serverSync:null,format:"dHMS",layout:"",compact:!1,significant:0,description:"",expiryUrl:"",expiryText:"",alwaysExpire:!1,onExpiry:null,onTick:null,tickInterval:1};g.extend(this._defaults,this.regional[""]);
this._serverSyncs=[]}function p(a,b){g.extend(a,b);for(var c in b)b[c]==null&&(a[c]=null);return a}g.extend(o.prototype,{markerClassName:"hasCountdown",_timer:setInterval(function(){g.countdown._updateTargets()},980),_timerTargets:[],setDefaults:function(a){this._resetExtraLabels(this._defaults,a);p(this._defaults,a||{})},UTCDate:function(a,b,c,d,e,g,f,j){typeof b=="object"&&b.constructor==Date&&(j=b.getMilliseconds(),f=b.getSeconds(),g=b.getMinutes(),e=b.getHours(),d=b.getDate(),c=b.getMonth(),b=
b.getFullYear());var i=new Date;i.setUTCFullYear(b);i.setUTCDate(1);i.setUTCMonth(c||0);i.setUTCDate(d||1);i.setUTCHours(e||0);i.setUTCMinutes((g||0)-(Math.abs(a)<30?a*60:a));i.setUTCSeconds(f||0);i.setUTCMilliseconds(j||0);return i},periodsToSeconds:function(a){return a[0]*31557600+a[1]*2629800+a[2]*604800+a[3]*86400+a[4]*3600+a[5]*60+a[6]},_settingsCountdown:function(a,b){if(!b)return g.countdown._defaults;var c=g.data(a,"countdown");return b=="all"?c.options:c.options[b]},_attachCountdown:function(a,
b){var c=g(a);c.hasClass(this.markerClassName)||(c.addClass(this.markerClassName),c={options:g.extend({},b),_periods:[0,0,0,0,0,0,0]},g.data(a,"countdown",c),this._changeCountdown(a))},_addTarget:function(a){this._hasTarget(a)||this._timerTargets.push(a)},_hasTarget:function(a){return g.inArray(a,this._timerTargets)>-1},_removeTarget:function(a){this._timerTargets=g.map(this._timerTargets,function(b){return b==a?null:b})},_updateTargets:function(){for(var a=this._timerTargets.length-1;a>=0;a--)this._updateCountdown(this._timerTargets[a])},
_updateCountdown:function(a,b){var c=g(a);if(b=b||g.data(a,"countdown")){c.html(this._generateHTML(b));c[(this._get(b,"isRTL")?"add":"remove")+"Class"]("countdown_rtl");if(c=this._get(b,"onTick")){var d=b._hold!="lap"?b._periods:this._calculatePeriods(b,b._show,this._get(b,"significant"),new Date),e=this._get(b,"tickInterval");(e==1||this.periodsToSeconds(d)%e==0)&&c.apply(a,[d])}if(b._hold!="pause"&&(b._since?b._now.getTime()<b._since.getTime():b._now.getTime()>=b._until.getTime())&&!b._expiring){b._expiring=
!0;if(this._hasTarget(a)||this._get(b,"alwaysExpire")){this._removeTarget(a);(c=this._get(b,"onExpiry"))&&c.apply(a,[]);if(c=this._get(b,"expiryText"))d=this._get(b,"layout"),b.options.layout=c,this._updateCountdown(a,b),b.options.layout=d;if(c=this._get(b,"expiryUrl"))window.location=c}b._expiring=!1}else b._hold=="pause"&&this._removeTarget(a);g.data(a,"countdown",b)}},_changeCountdown:function(a,b,c){b=b||{};if(typeof b=="string"){var d=b,b={};b[d]=c}if(c=g.data(a,"countdown"))this._resetExtraLabels(c.options,
b),p(c.options,b),this._adjustSettings(a,c),g.data(a,"countdown",c),b=new Date,(c._since&&c._since<b||c._until&&c._until>b)&&this._addTarget(a),this._updateCountdown(a,c)},_resetExtraLabels:function(a,b){var c=!1,d;for(d in b)if(d!="whichLabels"&&d.match(/[Ll]abels/)){c=!0;break}if(c)for(d in a)d.match(/[Ll]abels[0-9]/)&&(a[d]=null)},_adjustSettings:function(a,b){var c,d=this._get(b,"serverSync"),e=0;c=null;for(e=0;e<this._serverSyncs.length;e++)if(this._serverSyncs[e][0]==d){c=this._serverSyncs[e][1];
break}c!=null?(e=d?c:0,c=new Date):(e=d?d.apply(a,[]):null,c=new Date,e=e?c.getTime()-e.getTime():0,this._serverSyncs.push([d,e]));d=this._get(b,"timezone");d=d==null?-c.getTimezoneOffset():d;b._since=this._get(b,"since");if(b._since!=null)b._since=this.UTCDate(d,this._determineTime(b._since,null)),b._since&&e&&b._since.setMilliseconds(b._since.getMilliseconds()+e);b._until=this.UTCDate(d,this._determineTime(this._get(b,"until"),c));e&&b._until.setMilliseconds(b._until.getMilliseconds()+e);b._show=
this._determineShow(b)},_destroyCountdown:function(a){var b=g(a);b.hasClass(this.markerClassName)&&(this._removeTarget(a),b.removeClass(this.markerClassName).empty(),g.removeData(a,"countdown"))},_pauseCountdown:function(a){this._hold(a,"pause")},_lapCountdown:function(a){this._hold(a,"lap")},_resumeCountdown:function(a){this._hold(a,null)},_hold:function(a,b){var c=g.data(a,"countdown");if(c){if(c._hold=="pause"&&!b){c._periods=c._savePeriods;var d=c._since?"-":"+";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");this._addTarget(a)}c._hold=b;c._savePeriods=b=="pause"?c._periods:null;g.data(a,"countdown",c);this._updateCountdown(a,c)}},_getTimesCountdown:function(a){a=g.data(a,"countdown");return!a?null:!a._hold?a._periods:this._calculatePeriods(a,a._show,this._get(a,"significant"),new Date)},_get:function(a,b){return a.options[b]!=null?a.options[b]:
g.countdown._defaults[b]},_determineTime:function(a,b){var c=function(a){var b=new Date;b.setTime(b.getTime()+a*1E3);return b};(c=a==null?b:typeof a=="string"?function(a){for(var a=a.toLowerCase(),b=new Date,c=b.getFullYear(),f=b.getMonth(),j=b.getDate(),i=b.getHours(),h=b.getMinutes(),b=b.getSeconds(),m=/([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g,l=m.exec(a);l;){switch(l[2]||"s"){case "s":b+=parseInt(l[1],10);break;case "m":h+=parseInt(l[1],10);break;case "h":i+=parseInt(l[1],10);break;case "d":j+=parseInt(l[1],
10);break;case "w":j+=parseInt(l[1],10)*7;break;case "o":f+=parseInt(l[1],10);j=Math.min(j,g.countdown._getDaysInMonth(c,f));break;case "y":c+=parseInt(l[1],10),j=Math.min(j,g.countdown._getDaysInMonth(c,f))}l=m.exec(a)}return new Date(c,f,j,i,h,b,0)}(a):typeof a=="number"?c(a):a)&&c.setMilliseconds(0);return c},_getDaysInMonth:function(a,b){return 32-(new Date(a,b,32)).getDate()},_normalLabels:function(a){return a},_generateHTML:function(a){var b=this._get(a,"significant");a._periods=a._hold?a._periods:
this._calculatePeriods(a,a._show,b,new Date);for(var c=!1,d=0,e=b,k=g.extend({},a._show),f=0;f<=6;f++)c|=a._show[f]=="?"&&a._periods[f]>0,k[f]=a._show[f]=="?"&&!c?null:a._show[f],d+=k[f]?1:0,e-=a._periods[f]>0?1:0;for(var j=[!1,!1,!1,!1,!1,!1,!1],f=6;f>=0;f--)a._show[f]&&(a._periods[f]?j[f]=!0:(j[f]=e>0,e--));var c=this._get(a,"compact"),e=this._get(a,"layout"),i=c?this._get(a,"compactLabels"):this._get(a,"labels"),h=this._get(a,"whichLabels")||this._normalLabels,f=this._get(a,"timeSeparator"),m=
this._get(a,"description")||"",l=function(b){var c=g.countdown._get(a,"compactLabels"+h(a._periods[b]));return k[b]?a._periods[b]+(c?c[b]:i[b])+" ":""},n=function(c){var d=g.countdown._get(a,"labels"+h(a._periods[c]));return!b&&k[c]||b&&j[c]?'<span class="countdown_section"><span class="countdown_amount">'+a._periods[c]+"</span><br/>"+(d?d[c]:i[c])+"</span>":""};return e?this._buildLayout(a,k,e,c,b,j):(c?'<span class="countdown_row countdown_amount'+(a._hold?" countdown_holding":"")+'">'+l(0)+l(1)+
l(2)+l(3)+(k[4]?this._minDigits(a._periods[4],2):"")+(k[5]?(k[4]?f:"")+this._minDigits(a._periods[5],2):"")+(k[6]?(k[4]||k[5]?f:"")+this._minDigits(a._periods[6],2):""):'<span class="countdown_row countdown_show'+(b||d)+(a._hold?" countdown_holding":"")+'">'+n(0)+n(1)+n(2)+n(3)+n(4)+n(5)+n(6))+"</span>"+(m?'<span class="countdown_row countdown_descr">'+m+"</span>":"")},_buildLayout:function(a,b,c,d,e,k){for(var f=this._get(a,d?"compactLabels":"labels"),j=this._get(a,"whichLabels")||this._normalLabels,
i=function(b){return(g.countdown._get(a,(d?"compactLabels":"labels")+j(a._periods[b]))||f)[b]},h=function(a,b){return Math.floor(a/b)%10},i={desc:this._get(a,"description"),sep:this._get(a,"timeSeparator"),yl:i(0),yn:a._periods[0],ynn:this._minDigits(a._periods[0],2),ynnn:this._minDigits(a._periods[0],3),y1:h(a._periods[0],1),y10:h(a._periods[0],10),y100:h(a._periods[0],100),y1000:h(a._periods[0],1E3),ol:i(1),on:a._periods[1],onn:this._minDigits(a._periods[1],2),onnn:this._minDigits(a._periods[1],
3),o1:h(a._periods[1],1),o10:h(a._periods[1],10),o100:h(a._periods[1],100),o1000:h(a._periods[1],1E3),wl:i(2),wn:a._periods[2],wnn:this._minDigits(a._periods[2],2),wnnn:this._minDigits(a._periods[2],3),w1:h(a._periods[2],1),w10:h(a._periods[2],10),w100:h(a._periods[2],100),w1000:h(a._periods[2],1E3),dl:i(3),dn:a._periods[3],dnn:this._minDigits(a._periods[3],2),dnnn:this._minDigits(a._periods[3],3),d1:h(a._periods[3],1),d10:h(a._periods[3],10),d100:h(a._periods[3],100),d1000:h(a._periods[3],1E3),hl:i(4),
hn:a._periods[4],hnn:this._minDigits(a._periods[4],2),hnnn:this._minDigits(a._periods[4],3),h1:h(a._periods[4],1),h10:h(a._periods[4],10),h100:h(a._periods[4],100),h1000:h(a._periods[4],1E3),ml:i(5),mn:a._periods[5],mnn:this._minDigits(a._periods[5],2),mnnn:this._minDigits(a._periods[5],3),m1:h(a._periods[5],1),m10:h(a._periods[5],10),m100:h(a._periods[5],100),m1000:h(a._periods[5],1E3),sl:i(6),sn:a._periods[6],snn:this._minDigits(a._periods[6],2),snnn:this._minDigits(a._periods[6],3),s1:h(a._periods[6],
1),s10:h(a._periods[6],10),s100:h(a._periods[6],100),s1000:h(a._periods[6],1E3)},m=c,c=0;c<=6;c++)h="yowdhms".charAt(c),m=m.replace(RegExp("\\{"+h+"<\\}(.*)\\{"+h+">\\}","g"),!e&&b[c]||e&&k[c]?"$1":"");g.each(i,function(a,b){m=m.replace(RegExp("\\{"+a+"\\}","g"),b)});return m},_minDigits:function(a,b){a=""+a;if(a.length>=b)return a;a="0000000000"+a;return a.substr(a.length-b)},_determineShow:function(a){var a=this._get(a,"format"),b=[];b[0]=a.match("y")?"?":a.match("Y")?"!":null;b[1]=a.match("o")?
"?":a.match("O")?"!":null;b[2]=a.match("w")?"?":a.match("W")?"!":null;b[3]=a.match("d")?"?":a.match("D")?"!":null;b[4]=a.match("h")?"?":a.match("H")?"!":null;b[5]=a.match("m")?"?":a.match("M")?"!":null;b[6]=a.match("s")?"?":a.match("S")?"!":null;return b},_calculatePeriods:function(a,b,c,d){a._now=d;a._now.setMilliseconds(0);var e=new Date(a._now.getTime());if(a._since)d.getTime()<a._since.getTime()?a._now=d=e:d=a._since;else if(e.setTime(a._until.getTime()),d.getTime()>a._until.getTime())a._now=
d=e;var k=[0,0,0,0,0,0,0];if(b[0]||b[1]){var f=g.countdown._getDaysInMonth(d.getFullYear(),d.getMonth()),j=g.countdown._getDaysInMonth(e.getFullYear(),e.getMonth()),j=e.getDate()==d.getDate()||e.getDate()>=Math.min(f,j)&&d.getDate()>=Math.min(f,j),j=Math.max(0,(e.getFullYear()-d.getFullYear())*12+e.getMonth()-d.getMonth()+(e.getDate()<d.getDate()&&!j||j&&(e.getHours()*60+e.getMinutes())*60+e.getSeconds()<(d.getHours()*60+d.getMinutes())*60+d.getSeconds()?-1:0));k[0]=b[0]?Math.floor(j/12):0;k[1]=b[1]?
j-k[0]*12:0;d=new Date(d.getTime());f=d.getDate()==f;j=g.countdown._getDaysInMonth(d.getFullYear()+k[0],d.getMonth()+k[1]);d.getDate()>j&&d.setDate(j);d.setFullYear(d.getFullYear()+k[0]);d.setMonth(d.getMonth()+k[1]);f&&d.setDate(j)}var i=Math.floor((e.getTime()-d.getTime())/1E3),d=function(a,c){k[a]=b[a]?Math.floor(i/c):0;i-=k[a]*c};d(2,604800);d(3,86400);d(4,3600);d(5,60);d(6,1);if(i>0&&!a._since){a=[1,12,4.3482,7,24,60,60];d=6;e=1;for(f=6;f>=0;f--)b[f]&&(k[d]>=e&&(k[d]=0,i=1),i>0&&(k[f]++,i=0,
d=f,e=1)),e*=a[f]}if(c)for(f=0;f<=6;f++)c&&k[f]?c--:c||(k[f]=0);return k}});g.fn.countdown=function(a){var b=Array.prototype.slice.call(arguments,1);return a=="getTimes"||a=="settings"?g.countdown["_"+a+"Countdown"].apply(g.countdown,[this[0]].concat(b)):this.each(function(){typeof a=="string"?g.countdown["_"+a+"Countdown"].apply(g.countdown,[this].concat(b)):g.countdown._attachCountdown(this,a)})};g.countdown=new o})(jQuery);
