﻿function ETongDate(tableId,navigateId)
    {
        this.table = document.getElementById(tableId);
        this.dateRowTempLate = null;
        this.dateCellTempLate = null;
        var rows;
        
        for(var i=0;i<this.table.childNodes.length;i++)
        {
            if(this.table.childNodes[i].nodeName=="TBODY")
                rows = this.table.childNodes[i].childNodes;
        }
        this.date = null;         
        this.today = new Date();  
        this.navigate = document.getElementById(navigateId);
        this.onTdBound = null;
        this.onDateChanging = null;
        this.selectedDates = new Array(31);
        for(var i=0;i<this.selectedDates.length;i++)
            this.selectedDates[i] = null;
        for(var i=0;i<rows.length;i++)
        {
            
            if(rows[i].nodeName=="TR"&&rows[i].getAttribute("rowType")=="dateRowTempLate")  //找到行
            {
                this.dateRowTempLate = rows[i];
                rows[i].style.display = "none";
                var cells = rows[i].childNodes;
                for(var j=0;j<cells.length;j++)
                {
                    if(cells[j].nodeName=="TD"&&cells[j].getAttribute("cellType")=="dateCellTempLate") //找到列
                    {
                        this.dateCellTempLate = cells[j];
                        break;
                    }
                }
                break;
            }
        }
    }
    ETongDate.prototype.showDate = function()
    {
        if(this.date==null)
            this.date = new Date();
        if(this.navigate!=null)
            this.navigate.innerHTML = this.date.getFullYear()+"年"+(this.date.getMonth()+1)+"月";   //显示日期导航
        var rows;
        var body; 
        for(var i=0;i<this.table.childNodes.length;i++)  //获得表格的TBODY,和TR的集合
        {
            if(this.table.childNodes[i].nodeName=="TBODY")
            {
                rows = this.table.childNodes[i].childNodes;
                body = this.table.childNodes[i];
            }
        }
        for(var i=0;i<rows.length;i++)       //先移除先前显示的日期
        {
            if(rows[i].nodeName=="TR"&&rows[i].getAttribute("rowType")=="dateRow")
            {
                body.removeChild(rows[i]);
                i--;
            }
        }
        if(this.dateRowTempLate!=null&&this.dateCellTempLate!=null)
        {
            var days = getDayCount(this.date);   //获得要显示的月的天数
            var dateRow = null;
            for(var day=1;day<=days;day++)
            {
                this.date.setDate(day);   //当前打印显示的日期
                if(this.date.getDay()==0||day==1)  //星期天或当月的第一天时要创建新行
                {
                    dateRow = this.dateRowTempLate.cloneNode(false);
                    dateRow.style.display = "";
                    dateRow.setAttribute("rowType","dateRow");
                    for(var i=0;i<7;i++)
                    {
                        var me = this;
                        var cell = this.dateCellTempLate.cloneNode(false);
                        //cell.onmousedown = function(){me.onTdMouseDown(this,me.date,arguments[0]);}
                        //cell.onclick = function(){me.onTdClick(this,me.date,arguments[0]);}
                        dateRow.appendChild(cell);                        
                    }
                    body.appendChild(dateRow);
                }
                if(this.date.valueOf() == this.today.valueOf())
                {
                    dateRow.childNodes[this.date.getDay()].className = "rili_ing";
                    dateRow.childNodes[this.date.getDay()].innerHTML = "<span style=\"color:red\">"+day+"</span>";
                }
                else
                    dateRow.childNodes[this.date.getDay()].innerHTML = day;  //显示日期
                
                if(this.onTdBound!=null)        //TD显示日期后，触发事件
                    this.onTdBound(this.date,dateRow.childNodes[this.date.getDay()]);
            }
            this.date.setDate(1); 
        }
    }
    ETongDate.prototype.showNextMonth = function()
    {
        this.date.setMonth(this.date.getMonth()+1);
        if(this.onDateChanging!=null)
            this.onDateChanging(this.date);
        this.showDate();
    }
    ETongDate.prototype.showNextYear = function()
    {
        this.date.setYear(this.date.getFullYear()+1);
        if(this.onDateChanging!=null)
            this.onDateChanging(this.date);
        this.showDate();
    }
    ETongDate.prototype.showPrevMonth = function()
    {
        this.date.setMonth(this.date.getMonth()-1);
        if(this.onDateChanging!=null)
            this.onDateChanging(this.date);
        this.showDate();
    }
    ETongDate.prototype.showPrevYear = function()
    {
        this.date.setYear(this.date.getFullYear()-1);
        if(this.onDateChanging!=null)
            this.onDateChanging(this.date);
        this.showDate();
    }
//    ETongDate.prototype.onTdMouseDown = function(td,date,e)
//    {
//        e = window.event || e;
//        if(isIE)
//            e.returnValue = false;
//        else
//            e.preventDefault();

//    }
//    ETongDate.prototype.onTdClick = function(td,date,e)
//    {
//        e = window.event || e;
//        if(!e.ctrlKey)
//        {
//            for(var i=0;i<this.selectedDates.length;i++)
//            {
//                if(this.selectedDates[i])
//                {
//                    this.selectedDates[i].td.style.background = '';
//                    this.selectedDates[i] = null;
//                }
//            }
//            if(this.selectedDates[date.getDay()])
//            {
//                this.selectedDates[date.getDay()].td.style.background = '';
//                this.selectedDates[i] = null;
//            }
//            else
//            {
//                td.style.background = 'red';
//                this.selectedDates[date.getDay()] = {td:td,date:date};
//            }
//        }
//        else
//        {
//            if(this.selectedDates[date.getDay()])
//            {
//                this.selectedDates[date.getDay()].td.style.background = '';
//                this.selectedDates[i] = null;
//            }
//            else
//            {
//                td.style.background = 'red';
//                this.selectedDates[date.getDay()] = {td:td,date:date};
//            }
//        }
//    };
    function getDayCount(date)
    {
        var days = [];
        days[0] = 31;
        days[1] = 0;
        days[2] = 31;
        days[3] = 30;
        days[4] = 31;
        days[5] = 30;
        days[6] = 31;
        days[7] = 31;
        days[8] = 30;
        days[9] = 31;
        days[10] = 30;
        days[11] = 31;
        if(date.getMonth()!=1)
            return days[date.getMonth()];
        else
        {
            if (0==date.getFullYear()%4&&((date.getFullYear()%100!=0)||(date.getFullYear()%400==0)))
                return 29;
            else
                return 28;
        }
    }
