JALHotels.Calendar.settings = function(classPrefix, dateFormat, minDate, maxDate) {
  // dateFormat : accepted values 'yy/mm/dd' or 'yy/m/d'
  var self = this;
  this.holidays = JALHotels.Calendar.holidays;
  this.classPrefix = classPrefix;
  this.dateFormat = dateFormat;
  this.minDate = minDate;
  this.maxDate = maxDate;
  this.showOn = 'button';
  this.buttonImage = '/img/icon_calendar01.gif';
  this.buttonImageOnly = true;
  this.buttonText = 'Calendar';
  this.numberOfMonths = 1;
  this.duration = '';
  this.showButtonPanel = true;
  this.onSelect = function(date) {
    var f = $(this.form);
    // assume date format as either yy/mm/dd(2009/01/01) or yy/m/d(2009/1/1)
    // so we use the slash(/) to split the values
    var yearMonth = date.split('/')[0] + '-' + (parseInt(date.split('/')[1]) - 1);
    $('.' + self.classPrefix + 'YearMonth', f).val(yearMonth).trigger('change');
    var daySelEl = $('.' + self.classPrefix + 'Day', f);
    try {
      daySelEl.val(date.split('/')[2]).trigger('change');
    }
    catch(ex) {
      // this is for IE6, delay setting the value
      setTimeout(function() {
        daySelEl.val(date.split('/')[2]).trigger('change');
      }, 1); 
    }
  };
  this.beforeShow = function() {
    var f = $(this.form);
    var now = new Date();
    var yearMonth =  $('.' + self.classPrefix + 'YearMonth', f).val();
    var year = yearMonth.split('-')[0];
    var month = yearMonth.split('-')[1];
    var day =   $('.' + self.classPrefix + 'Day', f).val();
    var selectedDate = new Date(year, month, day, 23, 59, 59); // Dom rez defaults to today
    var formatedDate = $.datepicker.formatDate(self.dateFormat, selectedDate);
    $(this).val(formatedDate);  
  };
}

JALHotels.Calendar.factory = function(calId, calClass, calSettings) {
  var checkIn  = new calClass.settings(
    'ci',
    calSettings.format,
    calSettings.start,
    calSettings.end
  );
  var checkOut = new calClass.settings(
    'co',
    calSettings.format,
    calSettings.start,
    calSettings.end
  );
  $("#" + calId + "CheckInCal").datepicker(checkIn);
  $("#" + calId + "CheckOutCal").datepicker(checkOut);
}

JALHotels.Calendar.now = new Date();
JALHotels.Calendar.thisYear = JALHotels.Calendar.now.getFullYear();

// Dates
// format: 2009/1/1
// range: today until the end of next year
JALHotels.Calendar.Dates = {};
JALHotels.Calendar.Dates.format = 'yy/m/d';
JALHotels.Calendar.Dates.start ='+0';
JALHotels.Calendar.Dates.end = new Date(JALHotels.Calendar.thisYear + 1, 11, 31);
