function setPageHeight(){
	var pb = $("#page_body_container").height();
	var b = $("body").height();
	var d = $(document).height();
	var es = d-b;
	
	if(es > 0){
		var nh = pb + es - 30;
		$('#page_body_container').height(nh);
	}
}

$(document).ready(function() {
	//$('.entry *').addClass('fix');//this fixes an IE8 bug - this class add zoom:1 (similar to peekaboo)
	$('.wfu_link').click(function(){
		$('#toolbox ul').slideToggle("fast");
		return false;
	});
	
	$('.sidebar_widget:first-child').addClass('first');
	
	//setPageHeight();
	
	$('tr:visible:odd td').addClass("alt");
	
	$('a.more').click(function(){
		$(this).parent().next().slideToggle("slow");
	});
	
	//standard definition list slider
	$('dl.slider dt').click(function(){
		$(this).next().slideToggle("slow");
	});
	$('dl.slider dd').hide();
	$('dl.slider dd:first').slideToggle(1500);
		
	//custom faq slider...
	$('dl.faq_slider dt').click(function(){
		$(this).next().slideToggle("slow");
	});
	$('dl.faq_slider dd').hide();
	$('dl.faq_slider dd:first').slideToggle(1500);
		
		
	$('.entry a').each(function(){
		var this_href = $(this).attr('href');
		if(window.this_href !== undefined){
			this_href = this_href.substring(0,5);
			if(this_href == '#_ftn' || this_href == '#_edn'){
				$(this).addClass('smaller');
			}
			if($(this).attr('href') == '#_ftnref'){
				$(this).parent().hide();
			}
		}
	});
	
	$('.entry a[href*="#_edn"]').each(function(){
		$(this).addClass('smaller');
		var href = $(this).attr('href');
		
		//build the name and title fields
		if(href.search('ref') >= 0){
			var name = href.substring(0,5) + href.substring(8);
			var title = '';
		}else{
			var name = href.substring(0,5) + "ref" + href.substring(5);
			var title = $('.entry a[href='+name+']').parent().text();
			
			if(title != null){
				var maxlength = 200;
				if(title.length > maxlength){
					title = title.substr(0, maxlength) + " ...";
				}
			}
		}
		
		//this adds the name and title attributes
		$(this).attr('name', name);
		$(this).attr('title', title);
		
		
		//this enables the click to scroll to the endnote and back to reference point in the article
		$(this).click(function(){
			var $target = href;
			if (href.length >= 0) {
				var targetOffset = $('[name='+href+']').offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		});
	});
	
	var hash = window.location.hash;
	//alert(hash);
	if(hash == '')
		hash = '#overview';
		
	//set active tab on page load
	$('.tabs a[href='+hash+']').addClass("selected");
	$(hash+'_content').addClass("selected");
	
	//add click listener to tabs
	$('.tabs a').click(function(){
		//changed the selected tab
		$('.tabs a').removeClass('selected');
		$(this).addClass('selected');
		
		//swap out the content
		$('.tabs_content').removeClass('selected');
		var selectedTab = $(this).attr('href');
		$(selectedTab+'_content').addClass("selected");
	});	
	
	//if someone had a portal bookmarked, make the links show at the top
	var path = location.pathname;
	if(path == '/portals/students/' || path == '/portals/faculty/'){
		var portal = path.substr(9);
		portal = portal.slice(0, -1);
		$("#"+portal).show("slow");
	}
	
	//when someone clicks on portal, display appropriate links
	$("#portals .portal").click(function(){
		var path = $(this).attr('href');
		var pos = path.search("law.wfu.edu/");
		pos = pos + 20;
		var portal = path.substr(pos);
		portal = portal.slice(0, -1);
		$(".portal_data").not("#"+portal).slideUp("slow", function(){
			$("#"+portal).slideToggle("slow", function(){
				//$("#portal_content").css("opacity", .5).animate({opacity: 1}, 2500);
			});
		});
		return false;
	});
	
	
	//handle the go link feature
	var data;
	var test;
	$("#get_go_link").click(function(){
		var query_string = location.href.indexOf('?');
		if(query_string > -1){
			var page = location.href.substr(0,location.href.indexOf('?'));
		}else{
			var page = location.href;
		}
		
		//send the current page off to the handler
		$.getJSON("http://go.wfu.edu/?callback=?",
			{
				redirect: page
			}, 
			function(data){
				$("#the_go_link").html(data.input);
			}
		);
		
		//replace the link box with the short url
		$('#get_go_link').fadeOut(500, function() {
			$('#the_go_link').fadeIn(500);
		});

		return false;
	});
	
	
	//add search drop down to header
	$("#header #search dl").hover(
		function () {
			$("#header #search dd").stop(true, true).slideDown("slow");
		},
		function () {
			$("#header #search dd").delay(500).slideUp("slow");
		}
	);
	
	$("#header #search dl li").click(function(){
		var cx = $(this).attr("id");
		var the_class = $(this).attr("class");
		var site = $(this).html();

		$("#header #search input[name='cx']").val(cx);
		$("#header #search input[name='site']").val(site);
		
		$("#header #search dt").removeClass();
		$("#header #search dt").addClass(the_class);
		
		$("#header #search dd").slideUp();
		$("#header #search input").focus();
	});
	
	
	//add search drop down to search page
	$("#searchForm dl").hover(
		function () {
			$("#searchForm dd").stop(true, true).slideDown("slow");
		},
		function () {
			$("#searchForm dd").delay(500).slideUp("slow");
		}
	);
	
	$("#searchForm dl li").click(function(){
		var cx = $(this).attr("id");
		var the_class = $(this).attr("class");
		$("#searchForm input[name='cx']").val(cx);
		
		$("#searchForm dt").removeClass();
		$("#searchForm dt").addClass(the_class);
		
		$("#searchForm dd").slideUp();
		$("#searchForm input").focus();
	});
	
	
	
	//taking all links and putting them in as footnotes
	$("#main_content").addClass('noted');
	$("#main_content").append("<div id='noted_footnotes' class='hidden'><h3>Link Footnotes</h3><ol></ol></div>");
	$(".entry a:not(:has(img))").each(function(index){
		var noted_link = $(this).attr('href');
		var note_num = index + 1;
		$(this).append("<sup class='noted_sup'>"+note_num+"</sup>");
		$("#noted_footnotes ol").append("<li>"+noted_link+"</li>");
	});
	

	//swap a link for a text input with href for copy paste
	$(".copyLink").click(function(){
		var cl_href = $(this).attr('href');
		$('.copyLinkInput').val(cl_href);
		$(".copyLinkInput").fadeIn("slow");
		$(".copyLinkInput").select();
		return false;
	});

	$("#login_form input#username").focus();	
	/*
	//add site url to search string
	$(".search_button").click(function() {
	    // get all the inputs into an array.
	    var $inputs = $(this).siblings(":input");
	    var values = {};
	    $inputs.each(function() {
	    	values[this.name] = $(this).val();
	    });
		
		//build new search
		var new_q = values["q"] + "+site:" + values["site"];
		
		//write new search to q element
		$(this).siblings("input:text").val(new_q);
		
	});
	
	//remove site url from search box
	var q = $("input[name=q]").val();
	var pos = q.search("\\+site");
	if(pos >= 0){
		var new_q = q.substr(0,pos);
	}
	$("input[name=q]").val(new_q);
	*/
});
