/*
	@author Thomas Evan Lecklider
	@client UCSD Psychological & Counseling Services
	
	Notes for formatting your HTML:
		Any links that link to a real page that you want to showup with AJAX should have the class "jax"
		Any content within those pages you called in should be within a div#content in the HTML page being called.
		
	NOTE: to activate random header you need to take out the hash marks on Line 93	
*/

$(document).ready(function() {	
	// AJAX stuff for the Header
	function randHead () {
		var rand = Math.floor(Math.random()*17); // The trailing number determines the range of the random number minus 1. So 11 means that the random number will fall between 0 and 10
		var headLoad = rand+".html";
		loadHead(headLoad, false);
	}
	
	// Populates the header with whatever is passed to plus a boolean "animate" value which will either animate the effect or not
	function loadHead (i, animate) {
		j = 'headers/' + i;
		if (animate) {
			$('div#header').fadeOut("fast", function() {
				$('div#header').load(j).fadeIn("fast");
			});
		}
		else {
			$('div#header').load(j);
		}
	}
	
	// Shows the nested lists in the top navigation menu called "menu_upper"
	$('div#menu_upper ul li').hover(
		function () {
			$(this).find('ul').slideDown('fast');
		},
		function () {
			$(this).find('ul').slideUp('fast');
		}
	);
	
	// Check the url for bookmarks
	var hash = window.location.hash.substr(1);
	var href = $("div#ajax_div li a").each(function() {
		var href = $(this).attr("href");
		if(hash == href.substr(0, href.length-5)) {
			$(this).addClass("current").css({marginTop: "0", paddingTop: '10px'});
			var toLoad = hash + ".html #content";
			$("div#main_content").load(toLoad);
		}
		if(hash == "") {
			var toLoad = "students.html #content";
			$("div#main_content").load(toLoad);
			$('div#ajax_div a.jax.first').addClass('current').css({marginTop: "0", paddingTop: '10px'});
			window.location.hash = "#students";
		}
	});
	
	// AJAX calls for links
	$("div#ajax_div a.jax").click(function(event) {
		// Prevent the default link behavior
		event.preventDefault();
		
		// Remove the "current" class from the past link
		$("a.current").animate({
			marginTop: "8px",
			paddingTop: "0.2em"
		}, "fast", function() {
			$(this).removeClass("current");
		});
		
		// Set the location in the address bar
		window.location.hash = $(this).attr("href").substr(0, $(this).attr("href").length-5); // the minus 5 removes the filename extension of ".html" if your pages end in ".php" then change this number to 4
		
		// The actual AJAX stuff
		var toLoad = $(this).attr("href")+" #content";
		$("div#main_content").fadeOut("normal", function() {
			$("div#main_content").load(toLoad, '', function() {
				$("div#main_content").fadeIn("slow");
			});
		});
		// Adds the "current" class to whatever was clicked
		$(this).animate({
			marginTop: "0",
			paddingTop: "10px"
		}, "fast", function(){
			$(this).addClass("current");
		}).blur();
	});
		
	// Calls the random header generator
	//randHead();
});