(function($) {
  $(document).ready(function() {

    /* Ensure the correct date is being highlighted as today */

    // Find out which date is currently highlighted as today
    var calTodayHref = $("#calendarTable td.today a").attr("href");
    var calTodayDate = calTodayHref.substring(calTodayHref.length - 10, calTodayHref.length);

    // Format today's date into yyyy-mm-dd format
    var today = new Date();

    var todayDate = today.getDate() + "";
    if (todayDate.length == 1) {
      todayDate = "0" + todayDate;
    }

    var todayMonth = today.getMonth() + 1;
    todayMonth += "";
    if (todayMonth.length == 1) {
      todayMonth = "0" + todayMonth;
    }

    var todayFormatted = today.getFullYear() + "-" + todayMonth + "-" + todayDate;

    // If the calendar's "today's" date is not the same as the real today's date then change the date of "today" in the calendar
    if (calTodayDate != todayFormatted) {

      // Determine if today is the first of the month. If it isn't then simply remove the class from the old date and add the class to the new date
      if (todayDate != "01") {

        // remove class from today's date
        $("#calendarTable td.today").removeClass("today");

        // add the class "today" to the correct date
        $("#calendarTable td a[href$=" + todayFormatted + "]").parent().addClass("today");

      } else {

        // If today is the fist of the month, reload the calendar from Matrix
        
        // remove class from today's date
        $("#calendarTable td.today").removeClass("today");

/*        $("#calendarwrapper").load(
	  $("#eventsCalendarMonth span.nextLink a").attr("href") + "&SQ_PAINT_LAYOUT_NAME=noredirect",
	  {},
	  function() {
	    var correctHref = $("#calendarwrapper a.calendarNavLink").attr("href");
            correctHref = correctHref.substr(0, correctHref.indexOf("?"));
            changeCalendarLinks(correctHref);
          }
	);

        // add the class "today" to the correct date
        $("#calendarTable td a[href$=" + todayFormatted + "]").parent().addClass("today");
*/
      }
    }


    /* Change links in calendar */

    var correctHref = $("#calendarwrapper a.calendarNavLink").attr("href");
    correctHref = correctHref.substr(0, correctHref.indexOf("?"));

    changeCalendarLinks(correctHref);
  });

  var changeCalendarLinks = function(correctHref) {

    // change previous and next month < and > characters into images
    $("#eventsCalendarMonth span.prevLink a").html('<img src="./?a=4863" alt="Previous Month. " title="Previous Month" height="14" width="7" />');
    $("#eventsCalendarMonth span.nextLink a").html('<img src="./?a=4862" alt="Next Month. " title="Next Month" height="14" width="7" />');

    // change links in calendar table and calendar month navigation so that their url is correct
    $("#eventsCalendarMonth a, #calendarTable a").each( function() {
      var hrefSuffix = $(this).attr("href");
      hrefSuffix = hrefSuffix.substr(hrefSuffix.indexOf("?"));
      
      $(this).attr({href: correctHref + hrefSuffix});
    });
    
    // load calendar into page via ajax call
    $("#calendarwrapper a.calendarNavLink").click( function() {
      $("#calendarwrapper").load(
        $(this).attr("href") + "&SQ_PAINT_LAYOUT_NAME=noredirect",
        {},
        function(){changeCalendarLinks(correctHref);}
      );
      return false;
    });
  }; // end of changeCalendarLinks
  
})(jQuery);