var menu={
	playing:0,
	liv_1:[],
	queue:[],
	ScarpaSubMenu:null,
	cursor:{isOver:false},
	delay:{
		start:function(){
			setTimeout("(function(){if(!menu.cursor.isOver){menu.hide()}})()",1);
		}
	},
	hide:function(){
		menu.liv_1.each(function(li){
			var element_loop = li.select(".fix-B").first() ;
			if(element_loop)if(element_loop.visible())element_loop.hide();
		});
		$("overlapIEfix").hide();
	},
	fixIEoverlap:function(){},
	transition:function(DIV){
		Effect.SlideDown(DIV,{
			duration:1,
			beforeStart:function(e){
				menu.playing=1;
				menu.ScarpaSubMenu = e.element ;
				menu.hide();
			},
			afterUpdate:function(e){
				menu.fixIEoverlap(e);
			},
			afterFinish:function(){
				menu.playing=0;
				if(menu.queue.length>0){
					var q=menu.queue[0];
					menu.queue=[];
					var DIV=$(q).up().select(".fix-B").first();
					if(DIV){
						if(!DIV.visible()){
							menu.show(q);
						}
					}else{
						menu.show(q);
					}
				}
			}
		});		
	},
	select:function(){
		if(arguments.length>0){
			var liv=arguments[0];
			var depth=0;
		}else{
			var liv=$("menu-header").down("UL").childElements();
			var depth=1;
		}
		var found=-1;
		for(var i=0;i<liv.length;i++){
			var LI=liv[i],A=liv[i].down("A");
			var p=location.pathname;
			var h=A.readAttribute("href");
			if(p.charAt(0         )!="/")p="/"+p;
			if(h.charAt(0         )!="/")h="/"+h;
			if(p.charAt(p.length-1)!="/")p=p+"/";
			if(h.charAt(h.length-1)!="/")h=h+"/";
			if(p.indexOf(h)!=-1){
				found=i;
				break;
			}
		}
		if(found>=0){
			liv[found].addClassName("nodo");
			liv[found].down("A").addClassName("sel");
			var fb=liv[found].down(".fix-B");
			if(fb){
				var urlpath=location.pathname.split("/").without("");
				if(urlpath.length>=2){
					var submenu=liv[found].down("UL");
					if(submenu)menu.select(submenu.childElements());
				}
				fb.show();
			}
		}
	},
	show:function(){
		var q=arguments[0]||-1;
		if(q==-1){
			playing=0;
			menu.hide();
			return;
		}else{
			menu.hide();
		}
		var IFRAME=$("overlapIEfix");
		IFRAME.hide();
		var DIV = $(q).up().select(".fix-B").first() ;
		if(DIV){
			menu.transition(DIV)
		}else{
			menu.hide();
		}
	}
}

var MenuID="menu-header";

function initMenu(){
	var m=$(MenuID);
	var mu=$(MenuID).select("UL");
	var muu=$(MenuID).select("UL UL");
	
	// prepare for Scriptaculous bugs
	var ULs=muu.each(function(ul){
		var divA=new Element("div",{className:"fix-A"});
		var divB=new Element("div",{className:"fix-B",style:"display:none;"});
		var divC=new Element("div",{className:"fix-C"});
		ul.up("li").insert(
			divA.insert(divB.insert(divC.insert(ul.remove())))
		);
	});
	
	// animate
	var UL=mu.first();
	menu.liv_1=UL.childElements();
	
	var whichSelected=-1;
	
	// SCARPA:
	liv2position();
	Event.observe(window,'resize',liv2position);
	// :SCARPA
	menu.select();
	m.select(".clickable-0").each(function(A){
		A.writeAttribute({
			href:'javascript:;'
		}).setStyle({
			cursor:'default'
		});
	});	
	m.select(".clickable-2").each(function(A){
		A.writeAttribute({
			href:'javascript:;'
		});
	});
	
	$(MenuID).show();
}

function liv2position(){
	var m=$(MenuID);
	var P=m.getOffsetParent().cumulativeOffset();
	$(MenuID).select(".fix-A").each(function(DIV){
		DIV.setStyle({left:0});
	})

}
