var autoclosetimer = null;
var displayed_panels = new Array(null, null, null, null);


function getAbsoluteXPos(el)
{
	result = 0;
	while (el != null)
	{
		result += el.offsetLeft;
		el = el.offsetParent;	
		if (el.tagName == "DIV" && el.style.position != "absolute") break;
	}
	
	return result;
}

function getAbsoluteYPos(el)
{
	result = 0;
	while (el != null)
	{
		result += el.offsetTop;
		el = el.offsetParent;	
		if (el.tagName == "DIV" && el.style.position != "absolute") break;
	}
	
	return result;
}


// attivazione dello stile over menù verticale
function select_menu_item(menuid, itemid, level, opendirection)
{
	clear_delayed_unselect_menu_item();
	unselect_all_siblings_items(level == 2 ? 1 : level);
	
	displayed_panels[level] = new Array (menuid, itemid);

	// var item_el = document.all["td_" + menuid + "_" + itemid];
	// var arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
	// var sub_el = document.all["sub_" + menuid + "_" + itemid];
	
	var item_el = null;
	var arrow_el = null;
	var sub_el = null;
	
	if (document.layers)
    {
    	// Netscape 4 code fork...
    	item_el = document.layers["td_" + menuid + "_" + itemid];
      	arrow_el = document.layers["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.layers["sub_" + menuid + "_" + itemid];
    }
    else if (document.all) {
      	// IE 4/5 code fork...
      
      	item_el = document.all["td_" + menuid + "_" + itemid];
      	arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.all["sub_" + menuid + "_" + itemid];
    }
    else if (document.getElementById) {
      	// Standards Compliant code fork...
      
      	item_el = document.getElementById("td_" + menuid + "_" + itemid);
      	arrow_el = document.getElementById("td_" + menuid + "_" + itemid + "_arrow");
      	sub_el = document.getElementById("sub_" + menuid + "_" + itemid);
    }
    
	
	
	if (item_el.className == 'menu_l1_enabled')
	{
		item_el.className = 'menu_l1_hilight';
		
		if (arrow_el != null)
			arrow_el.src = '../menu/spacer.gif';
	}
	else if (item_el.className == 'menu_l2_enabled')
	{
		item_el.className = 'menu_l2_hilight';
		
		if (arrow_el != null)
			arrow_el.src = '../menu/spacer.gif';
	}	

	if (sub_el != null)
	{
		if (opendirection == 'R')
			desired_x_pos = getAbsoluteXPos(arrow_el) + 10;
		else
			desired_x_pos = getAbsoluteXPos(arrow_el) - item_el.offsetWidth - sub_el.offsetWidth + 12 ;
			
		desired_y_pos = getAbsoluteYPos(arrow_el) -5;

		sub_el.style.left = desired_x_pos;
		sub_el.style.top = desired_y_pos;

		sub_el.style.visibility = 'visible';
	}
}


// attivazione stile over menù orizzontale
function select_horiz_menu_item(menuid, itemid, level, opendirection)
{
	clear_delayed_unselect_menu_item();
	unselect_all_siblings_items(level);
	
	displayed_panels[level] = new Array (menuid, itemid);

	// var item_el = document.all["td_" + menuid + "_" + itemid];
	// var arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
	// var sub_el = document.all["sub_" + menuid + "_" + itemid];
	
	var item_el = null;
	var arrow_el = null;
	var sub_el = null;
	
	if (document.layers)
    {
    	// Netscape 4 code fork...
    	item_el = document.layers["td_" + menuid + "_" + itemid];
      	arrow_el = document.layers["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.layers["sub_" + menuid + "_" + itemid];
    }
    else if (document.all) {
      	// IE 4/5 code fork...
      
      	item_el = document.all["td_" + menuid + "_" + itemid];
      	arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.all["sub_" + menuid + "_" + itemid];
    }
    else if (document.getElementById) {
      	// Standards Compliant code fork...
      
      	item_el = document.getElementById("td_" + menuid + "_" + itemid);
      	arrow_el = document.getElementById("td_" + menuid + "_" + itemid + "_arrow");
      	sub_el = document.getElementById("sub_" + menuid + "_" + itemid);
    }
	
	if (item_el.className == 'menu_hl1_enabled')
		item_el.className = 'menu_hl1_hilight';

	if (sub_el != null)
	{
		if (opendirection == 'R')
			desired_x_pos = getAbsoluteXPos(arrow_el) + 0;
		else
			desired_x_pos = getAbsoluteXPos(arrow_el) + item_el.offsetWidth - sub_el.offsetWidth + 100;
			
		desired_y_pos = getAbsoluteYPos(arrow_el) + 18;

		sub_el.style.left = desired_x_pos;
		sub_el.style.top = desired_y_pos;

		sub_el.style.visibility = 'visible';
	}
}


// disattivazione dello stile over
function unselect_menu_item(menuid, itemid, level)
{
	// var item_el = document.all["td_" + menuid + "_" + itemid];
	// var arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
	// var sub_el = document.all["sub_" + menuid + "_" + itemid];
	
	var item_el = null;
	var arrow_el = null;
	var sub_el = null;
	
	if (document.layers)
    {
    	// Netscape 4 code fork...
    	item_el = document.layers["td_" + menuid + "_" + itemid];
      	arrow_el = document.layers["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.layers["sub_" + menuid + "_" + itemid];
    }
    else if (document.all) {
      	// IE 4/5 code fork...
      
      	item_el = document.all["td_" + menuid + "_" + itemid];
      	arrow_el = document.all["td_" + menuid + "_" + itemid + "_arrow"];
      	sub_el = document.all["sub_" + menuid + "_" + itemid];
    }
    else if (document.getElementById) {
      	// Standards Compliant code fork...
      
      	item_el = document.getElementById("td_" + menuid + "_" + itemid);
      	arrow_el = document.getElementById("td_" + menuid + "_" + itemid + "_arrow");
      	sub_el = document.getElementById("sub_" + menuid + "_" + itemid);
    }
	
	if (level == 1)
	{
		if (item_el.className == 'menu_hl1_hilight')
			item_el.className = 'menu_hl1_enabled';
	}
	else
	{
		if (item_el.className == 'menu_l1_hilight')
		{
			item_el.className = 'menu_l1_enabled';
			if (arrow_el != null)
				arrow_el.src = '../menu/spacer.gif';
		}
		else if (item_el.className == 'menu_l2_hilight')
		{
			item_el.className = 'menu_l2_enabled';
			if (arrow_el != null)
				arrow_el.src = '../menu/spacer.gif';
		}
	}

	if (sub_el != null)
		sub_el.style.visibility = 'hidden';	
}


function unselect_all_siblings_items(level)
{
	for (i = displayed_panels.length; i >= level; i--)
	{
		if (displayed_panels[i] != null)
		{
			unselect_menu_item(displayed_panels[i][0], displayed_panels[i][1], i);
			displayed_panels[i] = null;
		}
	}
}


function delayed_unselect_menu_item()
{
	clear_delayed_unselect_menu_item();
		
	autoclosetimer = setTimeout('unselect_all_siblings_items(0)', 100);
}


function clear_delayed_unselect_menu_item()
{
	if (autoclosetimer != null)
		clearTimeout(autoclosetimer);
	
	autoclosetimer = null;
}


function onmousedown_lx_menu(desturl, openinpage)
{
	if (desturl != '')
	{
		switch (openinpage)
		{
		case 0 :
			window.open(desturl, '_blank');
			break;
		case 2 :
			delayed_unselect_menu_item();
			eval(desturl);
			break;
		case 3 :
			window.open(desturl, 'main');
			break;			
		default:
			location.href = desturl;	
		}
	}
}


function onmousedown_l2menu(desturl, openinpage)
{
	if (desturl != '')
	{
		switch (openinpage)
		{
		case 0 :
			window.open(desturl, '_blank');
			break;
		case 2 :
			delayed_unselect_menu_item();
			eval(desturl);
			break;
		case 3 :
			window.open(desturl, 'main');
			break;
		default:
			location.href = desturl;	
		}
		
	}
}

// menù di primo livello
function o_m(bgimage, isbar)
{
	s = '';
	if (isbar)
	{
		s += '<DIV style="position:relative;top:0;left:0;z-index:110;">';
		s += '<table height="100%" align="right" border="0" cellspacing="0" style="background-image: URL(' + bgimage + ');">';
	}
	else
	{
		s += '<DIV style="position:relative;top:0;left:0;z-index:100;">';
		s += '<table height="100%" width="100%" border="0" cellspacing="0" cellpadding=0 style="background-image: URL(' + bgimage + ');">';
	}
	
	document.write(s);
}

// menù orizzontale
function w_m_l1h(menu_id, elem_id, elem_name, elem_url, open_in_page, open_direction, enabled, has_children)
{
	s = '<td valign="bottom" class=';
	if (enabled)
		s += '"menu_hl1_enabled"';
	else
		s +='"menu_hl1_disabled"';

	s += ' id="td_' + menu_id + '_' + elem_id + '"';
	s += ' onmouseover="select_horiz_menu_item(' + menu_id + ', ' + elem_id + ', 1, \'' + open_direction + '\');"';
	s += ' onmouseout="delayed_unselect_menu_item();"';

	if (elem_url.length > 0)
		s += ' onmousedown="onmousedown_lx_menu(\'' + elem_url + '\', ' + open_in_page + ');" style="cursor:hand;"';
	else
		s += ' style="cursor:hand;"';
			
	s += '>';
		
	if (has_children)
		
	s += elem_name;
	s += '</td>';
	
	document.write(s);
}


// voci del menù di primo livello
function w_m_l1v(menu_id, elem_id, elem_name, elem_url, open_in_page, open_direction, enabled, has_children)
{
	//s = '<tr><td style="padding-left:8px;" class=';
	s = '<tr><td style="padding-left:4px;" class=';
	if (enabled)
		s += '"menu_l1_enabled"';
	else
		s += '"menu_l1_disabled"';

	s += ' id="td_' + menu_id + '_' + elem_id + '"';
	s += ' onmouseover="select_menu_item(' + menu_id + ', ' + elem_id + ', 2, \'' + open_direction + '\');"';
	s += ' onmouseout="delayed_unselect_menu_item();"';

	if (elem_url.length > 0)
		s += ' onmousedown="onmousedown_l2menu(\'' + elem_url + '\', ' + open_in_page + ');" style="cursor:hand;"';
	else
		s += ' style="cursor:hand;"';
			
	s += ' noWrap>';
		
	if (has_children)
		s += '<img id="td_' + menu_id + '_' + elem_id + '_arrow" src="../menu/spacer.gif" align=right width=10px height=10px border=0px vspace=0px hspace=0px>';
		
	s += '<img src="../menu/cube.gif" border="0">&nbsp;&nbsp;' + elem_name;

//se ha un sottomenu	
	if (has_children)
		s += '';
	
	s += '</td></tr>';

	document.write(s);
}


//chiusura delle voci del menù di primo livello
function e_m_11lv()
{
	document.write('</table>');
}


// menu di secondo livello
function o_m_l2(menu_id, parent_id)
{
	document.write('<div id="sub_' + menu_id + '_' + parent_id + '" class="menu_div" style="position:absolute;top:0;left:0;visibility:hidden;">');
	document.write('<table width="100%" border="0">');	
}

// menu orizzontale di secondo livello
function o_m_l2h(menu_id, parent_id)
{
	document.write('<div id="sub_' + menu_id + '_' + parent_id + '" class="menu_div" style="position:absolute;top:0;left:0;visibility:hidden;">');
	document.write('<table width="130" border="0">');	
}

//voci del menù di secondo livello
function w_m_l2v(menu_id, elem_id, cur_level, elem_name, elem_url, open_in_page, open_direction, enabled, has_children)
{
	s = '<tr><td class=';
	
	if (enabled)
		s += '"menu_l2_enabled"';
	else
		s += '"menu_l2_disabled"';
	
	s += ' id="td_' + menu_id + '_' + elem_id + '"';
	s += ' onmouseover="select_menu_item(' + menu_id + ', ' + elem_id + ', ' + cur_level + ', \'' + open_direction + '\');"';
	s += ' onmouseout="delayed_unselect_menu_item();"';

	if (elem_url.length > 0)
		{
			s += ' onmousedown="onmousedown_l2menu(\'' + elem_url + '\', ' + open_in_page + ');" style="cursor:hand;"';
		}
	else
		s += ' style="cursor:default;"';

	s += ' noWrap>';
	
	if (has_children)
		s += '<img id="td_' + menu_id + '_' + elem_id + '_arrow" src="../menu/spacer.gif" align=right width=10px height=10px border=0px vspace=0px hspace=0px>';

	s += '&nbsp;&nbsp;' + elem_name + '&nbsp;&nbsp;';
	
	if (has_children)
		s += '&nbsp;&nbsp;&nbsp;';

	s += '</td></tr>';

	document.write(s);
}

function e_m_12lh()
{
	document.write('</tr></table>');
}


// chiusura menù di secondo livello
function c_m_l2()
{
	document.write('</table></div>');
}

// apertura menù di terzo livello
function o_m_l3(menu_id, parent_id)
{
	document.write('<div id="sub_' + menu_id + '_' + parent_id + '" class="menu_div" style="position:absolute;top:0;left:0;visibility:hidden;">');
	document.write('<table width="100%" border="0">');	
}


// voci di menù di terzo livello
function w_m_l3v(menu_id, elem_id, cur_level, elem_name, elem_url, open_in_page, open_direction, enabled, has_children)
{
	s = '<tr><td class=';
	
	if (enabled)
		s += '"menu_l2_enabled"';
	else
		s += '"menu_l2_disabled"';
	
	s += ' id="td_' + menu_id + '_' + elem_id + '"';
	s += ' onmouseover="select_menu_item(' + menu_id + ', ' + elem_id + ', ' + cur_level + ', \'' + open_direction + '\');"';
	s += ' onmouseout="delayed_unselect_menu_item();"';

	if (elem_url.length > 0)
		s += ' onmousedown="onmousedown_lx_menu(\'' + elem_url + '\', ' + open_in_page + ');" style="cursor:hand;"';
	else
		s += ' style="cursor:default;"';

	s += ' noWrap>';
	
	if (has_children)
		s += '<img id="td_' + menu_id + '_' + elem_id + '_arrow" src="../menu/spacer.gif" align=right width=10px height=10px border=0px vspace=0px hspace=0px>';

	s += '&nbsp;&nbsp;' + elem_name + '&nbsp;&nbsp;';
	
	if (has_children)
		s += '&nbsp;&nbsp;&nbsp;';

	s += '</td></tr>';

	document.write(s);
}


// chiusura menù di terzo livello
function c_m_l3()
{
	document.write('</table></div>');
}


// chiusura menù di primo livello
function c_m()
{
	document.write('</div>');
}
