// JavaScript Document
$(document).ready(function() {
	//UI INITIALIZATION
	$("#aboutLink, #aboutSubmenu").mouseenter(function() {
		$("#aboutSubmenu").stop().animate({
			height: "78px",
			opacity: 1.0,
			filter: "alpha(opacity=100)"
		}, 400);
		//$("#aboutSubmenu").css("visibility","visible");
	});
	$("#aboutLink, #aboutSubmenu").mouseleave(function() {
		$("#aboutSubmenu").stop().animate({height: "0", opacity: 0.0, filter: "alpha(opacity=0)"}, 100, function() {
			//$("#aboutSubmenu").css("visibility","hidden");
		});
	});
	//For IE8's sake, JS hover is implemented vs. CSS :hover:
	$("#aboutSubmenu li a").mouseover(function(event) {
		$(event.target).css("backgroundColor","#BDD8DB");
	});
	$("#aboutSubmenu li a").mouseout(function(event) {
		$(event.target).css("backgroundColor","#E9F1F2");
	});	
	
	//Ensures that the About submenu closes before the page changes (fixes the back button bug):
	$("#aboutSubmenu li a").click(function() {
		$("#aboutSubmenu").stop().animate({height: "0"}, 20, function() {
			//$("#aboutSubmenu").css("visibility","hidden");
		});		
	});
	
	//Ensures proper displaying of CSS loaded font(s) for IE:
	if (navigator.appName == "Microsoft Internet Explorer") {
		if ($("#menuWrapper").length == 1) {
			//$("#menuWrapper ul").css("fontWeight","bold");
		}
		
		//Addresses the IE menu bug (Intranet):
		if ($("#intranetMenu").length == 1) {
			$("#intranetMenu ul li").css("fontSize","9px");
			$("#intranetMenu ul li").css("fontWeight","bold");
			$("#intranetMenu ul li").css("margin","8px 7px 2px 12px");
		}
	}
	
	//Make current page's link active:
	makeCurrentPageLinkActive();
});

function makeCurrentPageLinkActive() {
	var pageTitle = $("title").html();
	var homePattern = new RegExp(" - Home","g");
	var aboutPattern = new RegExp(" - About Us","g");
	var boardPattern = new RegExp(" - Board","g");
	var newsPattern = new RegExp(" - News","g");
	var contactPattern = new RegExp(" - Contact","g");
	
	if (homePattern.test(pageTitle)) {
		$("#menuWrapper li a").eq(0).attr("class","activeLink");
	}
	else if (aboutPattern.test(pageTitle)) {
		$("#menuWrapper li a").eq(1).attr("class","activeLink");
	}
	else if (boardPattern.test(pageTitle)) {
		$("#menuWrapper li a").eq(2).attr("class","activeLink");
	}
	else if (newsPattern.test(pageTitle)) {
		$("#menuWrapper li a").eq(3).attr("class","activeLink");
	}
	else if (contactPattern.test(pageTitle)) {
		$("#menuWrapper li a").eq(4).attr("class","activeLink");
	}	
}

//Useful functions:
jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};
//Converts BC dates to human readable format:
function bcDateConvert(jQueryElement, monthStyle) {	
	var monthsLong = {"Jan":"January","Feb":"February","Mar":"March","Apr":"April","May":"May","Jun":"June","Jul":"July","Aug":"August","Sep":"September","Oct":"October","Nov":"November","Dec":"December"};
	var monthsShort = {"Jan":"Jan.","Feb":"Feb.","Mar":"Mar.","Apr":"Apr.","May":"May","Jun":"Jun.","Jul":"Jul.","Aug":"Aug.","Sep":"Sep.","Oct":"Oct.","Nov":"Nov.","Dec":"Dec."};		
	var oldDateValue = "";
	var oldDatePieces = new Array();
	var newDate = "";

	for (var i = 0; i < jQueryElement.length; i++) {	
		oldDateValue = jQueryElement.eq(i).html();
		oldDatePieces = oldDateValue.split("-");
		
		if (monthStyle == "long") {
			newDate = monthsLong[oldDatePieces[1]] + " " + oldDatePieces[0] + ", " + oldDatePieces[2];
		}
		else if (monthStyle == "short") {
			newDate = monthsShort[oldDatePieces[1]] + " " + oldDatePieces[0] + ", " + oldDatePieces[2];
		}		
		
		jQueryElement.eq(i).text(newDate);
	}
}
