
onload = SetLinks;

/**
 * Uses the url to find the link to the current page and select it
 */
function SetLinks()
{
	var links = document.getElementsByTagName( "a" );
	
	for( var i = 0; i < links.length; i++ )
	{
		var link = links[ i ];
		
		if( link.href == window.location || link.href.replace( "default.aspx", "" ) == window.location )
		{
			link.className += " Selected";
			ToggleDepartment( link );
		}
	}

}

/**
 * Go back to referring page
 */
function StepBack( defaultUrl )
{
	// This uses referrer because a simple history.back() will not force a reload of the page
	
	if( document.referrer.indexOf( window.location.host ) != -1 )
	{
		window.location = document.referrer;
		return;
	}
	
	window.location = defaultUrl;
}

/**
 * When clicking a department link, this opens up the child menu of lines
 */
function ToggleDepartment( link )
{
	var item = null;
	var departmentLink = null;
	
	// Find the lines div and department link to be highlighted
	if( link.parentNode.className == "Department" )
	{
		// The link clicked was department, so look at child nodes to find lines 
		var list = link.parentNode.childNodes;
		departmentLink = link;
		
		for( var i = 0; i < list.length; i++ )
		{
			if( list[ i ].className == "SubDepartments" )
			{
				item = list[ i ];
				break;
			}
		}
	}
	else if( link.parentNode.parentNode.parentNode.className == "SubDepartments" )
	{
		// The link clicked was sub-department, so look at parent nodes to find department
		item = link.parentNode.parentNode.parentNode;		
		var department = item.parentNode;
		
		for( var i = 0; i < department.childNodes.length; i++ )
		{
			if( department.childNodes[ i ].nodeName == "A" )
			{
				departmentLink = department.childNodes[  i ];
				break;
			}
		}
	}
	
	if( item )
	{
		// Toggle display property
		if( item.style.display != "block" )
		{
			item.style.display = "block";
			departmentLink.className = "Selected";
		}
		else
		{
			item.style.display = "none";
			departmentLink.className = "";
		}
		return true;
	}
	
	return false;
}

var dialogueWindows = new Object();

onunload = closeAllDialogues;

/**
 * Open a dialogue window
 * 
 * Window will be placed in the centre of screen, and a reference is kept so all windows can be closed by calling closeAllDialogues()
 */
function openDialogue( windowName, windowUrl, windowWidth, windowHeight ) 
{	
	var dialogue = dialogueWindows[ windowName ];
	var windowTop = ( screen.height / 2 ) - ( windowHeight / 2 );
	var windowLeft = ( screen.width / 2 ) - ( windowWidth / 2 );
	var features = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no,resizable=yes,status=no';
	
	if( dialogue && ! dialogue.closed )
		dialogue.close();
	
	dialogueWindows[ windowName ] = window.open( windowUrl, windowName, features );
}

/**
 * Close all windows that were opened by openDialogue()
 */
function closeAllDialogues()
{
	for( windowName in dialogueWindows )
	{	
		var dialogue = dialogueWindows[ windowName ];
		
		if( dialogue && ! dialogue.closed )
			dialogue.close();
	}
}

