﻿//  JScript 文件
//  日历
//  Design by Yin Yefeng
//  2007-10-11
//  Version 1.0
function Calendar()
{ 
       
        
        this.GetDataStr=function(y,m){
                this.DayArray=[];
                for(var i=0;i<42;i++)this.DayArray[i]="&nbsp;";
                for(var i=0;i<new Date(y,m,0).getDate();i++)this.DayArray[i+new Date(y,m-1,1).getDay()]=i+1;
                return this.DayArray;
                }
       
       
        this.GetTableStr=function(y,m)
        {
                var str ="";
                this.DateArray=["日","一","二","三","四","五","六"];
                this.DStr="<table oncontextmenu='return false' onselectstart='return false' class='CalendarBody' border='0' cellpadding='0' cellspacing='0'>\n"+
                "<tr><td colspan='7' class='TrOut'>"+
                "<table width='100%' class='CalendarHead' border='0' cellpadding='0' cellspacing='0'><tr align='center'>\n"+
                "<td  style='cursor:pointer;' onclick=\"var yy = document.getElementById('YearTD').innerText.split(' ')[0]; var mm = document.getElementById('MonthTD').innerText.split(' ')[0]; Calendar().Month(yy, mm , 'b'); \" onmouseover='this.style.color=\"#ff9900\"' onmouseout='this.style.color=\"\"'><</td>\n"+
                "<td id='YearTD'  onmousedown='Calendar().WriteSelect(this,this.innerText.split(\" \")[0],\"y\",false)'>"+y+" 年</td>\n"+
                "<td id='MonthTD'  onmousedown='Calendar().WriteSelect(this,this.innerText.split(\" \")[0],\"m\",false)'>"+m+" 月</td>\n"+
                "<td  style='cursor:pointer;' onclick=\"var yy = document.getElementById('YearTD').innerText.split(' ')[0]; var mm = document.getElementById('MonthTD').innerText.split(' ')[0]; Calendar().Month(yy, mm , 'n'); \"  onmouseover='this.style.color=\"#ff9900\"' onmouseout='this.style.color=\"\"'>></td></tr></table>\n"+
                "</td></tr>\n"+
                "<tr align='center'>\n";
                for(var i=0;i<7;i++)
                {
                    this.DStr+="<td class='TrOut'>"+DateArray[i]+"</td>\n";
                }
                this.DStr+="</tr>\n";
                var count = 0;
                for(var i=0;i<6;i++)
                {
                    this.DStr+="<tr align='center' class='CalendarDay'>\n";
                    for(var j=0;j<7;j++)
                    {
                        //判断是否是当前日
                        //var CS=new Date().getDate()==this.GetDataStr(y,m)[i*7+j]?" CurrentDay":"";                                                    
                        this.DStr+="<td id='OxSir"+count+"' onclick=\"Calendar().Day(event);\">"+ this.GetDataStr(y,m)[i*7+j] +"</td>\n";
                        count++;
                    }
                    this.DStr+="</tr>\n";
                }
                this.DStr+="</tabe>";
                return this.DStr;
        }
        
                
        this.WriteSelect=function(obj,values,action,getobj){
       
        if(values=="")return;
        if(getobj){
                obj.innerHTML=values+(action=="y"?" 年":" 月");
                this.RewriteTableStr(document.getElementById("YearTD").innerText.split(" ")[0],document.getElementById("MonthTD").innerText.split(" ")[0]);
                return false;
                }
        var StrArray=[];
        if(action=="y"){
                for(var i=0;i<15;i++){
                        var year=values-7+i;
                        StrArray[i]="<option value='"+year+"' "+(values==year?"selected":"")+"> "+year+"年</option>\n";
                        }
                if(navigator.userAgent.indexOf("MSIE")>0) 
                {
                    obj.innerHTML="<select id='selectYear' onchange='var yy=this.value, mm=document.getElementById(\"MonthTD\").innerText.split(\" \")[0];Calendar().Month(yy,mm,\"c\");Calendar().WriteSelect(parentElement,this.value,\"y\",true);' onblur='document.getElementById(\"YearTD\").innerText=this.value+\" 年\"'>\n"+StrArray.join("")+"</select>";
                }
                else
                {
                    obj.innerHTML="<select id='selectYear' onchange='var yy=this.value, mm=document.getElementById(\"MonthTD\").innerText.split(\" \")[0];Calendar().Month(yy,mm,\"c\");Calendar().WriteSelect(parentNode,this.value,\"y\",true);' onblur='document.getElementById(\"YearTD\").innerText=this.value+\" 年\"'>"+StrArray.join("")+"</select>";
                }
                document.getElementById("selectYear").focus();
                }
        if(action=="m"){
                for(var i=1;i<13;i++)
                        StrArray[i]="<option value='"+i+"' "+(i==values?"selected":"")+"> "+i+"月</option>\n";
                if(navigator.userAgent.indexOf("MSIE")>0) 
                {        
                    obj.innerHTML="<select id='selectMonth' onchange='var yy=document.getElementById(\"YearTD\").innerText.split(\" \")[0]; var mm=this.value; Calendar().Month(yy,mm,\"c\"); Calendar().WriteSelect(parentElement,this.value,\"m\",true)' onblur='document.getElementById(\"MonthTD\").innerText=this.value+\" 月\"'>\n"+StrArray.join("")+"</select>";
                }
                else
                {
                    obj.innerHTML="<select id='selectMonth' onchange='var yy=document.getElementById(\"YearTD\").innerText.split(\" \")[0]; var mm=this.value; Calendar().Month(yy,mm,\"c\"); Calendar().WriteSelect(parentNode,this.value,\"m\",true)' onblur='document.getElementById(\"MonthTD\").innerText=this.value+\" 月\"'>"+StrArray.join("")+"</select>";
                }
                document.getElementById("selectMonth").focus();
                }
        }
        
        
        this.RewriteTableStr=function(y,m)
        {           
            var isSelectedDate;            
            var TArray=this.GetDataStr(y,m);
            for(var i=0;i<TArray.length;i++)
            {
                document.getElementById("OxSir" + i).innerHTML=TArray[i];
                
                //if (this.MonthTable[TArray[i] -1] || this.IsCurrentDay(y, m, TArray[i], "true") == "true")//当前天强制认为有内容，可以点击
                if (this.MonthTable[TArray[i] -1])
                {
                    //有内容
                    document.getElementById("OxSir" + i).setAttribute("isOnClick", "true");
                    document.getElementById("OxSir" + i).setAttribute("year", y);
                    document.getElementById("OxSir" + i).setAttribute("month",m);
                    document.getElementById("OxSir" + i).setAttribute("day", TArray[i]);
                    isSelectedDate = "";
                    if (this.CurrentSelectedYear == y && this.CurrentSelectedMonth == m && this.CurrentSelectedDate == TArray[i])
                    {
                        isSelectedDate = " CurrentSelectedDay ";
                    }
                    document.getElementById("OxSir" + i).className = "OnDay " + this.IsCurrentDay(y, m, TArray[i], "CurrentDay") + isSelectedDate;
                }
                else
                {
                    //无内容
                    document.getElementById("OxSir" + i).setAttribute("isOnClick", "false");
                    document.getElementById("OxSir" + i).className = "OffDay " + this.IsCurrentDay(y, m, TArray[i], "CurrentDay");
                }                       
                
            }
        }
                
                
        this.JumpToRun=function(action)
        {
                var YearNO=document.getElementById("YearTD").innerText.split(' ')[0];
                var MonthNO=document.getElementById("MonthTD").innerText.split(' ')[0];
                if(action=="b"){
                        if(MonthNO=="1"){
                                MonthNO=13;
                                YearNO=YearNO-1;
                                }
                        document.getElementById("MonthTD").innerText=(MonthNO-1)+" 月";
                        document.getElementById("YearTD").innerText=YearNO+" 年";
                        this.RewriteTableStr(YearNO,MonthNO-1);
                        }
                if(action=="n")
                {
                        if(MonthNO=="12"){
                                MonthNO=0;
                                YearNO=YearNO-(-1);
                                }
                        document.getElementById("YearTD").innerText=YearNO+" 年";
                        document.getElementById("MonthTD").innerText=MonthNO-(-1)+" 月";
                        this.RewriteTableStr(YearNO,MonthNO-(-1));
                }
                if (action == "c")
                {
                    this.RewriteTableStr(YearNO, MonthNO);
                }
        }
        
        
        this.CalculateYearMonth = function(y, m, action)
        {
            //根据action计算出新的年份和月份
            var yy = y;
            var mm = m;
            switch (action)
            {
                case "b":
                    if (m == 1)
                    {
                        mm = 12;
                        yy--;
                    }
                    else
                    {
                        mm--;
                    }
                    break;
                case "n":
                    if (m == 12)
                    {
                        mm = 1;
                        yy++;
                    }
                    else
                    {
                        mm++
                    }                    
                    break;
                case "c":
                    break;
            }
            
            return {y:yy, m:mm};            
        }
        
        this.Day=function(ev)
        {
            ev = window.event || ev;
            //var curTarget = ev.srcElement;
            var curTarget = (ev.target) ? ev.target : ev.srcElement;
            if (curTarget.isOnClick == "false") return;            
            
            var y = curTarget.getAttribute("year");
            var m = curTarget.getAttribute("month");
            var d = curTarget.getAttribute("day");
                        
            if (this.CurrentSelectedDay)
            {
                this.CurrentSelectedDay.className = "OnDay " + this.IsCurrentDay(this.CurrentSelectedDay.year, this.CurrentSelectedDay.month, this.CurrentSelectedDay.day, "CurrentDay");
            }
            this.CurrentSelectedDay = curTarget;
            this.CurrentSelectedYear = y;
            this.CurrentSelectedMonth = m;
            this.CurrentSelectedDate = d;
            curTarget.className = "CurrentSelectedDay " + this.IsCurrentDay;(y, m, d, "CurrentDay");
            this.HitDay(y, m, d, this.BlogId, this.Dns, this.MoreMenuId, this.CType, Calendar().DayResponse);
        }
        
        
        this.DayResponse=function(response)
        {
            //response为返回的具体内容
            //返回内容的格式待定
            //如何输出以及输出区域自行定铎
            if (this.UserControl != null)
            {
                this.UserControl(response);
            }
            //alert(response);
        }
        
        
        this.Month=function(y, m, action)
        {
            //改变月份时
           
            //alert(y + "  " + m);
            var d = this.CalculateYearMonth(y, m, action)
            status = (d.y + "  " + d.m);
            this.CurrentSelectedDay = null;
            this.HitMonth(d.y, d.m, action, this.CType, this.ObjectId, Calendar().MonthResponse);
        }
        
        
        this.MonthResponse=function(response)
        {
            if (!response) {return;}
            this.MonthTable = [];
            var tmp = response.split(":");
            var day = tmp[0].split(",");
            for (var f = false, i = 0; i < 31; i++, f = false)
            {            
                for (var len = day.length, j = 0; j < len; j++)
                {
                    if (day[j] == i + 1)
                    {
                        f = true;
                        break;
                    }
                }
                this.MonthTable[i] = f;                
            }
            this.JumpToRun(tmp[1]);
        }
        

        //判断某年某月某日是否为系统当前日
        //若是系统当前日,则返回returnValue, 否则返回""
        this.IsCurrentDay = function(y, m, d, returnValue)
        {                     
            if(new Date().getFullYear() == y && new Date().getMonth() + 1 == m && new Date().getDate() == d)
            {
                return returnValue;
            }
            else
            {
                return "";
            }
        }
        
        
        this.Init = function(hd, hm, show, div, type, objectId, blogId, dns, moreMenuId)
        {
            this.HitDay = hd == null ? function(){;} : hd;
            this.HitMonth = hm == null ? function(){;} : hm;
            this.UserControl = show == null ? function(){;} : show;
            this.CType = type;
            this.ObjectId = objectId;
            this.BlogId = blogId;
            this.Dns = dns;
            this.MoreMenuId = moreMenuId;
            
            //document.write(Calendar().GetTableStr(new Date().getYear(),new Date().getMonth()+1));
            //if (div && div.innerHTML)
            {
                div.innerHTML = Calendar().GetTableStr(new Date().getFullYear(),new Date().getMonth()+1);
                Calendar().Month(new Date().getFullYear(), new Date().getMonth() + 1, 'c');
            }
           
            
        } 
        
        
        return this;
}