/*******************************************************************************
		Calendar 日历
		Author: ※绿色...
*******************************************************************************/

function Calendar(){
	var Calendar_MONTHS = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");  
	var Calendar_DAYS = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);  
	//var Calendar_WEEK = new Array("Sunday", "Monday", "Tuesday",  "Wednesday", "Thursday", "Friday", "Saturday");
	var Calendar_WEEK = new Array("日","一", "二", "三",  "四", "五", "六");  
	
	
	// 绑定的控件
	this.bindControl = new Array();
	// 绑定控件的索引
	this.index = -1;
	// 表格控件，用于显示日期
	this.source = null;
	// 上一次选定的日期(引用的TD元素)
	this.previousDate = null;
	this.isAutoHide = false;
	this.isAppendTime = false;
	this.isSynchronized = false;
	this.isCheckExpire = false;
	this.today = new Date();
	this.year = this.today.getFullYear();
	this.month = this.today.getMonth();
	this.date = this.today.getDate();
	this.week = this.today.getDay();
	this.expireDate = new Date(this.year,this.month,this.date,0,0,0,0);
	this.callback = null;
	this.parse = function(str){
		var timeValue = Date.parse(str.replace(/-/g, "/"));
		if(!isNaN(timeValue)){
			return new Date(timeValue);
		}else{
			return this.today;
		}
	};
	// 返回某月中的天数
	this.getDays = function(year,month){
		// 如果是二月,则判断是否为闰年
		if (1 == month){
			return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
		}else{
			return Calendar_DAYS[month];
		}
	}
	this.addControl = function(c,imgSource,beforeEvent, afterEvent){
		if(imgSource == undefined || imgSource == null){
			imgSource = 'images/calendar.gif';
		}
		document.write('<img src="' + imgSource + '" onclick="'+(beforeEvent?beforeEvent:'')+';calendar.show(' + this.bindControl.length + ');" />');
		this.callback = afterEvent;
		this.bindControl.push(c);
		this.index = this.bindControl.length - 1;
		return this.index;
	};
	this.days = this.getDays(this.year,this.month);
	this.setNowDate = function(){
		this.today = new Date();
		if(this.year != this.today.getFullYear() || this.month != this.today.getMonth() || this.date != this.today.getDate()){
			this.year = this.today.getFullYear();
			this.month = this.today.getMonth();
			this.date = this.today.getDate();
			this.week = this.today.getDay();
			this.onchange();
		}
	};
	this.show = function(index){
		if(!this.initialized) return;
		if(index < this.bindControl.length && index>=0){
			this.index = index;
			this.source.parentElement.style.display = 'block';
			this.source.parentElement.style.left = document.body.scrollLeft + event.x;
			this.source.parentElement.style.top = document.body.scrollTop + event.y;
			event.cancelBubble = true;
		}else{
			alert('无效索引,请检查是否添加了控件!');
		}
	}
	this.hide = function(){
		if(!this.initialized) return;
		this.source.parentElement.style.display = 'none';
	}
	this.setYear = function(year){
		year = parseInt(year);
		if(!isNaN(year) && this.year != year){
			this.year = year;
			this.onchange();
		}
	};
	this.setMonth = function(month){
		month = parseInt(month);
		if(!isNaN(month) && this.month != month){
			if(month < 0){
				this.year--;
				this.month = 11;
			}else if(month > 11){
				this.year++;
				this.month = 0
			}else{
				this.month = month;
			}
			this.onchange();
		}
	};
	this.setDate = function(td){
		var date = parseInt(td.innerText);
		if(td.className.indexOf('disabled') != -1) return false;
		if(!isNaN(date) && date >= 1 && date <= 31){
			if(this.date != date){
				this.date = date;
				this.week = td.cellIndex;
				if(this.previousDate != null){
					this.setStype(this.previousDate);
				}
				this.setStype(td);
				this.previousDate = td;
			}
			this.onchangedate();
			if(this.isAutoHide){
				this.hide();
			}
		}
	}
	this.onchangedate = function(){
		try{
			this.today = new Date();
			this.bindControl[this.index].value = this.year + '-' + (this.month+1) + '-' + this.date;
			if(this.isAppendTime){
				//this.bindControl[this.index].value += ' [' + Calendar_WEEK[this.week] + ']';
				this.bindControl[this.index].value += ' ' + this.today.getHours() + ':' + this.today.getMinutes() + ':' + this.today.getSeconds();
			}
			if(this.callback){
				this.callback();
			}
		}catch(e){
			alert('没有绑定到控件或绑定的控件无效!\n'+e.message);
		}
	}
	this.onchange = function(){
		if(!this.initialized) return;
		if(this.source == null){
			this.source = document.all["Calendar_source"];
		}
		this.days = this.getDays(this.year,this.month);
		if(this.date > this.days){
			this.date = this.days;
		}
		var tempDate = new Date(this.year,this.month,this.date);
		this.week = tempDate.getDay();
		var beginRow = 2;
		var row = beginRow,col = 0;
		this.previousDate = null;
		this.source.rows[0].cells[1].innerHTML = '<input type="text" value="' + this.year + '" onchange="calendar.setYear(this.value)" onkeydown="if(event.keyCode==13){calendar.setYear(this.value)}" onkeyup="this.value=this.value.replace(/[^0-9]/g,\'\')" onfocus="this.select()" style="border:0px;width=40px;text-align:right;" />年 <input type="text" value="' + (this.month + 1) + '" onchange="calendar.setMonth(parseInt(this.value)-1)" onkeydown="if(event.keyCode==13){calendar.setMonth(parseInt(this.value)-1)}" onkeyup="this.value=this.value.replace(/[^0-9]/g,\'\')" onfocus="this.select()" style="border:0px;width=20px;text-align:right;" />月';
		this.source.rows[this.source.rows.length-1].cells[0].innerText = '今天: ' + this.today.getYear() + '年' + (this.today.getMonth() + 1) + '月' + this.today.getDate() + '日';
		var beginCell = new Date(this.year,this.month,1).getDay();
		for(col = 0; col < beginCell; col++){
			this.source.rows[row].cells[col].innerText = '';
			this.source.rows[row].cells[col].className = '';
		}
		for(var i = 0; i < this.days; i++,col++){
			if(col > 0 && col % 7 == 0){
				row++;
				col=0;
			}
			this.source.rows[row].cells[col].innerText = i + 1;
			this.setStype(this.source.rows[row].cells[col]);
			if(this.date == i + 1){
				this.previousDate = this.source.rows[row].cells[col];
				if(this.index != -1 && this.isSynchronized){
					this.onchangedate();
				}
			}
		}
		for(; col < 7; col++){
			this.source.rows[row].cells[col].innerText = '';
			this.source.rows[row].cells[col].className = '';
		}
		for(var i = 0; i < this.source.rows.length-1; i++){
			this.source.rows[i].style.visibility = (i <= row ? "visible" : "hidden");
		}
	};
	this.refresh = function(){
		this.onchange();	
	}
	this.setStype = function(td){
		var tempCss = '';
		var day = parseInt(td.innerText);
		if(isNaN(day)){
			// 错误的日期,不设置样式
		}else{
			if(this.isCheckExpire && new Date(this.year,this.month,day).getTime()<this.expireDate.getTime()){
				tempCss=' disabled';
			}else{
				if(day == this.date){
					tempCss += ' current';
				}
				if(this.year == this.today.getYear() && this.month == this.today.getMonth()){
					if(day == this.today.getDate()){
						if(day == this.date){
							tempCss += ' today';
						}
						tempCss += ' hintToday';
					}
				}
				if(tempCss.length == 0 && (td.cellIndex == 0 || td.cellIndex == 6)){
					tempCss += ' playday';
				}
			}
			tempCss = 'hand' + tempCss;
		}
		td.className = tempCss;
	}
	this.initialized = false;
	this.initialize = function(){
		if(!this.initialized){
			if(this.isAutoHide){
				document.onclick = function(){calendar.hide();};
			}
			var sTemp = "";
			sTemp += '<style type="text/css">#Calendar_source,#Calendar_source input{font-size:12px;font-family:Verdana, Arial, Helvetica, sans-serif;}#Calendar_source th{background:#EFEBDB;}#Calendar_source td{text-align:center;}#Calendar_source .hand{cursor:pointer;}#Calendar_source .current{background:black;color:white;font-weight:bold;}#Calendar_source .today{background:#CC0000;color:white;font-weight:bold;}#Calendar_source .playday{color:#CC0000;}#Calendar_source .hintToday{border: solid 2px red;}.disabled{color:gray;}</style>';
			sTemp += '<div style="display:none;position:absolute;width:210px;height:160px;border:1px solid #EFEBDB;background:white;" onclick="event.cancelBubble = true;"><table id="Calendar_source" border="0" cellpadding="0" bgcolor="white" cellspacing="1" height="100%" width="100%" onmousedown="this.parentElement.cx=event.x;this.parentElement.cy=event.y;setCapture();" onmousemover="" onmouseup="releaseCapture();">';
			sTemp += '<tr><td><input type="button" name="btnPreviousMonth" value="&lt;&lt;" onclick="calendar.setMonth(calendar.month-1)" /></td><td colspan="5">&nbsp;</td><td><input type="button" name="btnNextMonth" value="&gt;&gt;" onclick="calendar.setMonth(calendar.month+1)" /></td></tr>';
			sTemp += '<tr>'
			for(var j = 0; j < Calendar_WEEK.length; j++){
				sTemp += '<th width="14%">' + Calendar_WEEK[j] + '</th>';
			}
			sTemp += '</tr>'
			for(var i = 1; i < 7; i++){
				sTemp += '<tr>';
				for(var j = 0; j < Calendar_WEEK.length; j++){
					sTemp += '<td onclick="calendar.setDate(this);" width="30">&nbsp;</td>';
				}
				sTemp += '</tr>';
			}
			sTemp += '<tr><td colspan="5" onclick="calendar.setNowDate()" class="hand" bgcolor="#EFEEE8">&nbsp;</td><td colspan="2" onclick="calendar.hide()" class="hand"><font style="font-size:10px;">CLOSE</font></td></tr>';
			sTemp += '</table></div>';
			document.write(sTemp);
			this.source = document.all["Calendar_source"];
			this.source.style.cursor = "move";
			this.source.onmousedown = function(){
				this.cx = event.x - this.parentNode.style.pixelLeft;
				this.cy = event.y - this.parentNode.style.pixelTop;
				this.setCapture();
			};
			this.source.onmousemove = function(){
				if(event.button == 1){
					this.parentNode.style.pixelLeft = event.x - this.cx;
					this.parentNode.style.pixelTop = event.y - this.cy;
				}
			};
			this.source.onmouseup = function(){
				this.releaseCapture();
			};
			this.initialized = true;
			this.onchange();
		}
	};
}
var calendar = new Calendar();
calendar.isAutoHide = true;
//calendar.isAppendTime = true;
calendar.isSynchronized = false;
calendar.isCheckExpire = true;
calendar.initialize();
