Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
var menuIntradel = new function() 
{
	this.init = function () 
	{
		
		$$('.menu-vertical .toggle ').invoke("next",'ul').invoke('hide');
		$$('.menu-vertical a').each(function(e)
		{
			$(e).observe('click',function(event){menuIntradel.onClick(e);});
		});
		var currentMenu = Cookie.get('menu');
		menuIntradel.showMenu (currentMenu);
		
	};
	this.showMenu = function(menuId)
	{
		
		$w($(menuId).className).each(
			function(strId)
			{
				if($(strId))
				{
					menuIntradel.setClassAndOpen($(strId));
				}
			});
		if($(menuId).down('a').hasClassName('toggle'))
		{
			menuIntradel.setClassAndOpen($(menuId));
		}
		var oppened = $w($(menuId).className);
		oppened.push(menuId.id); 
		menuIntradel.removeClassAndClose(oppened);
	};
	this.setClassAndOpen = function(elem)
	{
		elem.down('a').addClassName('current');
		if(elem.down('ul') && !elem.down('ul').visible() )
		{
			new Effect.BlindDown(elem.down('ul'),{duration: .35,queue: 'end'});
		}
	}
	this.removeClassAndClose = function (oppened)
	{
		var closable;
		closable = $$('.menu-vertical .toggle ').invoke('up').invoke('identify');
		

		closable.each(function(elem)
		{
			if(!oppened.inArray(elem) && $(elem).down('ul') && $(elem).down('ul').visible())
			{
				$(elem).down('a').removeClassName('current');
				new Effect.BlindUp($(elem).down('ul'),{duration: .50});
			}
			
		});
	}
	this.onClick = function(anchor)
	{
		Cookie.set('menu',anchor.up().id);
		menuIntradel.showMenu(anchor.up());
		if(anchor.getAttribute('href') == '#' || anchor.getAttribute('href') == '')
		{
			return false;
		}
		return true;
	};
}

Event.observe(window, 'load', function(){
menuIntradel.init();
});


