var gNow = new Date();
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
//var month=["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];

var nowmonthIndex=gNow.getMonth(); //getMonth()獲取当前月份(0-11,0代表1月)
var year=gNow.getFullYear();

var showDIV="";		//显示日歷的控件ID

function changeMonth(stu)
{
	if(stu=="prevMM")
	{
		nowmonthIndex=nowmonthIndex-1;		
		if(nowmonthIndex < 0 )
		{
			nowmonthIndex=11;
			year-=1;
		}
	}
	else if(stu=="nextMM")
	{
		nowmonthIndex=nowmonthIndex+1;
		if(nowmonthIndex > 11 )
		{
			nowmonthIndex=0;
			year+=1;
		}
	}
	//$("#nowmonth").html(month[nowmonthIndex]+"&nbsp;&nbsp;"+year);
	showDate();	
}

function format_day(vday)
{
	var vNowDay = gNow.getDate();
    var vNowMonth = gNow.getMonth();
    var vNowYear = gNow.getFullYear();
	var info="";
	
    if (vday == vNowDay && nowmonthIndex == vNowMonth && year == vNowYear)
	{
        info="<td class=\"today\" style=\"background-color:#666666;\"><FONT COLOR=\"FFFFFF\">" + getProgrameDate(vday,true) + "</FONT></td>";
	}
    else
	{
        info="<td>"+getProgrameDate(vday,false)+"</td>";
	}
	
	return (info);
}		  

//now 是否是当天
function getProgrameDate(vday, now) {
    var info = vday;
    var style = "";
    if (now) {
        style = "style=\"color:#FFFFFF\"";
    }
    var selDate = year + "-" + parseInt(nowmonthIndex + 1) + "-" + vday;
    var url = "programschedules.aspx?date=" + selDate;
    info = "<a " + style + " href=\"" + url + "\">" + vday + "</a>";
    return info;
}

function showDate()
{
	var code="";
	var maxDay=0;
	
	var month1=nowmonthIndex+1;
	if(month1==1 || month1==3 || month1==5 || month1==7 || month1==8 || month1==10 || month1==12){
          maxDay=31;
    }
	else if(month1==4 || month1==6 || month1==9 || month1==11){
           maxDay=30;
	}
	else if(month1==2){
		//闰年
		if((year%4==0 && year%100!=0) || year%400==0){
			maxDay=29;
		}
		else{
			maxDay=28;
		}
	}
	
	//判断每個月的第一天是星期几
	var nowMonthDate=new Date();
	nowMonthDate.setFullYear(year,nowmonthIndex,1);
	var week=nowMonthDate.getDay();		//getDay()獲取当前星期X(0-6,0代表星期天)
	code+="<table id=\"calendar\" cellspacing=\"0\" cellpadding=\"0\"  summary=\"This month's calendar\">";
	code += "<caption><a href=\"javascript:changeMonth('prevMM');\" title=\"previous month\" class=\"nav\" style=\"text-decoration: none;\">&laquo;</a> <span name=\"nowmonth\" id=\"nowmonth\" style=\"display:-moz-inline-box; display:inline-block; width:100px;\">" + month[nowmonthIndex] + "&nbsp;&nbsp;" + year + "</span><a href=\"javascript:changeMonth('nextMM');\" title=\"next month\" class=\"nav\" style=\"text-decoration: none;\">&raquo;</a></caption>";
	code+="<tr>";
	code+="<th scope=\"col\" abbr=\"Sunday\" title=\"Sunday\">S</th>";
	code+="<th scope=\"col\" abbr=\"Monday\" title=\"Monday\">M</th>";
	code+="<th scope=\"col\" abbr=\"Tuesday\" title=\"Tuesday\">T</th>";
	code+="<th scope=\"col\" abbr=\"Wednesday\" title=\"Wednesday\">W</th>";
	code+="<th scope=\"col\" abbr=\"Thursday\" title=\"Thursday\">T</th>";	
	code+="<th scope=\"col\" abbr=\"Friday\" title=\"Friday\">F</th>";
	code+="<th scope=\"col\" abbr=\"Saturday\" title=\"Saturday\">S</th>";
	code+="</tr>";
	code+="<tr>";
	//第一行的日期空白
	for(var i=0;i<week;i++)
	{		
		code+="<td>&nbsp;</td>";
	}	
	var vDay=1;
	//循环第一行日期
	for(var j=week;j<7;j++)
	{
		code+=format_day(vDay);
		vDay+=1;
	}
	code+="</tr>";
	
	//循环日期
	for(var k=2;k<7;k++)
	{
		code+="<tr>";
		for(var i=0;i<7;i++)
		{
			code+=format_day(vDay);
			vDay+=1;
			if(vDay>maxDay)
			{
				//判断每個月的最后一天是星期几
				nowMonthDate.setFullYear(year,nowmonthIndex,maxDay);
				var week1=nowMonthDate.getDay();
				//最后一行的日期空白
				for(var j=1;j<(7-week1);j++)
				{		
					code+="<td>&nbsp;</td>";
				}
				break;
			}
		}
		code+="</tr>";
		if(vDay>maxDay)
		{
			break;
		}		
	}
	
	code+="</table>";	
	$("#"+showDIV).html(code);
}

//show 显示的divID  defaultDate默认显示的日期
function Calendar(show)
{
	showDIV=show;
	showDate();
}

