﻿function initCalendar(){
    if(!$('calendar'))return;
    var calendar=new X2.UI.Calendar(null,_calendar.data,_calendar.year,_calendar.month,_calendar.date);

    var cache={};
    cache[calendar.year+'-'+calendar.month]=_calendar.data;

    calendar.onchange=function(date){
        //getBlogByDate(date);
        window.location = "index.aspx?d="+date;
    }

    calendar.onyearchange=function(year){
        var month=calendar.currentMonth;
        return loadData(year,month);
    }

    calendar.onmonthchange=function(month){
        var year=calendar.currentYear;
        if(month==-1){
        year--;
        month=11;
        }
        if(month==12){
            year++;
            month=0;
        }
        return loadData(year,month);
    }

    $('calendarContainer').appendChild(calendar.box);

    function loadData(year,month){
        if(cache[year+'-'+month]){
            calendar.setMark(cache[year+'-'+month]);
            calendar.construct();
            return true;
        }else{
            showLoading();
            var json=new X2.Json('../ajax2.aspx',X2.CommonTipBox);
            json.onlogicalsuccess=function(jso){
                calendar.currentYear=year;
                calendar.currentMonth=month;
                var arr=eval('('+jso.singleInfo+')');
                cache[year+'-'+month]=arr;
                calendar.setMark(arr);
                calendar.construct();
                showTitle();
            }

            json.onfailure=function(){
                alert('数据传输失败!');
                showTitle();
            }
            json.open('getCalendarData',{year:year,month:month+1});
        }
        return false;
    }

    function showLoading(){
        calendar.main.box.childNodes[2].firstChild.innerHTML='<font color="red">loading...</font>';
    }
    function showTitle(){
        calendar.main.box.childNodes[2].firstChild.innerHTML=calendar.currentYear+'年'+(calendar.currentMonth+1)+'月';
    }
}