YAHOO.namespace("firstchoice.calendar");

function setCalSelect(selObj, val) {
    if (selObj) {
        for (var i=0; i<selObj.options.length; i++) {
            if (selObj.options[i].value == val) {
                selObj.selectedIndex = i;
                break;
            }
        }
    }
}

function getCalSelect(selObj) {
    if (selObj) {
        if (arguments.length > 1) {
            // split
            var parts = selObj.options[selObj.selectedIndex].value.split(arguments[1]);
            for (var i=0; i<parts.length; i++)
                parts[i] = parseInt(parts[i], 10);
            return parts;
        } else {
            return parseInt(selObj.options[selObj.selectedIndex].value, 10);
        }
    }
}

// this gets called whenever a date is selected on the calendar widget
function calUpdate(type,args,obj) {
        var dates = args[0];
        var date = dates[0];
        var year = parseInt(date[0], 10);
        var month = parseInt(date[1], 10);
        var day = parseInt(date[2], 10);

        var selDate = document.getElementById("scuDateSelect");
        var selMonthYear = document.getElementById("scuMonthYearSelect");
        var selYear = document.getElementById("scuYearSelect");
        var selMonth = document.getElementById("scuMonthSelect");
        var selDay = document.getElementById("scuDaySelect");

        setCalSelect(selDate, month + "/" + day + "/" + year);
        setCalSelect(selMonthYear, month + "/" + year);
        setCalSelect(selYear, year);
        setCalSelect(selMonth, month);
        setCalSelect(selDay, day);

    functionsPanelOverlay( "scuDate", "hide", true );

    // if date panel throbbing is allowed at the moment, then do so
    if( window.searchPanelDateSectionMayThrob ){
      throbFadePanel( "scuDateFade" );
    }
    updateDayOfWeek();
    updateRoom();
    updateSearchPanel('IsuSelectDate');
}


function updateCalWithoutThrob(){
  // mark a global flag so that the date section can't throb while we're getting
  // the calendar up-to-date
  window.searchPanelDateSectionMayThrob = false;
  updateCal();
  window.searchPanelDateSectionMayThrob = true;
}

function updateCalWithSelectedValues()
{
   var selDate      = document.getElementById("scuDateSelect");
   var selMonthYear = document.getElementById("scuMonthYearSelect");
   var selYear      = document.getElementById("scuYearSelect");
   var selMonth     = document.getElementById("scuMonthSelect");
   var selDay       = document.getElementById("scuDaySelect");

   var year  = 2007;
   var month = 1;
   var day   = 1;

   if (selDate) {
      var thedate = getCalSelect(selDate, "/");
      year = thedate[2];
      month = thedate[0];
      day = thedate[1];
   }

   if (selYear)
      year = getCalSelect(selYear);

   if (selMonth)
      month = getCalSelect(selMonth);

   if (selMonthYear) {
      var monthyear = getCalSelect(selMonthYear, "/");
      year = monthyear[1];
      month = monthyear[0];
   }

   if (selDay)
      day = getCalSelect(selDay);

   if ( !isNaN(month) && !isNaN(day) && !isNaN(year) ) {

     var selectedDate = month + "/" + day + "/" + year;

      YAHOO.firstchoice.calendar.scuCalendar.cfg.setProperty("selected", selectedDate, false);
      YAHOO.firstchoice.calendar.scuCalendar.cfg.setProperty("pagedate", month + "/" + year);
      YAHOO.firstchoice.calendar.scuCalendar.render();
   }
}

function updateCal() {
        var selDate = document.getElementById("scuDateSelect");
        var selMonthYear = document.getElementById("scuMonthYearSelect");
        var selYear = document.getElementById("scuYearSelect");
        var selMonth = document.getElementById("scuMonthSelect");
        var selDay = document.getElementById("scuDaySelect");

        var year = 2007;
        var month = 1;
        var day = 1;

        if (selDate) {
            var thedate = getCalSelect(selDate, "/");
            year = thedate[2];
            month = thedate[0];
            day = thedate[1];
        }

        if (selYear)
            year = getCalSelect(selYear);

        if (selMonth)
            month = getCalSelect(selMonth);

        if (selMonthYear) {
            var monthyear = getCalSelect(selMonthYear, "/");
            year = monthyear[1];
            month = monthyear[0];
        }

        if (selDay)
            day = getCalSelect(selDay);

        if (!isNaN(month) && !isNaN(day) && !isNaN(year)) {
            YAHOO.firstchoice.calendar.scuCalendar.select(month + "/" + day + "/" + year);
            YAHOO.firstchoice.calendar.scuCalendar.cfg.setProperty("pagedate", month + "/" + year);
            YAHOO.firstchoice.calendar.scuCalendar.render();
        }
}

function init() {
        YAHOO.firstchoice.calendar.scuCalendar = new YAHOO.widget.Calendar("scuCalendar","scuCalendarContainer", {
            close:false,
            pagedate:"3/2007",
            LOCALE_WEEKDAYS:"medium",
            HIDE_BLANK_WEEKS:true,
            NAV_ARROW_LEFT_TEXT:"&#160;Earlier",
            NAV_ARROW_RIGHT_TEXT:"Later&#160;",
            IFRAME:false
        });

        var inputAvailability = document.getElementById("scuAvailability");
        var availability;

        if (inputAvailability)
            availability = inputAvailability.value;

        if (availability)
            YAHOO.firstchoice.calendar.scuCalendar.cfg.setProperty("available", availability);

    // initialize the calendar widget with the date from the dropdowns
        updateCalWithoutThrob();

        // Lets make sure we have the update
        updateDayOfWeek('yes');

    //set calUpdate() to be called whenever a date is selected on the calendar
        YAHOO.firstchoice.calendar.scuCalendar.selectEvent.subscribe(calUpdate, YAHOO.firstchoice.calendar.scuCalendar, true);

    // hook up the calendar widget so that it updates whenever the date dropdowns change
        YAHOO.util.Event.addListener(["scuDaySelect","scuMonthYearSelect","scuDateSelect"], "change", updateCalWithoutThrob);

}
YAHOO.util.Event.addListener(window, "load", init);




