//<![CDATA[

var popupCal = Class.create({
    initialize: function(options) {
      this.options = {
          syy:        'syy'
          , smm:      'smm'
          , sdd:      'sdd'
          , shh:      'shh'
          , sii:      'sii'
          , eyy:      'eyy'
          , emm:      'emm'
          , edd:      'edd'
          , ehh:      'ehh'
          , eii:      'eii'
          , stay:     'stay'
          , days:     'days'
          , hours:    'hours'
          , sdate:    false
          , edate:    false
          , rsv_sdate:''
          , rsv_edate:''
          , onSelectCallBack:''
      };
      this.calendar = [];

      this.setOptions(options);
      this.load();
    }


    , require: function(libraryName) {
        // inserting via DOM fails in Safari 2.0, so brute force approach
        document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
    }


    , isLoaded: function()
    {
      var isLoadedFlag = false;

      $A(document.getElementsByTagName("script")).findAll( function(s) {
          return (s.src && s.src.match(/calendar(-(setup|ko))?\.js(\?.*)?$/))
      }).each(function() {
        isLoadedFlag = true;
        throw $break;
      }.bind(this));

      return isLoadedFlag;
    }


    , load: function() {
        if (this.isLoaded()) {
          return;
        } // end if

        $A(document.getElementsByTagName("script")).findAll( function(s) {
            return (s.src && s.src.match(/cal\.js(\?.*)?$/))
        }).each( function(s) {
            var path = s.src.replace(/cal\.js(\?.*)?$/,'');
            var includes = s.src.match(/\?.*load=([a-z,]*)/);
            (includes ? includes[1] : 'calendar,calendar-setup,calendar-ko').split(',').each(
                function(include) { this.require(path+include+'.js') }.bind(this));
            // stylesheet
            document.write('<link rel="stylesheet" type="text/css" media="all" href="'+path+'calendar.css" title="summer" />');
        }.bind(this));
    }


    /**
     * @param   object  {year:'syy', month:'smm', day:'sdd', hour:'shh', min:'sii'}
     */
    , setOptions: function(options) {
        this.options = Object.extend(this.options, options || {});
    }


    , getDateElement: function(gubun) {
        gubun = gubun == 's' ? 's' : 'e';
        if (gubun == 's') {
            if ($(this.options.syy) == null && $(this.options.smm) == null && $(this.options.sdd) == null
                && $(this.options.shh) == null && $(this.options.sii) == null 
            ) {
                return false;
            } // end if

            return {yy: $(this.options.syy)
                    , mm: $(this.options.smm)
                    , dd: $(this.options.sdd)
                    , hh: $(this.options.shh)
                    , ii: $(this.options.sii)
            };
        }
        else {
            if ($(this.options.eyy) == null && $(this.options.emm) == null && $(this.options.edd) == null
                && $(this.options.ehh) == null && $(this.options.eii) == null 
            ) {
                return false;
            } // end if

            return {yy: $(this.options.eyy)
                    , mm: $(this.options.emm)
                    , dd: $(this.options.edd)
                    , hh: $(this.options.ehh)
                    , ii: $(this.options.eii)
            };
        } // end if
    }


    , selectStartDate: function(calendar) {
        this.selectDate(calendar, 's');
    }


    , selectEndDate: function(calendar) {
        this.selectDate(calendar, 'e');
    }


    , selectDate: function(calendar, gubun) {
        if (!calendar.dateClicked) {
            return;
        }
        var dateFmt = getDateFmt(calendar.date, 'y-m-d');
        var day     = dateFmt.split('-');
        var dateElement = this.getDateElement(gubun);

        if (this.options.rsv_sdate
            && this.options.rsv_edate
            && (this.options.rsv_sdate > calendar.date || this.options.rsv_edate < calendar.date)
        ) {
            calendar.callCloseHandler(); // this calls "onClose" (see above)

            alert('¿¹¾à °¡´É ±â°£À» ¹þ¾î³³´Ï´Ù.\n\n'
                    + '¿¹¾à °¡´É ±â°£:\n'
                    + getDateFmt(this.options.rsv_sdate, 'Y³â m¿ù dÀÏºÎÅÍ ')
                    + getDateFmt(this.options.rsv_edate, 'Y³â m¿ù dÀÏ±îÁö '));

            return;
        } // end if        
        
        this.setDateValue(dateElement.yy, Number(day[0]));
        this.setDateValue(dateElement.mm, Number(day[1]));
        this.setDateValue(dateElement.dd, Number(day[2]));

        // ¿ù¿¡ µû¶ó ±× ´ÞÀÇ ¸¶Áö¸· ÀÏÀÚ º¯°æ
        this.changeLastDay(calendar.date, gubun);

        this.setDate('s');
        this.setDate('e');
        if (gubun == 's') {
//            this.setDateValueRange();   // ½ÃÀÛÀÏ º¯°æ½Ã ±â°£¿¡ µû¶ó¼­ Á¾·áÀÏÀ» º¯°æÇÑ´Ù.
        } // end if
        this.getPeriod();

        calendar.callCloseHandler(); // this calls "onClose" (see above)

        if (typeof this.options.onSelectCallBack == 'function') {
          this.options.onSelectCallBack(this);
        } // end if
    }


    , setDateValue: function(element, value) {
        element = $(element);
        if (element == null) {
            return;
        } // end if

        element.value = value;
        if (element.type == 'select-one' && element.selectedIndex < 0) {
            element.selectedIndex = 0;
        } // end if
    }


    , setDate: function(gubun) {
        var date;
        var dateElement = this.getDateElement(gubun);
        if (!dateElement) {
            return;
        } // end if

        date = getDateObj(Number($F(dateElement.yy)) + '-'
                        + Number($F(dateElement.mm)) + '-'
                        + Number($F(dateElement.dd)) + ' '
                        + '00:00');

        if (date && dateElement.hh != null) {
            date.setHours(Number($F(dateElement.hh)));
        } // end if

        if (date && dateElement.ii != null) {
            date.setMinutes(Number($F(dateElement.ii)));
        } // end if

        if (gubun == 's') {
            this.options.sdate = date;
        }
        else {
            this.options.edate = date;
        } // end if
    }


    // ½ÃÀÛÀÏ º¯°æ½Ã ±â°£¿¡ µû¶ó¼­ Á¾·áÀÏÀ» º¯°æÇÑ´Ù.
    , setDateValueRange: function() {
        var period, dateFmt, day;

        if (!this.options.sdate || !this.options.edate
            || ($(this.options.stay) == null && $(this.options.days) == null && $(this.options.hours) == null)
        ) {
            return;
        } // end if

        if ($(this.options.stay) != null && (period = Number($F(this.options.stay))) > 0) {
            this.options.edate = operDate(this.options.sdate, 'd', period, '+');
        }
        else if ($(this.options.days) != null && (period = Number($F(this.options.days))) > 0) {
            this.options.edate = operDate(this.options.sdate, 'd', period - 1, '+');
        }
        else if ($(this.options.hours) != null && (period = Number($F(this.options.hours))) > 0) {
            this.options.edate = operDate(this.options.sdate, 'h', period, '+');
        } // end if

        dateFmt = getDateFmt(this.options.edate, 'y-m-d-h-i');
        day     = dateFmt.split('-');

        this.setDateValue(this.options.eyy, Number(day[0]));
        this.setDateValue(this.options.emm, Number(day[1]));
        this.setDateValue(this.options.edd, Number(day[2]));
        this.setDateValue(this.options.ehh, Number(day[3]));
        this.setDateValue(this.options.eii, Number(day[4]));
    }


    , getPeriod: function() {
        var stay    = getDateDiff(this.options.sdate, this.options.edate, 'd');
        var days    = '';
        var hours   = getDateDiff(this.options.sdate, this.options.edate, 'h') || '';
        var stayElement = $(this.options.stay);
        var daysElement = $(this.options.days);
        var hoursElement = $(this.options.hours);

        if (stay === false) {
            stay = '';
            days = '';
        }
        else {
            days = stay + 1;
        } // end if

        if (stayElement != null) {
          if (stay == '' && stayElement.type == 'select-one') {
            stayElement.selectedIndex = 0;
          }
          else {
            stayElement.value = stay;
          }
        } // end if

        if (daysElement != null) {
          if (days == '' && daysElement.type == 'select-one') {
            daysElement.selectedIndex = 0;
          }
          else {
            daysElement.value = days;
          }
        } // end if

        if (hoursElement != null) {
          if (hours == '' && hoursElement.type == 'select-one') {
            hoursElement.selectedIndex = 0;
          }
          else {
            hoursElement.value = hours;
          }
        } // end if
    }


    /**
     * @param   object      date object
     * @param   string      's' or 'e'
     */
    , changeLastDay: function(date, gubun) {
        var dateElement = this.getDateElement(gubun);

        if (dateElement.yy == null || dateElement.mm == null || dateElement.dd == null) {
            return;
        } // end if

        // lib.date.js: getLastDay(), getDateObj()
        var lastDay = getLastDay(date);

        if (dateElement.dd.length > lastDay) {
            // Á¦°Å
            for (var i=dateElement.dd.length-1; i>=lastDay; i--) {
                dateElement.dd.options[i] = null;
            }
        }
        else if (dateElement.dd.length < lastDay) {
            // Ãß°¡
            for (var i=dateElement.dd.length; i<lastDay; i++) {
                dateElement.dd.options[i] = new Option(i+1, i+1);
            }
        } // end if
    }


    , isDisableDate: function(date, y, m, d) {
        return (this.options.rsv_sdate > date || this.options.rsv_edate < date) ? true : false;
    }


    , setupCal: function(params) {
        this.calendar.push(Calendar.setup(params));
    }
});
//var pCal = new popupCal();
//]]>