window.addEvent("domready",function(){
	new mooTimer($("limit"));
});

/*-----------------------------------------------------
    mooTimer カウントダウン表示スクリプト
	
	@version    1.0.0
	@copyright Tenderfeel
	@author Tenderfeel(tenderfeel@gmail.com)
	@link http://tenderfeel.xsrv.jp/
	@MIT Style License
	
------------------------------------------------------*/	

/*//////////////// O P T I O N S ///////////////////////

	till    :  .tillの初期文字列
	format  :  期限過ぎた時のフォーマット
	after   :  期限過ぎた時の最後につける文句
	day     :  質問番号の前に入るHTML
	hour    :  質問番号の後に入るHTML
	minute  :  WTNへのリンク表示
	sec     :  ボタン文字列
	msec    :  再診断ボタン文字列
	
////////////////// Class and ID ////////////////////////
	
	<p id="limit">
		<em class="till">締切</em>は
		<span class="year">2010</span>年
		<span class="month">5</span>月
		<span class="day">30</span>日
		<span class="hour">30</span>日
		<span class="minute">30</span>日です
	</p>
	
///////////////////////////////////////////////////////*/

(function($,$$) {
	this.mooTimer = new Class({
		
		Implements: [Options],
	
		options: {
			till:'期限',
			format:'%Y年%M月%D日',
			after: 'でした',
			day:true,
			hour:true,
			minute:true,
			sec:true,
			msec:false
		},
		
		initialize: function(element,options) {
			this.setOptions(options);
			$this = this;
			this.element = element;
			if(!this.element)return;
			this.times = this.element.getChildren();
			this.limit = new Hash({ year:0, month:0, day:0, hour:0, minute:0, sec:0});
			this.till = new Element("em",{'class':'till','text':this.options.till});
			this.after = this.options.after;
			this.current = new Date();
			
			this.times.each(function(el,i){
				
				if($this.limit.has(el.className))
					if(el.className=="month") $this.limit.set(el.className,el.get('text').toInt()-1);
					else $this.limit.set(el.className, el.get('text').toInt());
				else if(el.className == "till")
					$this.till = el.clone();
			});
			
			this.limitObj = new Date(this.limit.year, this.limit.month, this.limit.day, this.limit.hour, this.limit.minute, this.limit.sec);
			
			this.times= this.limitObj.getTime() - this.current.getTime();
				
			this.element.empty();
				
			if(this.times > 0){
				this.till.appendText('まであと');
				this.countdown = new Element("strong",{'class':'timer'});
				this.element.grab(this.till).grab(this.countdown);
				var p = (this.options.msec? 1:(this.options.sec? 1000:(this.options.minute? 60000: (this.options.hour ? 3600000:0))));
				this.period = this.count.periodical(p,this);
			}else if(this.times){
				this.end();
			}
			
		},
		end:function(){
			this.element.set('text',this.till.get('text')+'は'+this.showLocalDate(this.options.format,this.limitObj)+this.after);
		},
		count: function() {
			
			this.current = new Date();
			this.times= this.limitObj.getTime() - this.current.getTime();
		
			var day = Math.floor(this.times/(1000*60*60*24));
			this.times -= (day*(1000*60*60*24));
			var hour = Math.floor(this.times/(1000*60*60));
			this.times -= (hour*(1000*60*60));
			var minute = Math.floor(this.times/(1000*60)); 
			this.times -= (minute*(1000*60));
			var sec = Math.floor(this.times/1000);
			this.times -= (sec*(1000));
			var ms = Math.floor(this.times/10);
			
			for(i=0;i<2;i++){ 
				minute = "0" + minute;
				sec = "0" + sec;
				ms = "0" + ms; 
			}
			
			minute = minute.substring(minute.length - 2,minute.length);
			sec = sec.substring(sec.length - 2,sec.length);
			ms = ms.substring(ms.length - 2,ms.length);
			
			if((day) >= 0){
				D = this.options.day ? '<span class="day">'+day+'</span>日':'';
				H = this.options.hour ? '<span class="hour">'+hour+'</span>時間':'';
				M = this.options.minute? '<span class="minute">'+minute+'</span>分':'';
				S = this.options.sec? '<span class="sec">'+sec+'</span>秒':'';
				m = this.options.msec? '<span class="msec">'+ms+'</span>':'';
				this.countdown.set('html',D+H+M+S+m);
			}else{
				$clear(this.period);
				this.end();
			}
			
		},
		showLocalDate:function(format,timestamp){
			var dt = timestamp;
			format = format.replace("%Y",dt.getFullYear());
			format = format.replace("%M",(dt.getMonth()+1));
			format = format.replace("%D",dt.getDate());
			format = format.replace("%h",dt.getHours());
			format = format.replace("%m",dt.getMinutes());
			return format;
		}
		
	});
})(document.id,document.search);
