// --------------------------------------------------------------------------
// #### MG: Global Variables
// --------------------------------------------------------------------------
var Netscape = new Boolean();
var IE = new Boolean();
var Opera = new Boolean();	
var Platform = new String();
var lastSelectedMenuItem = null;
var PlaceholderImage = null;
var SelectedImage = null;
var HighlightedImage = null;
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// #### MG: The initial Methods are called
// --------------------------------------------------------------------------
InitializeDefaults();
//CheckBrowser();
PrepareForInternetExplorerOnly();
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// #### MG: Initial Methods
// --------------------------------------------------------------------------

// #### MG: Initializes the default values of the variables
function InitializeDefaults()
{
	Netscape = false;
	IE = false;
	Opera = false;
	Platform = navigator.platform;
}

// #### MG: Initializes the images for the navigation
function InitializeImages(placeholderImage, selectedImage, highlightedImage)
{
	PlaceholderImage = placeholderImage;
	SelectedImage = selectedImage;
	HighlightedImage = highlightedImage;
}

// #### MG: Checks which kind of Internet Browser the user (client) is using	
function CheckBrowser()
{
	if(navigator.appName == "Netscape")
	{
		Netscape = true;
	}
	else if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(navigator.userAgent.indexOf("Opera") == -1)
		{
			IE = true;
		}
		else
		{
			Opera = true;
		}
	}

	//window.status = "Netscape = " + Netscape + " : " + "IE = " + IE + " : " + "Opera = " + Opera + " : " + "Platform = " + Platform;
}

function PrepareForInternetExplorerOnly()
{
	IE = true;	
}
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------	
// #### MG: Some helper methods
// --------------------------------------------------------------------------

// #### MG: Checks if the Table (table) is really a Table
function CheckTable(table)
{
	if(table != null && table.tagName == "TABLE")
	{
		return true;
	}
	else
	{
		return false;
	}
}

// #### MG: Checks if the TableRow (tr) is really a TableRow	
function CheckTr(tr)
{
	if(tr != null && tr.tagName == "TR")
	{
		return true;
	}
	else
	{
		return false;
	}
}

// #### MG: Checks if the Image (img) is really an Image
function CheckImg(img)
{
	if(img != null && img.tagName == "IMG")
	{
		return true;
	}
	else
	{
		return false;
	}
}

// #### MG: Gets the parent table of an element	
function FindTable(element)
{	
	if(element.tagName == "TABLE") 
	{
		return element;
	} 
	else if(element.tagName == "BODY") 
	{
		return null;
	} 
	else 
	{
		return FindTable(element.parentNode);
	}
}
// --------------------------------------------------------------------------	

// --------------------------------------------------------------------------	
// #### MG: Methods for collapsing, expanding and navigating the menuitems
// --------------------------------------------------------------------------

function NavigateItemAndChangeHeader(obj, image)
{
	NavigateItem(obj);
	ChangeHeader(image);
}

// #### MG: This method is called when the menuitem is a link and has no ChildNodes
function NavigateItem(obj)
{
	var tr = obj.parentNode.parentNode;
	
	if(CheckTr(tr))
	{
		var table = FindTable(tr.parentElement);
		
		if(CheckTable(table))		
		{
			if(Netscape)
			{
				CollapseAllMenus_Netscape(table);
			}
			else if(IE)
			{
				CollapseAllMenus_IE(table);
			}
			else if(Opera)
			{
				CollapseAllMenus_Opera(table);
			}
		
			for(var i = 0; i < table.rows.length; i++)
			{
				UnSelectItem(table.rows[i].firstChild);
			}
		}
	}
	
	SelectItem(obj.parentNode);
}

// #### MG: This method is called when the menuitem has ChildNodes and a specific HeaderImage
function ExpandMenuAndChangeHeader(obj, image)
{
	ExpandMenu(obj);
	ChangeHeader(image);
}

// #### MG: This method changes the header image
function ChangeHeader(image)
{	
	if(document.parentWindow != null && document.parentWindow.top.frames["frame_header"] != null)
	{
		document.parentWindow.top.frames["frame_header"].ChangeHeaderImage(image);
	}	
}

// #### MG: This method is called when the menuitem has ChildNodes, otherwise the method 'NavigateItem(...)' is called.
function ExpandMenu(obj)	
{	
	// #### MG: Because the cell (td) = obj is clickable, we have to get the parent row (tr) of it = obj.parentNode
	// #### MG: If the clicked Text is a Hyperlink, we have to go one node above, cause we want the tr	
	if(obj != null && obj.tagName == "A")
	{
		obj = obj.parentNode;
	}
	
	if(Netscape)
	{
		ExpandMenu_Netscape(obj.parentNode);
	}
	else if(IE)
	{
		ExpandMenu_IE(obj.parentNode);
	}
	else if(Opera)
	{
		ExpandMenu_Opera(obj.parentNode);		
	}
	else
	{
		alert("Functionality not implemented for the browser type.");
		return;
	}
	
	SelectItem(obj);
}	

function ExpandMenu_IE(tr)
{
	if(CheckTr(tr))
	{
		CollapseAllMenus_IE(FindTable(tr.parentElement));
		
		var TableToShow = tr.nextSibling.lastChild.lastChild;
	
		if(TableToShow != null)
		{
			if(TableToShow.style.display == "")
			{
				TableToShow.style.display = "none";
			}
			else
			{
				TableToShow.style.display = "";
			}
		}		
	}
}

function ExpandMenu_Netscape(tr)
{
	if(CheckTr(tr))
	{
		CollapseAllMenus_Netscape(FindTable(tr.parentNode));
		
		var TableToShow = tr.nextSibling.cells[tr.nextSibling.cells.length - 1].firstChild;
		
		if(TableToShow != null)
		{
			if(TableToShow.style.display == "")
			{
				TableToShow.style.display = "none";
			}
			else
			{
				TableToShow.style.display = "";
			}
		}
	}
}

function ExpandMenu_Opera(tr)
{
	// #### MG: Netscape-Method works also with Opera so we use it
	ExpandMenu_Netscape(tr);
}

// #### MG: This method collapses all the menus
function CollapseAllMenus_IE(parentTable)
{	
	if(parentTable.firstChild.hasChildNodes())
	{
		var tr = parentTable.firstChild.firstChild;
		
		
		while(tr != null)
		{
			if(tr.firstChild.hasChildNodes())
			{
				var rowFirstChild = tr.firstChild.firstChild;
				
				if(CheckTable(rowFirstChild))
				{
					rowFirstChild.style.display = "none";
					CollapseAllMenus_IE(rowFirstChild);
				}
				else if(CheckImg(rowFirstChild))
				{
					UnSelectItem_IE(tr);
				}
			}
			else
			{
				if(CheckImg(rowFirstChild))
				{
					UnSelectItem_IE(tr);
				}				
			}
			
			if(tr.parentElement.lastChild != tr)
			{
				tr = tr.nextSibling;
			}
			else
			{
				tr = null;
			}
		}
	}
}
	
function CollapseAllMenus_Netscape(parentTable)
{		
	if(parentTable.tBodies.length != 0 && parentTable.tBodies[0].hasChildNodes())
	{		
		var tr = parentTable.tBodies[0].firstChild;
		
		while(tr != null)
		{				
			if(tr.cells[0].hasChildNodes())
			{
				var rowFirstChild = tr.cells[0].firstChild;
			
				if(CheckTable(rowFirstChild))
				{
					rowFirstChild.style.display = "none";																													
					CollapseAllMenus_Netscape(rowFirstChild);
				}
				else if(CheckImg(rowFirstChild))
				{
					UnSelectItem_IE(tr);
				}
			}
			
			if(tr.parentNode.childNodes[tr.parentNode.childNodes.length - 2] != tr)
			{
				tr = tr.nextSibling;
			}
			else
			{					
				tr = null;
			}
		}
	}
}

function CollapseAllMenus_Opera(parentTable)
{
	// #### MG: For now the Netscape-Methods work, so this method is not necessary
}
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------	
// #### MG: Methods for the MouseOver-Effects
// #### MG: The Methods HighlightItem(...) and UnHighlightItem(...) call
// #### MG: other Methods depending on the browser
// --------------------------------------------------------------------------
function HighlightItem(obj)
{
	if(Netscape)
	{			
		HighlightItem_Netscape(obj.parentNode);			
	}
	else if(IE)
	{
		HighlightItem_IE(findRow(obj));
	}
	else if(Opera)
	{
		HighlightItem_Opera(obj.parentNode);
	}
	else
	{
		alert("Functionality not implemented for the browser type.");
		return;
	}
}	

function UnHighlightItem(obj)
{
	if(Netscape)
	{
		UnHighlightItem_Netscape(obj.parentNode);
	}
	else if(IE)
	{
		UnHighlightItem_IE(findRow(obj));
	}
	else if(Opera)
	{
		UnHighlightItem_Opera(obj.parentNode);
	}
	else
	{
		alert("Functionality not implemented for the browser type.");
		return;
	}
}

// #### MG: This method is called when the menuitem has no ChildNodes
function SelectItem(obj)
{
	// #### MG: We deselect the last clicked item and select the new one		
	if(lastSelectedMenuItem != null)
	{
		UnSelectItem(lastSelectedMenuItem);		
	}
	
	lastSelectedMenuItem = obj;
	
	if(Netscape)
	{
		SelectItem_Netscape(obj.parentNode);
	}
	else if(IE)
	{			
		SelectItem_IE(obj.parentNode);
	}
	else if(Opera)
	{
		SelectItem_Opera(obj.parentNode);
	}
	else
	{
		alert("Functionality not implemented for the browser type.");
		return;
	}
}

function UnSelectItem(obj)
{	
	if(Netscape)
	{
		UnSelectItem_Netscape(obj.parentNode);
	}
	else if(IE)
	{			
		UnSelectItem_IE(obj.parentNode);
	}
	else if(Opera)
	{
		UnSelectItem_Opera(obj.parentNode);
	}
	else
	{
		alert("Functionality not implemented for the browser type.");
		return;
	}
}

// #### MG: Highlights the menuitem when using Microsoft Internet Explorer
function HighlightItem_IE(tr)
{
	if(CheckTr(tr))
	{	
		var img = tr.firstChild.firstChild;
		
		if(CheckImg(img))
		{
			if(img.src.indexOf(SelectedImage) == -1)
			{
				img.src = HighlightedImage;
			}
		}	
	}
}

// #### MG: UnHighlights the menuitem when using Microsoft Internet Explorer
function UnHighlightItem_IE(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.firstChild.firstChild;
		
		if(CheckImg(img))
		{
			if(img.src.indexOf(SelectedImage) == -1)
			{
				img.src = PlaceholderImage;
			}
		}	
	}
}

// #### MG: Selects the menuitem when using Microsoft Internet Explorer
function SelectItem_IE(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.firstChild.firstChild;
		
		if(CheckImg(img))
		{
			img.src = SelectedImage;
		}
	}	
}

// #### MG: UnSelects the menuitem when using Microsoft Internet Explorer
function UnSelectItem_IE(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.firstChild.firstChild;
		
		if(CheckImg(img))
		{
			img.src = PlaceholderImage;
		}
	}
}

// #### MG: Highlights the menuitem when using Netscape Navigator
function HighlightItem_Netscape(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.cells[0].firstChild;
		
		if(CheckImg(img))
		{
			if(img.src.indexOf(SelectedImage) == -1)
			{
				img.src = HighlightedImage;
			}
		}	
	}
}

// #### MG: UnHighlights the menuitem when using Netscape Navigator
function UnHighlightItem_Netscape(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.cells[0].firstChild;
		
		if(CheckImg(img))
		{
			if(img.src.indexOf(SelectedImage) == -1)
			{
				img.src = PlaceholderImage;
			}
		}
	}
}

// #### MG: Selects the menuitem when using Netscape Navigator
function SelectItem_Netscape(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.cells[0].firstChild;
		
		if(CheckImg(img))
		{
			img.src = SelectedImage;
		}
	}	
}

// #### MG: UnSelects the menuitem when using Netscape Navigator
function UnSelectItem_IE(tr)
{
	if(CheckTr(tr))
	{
		var img = tr.cells[0].firstChild;
		
		if(CheckImg(img))
		{
			img.src = PlaceholderImage;
		}
	}
}

// #### MG: Highlights the menuitem when using Opera
function HighlightItem_Opera(tr)
{
	// #### MG: The method for IE works also for Opera, so we use it
	HighlightItem_IE(tr);	
}	

// #### MG: UnHighlights the menuitem when using Opera
function UnHighlightItem_Opera(tr)
{
	// #### MG: The method for IE works also for Opera, so we use it
	UnHighlightItem_IE(tr);	
}

// #### MG: Selects the menuitem when using Opera
function SelectItem_Opera(tr)
{
	// #### MG: The method for IE works also for Opera, so we use it
	SelectItem_IE(tr);	
}

// #### MG: UnSelects the menuitem when using Opera
function UnSelectItem_Opera(tr)
{
	// #### MG: The method for IE works also for Opera, so we use it
	UnSelectItem_IE(tr);
}
// --------------------------------------------------------------------------	
