function search(event, templateName) {	
	if (event.keyCode == 13) {
		doSearch();
		return false;
	}
	else {
		return true;
	}
}

function doSearch() {
	var search = escape(document.getElementById("searchTerms").value);
	var type = document.getElementById("eventType");
	var year = document.getElementById("college-year");
	var collegeName = document.getElementById("collegeName");

	reloadEvents(collegeName[collegeName.selectedIndex].value, year[year.selectedIndex].value,"","","",search,escape(type[type.selectedIndex].value));
}

function filterByType(year, month) {
	var type = document.getElementById("eventType");
	var collegeYear = document.getElementById("college-year");
	var collegeName = document.getElementById("collegeName");

	reloadCalendar(collegeName[collegeName.selectedIndex].value, collegeYear[collegeYear.selectedIndex].value, year, month, escape(type[type.selectedIndex].value));	
}

function initCalendar() {
	reloadCalendar(gup("college"), gup("year"),gup("y"), gup("m"), gup("type"));	
	reloadEvents(gup("college"), gup("year"),gup("y"), gup("m"), gup("d"), gup("search"), gup("type"));	
}

function reloadCalendar(collegeName, collegeYear, year, month, type) {
	$("#calendarTop").load("./ajax/calendar-control.php?college=" + collegeName + "&year=" + collegeYear + "&m=" + month + "&y=" + year + "&type=" + type );
}

function reloadEvents(collegeName, collegeYear, year, month, day, search, type) {
	if (month < 10 && month != "") {
		month = "0" + month;
	}
	if (day < 10 && day != "") {
		day = "0" + day;
	}
	
	$("#calendarEvents").fadeOut();
	$("#calendarEvents").load("./ajax/calendar-events.php?college=" + collegeName + "&year=" + collegeYear + "&m=" + month + "&y=" + year + "&d=" + day + "&search=" + escape( search ) + "&type=" + escape( type ), 
		function () { 
			$("#calendarEvents").fadeIn();
		}
	);
}

function showEvent(className) {
	$("."+className).toggle();
	if ( $("."+className+"Image").attr("src") != "./image/arrow-down.gif") {
		$("."+className+"Image").attr({src: "./image/arrow-down.gif"});
	}
	else {
		$("."+className+"Image").attr({src: "./image/arrow-across.gif"});
	}
}

function loadStaff(id, initials) {
	$(".staffOpen").removeClass("staffOpen");

	$("#staffHover" + id).load("./ajax/staff.php?initials="+initials, function(data) {
        if(data != "") {
		    $("#staffHover" + id).addClass("staffOpen");
        }
	});

	$(document).bind("click",function(e) {
		$("#staffHover" + id).removeClass("staffOpen");
		$(document).unbind("click");
	});

	$("#staffHover" + id).bind("mouseleave blur", function(e) {
		setTimeout(function() { 
			$("#staffHover" + id).removeClass("staffOpen");
		}, 1000);
		$("#staffHover" + id).unbind("mouseleave blur");
	});
}

/** source: http://www.netlobo.com/url_query_string_javascript.html */
function gup( name ) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
