/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.5.3.
   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. */
/*eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(x($){x 1d(){8.1w=[];8.1w[\'\']={1e:[\'2o\',\'2p\',\'2q\',\'2r\',\'2s\',\'2t\',\'2u\'],2v:[\'2w\',\'2x\',\'2y\',\'2z\',\'2A\',\'2B\',\'2C\'],1f:[\'y\',\'m\',\'w\',\'d\'],1x:\':\',1M:1a};8.1g={1N:B,1O:B,1P:B,1Q:\'2D\',1h:\'\',1R:1a,1y:\'\',1S:\'\',1T:\'\',1U:1a,1V:B,1W:B};$.1o(8.1g,8.1w[\'\'])}t s=\'F\';t Y=0;t O=1;t W=2;t D=3;t H=4;t M=5;t S=6;$.1o(1d.1X,{1i:\'2E\',2F:2G(x(){$.F.1Y()},2H),13:[],2I:x(a){8.1z(8.1g,a);1A(8.1g,a||{})},1B:x(a,b,c,e,f,g,h,i){z(1j b==\'2J\'&&b.2K==L){i=b.2L();h=b.1C();g=b.1D();f=b.1E();e=b.Q();c=b.V();b=b.X()}t d=I L();d.2M(b);d.1Z(1);d.2N(c||0);d.1Z(e||1);d.2O(f||0);d.2P((g||0)-(R.2Q(a)<30?a*1p:a));d.2R(h||0);d.2S(i||0);A d},20:x(a,b){t c=$(a);z(c.21(8.1i)){A}c.2T(8.1i);t d={14:$.1o({},b),u:[0,0,0,0,0,0,0]};$.Z(a,s,d);8.22(a)},1F:x(a){z(!8.1G(a)){8.13.2U(a)}},1G:x(a){A($.2V(a,8.13)>-1)},1q:x(b){8.13=$.2W(8.13,x(a){A(a==b?B:a)})},1Y:x(){1b(t i=0;i<8.13.1H;i++){8.1k(8.13[i])}},1k:x(a,b){t c=$(a);b=b||$.Z(a,s);z(!b){A}c.2X(8.23(b));c[(8.C(b,\'1M\')?\'2Y\':\'2Z\')+\'31\'](\'33\');t d=8.C(b,\'1W\');z(d){d.1r(a,[b.P!=\'24\'?b.u:8.1s(b,b.E,I L())])}t e=b.P!=\'1l\'&&(b.J?b.15.N()<=b.J.N():b.15.N()>=b.16.N());z(e&&!b.1I){b.1I=25;z(8.1G(a)||8.C(b,\'1U\')){8.1q(a);t f=8.C(b,\'1V\');z(f){f.1r(a,[])}t g=8.C(b,\'1T\');z(g){t h=8.C(b,\'1h\');b.14.1h=g;8.1k(a,b);b.14.1h=h}t i=8.C(b,\'1S\');z(i){34.35=i}}b.1I=1a}1m z(b.P==\'1l\'){8.1q(a)}$.Z(a,s,b)},22:x(a,b,c){b=b||{};z(1j b==\'1J\'){t d=b;b={};b[d]=c}t e=$.Z(a,s);z(e){8.1z(e.14,b);1A(e.14,b);8.26(e);$.Z(a,s,e);t f=I L();z((e.J&&e.J<f)||(e.16&&e.16>f)){8.1F(a)}8.1k(a,e)}},1z:x(a,b){t c=1a;1b(t n 1K b){z(n.K(/[27]28/)){c=25;11}}z(c){1b(t n 1K a){z(n.K(/[27]28[0-9]/)){a[n]=B}}}},36:x(a){t b=$(a);z(!b.21(8.1i)){A}8.1q(a);b.37(8.1i).38();$.39(a,s)},3a:x(a){8.P(a,\'1l\')},3b:x(a){8.P(a,\'24\')},3c:x(a){8.P(a,B)},P:x(a,b){t c=$.Z(a,s);z(c){z(c.P==\'1l\'&&!b){c.u=c.29;t d=(c.J?\'-\':\'+\');c[c.J?\'J\':\'16\']=8.1t(d+c.u[0]+\'y\'+d+c.u[1]+\'o\'+d+c.u[2]+\'w\'+d+c.u[3]+\'d\'+d+c.u[4]+\'h\'+d+c.u[5]+\'m\'+d+c.u[6]+\'s\');8.1F(a)}c.P=b;c.29=(b==\'1l\'?c.u:B);$.Z(a,s,c);8.1k(a,c)}},3d:x(a){t b=$.Z(a,s);A(!b?B:(!b.P?b.u:8.1s(b,b.E,I L())))},C:x(a,b){A(a.14[b]!=B?a.14[b]:$.F.1g[b])},26:x(a){t b=I L();t c=8.C(a,\'1P\');c=(c==B?-I L().3e():c);a.J=8.C(a,\'1O\');z(a.J){a.J=8.1B(c,8.1t(a.J,B))}a.16=8.1B(c,8.1t(8.C(a,\'1N\'),b));a.E=8.2a(a)},1t:x(k,l){t m=x(a){t b=I L();b.2b(b.N()+a*2c);A b};t n=x(a){a=a.3f();t b=I L();t c=b.X();t d=b.V();t e=b.Q();t f=b.1E();t g=b.1D();t h=b.1C();t i=/([+-]?[0-9]+)\\s*(s|m|h|d|w|o|y)?/g;t j=i.2d(a);3g(j){3h(j[2]||\'s\'){17\'s\':h+=18(j[1],10);11;17\'m\':g+=18(j[1],10);11;17\'h\':f+=18(j[1],10);11;17\'d\':e+=18(j[1],10);11;17\'w\':e+=18(j[1],10)*7;11;17\'o\':d+=18(j[1],10);e=R.1u(e,$.F.1c(c,d));11;17\'y\':c+=18(j[1],10);e=R.1u(e,$.F.1c(c,d));11}j=i.2d(a)}A I L(c,d,e,f,g,h,0)};t o=(k==B?l:(1j k==\'1J\'?n(k):(1j k==\'3i\'?m(k):k)));z(o)o.2e(0);A o},1c:x(a,b){A 32-I L(a,b,32).Q()},23:x(c){c.u=T=(c.P?c.u:8.1s(c,c.E,I L()));t d=1a;t e=0;1b(t f=0;f<c.E.1H;f++){d|=(c.E[f]==\'?\'&&T[f]>0);c.E[f]=(c.E[f]==\'?\'&&!d?B:c.E[f]);e+=(c.E[f]?1:0)}t g=8.C(c,\'1R\');t h=8.C(c,\'1h\');t i=(g?8.C(c,\'1f\'):8.C(c,\'1e\'));t j=8.C(c,\'1x\');t k=8.C(c,\'1y\')||\'\';t l=x(a){t b=$.F.C(c,\'1f\'+T[a]);A(c.E[a]?T[a]+(b?b[a]:i[a])+\' \':\'\')};t m=x(a){t b=$.F.C(c,\'1e\'+T[a]);A(c.E[a]?\'<U 1n="3j"><U 1n="2f">\'+T[a]+\'</U><3k/>\'+(b?b[a]:i[a])+\'</U>\':\'\')};A(h?8.2g(c,h,g):((g?\'<U 1n="1L 2f\'+(c.P?\' 2h\':\'\')+\'">\'+l(Y)+l(O)+l(W)+l(D)+(c.E[H]?8.G(T[H],2):\'\')+(c.E[M]?(c.E[H]?j:\'\')+8.G(T[M],2):\'\')+(c.E[S]?(c.E[H]||c.E[M]?j:\'\')+8.G(T[S],2):\'\'):\'<U 1n="1L 3l\'+e+(c.P?\' 2h\':\'\')+\'">\'+m(Y)+m(O)+m(W)+m(D)+m(H)+m(M)+m(S))+\'</U>\'+(k?\'<U 1n="1L 3m">\'+k+\'</U>\':\'\')))},2g:x(c,d,e){t f=8.C(c,(e?\'1f\':\'1e\'));t g=x(a){A($.F.C(c,(e?\'1f\':\'1e\')+c.u[a])||f)[a]};t h=x(a,b){A R.1v(a/b)%10};t j={3n:8.C(c,\'1y\'),3o:8.C(c,\'1x\'),3p:g(Y),3q:c.u[Y],3r:8.G(c.u[Y],2),3s:8.G(c.u[Y],3),3t:h(c.u[Y],1),3u:h(c.u[Y],10),3v:h(c.u[Y],19),3w:g(O),3x:c.u[O],3y:8.G(c.u[O],2),3z:8.G(c.u[O],3),3A:h(c.u[O],1),3B:h(c.u[O],10),3C:h(c.u[O],19),3D:g(W),3E:c.u[W],3F:8.G(c.u[W],2),3G:8.G(c.u[W],3),3H:h(c.u[W],1),3I:h(c.u[W],10),3J:h(c.u[W],19),3K:g(D),3L:c.u[D],3M:8.G(c.u[D],2),3N:8.G(c.u[D],3),3O:h(c.u[D],1),3P:h(c.u[D],10),3Q:h(c.u[D],19),3R:g(H),3S:c.u[H],3T:8.G(c.u[H],2),3U:8.G(c.u[H],3),3V:h(c.u[H],1),3W:h(c.u[H],10),3X:h(c.u[H],19),3Y:g(M),3Z:c.u[M],40:8.G(c.u[M],2),41:8.G(c.u[M],3),42:h(c.u[M],1),43:h(c.u[M],10),44:h(c.u[M],19),45:g(S),46:c.u[S],47:8.G(c.u[S],2),48:8.G(c.u[S],3),49:h(c.u[S],1),4a:h(c.u[S],10),4b:h(c.u[S],19)};t k=d;1b(t i=0;i<7;i++){t l=\'4c\'.4d(i);t m=I 2i(\'\\\\{\'+l+\'<\\\\}(.*)\\\\{\'+l+\'>\\\\}\',\'g\');k=k.2j(m,(c.E[i]?\'$1\':\'\'))}$.2k(j,x(n,v){t a=I 2i(\'\\\\{\'+n+\'\\\\}\',\'g\');k=k.2j(a,v)});A k},G:x(a,b){a=\'4e\'+a;A a.4f(a.1H-b)},2a:x(a){t b=8.C(a,\'1Q\');t c=[];c[Y]=(b.K(\'y\')?\'?\':(b.K(\'Y\')?\'!\':B));c[O]=(b.K(\'o\')?\'?\':(b.K(\'O\')?\'!\':B));c[W]=(b.K(\'w\')?\'?\':(b.K(\'W\')?\'!\':B));c[D]=(b.K(\'d\')?\'?\':(b.K(\'D\')?\'!\':B));c[H]=(b.K(\'h\')?\'?\':(b.K(\'H\')?\'!\':B));c[M]=(b.K(\'m\')?\'?\':(b.K(\'M\')?\'!\':B));c[S]=(b.K(\'s\')?\'?\':(b.K(\'S\')?\'!\':B));A c},1s:x(f,g,h){f.15=h;f.15.2e(0);t i=I L(f.15.N());z(f.J&&h.N()<f.J.N()){f.15=h=i}1m z(f.J){h=f.J}1m{i.2b(f.16.N());z(h.N()>f.16.N()){f.15=h=i}}t j=[0,0,0,0,0,0,0];z(g[Y]||g[O]){t k=$.F.1c(h.X(),h.V());t l=$.F.1c(i.X(),i.V());t m=(i.Q()==h.Q()||(i.Q()>=R.1u(k,l)&&h.Q()>=R.1u(k,l)));t n=x(a){A(a.1E()*1p+a.1D())*1p+a.1C()};t o=R.4g(0,(i.X()-h.X())*12+i.V()-h.V()+((i.Q()<h.Q()&&!m)||(m&&n(i)<n(h))?-1:0));j[Y]=(g[Y]?R.1v(o/12):0);j[O]=(g[O]?o-j[Y]*12:0);t p=x(a,b,c){t d=(a.Q()==c);t e=$.F.1c(a.X()+b*j[Y],a.V()+b*j[O]);z(a.Q()>e){a.2l(e)}a.4h(a.X()+b*j[Y]);a.4i(a.V()+b*j[O]);z(d){a.2l(e)}A a};z(f.J){i=p(i,-1,l)}1m{h=p(I L(h.N()),+1,k)}}t q=R.1v((i.N()-h.N())/2c);t r=x(a,b){j[a]=(g[a]?R.1v(q/b):0);q-=j[a]*b};r(W,4j);r(D,4k);r(H,4l);r(M,1p);r(S,1);A j}});x 1A(a,b){$.1o(a,b);1b(t c 1K b){z(b[c]==B){a[c]=B}}A a}$.4m.F=x(a){t b=4n.1X.4o.4p(4q,1);z(a==\'4r\'){A $.F[\'2m\'+a+\'1d\'].1r($.F,[8[0]].2n(b))}A 8.2k(x(){z(1j a==\'1J\'){$.F[\'2m\'+a+\'1d\'].1r($.F,[8].2n(b))}1m{$.F.20(8,a)}})};$.F=I 1d()})(4s);',62,277,'||||||||this|||||||||||||||||||||var|_periods|||function||if|return|null|_get||_show|countdown|_minDigits||new|_since|match|Date||getTime||_hold|getDate|Math||periods|span|getMonth||getFullYear||data||break||_timerTargets|options|_now|_until|case|parseInt|100|false|for|_getDaysInMonth|Countdown|labels|compactLabels|_defaults|layout|markerClassName|typeof|_updateCountdown|pause|else|class|extend|60|_removeTarget|apply|_calculatePeriods|_determineTime|min|floor|regional|timeSeparator|description|_resetExtraLabels|extendRemove|UTCDate|getSeconds|getMinutes|getHours|_addTarget|_hasTarget|length|_expiring|string|in|countdown_row|isRTL|until|since|timezone|format|compact|expiryUrl|expiryText|alwaysExpire|onExpiry|onTick|prototype|_updateTargets|setUTCDate|_attachCountdown|hasClass|_changeCountdown|_generateHTML|lap|true|_adjustSettings|Ll|abels|_savePeriods|_determineShow|setTime|1000|exec|setMilliseconds|countdown_amount|_buildLayout|countdown_holding|RegExp|replace|each|setDate|_|concat|Years|Months|Weeks|Days|Hours|Minutes|Seconds|labels1|Year|Month|Week|Day|Hour|Minute|Second|dHMS|hasCountdown|_timer|setInterval|980|setDefaults|object|constructor|getMilliseconds|setUTCFullYear|setUTCMonth|setUTCHours|setUTCMinutes|abs|setUTCSeconds|setUTCMilliseconds|addClass|push|inArray|map|html|add|remove||Class||countdown_rtl|window|location|_destroyCountdown|removeClass|empty|removeData|_pauseCountdown|_lapCountdown|_resumeCountdown|_getTimesCountdown|getTimezoneOffset|toLowerCase|while|switch|number|countdown_section|br|countdown_show|countdown_descr|desc|sep|yl|yn|ynn|ynnn|y1|y10|y100|ol|on|onn|onnn|o1|o10|o100|wl|wn|wnn|wnnn|w1|w10|w100|dl|dn|dnn|dnnn|d1|d10|d100|hl|hn|hnn|hnnn|h1|h10|h100|ml|mn|mnn|mnnn|m1|m10|m100|sl|sn|snn|snnn|s1|s10|s100|yowdhms|charAt|0000000000|substr|max|setFullYear|setMonth|604800|86400|3600|fn|Array|slice|call|arguments|getTimes|jQuery'.split('|'),0,{}))
*/

/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.5.3.
   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. */

/* Display a countdown timer.
   Attach it with options like:
   $('div selector').countdown(
       {until: new Date(2009, 1 - 1, 1, 0, 0, 0), onExpiry: happyNewYear}); */

(function($) { // Hide scope, no $ conflict

/* Countdown manager. */
function Countdown() {
	this.regional = []; // Available regional settings, indexed by language code
	this.regional[''] = { // Default regional settings
		// The display texts for the counters
		labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'],
		// The display texts for the counters if only one
		labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'],
		compactLabels: ['y', 'm', 'w', 'd'], // The compact texts for the counters
		timeSeparator: ':', // Separator for time periods
		isRTL: false // True for right-to-left languages, false for left-to-right
	};
	this._defaults = {
		until: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count down to
			// or numeric for seconds offset, or string for unit offset(s):
			// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
		since: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count up from
			// or numeric for seconds offset, or string for unit offset(s):
			// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
		timezone: null, // The timezone (hours or minutes from GMT) for the target times,
			// or null for client local
		format: 'dHMS', // Format for display - upper case for always, lower case only if non-zero,
			// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
		layout: '', // Build your own layout for the countdown
		compact: false, // True to display in a compact format, false for an expanded one
		description: '', // The description displayed for the countdown
		expiryUrl: '', // A URL to load upon expiry, replacing the current page
		expiryText: '', // Text to display upon expiry, replacing the countdown
		alwaysExpire: false, // True to trigger onExpiry even if never counted down
		onExpiry: null, // Callback when the countdown expires -
			// receives no parameters and 'this' is the containing division
		onTick: null // Callback when the countdown is updated -
			// receives int[7] being the breakdown by period (based on format)
			// and 'this' is the containing division
	};
	$.extend(this._defaults, this.regional['']);
}

var PROP_NAME = 'countdown';

var Y = 0; // Years
var O = 1; // Months
var W = 2; // Weeks
var D = 3; // Days
var H = 4; // Hours
var M = 5; // Minutes
var S = 6; // Seconds

$.extend(Countdown.prototype, {
	/* Class name added to elements to indicate already configured with countdown. */
	markerClassName: 'hasCountdown',
	
	/* Shared timer for all countdowns. */
	_timer: setInterval(function() { $.countdown._updateTargets(); }, 980),
	/* List of currently active countdown targets. */
	_timerTargets: [],
	
	/* Override the default settings for all instances of the countdown widget.
	   @param  options  (object) the new settings to use as defaults */
	setDefaults: function(options) {
		this._resetExtraLabels(this._defaults, options);
		extendRemove(this._defaults, options || {});
	},

	/* Convert a date/time to UTC.
	   @param  tz     (number) the hour or minute offset from GMT, e.g. +9, -360
	   @param  year   (Date) the date/time in that timezone or
	                  (number) the year in that timezone
	   @param  month  (number, optional) the month (0 - 11) (omit if year is a Date)
	   @param  day    (number, optional) the day (omit if year is a Date)
	   @param  hours  (number, optional) the hour (omit if year is a Date)
	   @param  mins   (number, optional) the minute (omit if year is a Date)
	   @param  secs   (number, optional) the second (omit if year is a Date)
	   @param  ms     (number, optional) the millisecond (omit if year is a Date)
	   @return  (Date) the equivalent UTC date/time */
	UTCDate: function(tz, year, month, day, hours, mins, secs, ms) {
		if (typeof year == 'object' && year.constructor == Date) {
			ms = year.getMilliseconds();
			secs = year.getSeconds();
			mins = year.getMinutes();
			hours = year.getHours();
			day = year.getDate();
			month = year.getMonth();
			year = year.getFullYear();
		}
		var d = new Date();
		d.setUTCFullYear(year);
		d.setUTCDate(1);
		d.setUTCMonth(month || 0);
		d.setUTCDate(day || 1);
		d.setUTCHours(hours || 0);
		d.setUTCMinutes((mins || 0) - (Math.abs(tz) < 30 ? tz * 60 : tz));
		d.setUTCSeconds(secs || 0);
		d.setUTCMilliseconds(ms || 0);
		return d;
	},

	/* Attach the countdown widget to a div.
	   @param  target   (element) the containing division
	   @param  options  (object) the initial settings for the countdown */
	_attachCountdown: function(target, options) {
		var $target = $(target);
		if ($target.hasClass(this.markerClassName)) {
			return;
		}
		$target.addClass(this.markerClassName);
		var inst = {options: $.extend({}, options),
			_periods: [0, 0, 0, 0, 0, 0, 0]};
		$.data(target, PROP_NAME, inst);
		this._changeCountdown(target);
	},

	/* Add a target to the list of active ones.
	   @param  target  (element) the countdown target */
	_addTarget: function(target) {
		if (!this._hasTarget(target)) {
			this._timerTargets.push(target);
		}
	},

	/* See if a target is in the list of active ones.
	   @param  target  (element) the countdown target
	   @return  (boolean) true if present, false if not */
	_hasTarget: function(target) {
		return ($.inArray(target, this._timerTargets) > -1);
	},

	/* Remove a target from the list of active ones.
	   @param  target  (element) the countdown target */
	_removeTarget: function(target) {
		this._timerTargets = $.map(this._timerTargets,
			function(value) { return (value == target ? null : value); }); // delete entry
	},

	/* Update each active timer target. */
	_updateTargets: function() {
		for (var i = 0; i < this._timerTargets.length; i++) {
			this._updateCountdown(this._timerTargets[i]);
		}
	},

	/* Redisplay the countdown with an updated display.
	   @param  target  (jQuery) the containing division
	   @param  inst    (object) the current settings for this instance */
	_updateCountdown: function(target, inst) {
		var $target = $(target);
		inst = inst || $.data(target, PROP_NAME);
		if (!inst) {
			return;
		}
		$target.html(this._generateHTML(inst));
		$target[(this._get(inst, 'isRTL') ? 'add' : 'remove') + 'Class']('countdown_rtl');
		var onTick = this._get(inst, 'onTick');
		if (onTick) {
			onTick.apply(target, [inst._hold != 'lap' ? inst._periods :
				this._calculatePeriods(inst, inst._show, new Date())]);
		}
		var expired = inst._hold != 'pause' &&
			(inst._since ? inst._now.getTime() <= inst._since.getTime() :
			inst._now.getTime() >= inst._until.getTime());
		if (expired && !inst._expiring) {
			inst._expiring = true;
			if (this._hasTarget(target) || this._get(inst, 'alwaysExpire')) {
				this._removeTarget(target);
				var onExpiry = this._get(inst, 'onExpiry');
				if (onExpiry) {
					onExpiry.apply(target, []);
				}
				var expiryText = this._get(inst, 'expiryText');
				if (expiryText) {
					var layout = this._get(inst, 'layout');
					inst.options.layout = expiryText;
					this._updateCountdown(target, inst);
					inst.options.layout = layout;
				}
				var expiryUrl = this._get(inst, 'expiryUrl');
				if (expiryUrl) {
					window.location = expiryUrl;
				}
			}
			inst._expiring = false;
		}
		else if (inst._hold == 'pause') {
			this._removeTarget(target);
		}
		$.data(target, PROP_NAME, inst);
	},

	/* Reconfigure the settings for a countdown div.
	   @param  target   (element) the containing division
	   @param  options  (object) the new settings for the countdown or
	                    (string) an individual property name
	   @param  value    (any) the individual property value
	                    (omit if options is an object) */
	_changeCountdown: function(target, options, value) {
		options = options || {};
		if (typeof options == 'string') {
			var name = options;
			options = {};
			options[name] = value;
		}
		var inst = $.data(target, PROP_NAME);
		if (inst) {
			this._resetExtraLabels(inst.options, options);
			extendRemove(inst.options, options);
			this._adjustSettings(inst);
			$.data(target, PROP_NAME, inst);
			var now = new Date();
			if ((inst._since && inst._since < now) ||
					(inst._until && inst._until > now)) {
				this._addTarget(target);
			}
			this._updateCountdown(target, inst);
		}
	},

	/* Reset any extra labelsn and compactLabelsn entries if changing labels.
	   @param  base     (object) the options to be updated
	   @param  options  (object) the new option values */
	_resetExtraLabels: function(base, options) {
		var changingLabels = false;
		for (var n in options) {
			if (n.match(/[Ll]abels/)) {
				changingLabels = true;
				break;
			}
		}
		if (changingLabels) {
			for (var n in base) { // Remove custom numbered labels
				if (n.match(/[Ll]abels[0-9]/)) {
					base[n] = null;
				}
			}
		}
	},

	/* Remove the countdown widget from a div.
	   @param  target  (element) the containing division */
	_destroyCountdown: function(target) {
		var $target = $(target);
		if (!$target.hasClass(this.markerClassName)) {
			return;
		}
		this._removeTarget(target);
		$target.removeClass(this.markerClassName).empty();
		$.removeData(target, PROP_NAME);
	},

	/* Pause a countdown widget at the current time.
	   Stop it running but remember and display the current time.
	   @param  target  (element) the containing division */
	_pauseCountdown: function(target) {
		this._hold(target, 'pause');
	},

	/* Pause a countdown widget at the current time.
	   Stop the display but keep the countdown running.
	   @param  target  (element) the containing division */
	_lapCountdown: function(target) {
		this._hold(target, 'lap');
	},

	/* Resume a paused countdown widget.
	   @param  target  (element) the containing division */
	_resumeCountdown: function(target) {
		this._hold(target, null);
	},

	/* Pause or resume a countdown widget.
	   @param  target  (element) the containing division
	   @param  hold    (string) the new hold setting */
	_hold: function(target, hold) {
		var inst = $.data(target, PROP_NAME);
		if (inst) {
			if (inst._hold == 'pause' && !hold) {
				inst._periods = inst._savePeriods;
				var sign = (inst._since ? '-' : '+');
				inst[inst._since ? '_since' : '_until'] =
					this._determineTime(sign + inst._periods[0] + 'y' +
						sign + inst._periods[1] + 'o' + sign + inst._periods[2] + 'w' +
						sign + inst._periods[3] + 'd' + sign + inst._periods[4] + 'h' + 
						sign + inst._periods[5] + 'm' + sign + inst._periods[6] + 's');
				this._addTarget(target);
			}
			inst._hold = hold;
			inst._savePeriods = (hold == 'pause' ? inst._periods : null);
			$.data(target, PROP_NAME, inst);
			this._updateCountdown(target, inst);
		}
	},

	/* Return the current time periods.
	   @param  target  (element) the containing division
	   @return  (number[7]) the current periods for the countdown */
	_getTimesCountdown: function(target) {
		var inst = $.data(target, PROP_NAME);
		return (!inst ? null : (!inst._hold ? inst._periods :
			this._calculatePeriods(inst, inst._show, new Date())));
	},

	/* Get a setting value, defaulting if necessary.
	   @param  inst  (object) the current settings for this instance
	   @param  name  (string) the name of the required setting
	   @return  (any) the setting's value or a default if not overridden */
	_get: function(inst, name) {
		return (inst.options[name] != null ?
			inst.options[name] : $.countdown._defaults[name]);
	},
	
	/* Calculate interal settings for an instance.
	   @param  inst  (object) the current settings for this instance */
	_adjustSettings: function(inst) {
		var now = new Date();
		var timezone = this._get(inst, 'timezone');
		timezone = (timezone == null ? -new Date().getTimezoneOffset() : timezone);
		inst._since = this._get(inst, 'since');
		if (inst._since) {
			inst._since = this.UTCDate(timezone, this._determineTime(inst._since, null));
		}
		inst._until = this.UTCDate(timezone, this._determineTime(this._get(inst, 'until'), now));
		inst._show = this._determineShow(inst);
	},

	/* A time may be specified as an exact value or a relative one.
	   @param  setting      (string or number or Date) - the date/time value
	                        as a relative or absolute value
	   @param  defaultTime  (Date) the date/time to use if no other is supplied
	   @return  (Date) the corresponding date/time */
	_determineTime: function(setting, defaultTime) {
		var offsetNumeric = function(offset) { // e.g. +300, -2
			var time = new Date();
			time.setTime(time.getTime() + offset * 1000);
			return time;
		};
		var offsetString = function(offset) { // e.g. '+2d', '-4w', '+3h +30m'
			offset = offset.toLowerCase();
			var time = new Date();
			var year = time.getFullYear();
			var month = time.getMonth();
			var day = time.getDate();
			var hour = time.getHours();
			var minute = time.getMinutes();
			var second = time.getSeconds();
			var pattern = /([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g;
			var matches = pattern.exec(offset);
			while (matches) {
				switch (matches[2] || 's') {
					case 's': second += parseInt(matches[1], 10); break;
					case 'm': minute += parseInt(matches[1], 10); break;
					case 'h': hour += parseInt(matches[1], 10); break;
					case 'd': day += parseInt(matches[1], 10); break;
					case 'w': day += parseInt(matches[1], 10) * 7; break;
					case 'o':
						month += parseInt(matches[1], 10); 
						day = Math.min(day, $.countdown._getDaysInMonth(year, month));
						break;
					case 'y':
						year += parseInt(matches[1], 10);
						day = Math.min(day, $.countdown._getDaysInMonth(year, month));
						break;
				}
				matches = pattern.exec(offset);
			}
			return new Date(year, month, day, hour, minute, second, 0);
		};
		var time = (setting == null ? defaultTime :
			(typeof setting == 'string' ? offsetString(setting) :
			(typeof setting == 'number' ? offsetNumeric(setting) : setting)));
		if (time) time.setMilliseconds(0);
		return time;
	},

	/* Determine the number of days in a month.
	   @param  year   (number) the year
	   @param  month  (number) the month
	   @return  (number) the days in that month */
	_getDaysInMonth: function(year, month) {
		return 32 - new Date(year, month, 32).getDate();
	},

	/* Generate the HTML to display the countdown widget.
	   @param  inst  (object) the current settings for this instance
	   @return  (string) the new HTML for the countdown display */
	_generateHTML: function(inst) {
		// Determine what to show
		inst._periods = periods = (inst._hold ? inst._periods :
			this._calculatePeriods(inst, inst._show, new Date()));
		// Show all 'asNeeded' after first non-zero value
		var shownNonZero = false;
		var showCount = 0;
		for (var period = 0; period < inst._show.length; period++) {
			shownNonZero |= (inst._show[period] == '?' && periods[period] > 0);
			inst._show[period] = (inst._show[period] == '?' && !shownNonZero ? null : inst._show[period]);
			showCount += (inst._show[period] ? 1 : 0);
		}
		var compact = this._get(inst, 'compact');
		var layout = this._get(inst, 'layout');
		var labels = (compact ? this._get(inst, 'compactLabels') : this._get(inst, 'labels'));
		var timeSeparator = this._get(inst, 'timeSeparator');
		var description = this._get(inst, 'description') || '';
		var showCompact = function(period) {
			var labelsNum = $.countdown._get(inst, 'compactLabels' + periods[period]);
			return (inst._show[period] ? periods[period] +

				(labelsNum ? labelsNum[period] : labels[period]) + ' ' : '');
		};
		var showFull = function(period) {
			var labelsNum = $.countdown._get(inst, 'labels' + periods[period]);
			return (inst._show[period] ?
				'<span class="countdown_section"><span class="countdown_amount">' +
				periods[period] + '</span><br/>' +
				(labelsNum ? labelsNum[period] : labels[period]) + '</span>' : '');
		};
		return (layout ? this._buildLayout(inst, layout, compact) :
			((compact ? // Compact version
			'<span class="countdown_row countdown_amount' +
			(inst._hold ? ' countdown_holding' : '') + '">' + 
			showCompact(Y) + showCompact(O) + showCompact(W) + showCompact(D) + 
			(inst._show[H] ? this._minDigits(periods[H], 2) : '') +
			(inst._show[M] ? (inst._show[H] ? timeSeparator : '') +
			this._minDigits(periods[M], 2) : '') +
			(inst._show[S] ? (inst._show[H] || inst._show[M] ? timeSeparator : '') +
			this._minDigits(periods[S], 2) : '') :
			// Full version
			'<span class="countdown_row countdown_show' + showCount +
			(inst._hold ? ' countdown_holding' : '') + '">' +
			showFull(Y) + showFull(O) + showFull(W) + showFull(D) +
			showFull(H) + showFull(M) + showFull(S)) + '</span>' +
			(description ? '<span class="countdown_row countdown_descr">' + description + '</span>' : '')));
	},

	/* Construct a custom layout.
	   @param  inst     (object) the current settings for this instance
	   @param  layout   (string) the customised layout
	   @param  compact  (boolean) true if using compact labels
	   @return  (string) the custom HTML */
	_buildLayout: function(inst, layout, compact) {
		var labels = this._get(inst, (compact ? 'compactLabels' : 'labels'));
		var labelFor = function(index) {
			return ($.countdown._get(inst,
				(compact ? 'compactLabels' : 'labels') + inst._periods[index]) ||
				labels)[index];
		};
		var digit = function(value, position) {
			return Math.floor(value / position) % 10;
		};
		var subs = {desc: this._get(inst, 'description'), sep: this._get(inst, 'timeSeparator'),
			yl: labelFor(Y), yn: inst._periods[Y], ynn: this._minDigits(inst._periods[Y], 2),
			ynnn: this._minDigits(inst._periods[Y], 3), y1: digit(inst._periods[Y], 1),
			y10: digit(inst._periods[Y], 10), y100: digit(inst._periods[Y], 100),
			ol: labelFor(O), on: inst._periods[O], onn: this._minDigits(inst._periods[O], 2),
			onnn: this._minDigits(inst._periods[O], 3), o1: digit(inst._periods[O], 1),
			o10: digit(inst._periods[O], 10), o100: digit(inst._periods[O], 100),
			wl: labelFor(W), wn: inst._periods[W], wnn: this._minDigits(inst._periods[W], 2),
			wnnn: this._minDigits(inst._periods[W], 3), w1: digit(inst._periods[W], 1),
			w10: digit(inst._periods[W], 10), w100: digit(inst._periods[W], 100),
			dl: labelFor(D), dn: inst._periods[D], dnn: this._minDigits(inst._periods[D], 2),
			dnnn: this._minDigits(inst._periods[D], 3), d1: digit(inst._periods[D], 1),
			d10: digit(inst._periods[D], 10), d100: digit(inst._periods[D], 100),
			hl: labelFor(H), hn: inst._periods[H], hnn: this._minDigits(inst._periods[H], 2),
			hnnn: this._minDigits(inst._periods[H], 3), h1: digit(inst._periods[H], 1),
			h10: digit(inst._periods[H], 10), h100: digit(inst._periods[H], 100),
			ml: labelFor(M), mn: inst._periods[M], mnn: this._minDigits(inst._periods[M], 2),
			mnnn: this._minDigits(inst._periods[M], 3), m1: digit(inst._periods[M], 1),
			m10: digit(inst._periods[M], 10), m100: digit(inst._periods[M], 100),
			sl: labelFor(S), sn: inst._periods[S], snn: this._minDigits(inst._periods[S], 2),
			snnn: this._minDigits(inst._periods[S], 3), s1: digit(inst._periods[S], 1),
			s10: digit(inst._periods[S], 10), s100: digit(inst._periods[S], 100)};
		var html = layout;
		// Replace period containers: {p<}...{p>}
		for (var i = 0; i < 7; i++) {
			var period = 'yowdhms'.charAt(i);
			var re = new RegExp('\\{' + period + '<\\}(.*)\\{' + period + '>\\}', 'g');
			html = html.replace(re, (inst._show[i] ? '$1' : ''));
		}
		// Replace period values: {pn}
		$.each(subs, function(n, v) {
			var re = new RegExp('\\{' + n + '\\}', 'g');
			html = html.replace(re, v);
		});
		return html;
	},

	/* Ensure a numeric value has at least n digits for display.
	   @param  value  (number) the value to display
	   @param  len    (number) the minimum length
	   @return  (string) the display text */
	_minDigits: function(value, len) {
		value = '0000000000' + value;
		return value.substr(value.length - len);
	},

	/* Translate the format into flags for each period.
	   @param  inst  (object) the current settings for this instance
	   @return  (string[7]) flags indicating which periods are requested (?) or
	            required (!) by year, month, week, day, hour, minute, second */
	_determineShow: function(inst) {
		var format = this._get(inst, 'format');
		var show = [];
		show[Y] = (format.match('y') ? '?' : (format.match('Y') ? '!' : null));
		show[O] = (format.match('o') ? '?' : (format.match('O') ? '!' : null));
		show[W] = (format.match('w') ? '?' : (format.match('W') ? '!' : null));
		show[D] = (format.match('d') ? '?' : (format.match('D') ? '!' : null));
		show[H] = (format.match('h') ? '?' : (format.match('H') ? '!' : null));
		show[M] = (format.match('m') ? '?' : (format.match('M') ? '!' : null));
		show[S] = (format.match('s') ? '?' : (format.match('S') ? '!' : null));
		return show;
	},
	
	/* Calculate the requested periods between now and the target time.
	   @param  inst  (object) the current settings for this instance
	   @param  show  (string[7]) flags indicating which periods are requested/required
	   @param  now   (Date) the current date and time
	   @return  (number[7]) the current time periods (always positive)
	            by year, month, week, day, hour, minute, second */
	_calculatePeriods: function(inst, show, now) {
		// Find endpoints
		inst._now = now;
		inst._now.setMilliseconds(0);
		var until = new Date(inst._now.getTime());
		if (inst._since && now.getTime() < inst._since.getTime()) {
			inst._now = now = until;
		}
		else if (inst._since) {
			now = inst._since;
		}
		else {
			until.setTime(inst._until.getTime());
			if (now.getTime() > inst._until.getTime()) {
				inst._now = now = until;
			}
		}
		// Calculate differences by period
		var periods = [0, 0, 0, 0, 0, 0, 0];
		if (show[Y] || show[O]) {
			// Treat end of months as the same
			var lastNow = $.countdown._getDaysInMonth(now.getFullYear(), now.getMonth());
			var lastUntil = $.countdown._getDaysInMonth(until.getFullYear(), until.getMonth());
			var sameDay = (until.getDate() == now.getDate() ||
				(until.getDate() >= Math.min(lastNow, lastUntil) &&
				now.getDate() >= Math.min(lastNow, lastUntil)));
			var getSecs = function(date) {
				return (date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds();
			};
			var months = Math.max(0,
				(until.getFullYear() - now.getFullYear()) * 12 + until.getMonth() - now.getMonth() +
				((until.getDate() < now.getDate() && !sameDay) ||
				(sameDay && getSecs(until) < getSecs(now)) ? -1 : 0));
			periods[Y] = (show[Y] ? Math.floor(months / 12) : 0);
			periods[O] = (show[O] ? months - periods[Y] * 12 : 0);
			// Adjust for months difference and end of month if necessary
			var adjustDate = function(date, offset, last) {
				var wasLastDay = (date.getDate() == last);
				var lastDay = $.countdown._getDaysInMonth(date.getFullYear() + offset * periods[Y],
					date.getMonth() + offset * periods[O]);
				if (date.getDate() > lastDay) {
					date.setDate(lastDay);
				}
				date.setFullYear(date.getFullYear() + offset * periods[Y]);
				date.setMonth(date.getMonth() + offset * periods[O]);
				if (wasLastDay) {
					date.setDate(lastDay);
				}
				return date;
			};
			if (inst._since) {
				until = adjustDate(until, -1, lastUntil);
			}
			else {
				now = adjustDate(new Date(now.getTime()), +1, lastNow);
			}
		}
		var diff = Math.floor((until.getTime() - now.getTime()) / 1000);
		var extractPeriod = function(period, numSecs) {
			periods[period] = (show[period] ? Math.floor(diff / numSecs) : 0);
			diff -= periods[period] * numSecs;
		};
		extractPeriod(W, 604800);
		extractPeriod(D, 86400);
		extractPeriod(H, 3600);
		extractPeriod(M, 60);
		extractPeriod(S, 1);
		return periods;
	}
});

/* jQuery extend now ignores nulls!
   @param  target  (object) the object to update
   @param  props   (object) the new settings
   @return  (object) the updated object */
function extendRemove(target, props) {
	$.extend(target, props);
	for (var name in props) {
		if (props[name] == null) {
			target[name] = null;
		}
	}
	return target;
}

/* Process the countdown functionality for a jQuery selection.
   @param  command  (string) the command to run (optional, default 'attach')
   @param  options  (object) the new settings to use for these countdown instances
   @return  (jQuery) for chaining further calls */
$.fn.countdown = function(options) {
	var otherArgs = Array.prototype.slice.call(arguments, 1);
	if (options == 'getTimes') {
		return $.countdown['_' + options + 'Countdown'].
			apply($.countdown, [this[0]].concat(otherArgs));
	}
	return this.each(function() {
		if (typeof options == 'string') {
			$.countdown['_' + options + 'Countdown'].apply($.countdown, [this].concat(otherArgs));
		}
		else {
			$.countdown._attachCountdown(this, options);
		}
	});
};

/* Initialise the countdown functionality. */
$.countdown = new Countdown(); // singleton instance

})(jQuery);
