// Motions Menu - (c) 2007 Paolo Dente <paolo at dente biz>

function getCookieValue(name)
	{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ')
				c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0)
				{
				return c.substring(nameEQ.length,c.length);
				}
		}
	return '';
	}
	
function setCookieValue(name, val, days)
	{
	c = name+'='+val+";";
	if(days)
		{
	  	date = new Date();
		date.setDate(date.getDate()+days);
		c += "expires=" + date.toUTCString();
		}
	document.cookie = c;
	}

function mtMenuItem(node) 
	{
	if(node.nodeType!=1)
		return '';
	var itm = document.createElement('LI');
	var link = document.createElement('A');
	itm.appendChild(link);

	if(node.parentNode.tagName=='item')
		{
		itm.id = node.parentNode.getAttribute("id")+'!'+node.getAttribute("id");
		itm.className = 'item_off';
		}
		else
		{
		itm.id = node.getAttribute("id");
		itm.className = 'menu_off';
		}


	img = node.getAttribute("image")
	
	if(img)
		{
		link.innerHTML = '<img src="'+img+'" alt="'+node.getAttribute("title")+'" title="'+node.getAttribute("title")+'" border="0"/>';
		}
	else
		{
		link.innerHTML = node.getAttribute("title");
		}
	link.onclick = mtOnItemClick;

	var subitems = node.getElementsByTagName('item');

	if(subitems.length)
		{
		var submenu = document.createElement('UL');
		for(var i=0; i<subitems.length; i++)
			{
			var subitm = mtMenuItem(subitems[i]);
			subitm.parent = itm;
			if(subitm)
				submenu.appendChild(subitm);
			}
		itm.appendChild(submenu);
		link.href = '#';
//		link.onclick = 'mtShowSubmenu('+itm.id+'); return false;';
		}
	else
		{
		try{
			link.href = node.firstChild.nodeValue;
			if(location.pathname == node.firstChild.nodeValue)
				{
				activeItem = itm.id;
				}
			} 
		catch(e) 
			{
			link.href = '#'
			}
		}
	return itm;
	}

function mtOnItemClick(evt)
	{
	var evt = evt || window.event;
	var src = evt.target || evt.srcElement;
	while (src && src.tagName != 'LI')
		{
		src = src.parentNode;
		}
	mtShowSubmenu(src);
	}

function mtShowSubmenu(src)
	{
	if(!src)
		return;
	var current = document.getElementById(activeItem);
	if(current)
		{
		if(current.parent)
			{
			current.className = 'item_off';
			current.parent.className = 'menu_off';
			}
		else
			{
			current.className = "menu_off";
			}
		}

	if(src.parent)
		{
		src.parent.className = 'menu_attivo';
		src.className = 'item_attivo';
		}
		else
		{
		src.className = 'menu_attivo';
		}
	activeItem = src.id;
	setCookieValue('activeMenuItem',src.id);
	}

function mtInitMenu()
	{
	if(!menuFile)
		menuFile = "menu.xml";
	mainMenu = document.createElement('DIV');
	mainMenu.id = 'mainMenu';
	menu = document.createElement('UL');
	mainMenu.appendChild(menu);
	// CHECK IMPLEMENTATION
	if(document.implementation && document.implementation.createDocument)
		{
		var xmlDoc = document.implementation.createDocument("", "doc", null);
		}
	else if(window.ActiveXObject)
		{
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}
	else
		{
		document.body.innerHTML="Questo browser non supporta le funzioni necessarie.";
		return;
		}
	try{
		xmlDoc.async = false;
		xmlDoc.load(menuFile);
		} 
	catch (e) //Safari
		{
		var req = new XMLHttpRequest();
		req.open("GET", menuFile, false);
		req.send("");
		if(req.readyState==4 && req.status==200)
			xmlDoc = req.responseXML;
		else
			document.body.innerHTML="Questo browser non supporta le funzioni necessarie.";
		}
	var c = xmlDoc.documentElement.childNodes;
	for(var i=0; i<c.length; i++)
		{
		var itm = mtMenuItem(c[i]);
		if(itm)
			menu.appendChild(itm);
		}
	return true;
	}


function mtBuildMenu()
	{
	activeItem = null;
	if(mtInitMenu()) // Il default è "menu.xml" - per usare un file diverso per sezione, passa il nome del file come parametro
		{
		document.getElementById('container').appendChild(mainMenu);
		if(!activeItem) activeItem = getCookieValue('activeMenuItem');
		if(activeItem)
			{
			mtShowSubmenu(document.getElementById(activeItem));
			}
		try{
			document.getElementById('drag').style.visibility = "visible";
			myScroll = new ScrollObj(6,21,767,"track","up","down","drag","contentMask","content");
			}
		catch(e)
			{
			}
		}

	}