var APP_CAL = {
    CURRENT_DATE : new Date(TODAY),
    init : function(){
        var that = APP_CAL;
        that.render();
        $('.calendar_widget caption a:first').click(function(){
            that.prev();
            return false;
        });
        $('.calendar_widget caption a:last').click(function(){
            that.next();
            return false;
        });
    },
    
    render : function(){
        var that = this,
            date = that.CURRENT_DATE,
            year = date.getFullYear(),
            month = date.getMonth() + 1,
            display_month = '',
            day  = date.getDate(),
            caption = '',
            tbody = [];
        display_month = ((month + '').length < 2) ? '0' + month : month;
        day = ((day + '').length < 2) ? '0' + day : day;
        caption = year + '.' + display_month;
        $('.calendar_widget caption span').html(caption);
        
        date = new Date();
        date.setTime(that.CURRENT_DATE.valueOf());
        
        date.setDate(1);
        var weekday = date.getDay();//本月1日是星期几
        date.setMonth(month);   //设置为下月 之前已经+1了
        date.setDate(0);
        var totalDays = date.getDate();
        
        var rowCounter = weekday;
        tbody[tbody.length] = '<tr>';
        for (var i = 0; i < weekday; i++){
            tbody[tbody.length] = '<td>&nbsp;</td>';
        }
        for (var i = 0; i < totalDays; i++){
            var nowDay = i + 1,
                nowDate = year + '/' + display_month + '/' + (((nowDay + '').length < 2) ? '0' + nowDay : nowDay),
                releaseHolder = '<div class="release">&nbsp;</div>',
                eventHolder = '<div class="event">' + nowDay + '</div>';
            
            if(APP_CAL_EVENT['OTECH'] && APP_CAL_EVENT['OTECH'][nowDate]){	//有OTECH就不显示其他的
                releaseHolder = '<div class="OTECH" rel="' + nowDate + '" type="OTECH">' + nowDay + '</div>';
                tbody[tbody.length] = '<td>' + releaseHolder + '</td>';
            } else {
            	 if(APP_CAL_EVENT['release'][nowDate]){
	                releaseHolder = '<div class="release release_avail" rel="' + nowDate + '" type="release">&nbsp;</div>';
	            }
	            if(APP_CAL_EVENT['event'][nowDate]){
	               eventHolder = '<div class="event event_avail" rel="' + nowDate + '" type="event">' + nowDay + '</div>';
	            }
	            tbody[tbody.length] = '<td>' + releaseHolder + eventHolder + '</td>';
            }
                
           
            if(++rowCounter % 7 == 0){
                tbody[tbody.length] = '</tr><tr>';
                rowCounter = 0;
            }
        }
        if(rowCounter > 0){
            for (var i = 7; i > rowCounter; i--){
                tbody[tbody.length] = '<td>&nbsp;</td>';
            }
        }
        tbody[tbody.length] = '</tr>';
        $('.calendar_widget tbody').html(tbody.join(''));
        $('.calendar_widget .release_avail, .calendar_widget .event_avail, .calendar_widget .OTECH').click(that.clickHandler);
    },
    
    prev : function(){
         var that = this,
            date = that.CURRENT_DATE,
            month = date.getMonth();
         
         that.CURRENT_DATE.setDate(1);
         that.CURRENT_DATE.setMonth(month - 1);
         that.render();
    },
    next : function(){
         var that = this,
            date = that.CURRENT_DATE,
            month = date.getMonth();
         that.CURRENT_DATE.setDate(1);
         that.CURRENT_DATE.setMonth(month + 1);
         that.render();
    },
    
    clickHandler : function(e){
        var target=(e)?e.target:event.srcElement,
            target = $(target),
            type = target.attr('type'),
            id  = target.attr('rel').replace(/\//g, '-'),
            html = [],
            popupClass = (type == 'release') ? ' release_popup' : ''
        
        html[html.length] = '<div class="calendar_widget_popup' + popupClass + '"><div class="right"><a href="#" id="calendar_widget_close"><img src="images/closeapp.gif"/></a></div><div id="calendar_widget_popup_container">读取中..</div></div>'    
        kiDulty_PopDiv.show({
			target : $('.calendar_widget tbody'),
			html : html.join(''),
			style:{
				width : '400px'
			},
			clickHandler : function(){
				
				$('#calendar_widget_close').click(function(){
					kiDulty_PopDiv.hide();
					return false;
				});
			}
		});
		
		$.get(BASE + 'user/ajax_cal_event/' + type + '/' + id,{},function(html){
		    $('#calendar_widget_popup_container').html(html);
		});
    }
};