$(document).ready(function() {
	var timeout = new Object;
	var strLastSubMenu;

	// Make the login error flag disappear after a short while, if it exists
	if ($('#login_failed').length > 0) {
		setTimeout(function() { $('#login_failed').fadeOut('slow')}, 5000);
	};

	$('.menu').hover(function() {
		strMenu=$(this).attr('id').replace('menu_','');
		
		// Check if there's a submenu named the same
 		if ($('#submenu_'+strMenu).length > 0) {
			clearTimeout(timeout[strMenu]);
			$('#submenu_'+strMenu).css('top', $(this).offset().top + 9 + $(this).height() + 'px');
			$('#submenu_'+strMenu).css('left', $(this).offset().left + 'px');
			$('#submenu_'+strMenu).css('width', $(this).outerWidth());
			$('#submenu_'+strMenu).css('display','block');
			$(this).addClass('over');
		};
	}, function() {
		strMenu=$(this).attr('id').replace('menu_','');
 		if ($('#submenu_'+strMenu).length > 0) {
			timeout[strMenu]=setTimeout("hideMenu('"+strMenu+"')",300);
		};
	});
	
	$('.menu').click(function() {
		$(this).children('a').trigger('click');
		return false;
	});
	
	$('.submenu').hover(function() {
		strMenu=$(this).attr('id').replace('submenu_','');
		clearTimeout(timeout[strMenu]);		
	}, function() {		
		strMenu=$(this).attr('id').replace('submenu_','');
		timeout[strMenu]=setTimeout("hideMenu('"+strMenu+"')",300);
	});
	
	// Handle submenu item click
	$('.submenu li').click(function() {
		strHref=$(this).children('a').attr('href');
		if (strHref!=undefined)
			window.location.href=strHref;
	});
	
	// Handle submenu item hover (produce subsubmenu)
	$('.submenu li').hover(function() {
		strMenuID=$(this).attr('id').replace('menuid_','');
		
		if ($('#subsubmenu_'+strMenuID).length > 0) {
			$('#subsubmenu_'+strMenuID).css('display','block');
			$('#subsubmenu_'+strMenuID).css('left', $(this).offset().left + 5 + $(this).width() + 'px');
			$('#subsubmenu_'+strMenuID).css('top', $(this).offset().top + 'px');
		};
		
		// Remember the last submenu item we were on, that way we can prevent it from disappearing if we hover on a subsubmenu
		strLastSubMenu=$(this).parent().parent().attr('id').replace('submenu_','');
	}, function() {
		strMenuID=$(this).attr('id').replace('menuid_','');
		timeout[strMenuID]=setTimeout("hideSubSubMenu('"+strMenuID+"')",300);
	});
	
	// Handle subsubmenu item hover (prevent submenu from being killed)
	$('.subsubmenu').hover(function() {
		strMenuID=$(this).attr('id').replace('subsubmenu_','');	
		clearTimeout(timeout[strLastSubMenu]);
		clearTimeout(timeout[strMenuID]);
	}, function() {
		strMenuID=$(this).attr('id').replace('subsubmenu_','');
		timeout[strMenuID]=setTimeout("hideSubSubMenu('"+strMenuID+"')",300);
		timeout[strMenu]=setTimeout("hideMenu('"+strLastSubMenu+"')",300);
	});
	
	$('#login_button').click(function() {
		// Make sure we've entered a username and password
		if ($('#login_user').val()!='' && $('#login_password').val()!='') {
			$('#login_form').submit();
		} else {
			alert("Please be sure to provide a user name and password first.");
		};
	});
	
	// In case there are any lbtable tables, apply every other cell formatting styles
	$('table.lbtable tr:nth-child(even)').addClass('alternate');

	// Call routine to position objects
	setupDisplay();

	// Make sure we redraw anything positioned via JS if we resize the window
	$(window).resize(function() {
		setupDisplay();
	});
});

function hideMenu(id) {
	$('#submenu_'+id).css('display','none');
	$('#menu_'+id).removeClass('over');
};

function hideSubSubMenu(id) {
	$('#subsubmenu_'+id).css('display','none');
};

function submitOnEnter(field, e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13) {
		$('#login_button').click();
//		field.form.submit();
		return false;
	} else
		return true;
};

function setupDisplay() {
	// Ensure page container is tall enough
	if ($('#panel').height() < $(window).height()) {		
//		$('#main').height($(window).height());
		$('#contentwave').height($(window).height());
	} else {
		$('#main').css('height', 'auto');
		$('#contentwave').css('height', 'auto');
	};
	
	// Position sidebar menu to the left edge of 
//	$('#sidemenu').css('left', $('#pagecontainer').offset().left);
//	$('#sidemenu').css('top', $('#menubar').offset().top + $('#menubar').outerHeight());
//	$('#sidemenu').css('top', '0');

};
