var Calendar = Class.create();

Calendar.prototype = {
	
	// Date Object
	objDate : null,
	
	// SelectedDateObject
	selectedDate : null,
	
	// DOM Array
	dateDomArray : new Array,
	
	// the number of days
	DATE_NUM : [31,28,31,30,31,30,31,31,30,31,30,31],
	
	// window (parent DOM)
	wdw : null,
	
	// img dir
	img_dir : "/img/calendar_icon/",
	
	/**
	 * initialize
	 */
	initialize : function( wx, wy, mydate,func ){
		
		if ( !$("caltable") ){
			
			this.setD = func;
			if ( mydate ){
				this.objDate = mydate;
			} else {
				this.objDate = new Date();
			}
			this.selectedDate = this.objDate;
			
			if ( this.objDate == null ){
				
				alert( 'no Date object ...' );
			} else {
				
				// Get window instance.
				var params = {
						w:170,
						h:170,
						x:wx,
						y:wy
					};
					
				this.wdw = new Wndw( params );
				
				// Rendering.
				this.drawCal();
			}
		}
	}, 
	
	/**
	 * drawCal
	 */
	drawCal : function(){
		
		// Make DOM data.
			this.setData();
			
			
		if ( $("caltable") ){
				
			$("caltable").parentNode.removeChild( $("caltable") );
			$("cntrlr").parentNode.removeChild( $("cntrlr") );
		}
		
		// Set title.
		this.wdw.setTitle( this.objDate.getFullYear() + "/" + (this.objDate.getMonth()+1) );
		
		// Set calendar.
		var table = document.createElement("TABLE");
			table.id = "caltable";
			table.className = "calendar";
		
		this.wdw.setContents( table );
		
		$("caltable").insertRow(0);
		
		this.dateDomArray.each( function( value,index ){
			
			if ( value ){
				
				$('caltable').rows[$('caltable').rows.length-1].appendChild( value );
				
				if ( index%7 == 0 && index!=0 ){
					$('caltable').insertRow( $("caltable").rows.length );
				}
			}
		});
		
		// Set Contoroller
		var cntrlr = document.createElement('DIV');
			cntrlr.id = "cntrlr";
		var html = '<table border="0" cellpadding="0" cellspacing="0"><tr><td align="left">';
			html+= '<a id="prev_month" style="cursor:pointer;">前月</a>';
			html+= '</td><td align="center">';
			html+= '<a id="today" style="cursor:pointer;">今月</a>';
			html+= '</td><td align="right">';
			html+= '<a id="next_month" style="cursor:pointer;">次月</a>';
			html+= '</td></tr></table>';
		cntrlr.innerHTML = html;
		this.wdw.setContents( cntrlr );
		Event.observe( $('prev_month'), "click", this.prevMonth.bind(this),false );
		Event.observe( $('next_month'), "click", this.nextMonth.bind(this),false );
		Event.observe( $('today'), "click", this.moveToday.bind(this),false );
	},
	
	/**
	 * setData
	 * Set DOM data
	 */
	setData: function(){
		
		this.dateDomArray = new Array();
		
		// 
		for ( i=1; i<=42; i++ ){
			this.dateDomArray[i] = this.getEmptyCell();
		}
		
		// Get 1st day.
		var firstDate = new Date( this.objDate.getFullYear(), this.objDate.getMonth(), 1 );
		var frrstDay = firstDate.getDay();
		
		// Get dates.
		if ( this.objDate.getFullYear()%4 == 0 ){
			this.DATE_NUM[1] = 29;
		} else if ( this.objDate.getFullYear()%100 == 0 ){
			this.DATE_NUM[1] = 28;
		} else if ( this.objDate.getFullYear()%400 == 0 ){
			this.DATE_NUM[1] = 29;
		}
		var dateNum = this.DATE_NUM[ this.objDate.getMonth() ];
		
		// insert
		var d = frrstDay;
		if  (d == 0){
			d = 7;
		}
		var nowDate = new Date();
		for ( j=1; j<=dateNum; j++ ){
			
			var day = new Date( this.objDate.getFullYear(), this.objDate.getMonth(), j ).getDay();
			
			if ( this.objDate.getFullYear()== this.selectedDate.getFullYear() && this.objDate.getMonth()==this.selectedDate.getMonth() && j==this.selectedDate.getDate() ){
				
				this.dateDomArray[d] = this.getSelectedCell(j);
			} else if ( this.objDate.getFullYear()==nowDate.getFullYear() && this.objDate.getMonth()==nowDate.getMonth() && j==nowDate.getDate() ){
				
				this.dateDomArray[d] = this.getTodayCell(j);
			} else if ( day == "6" ){
				
				this.dateDomArray[d] = this.getSaturdayCell(j);
			} else if ( day == "0" ){
				
				this.dateDomArray[d] = this.getSundayCell(j);
			} else {
				
				this.dateDomArray[d] = this.getWeekdayCell(j);
			}
			this.dateDomArray[d].innerHTML = j;
			this.dateDomArray[d].date = j;
			this.dateDomArray[d].day = day;
			
			d++;
		}
	},
	
	
	// nodata
	getEmptyCell: function(){
		
		var dom = document.createElement('TD');
			dom.className = "nodata";
			
		return dom;
	},
	
	// weekday
	getWeekdayCell: function(d){
		
		var dom = document.createElement('TD');
			dom.className = "weekday";
			
			dom.resultResdponse = {
				year:this.objDate.getFullYear(),
				month:this.objDate.getMonth()+1,
				date:d
			}
			
			Event.observe( dom, 'click', this.setD.bind(dom), false );
			
		return dom;
	},
	
	// saturday
	getSaturdayCell: function(d){
		
		var dom = document.createElement('TD');
			dom.className = "saturday";
			
			dom.resultResdponse = {
				year:this.objDate.getFullYear(),
				month:this.objDate.getMonth()+1,
				date:d
			}
			
			Event.observe( dom, 'click', this.setD.bind(dom), false );
			
		return dom;
	},
	
	// sunday
	getSundayCell: function(d){
		
		var dom = document.createElement('TD');
			dom.className = "sunday";
			
			dom.resultResdponse = {
				year:this.objDate.getFullYear(),
				month:this.objDate.getMonth()+1,
				date:d
			}
			
			Event.observe( dom, 'click', this.setD.bind(dom), false );
			
		return dom;
	},
	
	//getSelectedCell
	getSelectedCell: function(d){
		
		var dom = document.createElement('TD');
			dom.className = "selectedday";
			
			dom.resultResdponse = {
				year:this.objDate.getFullYear(),
				month:this.objDate.getMonth()+1,
				date:d
			}
			
			Event.observe( dom, 'click', this.setD.bind(dom), false );
			
		return dom;
	},
	
	// today
	getTodayCell: function(d){
		
		var dom = document.createElement('TD');
			dom.className = "today";
			
			dom.resultResdponse = {
				year:this.objDate.getFullYear(),
				month:this.objDate.getMonth()+1,
				date:d
			}
			
			Event.observe( dom, 'click', this.setD.bind(dom), false );
			
		return dom;
	},
	
	/**
	 * prevMonth
	 * 
	 * @param {Object} e
	 */
	prevMonth: function( e ){
		
		if ( this.objDate.getMonth+1 == 1 ){
			
			var y = this.objDate.getFullYear()-1;
			
			this.objDate = new Date( y, 12, 1 );
		} else {
			
			var y = this.objDate.getFullYear();
			var m = this.objDate.getMonth()-1;
			this.objDate = new Date( y, m, 1 );
		}
		
		this.drawCal();
	},
	
	/**
	 * nextMonth
	 * @param {Object} e
	 */
	nextMonth: function( e ){
		
		if ( this.objDate.getMonth+1 == 12 ){
			
			var y = this.objDate.getFullYear()+1;
			
			this.objDate = new Date( y, 1, 1 );
		} else {
			
			var y = this.objDate.getFullYear();
			var m = this.objDate.getMonth()+1;
			this.objDate = new Date( y, m, 1 );
		}
		
		this.drawCal();
	},
	
	/**
	 * moveToday
	 * @param {Object} e
	 */
	moveToday: function( e ){
		
		this.objDate = new Date();
		this.drawCal();
	}
}