

$(document).ready( function() {
	
	// Fix for styleing <ABBR> tags in IE 6
	styleAbbr();
	initFilters();
	
	// Rollover styling for DIV.result panels (search / doc downloads etc.)
	$("div.result").hover(function() {$(this).addClass("hover");}, function() {$(this).removeClass("hover");} ).click( function() { location.href=$(this).find("a").attr("href");} );	 
	
	// EVENT FILTERS
	$("#eventFilterAll").click( function() {					 
		$.ajax({
		    type: "GET", 
			url: "/events/?action=clearfilters", 
			dataType: "html", 
			async: false,
			success: function(msg) { clearFilters(msg); },
			error: function() { alert("Error!"); }
	 	});
	 });
	
	$(".eventFilter").click( function() {		
		var filters = "";		
		$(".eventFilter[@checked]").each( function() {	filters += $(this).val() + ","; 	});
		$.ajax({
		    type: "GET", 
			url: "/events/?action=setfilters&filters="+filters, 
			dataType: "html", 
			async: false,
			success: function(msg) { setFilters(msg, filters); },
			error: function() { alert("Error!"); }
	 	});		
	});
	
});


function styleAbbr() {
  var oldBodyText, newBodyText, reg
  if (isIE) {
    oldBodyText = document.body.innerHTML;
    reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
    newBodyText = oldBodyText.replace(reg, '<DIV $1><ABBR $1>$2</ABBR></DIV>');
    document.body.innerHTML = newBodyText;
  }
}

isIE = (document.all) ? true:false;


function clearFilters(msg) {
	//alert(msg);
	if (msg == "CLEARED") {		
		$(".event_panel").removeClass("hidden");
		$(".eventFilter").attr("checked", "checked");
		$("#eventFilterAll").removeAttr("checked");
	};
}


function setFilters(msg, filters) {
	//alert(filters);
	//alert(msg);
	if (msg == "SET" && filters != "") {		
		var arr = filters.split(",");
		$(".event_panel").addClass("hidden");
		$.each(arr, function(i,n) { $("."+n).removeClass("hidden"); });
	};
}

function initFilters() {
	var sFilters = "";
	$(".eventFilter[@checked]").each( function() {	sFilters += $(this).val() + ","; 	});
	setFilters("SET", sFilters);
}