TSNMenu = function()
{
	var ths=this;
	ths.container="";
	ths.menus=[];
	ths.max=0;
	ths.cb=null;
	ths.type="hover";
	ths.release=""; // variabile evento di release 
	
	ths.init= function(cnt,mnu,tp,tp2)
	{
    ths.release=tp2;
    
   	ths.container="#"+cnt;
		ths.menus=mnu;
		ths.max=mnu.length;
		if (tp=="click")
		{
			ths.type=tp;
		}
		ths.setup_events();
	};
	
	ths.setcb = function(cb)
	{
		ths.cb=cb;
	}
	
	ths.setup_events= function()
	{
		if (ths.type=="hover")
		{
		  // in questo caso chiude tutti tranne lui
			$(ths.container).mouseleave({ths:ths}, ths.menu_close_all);
    }
    
		if (ths.release=="hover")
		{
		  // in questo caso chiude tutti tranne lui
			$(ths.container).mouseleave({ths:ths}, function(){
			  $(ths.container).hide();
        ths.menu_close_all();
      });
    }
    
    for (var k=0;k<ths.max;k++)
		{
    	if (ths.type=="hover")
			{
				$("#"+ths.menus[k]).mouseenter({ths:ths},ths.menu_open);
			}
			else
			{
    		$("#"+ths.menus[k]).click({ths:ths},ths.menu_open);
			}
		}
	};
	
	ths.menu_open= function(evt)
	{
		n=ths.find_id(evt.target.id);
		
    display_container = $(ths.container).is(':visible');
		
		ths.menu_close_all(n);
		if (ths.type=="hover")
		{
 			$("#"+evt.target.id+"_cont").slideDown({duration:100,easing:"linear"});
		}
		else
		{
      if(!display_container)
      {
        $("#"+evt.target.id+"_cont").slideDown({duration:100,easing:"linear"});  
      }
      else
      {
        if(ths.release!="hover")
        {
          $(ths.container).slideUp({duration:100,easing:"linear"});
        }
       }
		}
		if (ths.cb!=null)
		{
			ths.cb();
		}
	};
	
	ths.menu_close_all= function(evt)
	{
		if (isNaN(evt))
		{ 
			evt=-1;
		} 
		for (var k=0;k<ths.max;k++)
		{
			if (evt!=k)
			{
			  if((ths.release!="hover")||("#"+ths.menus[k]+"_cont"!=ths.container))
			  	$("#"+ths.menus[k]+"_cont").hide();
			}
		}
	};
	
	ths.find_id= function(mn)
	{
		for (var k=0;k<ths.max;k++)
		{
			if ($("#"+ths.menus[k])[0].id==mn)
			{
				break;
			}
		}
		return(k);
	};
};
