/**
 * 周日历
 */
moment.locale('zh-cn');
var calendar_config = {
    calendar_day_focus_class : 'datefocus',
    calendar_day_tpl : '<li {class}  ><a href="javascript:;" {data} >{day}</a></li>',
    calendar_loading : function() {}, //加载日历后的回调函数
    calendar_prev_week_click : function(prev_week_first_day, prev_week_last_day) {}, //点击上一周回调函数
    calendar_next_week_click : function(next_week_first_day, next_week_last_day) {}, //点击下一周回调函数
};

/**
 * 获取日历的头部年周信息
 */
function getCalendarTitle(date) {
    var calendar_year = moment(date).get('year');
    var calendar_month = moment(date).get('month') + 1;
    var calendar_year_week = moment(date).week();//当前周数
    var calendar_title = calendar_year + '年' + calendar_month + '月 第' + calendar_year_week + '周';
    return calendar_title;
}

/**
 * 根据某一天获取这一周的日期显示
 *
 * @param string date 日期
 */
function getCalendarDayShow(date) {
    var calendar_week_day;
    var calendar_week_date;
    var calendar_day_show = '';
    for (var i = 0; i < 7 ;i++) {
        var temp = calendar_config.calendar_day_tpl;
        if (i == 0) {
            calendar_week_day = moment(date).startOf('week').format('DD');
            calendar_week_date = moment(date).startOf('week').format('YYYY-MM-DD');
        }
        else {
            calendar_week_day = moment(date).startOf('week').add(i, 'days').format('DD');
            calendar_week_date = moment(date).startOf('week').add(i, 'days').format('YYYY-MM-DD');
        }
        temp = temp.replace(/{day}/, calendar_week_day);
        temp = temp.replace(/{data}/, 'data-date="' + calendar_week_date + '"');
        temp = temp.replace(/{class}/, (moment().format('YYYY-MM-DD') == calendar_week_date) ? 'class="' + calendar_config.calendar_day_focus_class +  '"' : '');
        calendar_day_show += temp;
    }
    return calendar_day_show;
}

/**
 * 加载日历
 */
function loadCalendar(date) { 
    var date = date || moment().format('YYYY-MM-DD');
    $("#calendar-title").html(getCalendarTitle(date));
    $("#calendar-list").html(getCalendarDayShow(date));
    if (calendar_config.calendar_loading !== undefined) {
        calendar_config.calendar_loading();
    }
}


//下一周
$("#calendar-next-week").on('click', function() {
    var week_last_day = $("#calendar-list li:last-child a").data('date');
    var next_week_first_day = moment(week_last_day).add(1, 'days').format('YYYY-MM-DD');
    var next_week_last_day = moment(week_last_day).add(7, 'days').format('YYYY-MM-DD');
    loadCalendar(next_week_first_day);
    if (calendar_config.calendar_next_week_click !== undefined) {
        calendar_config.calendar_next_week_click(next_week_first_day, next_week_last_day);
    }
    
});

//上一周
$("#calendar-prev-week").on('click', function() {
    var week_first_day = $("#calendar-list li:first-child a").data('date');
    var prev_week_last_day = moment(week_first_day).subtract(1, 'days').format('YYYY-MM-DD');
    var prev_week_first_day = moment(week_first_day).subtract(7, 'days').format('YYYY-MM-DD');
    loadCalendar(prev_week_last_day);
    if (calendar_config.calendar_prev_week_click !== undefined) {
        calendar_config.calendar_prev_week_click(prev_week_first_day, prev_week_last_day);
    }
});

