// menu.js
// -------------------------------------------------------------------------------------------------
// Javascript for 3-level menu for John McAslan
// -------------------------------------------------------------------------------------------------

var menu = new Object; // Contains all menu related functions

menu.isEnventAdded = false;

menu.init = function()
{
	menu.tree = new Array();
	var menu_a = $ES('a', 'menu'); // Get all the anchor tags within 'menu'
	var i = 0;
	menu_a.each(
		function(a)
		{
			// var uid = 'm_' + a.getText().replace(/\W/g, '_').toLowerCase();			
			a.set({
					id: 'm' + i
				 });
			if(a.href.indexOf('#') != -1)
			{
				a.set({onclick:'return false'});
			}
			a.addEvent('click', function(){menu.menuclick(this);});
			menu.tree[i] = a.getText();
			i++;
		}
	);
	
	if ($('breadcrumbs'))
	{
		
		menu.bread_a = $ES('a', 'breadcrumbs'); // Get all the anchor tags within 'breadcrumbs'
		menu.bread_a.each( function(a) { a.addEvent('click', menu.breadclick ); a.says = a.getText(); } );
	
		// now fade the breadcrumbs in
		$('breadcrumbs').setStyle('opacity', 0);
		var fadeinbread = new Fx.Style('breadcrumbs', 'opacity', {duration: 350}); // Does this work in IE?
		fadeinbread.start(0, 1);
		//and fade the cover out
		if ($('cover'))
		{
			var fadeoutcover = new Fx.Style('cover', 'opacity', {duration: 350});
			fadeoutcover.start(1, 0);
		}

	}
	
	if ($('p_back')) $('p_back').addEvent('click', function() { archive.go(-1); } );
	if ($('p_fwd'))  $('p_fwd').addEvent('click',  function() { archive.go(1);  } );
	
	if ($('homepage_gallery')) $('homepage_gallery').addEvent('click', menu.closemenu );
}

menu.closemenu = function()
{
	var menu_a = $ES('a', 'menu'); // Get all the anchor tags within 'menu'
	var deselect = function(a)
	{
		if(a.hasClass('selected'))
		{
			a.removeClass('selected');
			var siblings = $ES('ul', a.parentNode.parentNode);
			siblings.each
			(
				function(ul)
				{
					ul.removeClass('activesub');
				}
			);
		}
	}
	menu_a.each(deselect);
}

menu.menuclick = function(el)
{
	var mi = $(el.id);
	mi.blur();
	// Mark clicked item as selected and unmark siblings
	var isLinked = mi.href.indexOf('#') == -1;
	if(!isLinked && !mi.hasClass('selected'))
	{
		menu.clearContent();
	}
	var siblings = $ES('a', mi.parentNode.parentNode);
	siblings.each(
		function(a)
		{
			a.removeClass('selected');
		}
	)
	mi.addClass('selected');
	
	// Now show the next submenu, and hide others on the same level
	var siblings = $ES('ul', mi.parentNode.parentNode);
	siblings.each( function(ul)
		{
			ul.removeClass('activesub');
		}
	)
	var submenu = $E('ul', mi.parentNode);
	if (submenu) 
	{
		submenu.addClass('activesub');
	}
	//add event
	if($chk($('cover')))
	{
		$('cover').setStyle('visibility', 'visible');
		if(!menu.isEnventAdded)
		{
			$('cover').addEvent('click', menu.hide);
		}
	}
	if(isLinked)
	{
		return true;
	}
	else
	{
		return false;
	}
}

menu.breadclick = function()
{
	$('cover').setStyle('visibility', 'visible');
	$('cover').addEvent('click', menu.hide);
	menu.isEnventAdded = true;
	
	// Show the menu, with the current location highlighted, and hide the breadcrumb text
	this.blur();
	if (menu.bread_a[0].innerHTML == 'x')//<img src="/images/menu/x.gif" alt="close" />
	{
		menu.bread_a[0].removeClass('xmenu');
		menu.hide();
		return;
	}
	
	var i;
	var offset = 0;
	menu.bread_a.each(
		function(a)
		{
			var tree = menu.tree.copy(offset);		
			i = tree.indexOf(a.getText());
			var menu_pos = $('m' + (i + offset));
			if (menu_pos)
			{
				menu_pos.addClass('selected');
				menu_pos.getParent().getParent().addClass('activesub');
			}
			a.setText('');
			offset += i + 1;
		}
	);
	if ($chk($('sub_bread'))) $('sub_bread').setOpacity(0); 
	
	//menu.bread_a[0].innerHTML = 'x';
	//menu.bread_a[0].addClass('xmenu');

	$('menu').setStyle('display', 'block');
	if (!window.ie)
	{
		var fadein = new Fx.Style('menu', 'opacity', {duration: 350}); 
		fadein.start(0, 1);
	}
}

menu.clearContent = function()
{
	if ($('cover')){
		if ($('cover').style.opacity == 0){
			var fadeincover = new Fx.Style('cover', 'opacity', {duration: 350});
			fadeincover.start(0, 1);
		}
	}	
}

menu.showContent = function()
{
	if ($('cover')){
		if ($('cover').style.opacity > 0){
			var fadeoutcover = new Fx.Style('cover', 'opacity', {duration: 350});
			fadeoutcover.start(1, 0);				
		}
	}
}

menu.hide = function()
{
	//remove events
	if($('cover')) $('cover').removeEvent('click', menu.hide);
	menu.isEnventAdded = false;
	// Hide the menu and reinstate the breadcrumbs	
	if (!window.ie)
	{
		var fadeout = new Fx.Style('menu', 'opacity', {duration: 350}); // Does this work in IE?
		fadeout.start(1, 0).chain( function() {
			$('menu').setStyle('display', 'none');
		});
	}
	else
	{
		$('menu').setStyle('display', 'none');
	}
	menu.bread_a.each(
		function(a)
		{
			a.setHTML('<span>'+a.says+'<span>');
			var b = a.getChildren();
			b.each( function(me) { me.setStyle('opacity', '0'), new Fx.Style(me, 'opacity', {duration: 350}).start(0, 1) } );
		}
	);
	if ($('sub_bread')) new Fx.Style('sub_bread', 'opacity', {duration: 350}).start(0, 1); 
	
	menu.showContent();
}

// When the DOM is loaded attach events to the menu
window.addEvent('domready', menu.init);

