var delay = 300;
if (typeof(window["console"]) == "undefined") {
	console= {}; //
}

var slideOpen = false; //slide "auspicable" status
var actualTarget = "none"; //actually open subMenu
var actualPage = "none";  //actual page (for offering and profile)
var ie = (navigator.appName=="Microsoft Internet Explorer"); //is it the monster?

window.addEvent('domready', function() {
	var myHorizontalSlide = new Fx.Slide('menu2', {mode: 'horizontal'}); //create horizontal slide

	//slideIn events: mouse enters offering or profile
	$('offering').addEvent('mouseenter', function(e){
		if (console.log) console.log('slideIn');
		slideOpen = true;
		myHorizontalSlide.slideIn();
	});

	$('profile').addEvent('mouseenter', function(e){
		if (console.log) console.log('slideIn');
		slideOpen = true;
		myHorizontalSlide.slideIn();
	});

	//mouseOve menu behaviour: when to slideOut, when to show submenu
	$('menus').addEvent('mouseover', function(e){
		if (!e) var e = window.event;
		var target = (e.target) ?  e.target : e.srcElement; //get Element mouse is entering
		if (target.id != "menu2" && target.className != "navLink" && target.parentNode.className != "innerNav" && target.className != "innerNav" && target.parentNode.id != actualTarget && target.id != actualTarget ) { //if target is inside the slider, mouse is moving from the slider to the main element, or actualPage menu is already "out" do nothing.

				if (target.getAttribute('tabIndex') > 0) {
					if (target.getAttribute('tabIndex') <= 2) { //actually offering or profile
						hideMenu2(); //hide all menus in slider
						var menuToOpen = $($('mainMenu').getElementsByTagName('ul')[target.getAttribute('tabIndex') - 1]); //menu to open (tabindex-1)

						menuToOpen.setStyle('opacity', 0); //opacity reset: invisible
						menuToOpen.style.display = 'block';
						setTimeout(function(){
							if (ie) {
								menuToOpen.setStyle('opacity', 1);
							}else{
								menuToOpen.tween('opacity', 1); //animation
							}
						}, delay); //delay should be high for the first animation after slideIn, short thereafter
						if (console.log)
							console.log("block" + menuToOpen.parentNode.id + " " + actualTarget + " from " + target + target.id);
						actualTarget = menuToOpen.parentNode.id;
						delay = 10;
					}
					else { //if mouse is over other items, hide all menu and slideOut
						hideMenu2();
						slideOpen = false;
						myHorizontalSlide.slideOut();
						actualTarget = "none";
						delay = 300;
					}
				}
		}
		//prova.style.backgroundColor = '#000000';
	});

	$('menus').addEvent('mouseleave', function(e){ //Mouse leaves menu. Slidein o Slideout?
		if (actualPage == "none"){ //normally, the slide would close (hiding open menus)
			hideMenu2();
			slideOpen=false;
			myHorizontalSlide.slideOut();
			actualTarget = "none";
			delay=300;
			if (console.log) console.log('slideOut leave menu');
		}else if (actualPage != actualTarget){ //if the actual page is offering or profile, and Menu is not shown, slide opens and shows submenu
			hideMenu2 ()
			if (console.log) console.log('slideIn');
			slideOpen = true;
			myHorizontalSlide.slideIn();
			actualTarget = actualPage;
			//Open actual page with animation.
			menuToOpen = $(actualPage).getElements(".innerNav")[0];
			menuToOpen.setStyle('opacity', 0); //opacity reset: invisible
			menuToOpen.style.display = 'block';
			setTimeout(function(){
				if (ie) {
					menuToOpen.setStyle('opacity', 1);
				}else{
					menuToOpen.tween('opacity', 1); //animation
				}
			}, delay); //delay should be high for the first animation after slideIn, short thereafter

			if (console.log)
				console.log("slide reopened");
			delay = 10;
		}
	});



	function checkSlideStatus () { //checks if slide is in the right position and starts animation if needed (to avoid "queing effects" glitch)
		if (slideOpen & !myHorizontalSlide.open ){
			myHorizontalSlide.slideIn();
		}else if (!slideOpen & myHorizontalSlide.open){
			hideMenu2();
			myHorizontalSlide.slideOut();
			actualTarget = "none";
		}
	}
	setInterval(checkSlideStatus, 100); //always checks!

	function hideMenu2 () { //hides submenus
		var secMenus = $('mainMenu').getElementsByTagName('ul');
		for (var i = 0, len = secMenus.length; i < len; ++i) {
			secMenus[i].style.display = 'none';
			//if (console.log) console.log('via');
		}
	}

	//Open slide if needed, close it otherwise
	var secMenus = $('mainMenu').getElementsByTagName('ul');
	for (var i = 0; i < 2; ++i) {
		if (secMenus[i].parentNode.className == "selected") {
			slideOpen=true;
			secMenus[i].style.display = 'block';
			actualPage=secMenus[i].parentNode.id;
			actualTarget=actualPage;
			if (console.log) console.log(actualPage + "ciao");
			delay=10;
		}
	}
	if (actualPage=="none"){
			myHorizontalSlide.hide();
	}

});

