$(document).ready(pageLoaded);

var menuSlideSpeed = 200;
var mouseOnLinkWithSubmenu = false;

function pageLoaded()
{
	//Set-up tabs
	$("#tabs").tabs();
	$(".notabs").tabs('disable');
	
	//Set-up clickToShow
	$("#click_to_show_more").append(" (click on events for more information)");
	$(".click_to_show .click_to_show_content").hide();
			
	$(".click_to_show .click_to_show_header").click(function(){ clickToShow(this); });
	$(".click_to_show .click_to_show_header").css({'cursor':'hand', 'cursor':'pointer'});
	
	$(".click_to_show .click_to_show_header .third").css({'text-decoration':'underline'});
	
	$(".click_to_show .click_to_show_header").bind("mouseenter", function()
	{
		$(this).css({'background':'#DDD'});
		$(this).next(".click_to_show_content").css({'background':'#DDD'});
	});
	
	$(".click_to_show .click_to_show_header").bind("mouseleave", function()
	{
		$(this).css({'background':'#fff'});
		$(this).next(".click_to_show_content").css({'background':'#fff'});
	});
	
	
	//Remove the hrefs from links with submenus
	$("#mainmenu a.haveSubMenus").removeAttr("href");
	//Still want them to appear as links though - so cursor needs to be hand
	$("#mainmenu a").css({'cursor':'hand', 'cursor':'pointer'});
	
	//Set up the links with submenus to respond to clicks
	$("#mainmenu a.haveSubMenus").each(function(){
		//Sub-menus must be named as the id of the link plus "_submenu"
		$(this).click(function(){itemWithSubmenuClicked(this.id);});
	});
	
	//Set it up so that clicking on anything except the links/submenu will close the submenu
	$(document.body).click(bodyClicked);
	//Detecting mouse over link/submenu
	$("#mainmenu a.haveSubMenus").bind("mouseenter", function(){mouseOnLinkWithSubmenu=true;});
	$("#mainmenu li ul").bind("mouseenter", function(){mouseOnLinkWithSubmenu=true;});
	//Detecting mous no longer over link/submenu
	$("#mainmenu a.haveSubMenus").bind("mouseleave", function(){mouseOnLinkWithSubmenu=false;});
	$("#mainmenu li ul").bind("mouseleave", function(){mouseOnLinkWithSubmenu=false;});
}

function clickToShow(itemClickedOn)
{
	var theContent = $(itemClickedOn).next(".click_to_show_content");

	if ($(theContent).is(":hidden"))
	{
		$(theContent).show();
		$(itemClickedOn).children("td").css({'border':'none','border-top':'1px solid #BBB'});
	}
	else
	{
		$(theContent).hide();
		$(itemClickedOn).children("td").css({'border':'none', 'border-left':'1px solid #BBB'});
		$(itemClickedOn).children(".first").css({'border':'none'});
	}
}


function bodyClicked()
{
	if (!mouseOnLinkWithSubmenu)
	{
		$("#mainmenu a").css({'border-top':'none'});
		$("#navigation li ul").slideUp(menuSlideSpeed);
	}
}


function itemWithSubmenuClicked(itemClickedOn) //thePath is the link that was clicked on's specific submenu
{
	var thePath = "#navigation li ul#" + itemClickedOn + "_submenu";

	//If the submenu is hidden then show it
	if ($(thePath).is(":hidden"))
	{
		//If another submenu is showing then hide that first
		if ($("#navigation li ul").is(":visible"))
		{
			$("#navigation li ul").slideUp(menuSlideSpeed);
			$("#mainmenu a").css({'border-top':'none'});
			
			setTimeout(function(){$(thePath).slideDown(menuSlideSpeed);}, menuSlideSpeed);
			$("#mainmenu #" + itemClickedOn).css({'border-top':'1px solid white'});
		}
		else
		{
			$("#mainmenu #" + itemClickedOn).css({'border-top':'1px solid white'});
			$(thePath).slideDown(menuSlideSpeed);
		}
	}
	else
	{
		//Hide the submenu when clicked on a second time
		$("#mainmenu a").css({'border-top':'none'});
		$("#navigation li ul").slideUp(menuSlideSpeed);
	}

}