FujiDreamAirlines.Calendar.settings = function() {
  // dateFormat : accepted values 'yy/mm/dd' or 'yy/m/d'
  var self = this;
  this.holidays = FujiDreamAirlines.Calendar.holidays;
  this.classPrefix = 'dept';
  this.dateFormat = 'yy/mm/dd'; // format: 2010/01/31
  this.minDate = '+0';     // range - from now, until 60 days from today
  this.maxDate = '+5m';
  this.showOn = 'button';
  this.buttonImage = 'http://www.info.fujidreamairlines.com/common_rn/img/icon_calendar01.gif';
  this.buttonImageOnly = true;
  this.buttonText = '';
  this.numberOfMonths = 3;
  this.duration = '';
  this.showButtonPanel = true;
  this.hideIfNoPrevNext = true;
  this.onSelect = function(date) {
    var f = $(this.form);
    var selectPrefix = this.id.split('-')[0];
    // 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
    // trigger the month change event to repopulate the day select element
    $('select[name=' + selectPrefix + 'Month]', f).val(date.split('/')[1]).trigger('change');
    // trigger the day change event to apply changes to check-in/check-out
    var daySelEl = $('select[name=' + selectPrefix + '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 selectPrefix = this.id.split('-')[0];
    var now = FujiDreamAirlines.Calendar.now;
    var month = $('select[name=' + selectPrefix + 'Month]', f).val();
    var day = $('select[name=' + selectPrefix + 'Day]', f).val();
    var year = (month < now.getMonth()) ? now.getFullYear() + 1 : now.getFullYear();
    var selectedDate = new Date(year, (month - 1), day, 23, 59, 59);
    var formatedDate = $.datepicker.formatDate(self.dateFormat, selectedDate);
    $(this).val(formatedDate);  
  };
}

FujiDreamAirlines.Calendar.nThDayOfMonth = function(nth, day, month, year) {
  var firstDate = new Date(year, month, 1);
  var dayModif = 6 - day;
  var date = nth * 7 - (firstDate.getDay() + dayModif) % 7;
  return new Date(year, month, date);
}

FujiDreamAirlines.Calendar.japaneseHolidays = function(year){
  var h = [];  // all holidays
  var hi = []; // holiday index - to find holiday existence
  var th = []; // transfer holidays
  var verDay = parseInt(0.24242 * year - parseInt(year / 4) + 35.84, 10);
  var autDay = parseInt(0.24204 * year - parseInt(year / 4) + 39.01, 10);

  h.push(new Date(year, 0, 1));         // New Year's Day - Jan 1
  h.push(FujiDreamAirlines.Calendar.nThDayOfMonth(2, 1, 0, year)); // Seijin no hi - 2nd Mon of Jan
  h.push(new Date(year, 1, 11));        // Kenkoku kinen no hi - Feb 11
  h.push(new Date(year, 2, verDay));    // Vernal equinox - Around Mar 20
  h.push(new Date(year, 3, 29));        // Showa no hi - Apr 29
  h.push(new Date(year, 4, 3));         // GW: Kenpo kinenbi - May 3
  h.push(new Date(year, 4, 4));         // GW: Midori no hi - May 4
  h.push(new Date(year, 4, 5));         // GW: Kodomo no hi - May 5
  h.push(FujiDreamAirlines.Calendar.nThDayOfMonth(3, 1, 6, year)); // Umi no hi - 3rd Mon of Jul
  h.push(FujiDreamAirlines.Calendar.nThDayOfMonth(3, 1, 8, year)); // Keiro no hi - 3rd Mon of Sep
  h.push(new Date(year, 8, autDay));    // Autumnal equinox - Around Sep 23
  h.push(FujiDreamAirlines.Calendar.nThDayOfMonth(2, 1, 9, year)); // Taiiku no hi - 2nd Mon of Oct
  h.push(new Date(year, 10, 3));        // Bunka no hi - Nov 3
  h.push(new Date(year, 10, 23));       // Kinro kansha no hi - Nov 23
  h.push(new Date(year, 11, 23));       // Tenno tanjobi - Dec 23

  for(var x = 0; x < h.length; x++) {
    if (h[x].getDay() == 0) { // Sun holidays are transfer holidays
      th.push(h[x].getTime());
    }
    h[x] = h[x].getTime();
    hi[h[x]] = true;
  }
  // transfer holidays - Sun holidays are transferred to the next non-holiday date
  for(var x = 0; x < th.length; x++) {
    var nextDay = th[x] + (60 * 60 * 24 * 1000);
    while(hi[nextDay]) {
      nextDay = nextDay + (60 * 60 * 24 * 1000);
    }
    h.push(nextDay);
  }
  // between holidays
  for(var x = 0; x < h.length; x++) {
    var hDay = new Date(h[x]);
    var nextDay = h[x] + (60 * 60 * 24 * 1000);
    var nextNextDay = h[x] + ((60 * 60 * 24 * 1000) * 2);
    if ((hDay.getDay() != 0) && // NOT Sun
        (! hi[nextDay]) &&      // next day is NOT holiday
        (hi[nextNextDay])) {    // next next day IS holiday
      h.push(nextDay);
    }
  }
  return h;
}

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

// holiday range
FujiDreamAirlines.Calendar.holidayStart = FujiDreamAirlines.Calendar.thisYear;
FujiDreamAirlines.Calendar.holidayEnd   = FujiDreamAirlines.Calendar.thisYear + 1;
FujiDreamAirlines.Calendar.holidays = FujiDreamAirlines.Calendar.japaneseHolidays(FujiDreamAirlines.Calendar.holidayStart);
for (var year = FujiDreamAirlines.Calendar.holidayStart + 1; year <= FujiDreamAirlines.Calendar.holidayEnd; year ++) {
  FujiDreamAirlines.Calendar.holidays = FujiDreamAirlines.Calendar.holidays.concat(FujiDreamAirlines.Calendar.japaneseHolidays(year));
}

$(document).ready(function(){
  var fda = FujiDreamAirlines;
  // initialize calendars
  $('input[type=hidden].fdaCal').each(function() {
    var calSettings  = new fda.Calendar.settings();
    $("#" + $(this).attr('id')).datepicker(calSettings);
  });
});
