var m1InitPosY = new Array();
var spM2 = 25; // interlinea tra la voci dei menu
var interLineaM2 = 0.8;

// Restituisce l'id "puilito" del bottone di primo livello cliccato
function getId(m)
{
	var id = $(m).attr('id').substring(($(m).attr('id').length - 2));
	id = (id.indexOf('0') == 0)?id.substring(1):id;
	return id;	
}

function getMenu2H()
{
	return $("#menu2").height();
}

// Popola il menu2 con l'html corrispondente al bottone di primo livello cliccato
function popMenu2(m)
{
	var m2Html = '';
	$("#menu2").empty();
	for(var i=0; i<m2Items[getId(m)].length; i++)
	{
		var nTmp = (i <= 9)?'0'+i:i;
		var id = 'm2'+nTmp;
		$("#menu2").height(m2Items[getId(m)].length*(spM2*interLineaM2));
		if(gup('m2') == id)
		{
			$("#menu2").append($('<p id=\''+id+'\' class=\'menuItem2 menuItem2Curr\' style=\'top: '+(i*(spM2*interLineaM2))+'px;\' rel="'+path+'pages/'+m2Items[getId(m)][i][1]+'?m2='+id+'">'+m2Items[getId(m)][i][0]+'</p><br/>'));
		}
		else
		{
			$("#menu2").append($('<a href="'+path+'pages/'+m2Items[getId(m)][i][1]+'?m2='+id+'" id=\''+id+'\' class=\'menuItem2\' style=\'top: '+(i*(spM2*interLineaM2))+'px;\'>'+m2Items[getId(m)][i][0]+'</a><br/>'));
		}
	}
	// Se siamo nella versione inglese, aggiungo a tutti i link di secondo livello il param eng
	checkLang();
	resetPosy(m);
}

function initM1()
{
	$("#menu a.menuItem1").each(
		function(i)
		{
			m1InitPosY[i] = $(this).css('top');
		}
	);
}

function resetPosy(m)
{
	$("#menu a.menuItem1").each(
		function(i)
		{
			$(this).animate(
				{"top":  m1InitPosY[i]},
				"slow"
				);
		}
		);
	openM1(1, m);
}

function openM1(i, m)
{
	// Trovo l'id del menu cliccato e lo uso come indice per estrarre dall'array la posizione iniziale
	var id = getId(m);
	var t = m1InitPosY[id].substring(0, (m1InitPosY[id].length - 2))*1+(spM2*interLineaM2)+'px';
	// Quindi sposto in giu' tutti i menu sottostanti a quello cliccato e faccio apparire menu2 nel punto giusto
	$(m).nextUntil("div")
	.animate({"top": "+="+getMenu2H()+"px"}, "slow",function(){$("#menu2").css({top: t}).slideDown("slow")});
}

function m1Click()
{
	$("#menu2").hide();
	popMenu2($(this));
	//resetPosy($(this));
	
	//return false;
}

function m1Over()
{
	//console.log('m1Over');
}


function m1Out()
{
	//console.log('m1Out');
}

function showM2(m)
{
	/* */
}

function setM2Curr()
{
	/* */
}

/**
********************************************************************************
* document ready
********************************************************************************
*/



$(document).ready(function()
{
	$("#menu2").hide();
	initM1();
	$("#menu a.menuItem1").click(m1Click);
	//$("#menu a.menuItem1").bind('click', m1Click);
	$("#menu a.menuItem1").hover(m1Over, m1Out);
}); // close document.ready