(function() {    
    function IntervalCalendar(container, cfg) {
        this._iState = 0;
     	this.lastDate = '';       
        cfg = cfg || {};
        cfg.multi_select = true;
        IntervalCalendar.superclass.constructor.call(this, container, cfg);
        this.beforeSelectEvent.subscribe(this._intervalOnBeforeSelect, this, true);
        this.selectEvent.subscribe(this._intervalOnSelect, this, true);
        this.beforeDeselectEvent.subscribe(this._intervalOnBeforeDeselect, this, true);
        this.deselectEvent.subscribe(this._intervalOnDeselect, this, true);
		var months = ["\u4e00\u6708","\u4e8c\u6708", "\u4e09\u6708", "\u56db\u6708", "\u4e94\u6708", "\u516d\u6708", "\u4e03\u6708", "\u516b\u6708", "\u4e5d\u6708", "\u5341\u6708", "\u5341\u4e00\u6708", "\u5341\u4e8c\u6708"];
		var weekdays = ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d"];
		this.cfg.setProperty("MONTHS_SHORT", months);
		this.cfg.setProperty("MONTHS_LONG", months);
		this.cfg.setProperty("WEEKDAYS_1CHAR", weekdays);
		this.cfg.setProperty("WEEKDAYS_SHORT", weekdays);
		this.cfg.setProperty("WEEKDAYS_MEDIUM", weekdays);
		this.cfg.setProperty("WEEKDAYS_LONG", weekdays);
		this.cfg.setProperty("START_WEEKDAY", 1);
		
		this.cfg.setProperty("MY_LABEL_YEAR_POSITION",  1);
		this.cfg.setProperty("MY_LABEL_MONTH_POSITION",  2);
		this.cfg.setProperty("MY_LABEL_YEAR_SUFFIX",  "\u5E74");		
    }
    
    IntervalCalendar._DEFAULT_CONFIG = YAHOO.widget.CalendarGroup._DEFAULT_CONFIG;
    YAHOO.lang.extend(IntervalCalendar, YAHOO.widget.CalendarGroup, {
					  
        _dateString : function(d) {
            var a = [];
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_MONTH_POSITION.key)-1] = (d.getMonth() + 1);
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_DAY_POSITION.key)-1] = d.getDate();
            a[this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.MDY_YEAR_POSITION.key)-1] = d.getFullYear();
            var s = this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.DATE_FIELD_DELIMITER.key);
            return a.join(s);
        },
        
        _dateIntervalString : function(l, u) {
            var s = this.cfg.getProperty(IntervalCalendar._DEFAULT_CONFIG.DATE_RANGE_DELIMITER.key);
            return (this._dateString(l)
                    + s + this._dateString(u));
        },
        
        getInterval : function() {
            
            var dates = this.getSelectedDates();
            if(dates.length > 0) {
                
                var l = dates[0];
                var u = dates[dates.length - 1];
                return [l, u];
            }
            else {
                
                return [];
            }
        },
        
        setInterval : function(d1, d2) {
            
            var b = (d1 <= d2);
            var l = b ? d1 : d2;
            var u = b ? d2 : d1;
            
            this.cfg.setProperty('selected', this._dateIntervalString(l, u), false);
            this._iState = 2;
        },
        
        resetInterval : function() {
            
            this.cfg.setProperty('selected', [], false);
            this._iState = 0;
        },
        
        _intervalOnBeforeSelect : function(t,a,o) {
            
            this._iState = (this._iState + 1) % 3;
            if(this._iState == 0) {
                
                this.deselectAll();
                this._iState++;
            }
        },
        
        _intervalOnSelect : function(t,a,o) {
            
            var dates = this.getSelectedDates();
            if(dates.length > 1) {
                var l = dates[0];
                var u = dates[dates.length - 1];
                this.cfg.setProperty('selected', this._dateIntervalString(l, u), false);
            }
            
            this.render();
        },
        
        _intervalOnBeforeDeselect : function(t,a,o) {
            if(this._iState != 0) {
                return false;
            }
        },
        _intervalOnDeselect : function(t,a,o) {
            if(this._iState != 0) {
                
                this._iState = 0;
                this.deselectAll();
                
                var d = a[0][0];
                var date = YAHOO.widget.DateMath.getDate(d[0], d[1] - 1, d[2]);
                var page = this.getCalendarPage(date);
                if(page) {
                    page.beforeSelectEvent.fire();
                    this.cfg.setProperty('selected', this._dateString(date), false);
                    page.selectEvent.fire([d]);
                }
                
                return false;
            }
        }
    });
    YAHOO.namespace("example.calendar");
    YAHOO.example.calendar.IntervalCalendar = IntervalCalendar;
})();

function s2d(str){
	var d=new Date(str);
	return d; 
}
