jQuery(function()
{
  initCalendars();
});


function initCalendars()
{
  // Set up the $_SESSION['Calendars'] array for all 
  // apartments.  Needs to be done in 1 call to the 
  // server otherwise we will have concurrency problems
  // when updating $_SESSION.
  jQuery.post("../php/init_session.php", {},
      function(xml) { changeAllCalendars(); });
}

function changeAllCalendars()
{
  changeCalendar(1);
  changeCalendar(2);
  changeCalendar(3);
}


function setCalendarOffsetHandler( apartmentId, monthOffset )
{
  var cssId;
  
  if (monthOffset < 0)
    cssId = "#calendar_earlier_" + apartmentId;
  else
    cssId = "#calendar_later_" + apartmentId;

  
  jQuery(cssId).hover(function()
  {
    jQuery(this).css({ "background-color":"yellow", color:"black", cursor:"pointer" });
  }, function()
  {
    jQuery(this).css({ "background-color":"#e1e1e1", color:"black", cursor:"auto" });
  });
	
  jQuery(cssId).click(function()
  {	
    // unbind the click handler so user can't click while waiting...
    //alert("Unbinding \"" + cssId + "\"");
    jQuery(cssId).unbind("click");
    
    jQuery.post("../php/set_calendar_month.php",
      { ApartmentId: apartmentId, 
	    MonthOffset: monthOffset },
      function(xml) { changeCalendar(apartmentId); }
    );   
  }); 
}


function changeCalendar(apartmentId)
{ 
  availabilityCalendar("month1",apartmentId,0);
}


function availabilityCalendar(elementId,apartmentId,month)
{ 
  //
  // Need to make these calls synchronous in JavaScript, as the server
  // script updates $_SESSION so we can't have that happening
  // asynchronously (PHP takes a copy of the session and writes
  // the entire variable back when it's finished, so doesn't
  // work concurrently).  The effect of running ascync is that
  // the 'Calendars' array in $_SESSION only contains data for apartment
  // Id 3 (BRT), so the other appts you have to click on the month
  // arrow twice to make it work!!!
  //
  // TO-DO !!!!
  
  
  jQuery.post("../php/availability_calendar.php",
    { 
	  MonthOffset: month,
	  ApartmentId: apartmentId,
	  ElementId: elementId 
	},
    function(xml) { processAvailability(xml); }
  );   
}


function processBookingDetails(xml)
{
  // Called when a calendar date is clicked
  // Anything required for customer facing calendar?
}
 

function processAvailability(xml)
{
  var monthText;
  var yearText;
  var elementId;
  var weekIdx, dayIdx=0;
  var openRowText, closeRowText;
  var weekSelector;
  var tbodyId;
  var dayId, dayIdText;
  var daySelected;
  var apartmentId;
  var dayData, dayStatus, dayNum, classText;
  
  jQuery("calendar",xml).each(function(id)
  {
    var calendarData = jQuery("calendar",xml).get(id);
    monthText = jQuery("month",calendarData).text();
    yearText = jQuery("year",calendarData).text();
    apartmentId = jQuery("apartmentId",calendarData).text();
	monthElementClass = jQuery("elementId",calendarData).text();    
  });
  
  // Empty target calendar div
  var calendarId = "#apartment_calendar_" + apartmentId;
  jQuery(calendarId).empty();
  jQuery(calendarId).append("<div class=\"" + monthElementClass + "\"></div>");
  
  elementId = "#apartment_calendar_" + apartmentId + " div." + monthElementClass;
  
  jQuery(elementId).empty();
  jQuery(elementId).append(
    "<div class=\"calendar_month\">" +
    "<table class=\"availability_calendar\">" +
    "<tr><td colspan=\"1\" class=\"calendar_button\" id=\"calendar_earlier_" + apartmentId + "\">&lt;</td>" + 
	"<th colspan=\"5\">" + getMonthName(monthText) + " " + yearText + "</th>" +
	"<td colspan=\"1\" class=\"calendar_button\" id=\"calendar_later_" + apartmentId + "\">&gt;</td></tr>" + 
    "<tr class=\"days\"><td>S</td><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td></tr>");

  jQuery("day",xml).each(function(id)
  {    
    dayData = jQuery("day",xml).get(id);
    dayStatus = jQuery("status",dayData).text();
    dayNum = jQuery("number",dayData).text();    
    classText = "";
    
    if (dayNum == 0)
      classText = " class=\"empty\"";
    else if (dayStatus == "arrive")
      classText = " class=\"arrive\"";
    else if (dayStatus == "depart")
      classText = " class=\"depart\"";
    else if (dayStatus == "booked")
      classText = " class=\"booked\"";
    
	var dayText;
	  
    if (dayNum == 0)
      dayText = "";
    else
      dayText = dayNum;
    
    if (dayIdx % 7 == 0)
    {
      // Start of a week (i.e. start of row)
      tbodyId = elementId + " table.availability_calendar tbody";
      jQuery(tbodyId).append("<tr id=week" + (dayIdx / 7) + ">");
	}
    else if (dayIdx % 7 == 6)
    {
      // End of a week (i.e. end of row)
      jQuery(tbodyId).append("</tr>");
	}
    
    // Append the cell to the correct row (i.e. in the "current" week)
    dayIdText = yearText + "-" + monthText + "-" + dayText;
    weekSelector = elementId + " tr#week" + Math.floor(dayIdx / 7);
    jQuery(weekSelector).append(
	    "<td" + classText + " id=\"" + dayIdText + "\">" + dayText + "</td>");
    
    
    //
    // Create event handlers for day just created
    //
    /**** No need for hover styles on customer-facing calendars
    if (dayNum != 0)
    {
      dayId = "td#" + dayIdText;
      jQuery(dayId).hover(function()
      {
        jQuery(this).css({ color:"white", cursor:"pointer" });
      }, function()
      {
        jQuery(this).css({ color:"black", cursor:"auto" });
      });
    }  
    **********/
	//
    // End event handlers
    //
	  
	dayIdx++;                          
  });                             
  
  // Configure "month earlier" and "month later" calendar buttons
  setCalendarOffsetHandler(apartmentId,1);
  setCalendarOffsetHandler(apartmentId,-1);
  
}
