var FS_UI_Countdown=new Class({Implements:[Options,Events],options:{'id':null,'debug':false,'stopmessage':'No Time Remaining'},units:{'w':604800,'d':86400,'h':3600,'m':60,'s':1},units_order:['w','d','h','m','s'],debug:function(str){if(!this.options.debug)return;if(typeof console=='undefined')return;var name='FS_UI_Countdown'+(this.options.id?': '+this.options.id:'');return console.log(name+': '+str);},time:function(){return Math.round(new Date().getTime()/1000);},initialize:function(options){this.setOptions(options);if(!this.options.id){this.debug('Missing ID parameter');return;}
this.div=$(this.options.id);if(!this.div){this.debug('Missing element with ID: '+this.options.id);return;}
this.seconds=this.parse(this.div.get('html'));if(this.seconds==null){this.debug('Failed to parse countdown string');return;}
this.debug('Seconds remaining: '+this.seconds);this.timestamp=this.time();this.deadline=this.timestamp+this.seconds;this.debug('Countdown started');this.interval=this.update.periodical(1000,this);this.update();},parse:function(str){this.debug('Parsing: '+str);var seconds=0;components=str.split(/\s+/);for(var i=0;i<components.length;i++){var t=components[i].match(/^(\d+)(\w+)$/);if(t&&t.length==3&&this.units[t[2]]){seconds+=t[1]*this.units[t[2]];}
else{this.debug('Bad time component: '+components[i]);}}
return seconds;},format:function(seconds){var result=[];var zeroes=false;var mult;var interval;$each(this.units_order,function(unit,i){mult=this.units[unit];if(seconds>=mult||mult==1||zeroes){interval=Math.floor(seconds/mult);result.push((zeroes&&interval<10&&mult<=3600?'0':'')+interval+unit);zeroes=true;}
seconds%=mult;},this);return result.join(' ');},update:function(){this.seconds=this.deadline-this.time();if(this.seconds<=0){this.debug('Countdown expired');$clear(this.interval);this.div.set('html',this.options.stopmessage);this.fireEvent('onComplete');return;}
var seconds=this.format(this.seconds);this.div.set('html',seconds);this.fireEvent('onUpdate',this.seconds);}});