
/*  ---------------------------------------------------------------------------
 *  MENU DOM
 *  Christoph Studer
 *  version 1.0, 16.06.2003
 *  ---------------------------------------------------------------------------
 *  This scripts handle the interaction between the menu structure (in menu.js)
 *  and the DOM / HTML
 *  e.g. it catches the onclick event, marks menus, opens and closes them etc.
 *  ---------------------------------------------------------------------------
 */
 
// the last menu clicked on or marked by a page load.
var lastselected = null;
 
// put click handlers here if you like!
function clicked(num)
{
	pageLoaded(num);
	return true;	
}

// use this function from within an other page to open the
// menu at a certain node...
function pageLoaded(id)
{
	var menuitem = divmenu.getById(id);	
	if (menuitem)
	{
		if ( (lastselected && lastselected.id != id) || !lastselected)
		// only do something if the menu is not already open and selected...
		{			
			openMenu(id);
			markItem(id);
		}
	}
	else
	{
		if (lastselected)
		{
			closeMenu(lastselected.id,true);
			markItem(lastselected.id,'off');
		}
		lastselected = null;
	}
}

// open the menu (inclusive the parents...)
function openMenu(num)
{
	var el = document.getElementById('submenus'+num);
	// exit if it's already open

	var others = new Array();
	var parent = null;
	curitem = divmenu.getById(num);
	parent = curitem.parent;
	others = curitem.getBrothers();
	
	// close lastselected recursively (inclusive parents)
	if (lastselected)
		closeMenu(lastselected.id,true);
	
	// also open the parent's menu
	if (parent)
	{
		//alert('also open my parent');
		openMenu(parent.id);
	}
	//menuitemel.className = menuitemel.className + 'on';
	if (el) 
		el.style.display = "block";
}

// close the menu (leave children open)
function closeMenu(id,recursive)
{
	var el = document.getElementById('submenus'+id);
	if (el)
	{
		el.style.display = "none";
	}
	if (recursive)
	{
		var menuitem = divmenu.getById(id);
		var parent = menuitem.parent;
		if (parent)
			closeMenu(parent.id,true);
	}
}

function menuOver(id)
{
	if (!lastselected || lastselected.id!=id)
	{
			menuitem = mpmenu.getById(id);
			curstyles = levels[menuitem.level];
			for (i=0;i<curstyles['ids'].length;i++)
			{
				el = document.getElementById(curstyles['ids'][i]+id);
				if(el)
				{
					el.className = curstyles['over'][i];
				}
			}
	}
}

function menuOut(id)
{
	if (!lastselected || lastselected.id!=id)
	{
		menuitem = mpmenu.getById(id);
		curstyles = levels[menuitem.level];
		for (i=0;i<curstyles['ids'].length;i++)
		{
			el = document.getElementById(curstyles['ids'][i]+id);
			if(el)
			{
				el.className = curstyles['off'][i];
			}
		}
	}
}

// mark this menu item (inclusive the parents...) as selected
// unmark all others

function markItem(id,off,recursed)
{	
	var curid = "";
	var curstyles = new Array();
	var menuitem = divmenu.getById(id);	
	var curitem = menuitem;
	var parent = menuitem.parent;
	var el = null;
	var mode = off ? 'off':'on';
	
	if (lastselected && lastselected.id == id && mode == 'on') {
		return;
	}
	if (lastselected && mode == 'on' && lastselected.level>=menuitem.level)
	{
		// unmark the last selected item but only if it's on the same level or deeper			
		markItem(lastselected.id,'off',true);
	}
	if (parent)
	{
		markItem(parent.id,off,true);
	}

	curstyles = levels[menuitem.level];
	for (i=0;i<curstyles['ids'].length;i++)
	{
		el = document.getElementById(curstyles['ids'][i]+menuitem.id);
		if(el)
		{
			el.className = curstyles[mode][i];	
		}
	}
	if (!recursed)
		lastselected = menuitem;
}

function writePathHTML(id)
{
	if (typeof(pathframe) == 'undefined') return;
	var menuitem = divmenu.getById(id);
	var el = pathframe.document.getElementById('pathcontainer');
	var html = '';
	if (menuitem)
	{
		var path = menuitem.getPath();
		var itemhtml = '';
	
		for (var i=0;i<path.length;i++)
		{
			if (i==0)
				itemhtml = pathitemfirst;
			else if (i==path.length-1)
				itemhtml = pathitemlast;
			else
				itemhtml = pathitem;
			
			if (i>0) html += pathseperator;
			html += itemhtml.replace('%name%',path[i].name).replace('%url%',path[i].link).replace('%id%',path[i].id);
		}
	} else
		html = '';
	el.innerHTML = html;
}
