$(document).ready(function(){

	var d=new Date();
	var currYear=d.getFullYear();
	$("#footerYear").html(currYear);
		
	$.each($(".button"), function(){
		$(this).wrap("<div class='buttonWrapper'></div>");
	});
	$(".buttonWrapper").corners({radio: '10', outColor: '#ecf3d7'});
	
	//asssign window title to content heading
	document.title=$("h1").html();

	// Hide body content and erase tab bottom borders
	$(".tab").css("border-bottom", "none");
	$(".lastTab").css("border-bottom", "none");
	
	var windowLocation = window.document.URL.toString();
	var currentPageHTMLFileName = windowLocation.substring(windowLocation.lastIndexOf('/') + 1, windowLocation.length);
	var idOfSelectedTab = "#" + currentPageHTMLFileName.substring(0,currentPageHTMLFileName.indexOf('.html'));
	var isIE=false;
	var firstTime=false;

	if(idOfSelectedTab.length < 2)
	{
		idOfSelectedTab="#index";
	}

	$(".right", idOfSelectedTab).css("color", "#677d42");

	// Take care of browser differences
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && val==true)
		{
			isIE=true;
		}
	});
	
	if (isIE)
	{
		$(idOfSelectedTab).css("border-bottom", "8px solid #a0c439");
		$("#bodyContent").css("margin-top", "-1");
	}
	else
	{
		$(idOfSelectedTab).css("border-bottom", "1px solid #a0c439");
		$(".columns").equalHeights();
		$("#rightCol").css("margin-right", "30px");
	}

	//handle expansion/contraction of answers on FAQ page
	$("#faq h3").click(function (){
		var leftColHt=$("#leftCol").css("height").substring(0,$("#leftCol").css("height").indexOf("px"));
		var newHt;
		var beginIndexOfThisQuestionsParagraphResponses=$("#faq").children().index($(this).next());
		var indexOfThisQuestionWithinAllQuestions=$("#faq h3").index(this);
		var indexOfNextQuestionWithinAllQuestions=indexOfThisQuestionWithinAllQuestions+1;
		var endIndexOfThisQuestionsParagraphResponses=$("#faq").children().index($("#faq h3").eq(indexOfNextQuestionWithinAllQuestions));
		endIndexOfThisQuestionsParagraphResponses-=1;

		if(beginIndexOfThisQuestionsParagraphResponses == endIndexOfThisQuestionsParagraphResponses)
		{
			if($(this).next().css("display")=="none")
			{
				$(this).next().fadeIn(500);
			}
			else
			{
				$(this).next().fadeOut(500);
			}
		}
		else if (endIndexOfThisQuestionsParagraphResponses < 0)
		{
			if($("#faq").children().eq(beginIndexOfThisQuestionsParagraphResponses).css("display")=="none")
			{
				$("#faq").children().slice(beginIndexOfThisQuestionsParagraphResponses, $("#faq").children().length).fadeIn(500);
			}
			else
			{
				$("#faq").children().slice(beginIndexOfThisQuestionsParagraphResponses, $("#faq").children().length).fadeOut(500);
			}
		}
		else
		{
			if($("#faq").children().eq(beginIndexOfThisQuestionsParagraphResponses).css("display")=="none")
			{
				$("#faq").children().slice(beginIndexOfThisQuestionsParagraphResponses, endIndexOfThisQuestionsParagraphResponses).fadeIn(500);
			}
			else
			{
				$("#faq").children().slice(beginIndexOfThisQuestionsParagraphResponses, endIndexOfThisQuestionsParagraphResponses).fadeOut(500);
			}
		}
	
		recalculateLeftColHt(isIE);
	});

	//handle expansion/contraction of project news items
	$.each($(".newsItemDate"), function(){
		var dateWithParentheses = "(" + $(this).html() + ")";
		$(this).html(dateWithParentheses);
	});

	$.each($("#projectNewsContent .newsSummary"), function(){
		if($(this).parent().find(".newsContent").html().length > 19)
		{
			var moreLink = "<div class='more'>More</div>";
			$(this).html($(this).html()+moreLink);
		}
	});

	$(".newsItem:gt(2)").hide();
	$(".newsSummary:eq(2)").css("border-bottom", "none");
	$("#archives").html("show archived items");
	$("#archives").click(function() {
		if($("#archives").html()=="hide archived items")
		{
			$(".newsItem:gt(2)").fadeOut(500);
			$("#projectNewsContent .buttonWrapper").hide();
			$("#archives").html("show archived items");
			$("#projectNewsContent .buttonWrapper").fadeIn(1000);
			$(".newsSummary:eq(2)").css("border-bottom", "none");
			$(".newsContent:eq(2)").css("border-bottom", "none");
		}
		else
		{
			$(".newsItem:gt(2)").fadeIn(500);
			$(".newsContent:eq(2)").css("border-bottom", "solid 1px #a0c439");
			$(".newsSummary:last").css("border-bottom", "none");
			$("#projectNewsContent .buttonWrapper").hide();
			$("#archives").html("hide archived items");
		}
		recalculateLeftColHt(isIE);
	});

	$("#projectNewsContent .newsContent").hide();
	$("#projectNewsContent .more").click(function (){
		if($(this).parent().parent().find(".newsContent").css("display")=="none")
		{
			$(this).parent().parent().find(".newsContent").fadeIn(500);
			$(this).parent().parent().find(".newsSummary").css("border-bottom", "none");
			if(
				(
					($(".newsSummary").index($(this).parent().parent().find(".newsSummary"))!=2) 
					&& 
					($("#archives").html()=="show archived items")
				)
				|| 
			    (
					($(".newsSummary").index($(this).parent().parent().find(".newsSummary"))!=($(".newsSummary").length - 1)) 
					&& 
					($("#archives").html()=="hide archived items")
				)
			  )
			{
				$(this).parent().parent().find(".newsContent").css("border-bottom", "solid 1px #a0c439");
			}
			$(this).html("Less");
			recalculateLeftColHt(isIE);
		}
		else
		{
			$(this).html("More");
			$(this).parent().parent().find(".newsContent").fadeOut(500);
			$(this).parent().parent().find(".newsContent").css("border-bottom", "none");
			if(
				(
					($(".newsSummary").index($(this).parent().parent().find(".newsSummary"))!=2) 
					&& 
					($("#archives").html()=="show archived items")
				)
				|| 
			    (
					($(".newsSummary").index($(this).parent().parent().find(".newsSummary"))!=($(".newsSummary").length - 1)) 
					&& 
					($("#archives").html()=="hide archived items") 
				)
			  )
			{
				$(this).parent().parent().find(".newsSummary").css("border-bottom", "solid 1px #a0c439");
			}
			recalculateLeftColHt(isIE);
		}
	});
	
	var loc=String(window.location);
	if(loc.indexOf("projectNews") > -1)
	{
		recalculateLeftColHt(isIE);
	}
});

function recalculateLeftColHt(isIE){
	if(!isIE)
	{
		var newLeftColHt=0;
		$.each($("#leftCol *"), function(){
			var thisHt=$(this).attr("offsetHeight");
			if(thisHt!=null)
			{
				newLeftColHt+=Number(thisHt);
			}
		});
		$("#leftCol").css("height", newLeftColHt+30);
	}
}