var Menus = new Array('ambiente', 'materiale', 'colori');

var Duration = 500;
var OverDur = Duration;
var EffectDur = Duration / 2;
var Effect = "swing";
var Effect2 = "swing";
var Try = 10;
var Timing = false;
var Animated = new Array(true, true); //In, Out
//Animated = new Array(false, false); //In, Out
var DiffTarg = true;
var InColour = "#FFFFFF";
var OutColour = "#000000";

var Animating = 0;
var Timer = new Array(-1, -1)
var CurrentVoice = 0;

function PopMenuIn(ID, Elem, event, Timered)
{
	if ((Elem == event.target) || DiffTarg)
	{
		if (parseInt($("." + Menus[ID] + " .menu_popup").css("margin-left")) > 0)
		{
			PopMenuOutAll(Elem, event);
		}

		else
		{
			if (!Timered)
			{
				PopMenuOutAll(Elem, event, ID);
			}

			if (Animating && Timing && !Timered)
			{
				return;
			}

			$("." + Menus[ID] + " ." + Menus[ID] + "_text").css("background-color", InColour);
			$("." + Menus[ID] + " ." + Menus[ID] + "_text").addClass("item_active");
			$("." + Menus[ID] + " ." + Menus[ID] + "_text").stop().animate({backgroundColor: InColour}, OverDur);

			if (Animated[0])
			{
				Animating++;

				var El = null;

				if (Timered)
				{
					El = $("." + Menus[ID] + " .menu_popup");
				}

				else
				{
					El = $("." + Menus[ID] + " .menu_popup").stop();
				}

				El.animate({"margin-left": "15%", backgroundColor: InColour}, Duration, Effect, function()
				{
					Animating--;

					$("." + Menus[ID] + " ." + Menus[ID] + "_text").addClass("item_active");
					$("." + Menus[ID] + " ." + Menus[ID] + "_text").stop().animate({backgroundColor: InColour}, OverDur);
				});
			}

			else
			{
				$("." + Menus[ID] + " ." + Menus[ID] + "_text").css("width", "100%");
			}
		}
	}
}

function PopMenuOut(ID, Elem, event, Timered)
{
	if ((Elem == event.target) || DiffTarg)
	{
		if (Animating && Timing && !Timered)
		{
			return;
		}

		if (Animated[1])
		{
			Animating++;

			var El = null;

			if (Timered)
			{
				El = $("." + Menus[ID] + " .menu_popup");
			}

			else
			{
				El = $("." + Menus[ID] + " .menu_popup").stop();
			}

			$("." + Menus[ID] + " ." + Menus[ID] + "_text").stop().animate({backgroundColor: OutColour}, OverDur);

			El.animate({"margin-left": "-100%", backgroundColor: OutColour}, Duration, Effect2, function()
			{
				Animating--;

				$("." + Menus[ID] + " ." + Menus[ID] + "_text").removeClass("item_active");
			});
		}

		else
		{
			$("." + Menus[ID] + " .menu_popup").css("margin-left", "-80%");

			$("." + Menus[ID] + " ." + Menus[ID] + "_text").removeClass("item_active");
		}
	}
}

function PopMenuOutAll(Elem, event, exclude)
{
	if (typeof(exclude) == 'undefined')
	{
		exclude = -1;
	}

	for (i = 0; i < Menus.length; i++)
	{
		if (($("." + Menus[i]).width() > 29) && (i != exclude))
		{
			PopMenuOut(i, Elem, event);
		}
	}
}

function SetCurrentMenuVoice(Voice)
{
	if (Voice == 5) //Ambienti
	{
		Voice = 0;
	}

	else if (Voice == 6) //Tipologie
	{
		Voice = 1;
	}

	else if (Voice == 4) //Colori
	{
		Voice = 2;
	}

	CurrentVoice = Voice;
}

$(document).ready(function()
{
	InColour = $(".body_menu .menu_popup").css("background-color");
	OutColour = $(".body_menu ." + Menus[0] + " ." + Menus[0] + "_text").css("background-color");

	if (EffectDur)
	{
		/*for (var i = 0; i < Menus.length; i++)
		{
			setTimeout("PopMenuIn(" + i + ", 0, 0, true);", EffectDur * ((i + 1) * 4));
			setTimeout("PopMenuOut(" + i + ", 0, 0, true);", (EffectDur) * ((i + 1) * 4));
		}*/
	}

	for (var i = 0; i < Menus.length; i++)
	{
		$("." + Menus[i] + " ." + Menus[i] + "_text").hover(function()
		{  
			$(this).stop().animate({ backgroundColor: InColour}, OverDur);  
		},function()
		{
			if (!$(this).hasClass("item_active"))
			{
				$(this).stop().animate({ backgroundColor: OutColour}, OverDur);
			}
		}); 
	}

	if ((CurrentVoice >= 0) && (CurrentVoice < Menus.length))
	{
		PopMenuIn(CurrentVoice, 0, 0);
	}
});

$(".ambiente_text").mouseover(function()
{
	PopMenuIn(0, this, event);
});

$(".ambiente_text").mouseout(function()
{
	PopMenuOut(0, this, event);
});
