// Some globals for determining what was our last menu and sub-menu we visited.
var oldmenu = null;
var oldsubmenu = null;
var oldmenuNum = -1;
var oldsubmenuNum = -1;

// Load up a part of our resume using ajax.
function loadPage (page) {
	// Check if we havn't setup a link yet...
	if(page == null) {
	//	return;
	}

	// Put an animated GIF image insight of content
	$("#resume").empty().html('<div style="margin-top:230px"><center><img src="/images/loading2.gif" /></center></div>');

	// Make AJAX call
	$("#resume").load(page);
}

// Unselect any menu items.
function setUnselectMenu () {
	$('#sidebarmenu > ul > li > a').each(
		function (index) {
			// Hide all sub-menus.
			$(this).parent().find('#sidesubbarmenu').hide();
			
			// Setup up click even on sub-menus.
			$(this).parent().find('#sidesubbarmenu').find('ul').find('li').find('a').each(
				function (index) {
					// Deselect our sub-menu.
					$(this).removeClass("selected");
				}
			);
		
			// Deselect our menu.
			$(this).removeClass("selected");
		}
	);
}

// Select a menu based upon it's index.
function setMenu (num) {
	$('#sidebarmenu > ul > li > a').each(
		function (index) {
			if(num == index) {
				// Grab our submenu.
				var submenu = $(this).parent().find('#sidesubbarmenu');

				// Remove the selection on the old item.
				if(oldmenu) { 
					$(oldmenu).removeClass("selected");
				}
				
				// Select the new item.
				$(this).addClass("selected");

				// Hide any old sub menus.
				if(oldmenu) { 
					$(oldmenu).parent().find('#sidesubbarmenu').slideUp("slow");
				}
				
				// Show any sub menus.
				if(submenu.size() > 0) {
					submenu.slideDown("slow");
				}
				
				// If we don't have any sub-menus, display the contents of this tab in the right section of the page.
				if(submenu.size() == 0) {
					$('#rightSection > h1').html("&#9002; " + $(this).text());
					
					// Load up the apge.
					loadPage($(this).attr('ajax'));
				}
				
				// Update who was the old menu.
				oldmenu = this;
				
				// Since were changing menus, unselect all sub-menus.
				if(oldsubmenu) { 
					$(oldsubmenu).removeClass("selected");
					oldsubmenu = null;
				}
			}
		}
	);
}

// Select a sub-menu based upon it's index.
function setSubMenu (num) {
	// Setup up click even on sub-menus.
	$(oldmenu).parent().find('#sidesubbarmenu').find('ul').find('li').find('a').each(
		function (index) {
			if(num == index) {
				// Remove the selection on the old item.
				if(oldsubmenu) { 
					$(oldsubmenu).removeClass("selected");
				}
				
				// Select the new item.
				$(this).addClass("selected");
				
				// Display the contents of this tab in the right section of the page.
				$('#rightSection > h1').html("&#9002; " + $(this).text());
				
				// Load up the apge.
				loadPage($(this).attr('ajax'));
				
				// Update who was the old submenu.
				oldsubmenu = this;
			}
		}
	);
}

$(document).ready(function() {	

	// Make sure were not displaying any sub-menus when we begin.
	$('#sidebarmenu > ul > li > a').each(
		function (index) {
			// Hide all sub-menus.
			$(this).parent().find('#sidesubbarmenu').hide();
		}
	);
	
	
	// Check if were using the back button for one of our ajax pages.
    $.history.init(function(hash){
        if(hash == "") {
			// Load up our default page on startup.
			setMenu(0);
        } else {		
			// There is a hash, so lets load our page based on it.
			var menu    = hash.split('.')[0];
			var submenu = hash.split('.')[1];
			
			if(oldmenuNum != menu) {
				setMenu(menu);
			}
			
			setSubMenu(submenu);
			
			oldsubmenuNum = submenu;
			oldmenuNum = menu;
        }
    },
    { unescape: ",/" });
});
	
