﻿//fyy
function Calendar(){
	var objcalendar=this;
	//外观
	var nowDayStyle;//当天日期的字体样式
	var especiallyDayStyle;//特殊日期的日期样式
	var tableStyle;//表格样式
	var fontStyle;//所有日期的样式
	var head1Style;//表头tr1
	var head2Style;//表头tr2
	var weekEndHeadStyle;//六 日 两字的样式
	this.setHead1Style=function(className){head1Style=className;}
	this.setHead2Style=function(className){head2Style=className;}
	this.setWeekEndHeadStyle=function(className){weekEndHeadStyle=className;}
	this.setNowDayStyle=function(className){nowDayStyle=className;}
	this.setEspeciallyDayStyle=function(className){especiallyDayStyle=className;}
	this.setTableStyle=function(className){tableStyle=className;}
	this.setFontStyle=function(className){fontStyle=className;}
	
	//日历控件的属性
	especiallyDates=[];//特殊日期的数组  格式为=["2007-7-20","2007-7-10","2007-7-5"] 住 不能2007-07-05这种格式
	isAllDayClick=false;//是否可以点击所有日期
	isNowDayClick=true;//是否可以点击今天
	isEspeciallyClick=true;//是否可以点击特殊日期
	serverDate=new Date();
	this.setAllDayClick=function setAllDayClick(isOn){isAllDayClick=isOn;}
	this.setNowDayClick=function setNowDayClick(isOn){isNowDayClick=isOn;}
	this.setEspeciallyClick=function setEspeciallyClick(isOn){isEspeciallyClick=isOn;}
	this.setEspeciallyDates=function setEspeciallyDates(especiallyDateArray){especiallyDates=especiallyDateArray;}
	this.setServerDate=function setServerDate(date){serverDate=new Date(date);}
	//事件接口
	this.ClickDay=function(date){}//点击的日期
	this.ClickNowDay=function(date){}//点击的今天的日期
	this.ClickEspeciallyDay=function(date){}//点击的特殊的日期
	
	var date;
	var nowDisplayMonth;//当前显示的月份
	var nowDisplayYear;//当前显示的年
	var nowDay;//用于判断今天
	//显示日历
	this.show=function show(calendardiv){
		date=serverDate;
		nowDisplayMonth=date.getMonth();
		nowDisplayYear=date.getFullYear();
		nowDay=date.getDate();
		createTable(calendardiv);//创建表格
		clearTable();
		document.getElementById("c_table_head1_tr").className=head1Style;
		document.getElementById("c_table_head2_tr").className=head2Style;
		document.getElementById("calendarTable").className=tableStyle;
		document.getElementById("goBack").onclick=goBack;
		document.getElementById("goUp").onclick=goUp;
		var we6=document.getElementById("c_table_head1_span_we2");
		var we7=document.getElementById("c_table_head1_span_we1");
		if(weekEndHeadStyle)
		{
			we6.className=weekEndHeadStyle;
			we7.className=weekEndHeadStyle;
		}
		else
		{
			we6.style.color="red";
			we7.style.color="red";
		}
		
		
		showNowTime();//显示当前时间
		window.setInterval(showNowTime,1000);//重复设定显示当前时间的单元格
		fillTable(date.getMonth(),this.especiallyDates);//填充表格
	}
	
	//设置显示当前时间的单元格
	function showNowTime(){
		date=new Date();
		var nowTimeSpan=document.getElementById("NowTimeSpan");
		var nowtime=nowDisplayYear+"-"+(nowDisplayMonth+1)+"-"+date.getDate();
		nowTimeSpan.innerHTML=nowtime;
	}
	//查看以前的月份
	function goBack(){
		if(nowDisplayMonth>0){
			nowDisplayMonth=nowDisplayMonth-1;
			clearTable();
			fillTable(nowDisplayMonth,especiallyDates);
			showNowTime();
		}else{
			nowDisplayYear=nowDisplayYear-1;
			nowDisplayMonth=12;
			goBack();
		}
	}
	//查看以后的月份
	function  goUp(){
		if(nowDisplayMonth<11){
			nowDisplayMonth=nowDisplayMonth+1;
			clearTable();
			fillTable(nowDisplayMonth,especiallyDates);
			showNowTime();
		}else{
			nowDisplayYear=nowDisplayYear+1;
			nowDisplayMonth=1;
			goBack();
		}
	}
	//填充表格
	function fillTable(month){
		var especiallyDate=arguments[1];
		var optionStyle=true;
		date=new Date();
		date.setYear(nowDisplayYear);//设置当前显示的年份
		date.setDate(1);//从1号开始
		date.setMonth(month);//设置当前显示的月份
		
		for(var i=date.getDay()+1,j=1;i<=42;i++){
			var span=document.getElementById("td"+i);
			optionStyle=true;
			date.setDate(j);
			if(date.getMonth()!=month){
				break;
			}
			if(j==nowDay&&date.getFullYear()==(new Date()).getFullYear()&&date.getMonth()==(new Date()).getMonth()&&isNowDayClick){
				optionStyle=false;
				span.className=nowDayStyle;
				if(span.className=="undefined"){
					span.style.cssText="font-size:14px";//设置当天的但日期的样式
				}
				span.onclick=function(){
					objcalendar.ClickNowDay(date.getFullYear()+"-"+date.getMonth()+"-"+this.innerHTML);
				}
				span.style.cursor="pointer";
			}
			if((especiallyDates.length>0)&&isEspeciallyClick){
				var fillDate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
				for(var k=0;k<especiallyDates.length;k++){
					if(fillDate==especiallyDates[k]){
						optionStyle=false;
						span.className=especiallyDayStyle;
						if(span.className=="undefined"){
						span.style.cssText="text-decoration:underline";//设置特殊日期
						}
						span.onclick=function(){
							objcalendar.ClickEspeciallyDay(date.getFullYear()+"-"+date.getMonth()+"-"+this.innerHTML);
						}
						span.style.cursor="pointer";
					}
				}		
			}
			if(optionStyle){
				span.className=fontStyle;
				}
			if(isAllDayClick){
				span.style.cursor="pointer";
				span.onclick=function(){
					objcalendar.ClickDay(date.getFullYear()+"-"+date.getMonth()+"-"+this.innerHTML);
				}
			}
			span.innerHTML=date.getDate();
			j++;
		}
	}
	//清空表格
	function clearTable(){
		for(var i=1;i<=42;i++){
			var span=document.getElementById("td"+i);
			span.innerHTML=" ";
			span.style.cssText="";
			span.className="";
		}
	}
	//创建表格
	function createTable(calendardiv){
		calendardiv.innerHTML="<table width=\"100%\" height=\"100%\" id=\"calendarTable\" cellpadding=\"0\" cellspacing=\"0\">"+
		"<tr align=\"center\" id=\"c_table_head1_tr\">"+
		  "<td><span style=\"cursor:pointer\" id=\"goBack\">&lt;&lt;</span></td>"+
		  "<td colspan=\"5\"><span id=\"NowTimeSpan\">&nbsp;</span></td>"+
		  "<td><span style=\"cursor:pointer\" id=\"goUp\">&gt;&gt;</span></td>"+
		"</tr>"+
		"<tr align=\"center\" id=\"c_table_head2_tr\">"+
		   "<td><span id=\"c_table_head1_span_we1\">日</span></td>"+
		  " <td><span>一</span></td>"+
		  " <td><span>二</span></td>"+
		  " <td><span>三</span></td>"+
		  " <td><span>四</span></td>"+
		  " <td><span>五</span></td>"+
		  " <td><span id=\"c_table_head1_span_we2\">六</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		 "  <td><span id=\"td1\">&nbsp;</span></td>"+
		 "  <td><span id=\"td2\">&nbsp;</span></td>"+
		 "  <td><span id=\"td3\">&nbsp;</span></td>"+
		  " <td><span id=\"td4\">&nbsp;</span></td>"+
		  " <td><span id=\"td5\">&nbsp;</span></td>"+
		 "  <td><span id=\"td6\">&nbsp;</span></td>"+
		  " <td><span id=\"td7\">&nbsp;</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		  " <td><span id=\"td8\">&nbsp;</span></td>"+
		  " <td><span id=\"td9\">&nbsp;</span></td>"+
		  " <td><span id=\"td10\">&nbsp;</span></td>"+
		  " <td><span id=\"td11\">&nbsp;</span></td>"+
		   "<td><span id=\"td12\">&nbsp;</span></td>"+
		   "<td><span id=\"td13\">&nbsp;</span></td>"+
		   "<td><span id=\"td14\">&nbsp;</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		 " <td><span id=\"td15\">&nbsp;</span></td>"+
		  " <td><span id=\"td16\">&nbsp;</span></td>"+
		   "<td><span id=\"td17\">&nbsp;</span></td>"+
		   "<td><span id=\"td18\">&nbsp;</span></td>"+
		   "<td><span id=\"td19\">&nbsp;</span></td>"+
		   "<td><span id=\"td20\">&nbsp;</span></td>"+
		   "<td><span id=\"td21\">&nbsp;</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		   "<td><span id=\"td22\">&nbsp;</span></td>"+
		   "<td><span id=\"td23\">&nbsp;</span></td>"+
		   "<td><span id=\"td24\">&nbsp;</span></td>"+
		   "<td><span id=\"td25\">&nbsp;</span></td>"+
		   "<td><span id=\"td26\">&nbsp;</span></td>"+
		   "<td><span id=\"td27\">&nbsp;</span></td>"+
		   "<td><span id=\"td28\">&nbsp;</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		   "<td><span id=\"td29\">&nbsp;</span></td>"+
		  " <td><span id=\"td30\">&nbsp;</span></td>"+
		   "<td><span id=\"td31\">&nbsp;</span></td>"+
		   "<td><span id=\"td32\">&nbsp;</span></td>"+
		   "<td><span id=\"td33\">&nbsp;</span></td>"+
		  " <td><span id=\"td34\">&nbsp;</span></td>"+
		   "<td><span id=\"td35\">&nbsp;</span></td>"+
		"</tr>"+
			"<tr align=\"center\">"+
		  " <td><span id=\"td36\">&nbsp;</span></td>"+
		  " <td><span id=\"td37\">&nbsp;</span></td>"+
		   "<td><span id=\"td38\">&nbsp;</span></td>"+
		   "<td><span id=\"td39\">&nbsp;</span></td>"+
		   "<td><span id=\"td40\">&nbsp;</span></td>"+
		   "<td><span id=\"td41\">&nbsp;</span></td>"+
		  " <td><span id=\"td42\">&nbsp;</span></td>"+
		"</tr>"+
	  "</table>"
	}
}
