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

    // Ensure main nav drop downs work in good ol' IE6 too

    $("ul.nav-pri-list").superfish();
    $("li.nav-pri-7946").hover(function () {
          $(this).addClass("sfHover");
        }, function () {
          $(this).removeClass("sfHover");
        }
      );

    //If anchor rel is Remote Content add an _blank to target attribute
    $('a[@rel=External Redirect Page]').attr('target', '_blank');

    // Create anchor links at the top of the page. The links link to the <h2> tags in the content area of the page.

    if ($("#anchor-links").attr("class") == "yes") {
      $("#anchor-links").append("<ul>");

        var i = 0;
        $("#content-primary-general h2, #content-primary-intranet h2").each(function() {
          // Add unique id to the h2 tag
          var anchorId = "a" + i++;
          $(this).attr({id: anchorId});

          // Create <a> tag in anchor-links div to the heading
          var anchorTag = '<li><a href="#' + anchorId + '">' + $(this).text() + '</a></li>';
          $("#anchor-links ul").append(anchorTag);
        });

      $("#anchor-links").append("</ul>");
      $("#anchor-links").show();
    };

    /*------- Resize text controls ---------*/

    var originalFontSize = $("html").css("font-size");

    // if cookie exists, load saved value
    var $cookie_name = "fndcCurrentFontSize";
    if($.cookie($cookie_name)) {
      var $getSize = $.cookie($cookie_name);
      $("html").css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
    }

    // Reset Font Size
    $("#nav-utility .resetFont").click(function(){
      $("html").css("font-size", "16px");
      $.cookie($cookie_name, null, { path: '/' });  // delete cookie
    });

    // Increase Font Size
    $("#nav-utility .increaseFont").click(function(){
      var currentFontSize = $("html").css("font-size");
      var currentFontSizeNum = parseFloat(currentFontSize, 10);
      var newFontSize = currentFontSizeNum*1.2;
      if (newFontSize, 11) {
        $("html").css("font-size", newFontSize);
        $.cookie($cookie_name, newFontSize, { path: '/' });
      }
      return false;
    });

    /*------- End of Resize text controls ---------*/


    // Add default value to main site search box
    $("#queries_sitesearch_query").DefaultValue("Search the Site");

    // Set up calendar if it is present on page.
    if ($("#calendarWrapper").length > 0) {
    
      /* 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");
      if (calTodayHref != undefined) {
        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. This is to overcome squid caching issues.
        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");

          }

        }
      }

      /* Change links in calendar */

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

      changeCalendarLinks(correctHref);

    };

  });  

  var changeCalendarLinks = function(correctHref) {

    // 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});
    });
    
    // append the date (the 1st of the current month being shown) to the monthly view of events link
    var firstOfMonthHref = $("#calendarTable td.date a").attr("href");
    var monthlyCalendarLinkHref = firstOfMonthHref.replace(/day/,"month");
    $("#calendarLinks a#monthlyCalendarLink").attr({href: monthlyCalendarLinkHref});

    // on click, load calendar into page via ajax call
    $("#calendarWrapper a.calendarNavLink").click(function() {
      $.ajax({
        url: $(this).attr("href") + "&SQ_PAINT_LAYOUT_NAME=noredirect",
        async: false,
        success: function(html){
            $("#calendarWrapper").html(html);
            changeCalendarLinks(correctHref);
        }
      });
      return false;
    });

  }; // end of changeCalendarLinks

    // hides the checkboxes and labels those for cascading settings
    $("input[id*='all_children']").hide();
    $("input[id*='cascade_value']").hide();
    $("label[for*='all_children']").hide();
    $("label[for*='cascade_value']").hide();

})(jQuery);
