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

               //Remove empty Top Links (because Category is empty) in Publications List
               $('#publications-top-links a:empty').parent('li').remove();

               //Remove Top links DIV completly if they have no or only one category
               if ($("#publications-top-links ul li").length <= 1) {
                  $("#publications-top-links ul li").remove();
               };

               // Remove "Entire site" from root node selector in Search results page if .hide is given. 
               // Works with CSS display:none in Firefox but not in  IE, therefor: Jquery
               $("#site-search-root-node-selector .hide").remove();
     
		//Fix Matrix issue that shows empty uls in menu_areas due to excluded assets
		$('.sf-menu>li>ul').each(function(){
			if($(this).children('li').length == 0){
				$(this).remove();
			}
		})
		
	        // Add feature slider functionality to homepage
		if ($("#page-assetid-2661").length) {
			$("#slider").easySlider({
				numeric: true
			});
			if ($("#controls li").length <= 1){
				$("#controls").hide();
			}
		}


		// Enable superfish for the top navigation
		$("#nav-first>ul").superfish({
			autoArrows:    false,
			animation:   {height:'show'}
		});
		$("#nav-second>ul").superfish({
			animation:   {height:'show'}
		});
		$("#nav-third>ul").superfish({
			autoArrows:    false,
			animation:   {height:'show'}
		});
		$("#nav-third2>ul").superfish({
			autoArrows:    false,
			animation:   {height:'show'}
		});



		// Hide "back to top" link on homepage
		$("#page-assetid-2661 #back-to-top").hide();



		// Hide "back to top" link on homepage
		$("#page-assetid-2661 #content-main-ultrawide h1").hide();


                // Remove empty space on the beginning of H1 if there is no personal title (Prof.)
                $("h1").each(function() {
                   $(this).html($(this).html().replace(/^&nbsp;/, ""));
                });


		// Initiate Calendar function
		calendar();



		// Toggle abstract div of NZJFS
		$('div.nzjfs-abstract-content').hide();

		$('a.nzjfs-abstract-toggle').toggle(
		function () {
			$(this).parent('div').children('div.nzjfs-abstract-content').slideToggle('slow');
			$(this).html("<a class='nzjfs-abstract-toggle' href=''><span class='double-keith'>»</span> Hide abstract</a>");
		},
		function () {
			$(this).parent('div').children('div.nzjfs-abstract-content').slideToggle('slow');
			$(this).html("<a class='nzjfs-abstract-toggle' href=''><span class='double-keith'>»</span> Show abstract</a>");
		}
		);

                // Toggle abstract div for all publications
		$('div.list-publications-abstract-content').hide();

		$('a.abstract-toggle').toggle(
		function () {
			$(this).parent('div').parent('div').children('div.list-publications-abstract-content').slideToggle('slow');
			$(this).html("<a class='abstract-toggle' href=''><span class='double-keith'>»</span> Hide abstract</a>");
		},
		function () {
			$(this).parent('div').parent('div').children('div.list-publications-abstract-content').slideToggle('slow');
			$(this).html("<a class='abstract-toggle' href=''><span class='double-keith'>»</span> Show abstract</a>");
		}
		);

	}) //end document).ready(function() {
})(jQuery); //end (function($)


/*------- Mini calendar ---------*/
function calendar() {

	// 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);

	};

};
// end of calendar()


var changeCalendarLinks = function(correctHref) {

	// change links in calendar table and calendar month navigation so that their url is correct
	var currentMonthDateText = $("#eventsCalendarMonth h3").text();
	var currentMonthText = currentMonthDateText.substr(0, currentMonthDateText.indexOf(" "));
	var currentYearText = currentMonthDateText.substr(currentMonthDateText.indexOf(" ") + 1);
	var currentMonth;

	switch (currentMonthText) {

		case "January":
		currentMonth = "01";
		break;

		case "February":
		currentMonth = "02";
		break;

		case "March":
		currentMonth = "03";
		break;

		case "April":
		currentMonth = "04";
		break;

		case "May":
		currentMonth = "05";
		break;

		case "June":
		currentMonth = "06";
		break;

		case "July":
		currentMonth = "07";
		break;

		case "August":
		currentMonth = "08";
		break;

		case "September":
		currentMonth = "09";
		break;

		case "October":
		currentMonth = "10";
		break;

		case "November":
		currentMonth = "11";
		break;

		case "December":
		currentMonth = "12";
		break;

	}

	currentMonthDate = currentYearText + "-" + currentMonth + "-01";

	var linkTag = '<a href="' + correctHref + '?SQ_CALENDAR_VIEW=month&SQ_CALENDAR_DATE=' + currentMonthDate + '">' + currentMonthDateText + '</a>';
	$("#eventsCalendarMonth h3").html(linkTag);


	$("#eventsCalendarMonth a, #calendarTable a").each( function() {
		var hrefSuffix = $(this).attr("href");
		hrefSuffix = hrefSuffix.substr(hrefSuffix.indexOf("?"));

		$(this).attr({href: correctHref + hrefSuffix});
	});

	// when clicking on previous/next links, load calendar into page via ajax call
	$("#calendarWrapper a.calendarNavLink").click(function() {

		var abc = $(this).attr("href") + "&SQ_PAINT_LAYOUT_NAME=noredirect";

		$.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


