//Copyright Alexander Augustynska
var fontSize = 10;
var left = 10;
var top = 25;
var border = 3;
var height = 15;
var width = 125;
var backCentrale = "#FF9933";
var fontCentrale = "black";
var backLaterale = "#CCCCCC";
var fontLaterale = "black";
var bordCol = "#FFCC66";
var time = 10;
var timeOut;
var step = 4;
var lvlArray = new Array();
var ritorno = false;
var id = -1;

function Menu(voce,url)
{
	this.voce = voce;
	this.url = url;
	this.sub = new Array();
}

function init(nomeMenu)
{
	var livello = new Lvl(nomeMenu,left,top,0);
	livello.elem.style.clip = "rect(0px "+ width + "px "+ height +"px 0px)";
	scorriP(livello,height,height*nomeMenu.sub.length,false);
}

function scorriP(l,a,b,v)
{
	this.l = l;
	this.a = a;
	this.b = b;
	this.v = v;
	if (this.a < this.b)
	{
		if (this.v)
		{
			(this.b - step < height) ? this.b = height : this.b -= step;
			this.l.elem.style.height = this.b + "px";
			this.l.elem.style.clip = "rect(0px "+ width + "px "+ this.b +"px 0px)";

		}
		else
		{
			(this.a + step > height*this.l.nome.sub.length) ? this.a = height*this.l.nome.sub.length : this.a += step;
			this.l.elem.style.height = this.a + "px";
			this.l.elem.style.clip = "rect(0px "+ width + "px "+ this.a +"px 0px)";
		}
		timeOut = setTimeout("scorriP(this.l,this.a,this.b,this.v)",time);
	}
	else
	{
		if (ritorno)
		{
			var sfondo = new Lvl(null,left,top-height,0);
			sfondo.elem.id = "sfondo";
			sfondo.elem.lastChild.nodeValue = "C a t a l o g o";
			toTop(top,top-height,null,sfondo);			
		}
		else if (this.v)
		{
			var sfondo = new Lvl(null,left,top,0);
			sfondo.elem.id = "sfondo";
			var cima = new Lvl(null,left,top,-1);
			cima.elem.id = "cima";
			cima.elem.lastChild.nodeValue = "C a t a l o g o";
			toTop(top,top-height,cima,sfondo);
		}
		else
		{
			clearTimeout(timeOut);
			var lvs = new Array();
			var alt = top;
			for (var i in this.l.nome.sub)
			{
				lvs.push(new Lvl(this.l.nome.sub[i],left,alt,1));
				alt += height;
			}
			scorriS(lvs,left,width+left,false,null);
		}
	}
}

function scorriS(es,a,b,v,l)
{
	this.es = es;
	this.a = a;
	this.b = b;
	this.v = v;
	this.l = l;
	if (this.a < this.b)
	{
		if (this.v)
		{
			if (this.b - step < left)
				this.b = left;
			else
				this.b -= step;
		}
		else if (this.a + step > width + left)
				this.a = width + left;
			else
				this.a += step;
		for (var i in this.es)
		{
			if (this.v)
			{
				this.es[i].elem.style.left = this.b + "px";
				this.es[i].elem.style.clip = "rect(0px " + width + "px " + height + "px " + (left+width-this.b) + "px)" ;
			}
			else
			{
				this.es[i].elem.style.left = this.a + "px";
				this.es[i].elem.style.clip = "rect(0px " + width + "px " + height + "px " + (left+width-this.a) + "px)";
			}
		}
		timeOut = setTimeout("scorriS(this.es,this.a,this.b,this.v,this.l)",time);
	}
	else
	{
		clearTimeout(timeOut);
		if (this.v)
		{
			for (var i in this.es)
			{
				this.es[i].elem.style.left = left + "px";
				document.body.removeChild(this.es[i].elem);
			}
			if (this.l)
				toUp(this.l);
			else
				scorriP(lvlArray[1],height,height*lvlArray[1].nome.sub.length,true);
		}
		else
			for (var i in lvlArray)
				lvlArray[i].move = false;
	}
}

function Lvl(nome,l,t,num)
{
	this.nome = nome;
	this.num = num;
	this.elem = document.createElement("DIV");
	this.nome ? this.elem.id = this.nome.url.substring(0,this.nome.url.indexOf(".")) : null;
	this.elem.style.left = l + "px";
	this.elem.style.top = t + "px";
	this.elem.style.height = height + "px";
	this.elem.style.width = width + "px";
	if (this.num == 0)
		this.elem.style.backgroundColor = backCentrale;
	else
		this.elem.style.backgroundColor = backLaterale;
	((this.num == 1) && imag) ? this.elem.style.backgroundImage = "url("+imag["imgOff"].src+")" : null;
	((this.num == -1) && imag) ? this.elem.style.backgroundImage = "url("+imag["imgOffC"].src+")" : null;
	this.elem.style.position = "absolute";
	if (this.num != 1)
	{
		this.elem.style.borderLeftStyle = "solid";
		this.elem.style.borderLeftWidth = border + "px";
		this.elem.style.borderLeftColor = bordCol;
	}
	else
	{
		this.elem.style.borderRightStyle = "solid";
		this.elem.style.borderRightWidth = border + "px";
		this.elem.style.borderRightColor = bordCol;
	}
	if  (this.num == -1)
	{
//		this.elem.style.borderTopStyle = "solid";
//		this.elem.style.borderTopWidth = border + "px";
//		this.elem.style.borderTopColor = bordCol;
		this.elem.style.borderRightStyle = "solid";
		this.elem.style.borderRightWidth = border + "px";
		this.elem.style.borderRightColor = bordCol;
	}
	if  (this.num == 0)
	{
//		this.elem.style.borderBottomStyle = "solid";
//		this.elem.style.borderBottomWidth = border + "px";
//		this.elem.style.borderBottomColor = bordCol;
	}

	this.elem.style.textAlign = "center";
	this.elem.style.fontSize = fontSize;
	this.elem.style.fontFamily = "Verdana";//, Arial, Helvetica, sans-serif";
	this.num != 1 ? this.elem.style.fontWeight = "bold" : null;
	this.elem.style.clip = "rect(0px 0px 0px 0px)";
	if (num == 0)
		this.elem.style.color = fontCentrale;
	else
		this.elem.style.color = fontLaterale;
	num == 0 ? this.elem.style.cursor = "default" : this.elem.style.cursor = "pointer";
	this.move = true;
	this.nome ? this.elem.appendChild(document.createTextNode(this.nome.voce)) : this.elem.appendChild(document.createTextNode(""));
	document.body.appendChild(this.elem);
	if (this.num != 0)
		setEvent(this);
	lvlArray.push(this);
}

function setEvent(lv)
{
	lv.elem.onmouseover = function()
	{
//		lv.elem.style.fontSize = fontSize + 1;
		((lv.num == 1) && imag) ? lv.elem.style.backgroundImage = "url("+imag["imgOn"].src+")" : null;
		((lv.num == -1) && imag) ? lv.elem.style.backgroundImage = "url("+imag["imgOnC"].src+")" : null;
	}
	lv.elem.onmouseout = function()
	{
//		lv.elem.style.fontSize = fontSize;
		((lv.num == 1) && imag) ? lv.elem.style.backgroundImage = "url("+imag["imgOff"].src+")" : null;
		((lv.num == -1) && imag) ? lv.elem.style.backgroundImage = "url("+imag["imgOffC"].src+")" : null;
	}
	lv.elem.onclick = function()
	{
		if (!lv.move && (lv.num != 0))
		{
			if (lv.nome.sub.length)
			{
				var fratelli = new Array();
				var ind = new Array();
				for (var i in lvlArray)
				{
					lvlArray[i].move = true;
					if ((lv.num == 1) && (lvlArray[i].elem.id != lv.elem.id) && (lvlArray[i].num == 1))
					{
						ind.push(i);
						fratelli.push(lvlArray[i]);
					}
					else if ((lv.num == -1) && (lvlArray[i].num == 1))
					{
						ind.push(i);
						fratelli.push(lvlArray[i]);
					}
				}
				var t = 0;
				for (var j in ind)
				{
					lvlArray.splice(ind[j]-t,1);
					t++;
				}
				if (lv.num == 1)
					scorriS(fratelli,left,width+left,true,lv);
				else if (lv.num == -1)
				{
					ritorno = true;
					scorriS(fratelli,left,width+left,true,null);
				}
			}
			if (lv.num == -1)
			{
				id = -1;
				parent.frames["center"].location.href = lv.nome.url;
			}
			else if (id == -1)
			{
				if (lv.nome.sub.length)
				{
					for (var i in lvlArray[0].nome.sub)
						if (lvlArray[0].nome.sub[i].voce == lv.nome.voce)
							id = i;
				}
				else
					id = 2;
			}
			else
			{
				parent.frames["center"].location.href = "cat.php?id=" + id + "&pg=" + escape(lv.elem.id).replace('+',"%2B") + "&num=0";
			}
			if (id == 2)
			{
				parent.frames["center"].location.href = "cat.php?id=" + id + "&pg=" + escape(lv.elem.id).replace('+',"%2B") + "&num=0";
				id = -1;
			}
		}
	}
}

function toUp(l)
{
	this.l = l;
	if (parseInt(this.l.elem.style.top) > top)
	{
		if ((parseInt(this.l.elem.style.top) - step) > top)
			this.l.elem.style.top = (parseInt(this.l.elem.style.top) - step) + "px";
		else
			this.l.elem.style.top = top + "px"
		timeOut = setTimeout("toUp(this.l)",time);
	}
	else
	{
		clearTimeout(timeOut);
		scorriP(lvlArray[0],height,height*lvlArray[0].nome.sub.length,true);
	}
}

function toTop(a,b,c,s)
{
	this.a = a;
	this.b = b;
	this.c = c;
	this.s = s;
	if (this.a > this.b)
	{
		this.a - step > this.b ? this.a -= step : this.a = top - height;
		this.s.elem.style.clip = "rect(" + (height-top+this.a) + "px " + width + "px " + height + "px 0px)";
		if (this.c)
		{

			this.c.elem.style.clip = "rect(0px " + width + "px " + (top-this.a) + "px 0px)"
			this.c.elem.style.top = this.a + "px";
			lvlArray[0].elem.style.top = this.a + "px";
			lvlArray[0].elem.style.clip = "rect(" + (top-this.a) +"px " + width + "px " + height + "px 0px)";
		}
		else
		{
			this.s.elem.style.top = (2*top-height-this.a) + "px";
			lvlArray[0].elem.style.top = (2*top-height-this.a) + "px";
			lvlArray[0].elem.style.clip = "rect(0px " + width + "px " + (height-top+this.a) + "px 0px)";
		}
		timeOut = setTimeout("toTop(this.a,this.b,this.c,this.s)",time);
	}
	else
	{
		clearTimeout(timeOut);
		if (this.c) {
			document.body.removeChild(lvlArray[0].elem);
			lvlArray[0] = new Lvl(lvlArray[0].nome,left,top-height,-1);
			lvlArray[0].elem.style.clip = "rect(0px "+ width + "px "+ height +"px 0px)";
			lvlArray.pop();
			document.body.removeChild(this.c.elem);
			lvlArray.pop();
			var centro = new Lvl(null,left+width,top,0);
			centro.elem.id = "centro";
			centro.elem.lastChild.nodeValue = lvlArray[1].nome.voce;
			toCenter(left+width-1,left,centro);
		}
		else
		{
			document.body.removeChild(lvlArray[0].elem);
			document.body.removeChild(lvlArray[1].elem);
			var centro = new Lvl(lvlArray[0].nome,left,top,0);
			centro.elem.style.clip = "rect(0px "+ width + "px "+ height +"px 0px)";
			lvlArray[0] = centro;
			document.body.removeChild(this.s.elem);
			lvlArray.pop();
			lvlArray.pop();
			lvlArray.pop();
			ritorno = false;
			scorriP(centro,height,height*centro.nome.sub.length,false);
		}
	}
}

function toCenter(a,b,c)
{
	this.a = a;
	this.b = b;
	this.c = c;
	if (this.a > this.b)
	{
		this.a - step > this.b ? this.a -= step : this.a = left;
		this.c.elem.style.left = this.a + "px";
		this.c.elem.style.clip = "rect(0px " + (width+left-this.a) +"px " + height + "px 0px)";
		lvlArray[1].elem.style.left = this.a + "px";
		timeOut = setTimeout("toCenter(this.a,this.b,this.c)",time);
	}
	else
	{
		clearTimeout(timeOut);
		document.body.removeChild(lvlArray[1].elem);
		var el = new Lvl(lvlArray[1].nome,left,top,0);
		el.elem.style.clip = "rect(0px "+ width + "px "+ height +"px 0px)";
		lvlArray[1] = el;
		lvlArray.pop();
		document.body.removeChild(lvlArray[2].elem);
		document.body.removeChild(lvlArray[3].elem);
		lvlArray.pop();
		lvlArray.pop();
		el.nome.sub.length ? scorriP(el,height,height*el.nome.sub.length,false) : lvlArray[0].move = false;
	}		
}
