var executeOnce = true;
var pointingAtMenu = false;
var pointingAtButton = false;
function autoCloseMenu(){
	// sets an updater to see if the menu pointer is a decendent of either the trigger button or menu
	new PeriodicalExecuter(function(pe) {
		if ((!pointingAtMenu) && (!pointingAtButton)){
			pe.stop();	// stop the updater
			if (!$('prog-menu').style.length) $('prog-menu').toggle();	// hide menu if its still displaying
			// stop all body event listeners
			Event.stopObserving(document.body, 'click');
			Event.stopObserving(document.body, 'mouseover');
		}
	}, 1);	// check every 1 second
}
Event.observe('programme-menu', 'click', function(e) {
	$('prog-menu').toggle();	// display or hide the menu
	executeOnce = true;
	Event.stop(e);
	Event.observe(document.body, 'click', function(event) {
	// check for clicks anywhere on the screen, if click is not on the button or menu execute
		if ((!pointingAtMenu) && (!pointingAtButton)){
			$('prog-menu').toggle();
			Event.stopObserving(document.body, 'click');
			Event.stopObserving(document.body, 'mouseover');
		}
	});
	Event.observe(document.body, 'mouseover', function(e) {
		Event.element(e).up().identify();	// assign ID to trigger container
		// check if the mouse is pointing at the menu or trigger container
		pointingAtMenu = Event.element(e).descendantOf('navbar');
		pointingAtButton = Event.element(e).descendantOf('anonymous_element_1');
		if (executeOnce){
//			autoCloseMenu(); // trigger PeriodicalExecuter once only 
			executeOnce = false;
		}
	});
});
var first;
$$('#presenter_list li a').each(function (el){
	if (!first){
		first = $('presenter_info_' + el.up().identify().replace(/presenter_link_/, '') );
		first.addClassName('shown');
		el.addClassName('selected');
	}
	el.observe('click', function(e){
		var id = $('presenter_info_' + this.up().identify().replace(/presenter_link_/, '') );
		$$('#presenters .shown', '#presenter_list li a.selected').each(function (el){
			if (el.hasClassName('shown')){
				el.removeClassName('shown');
			} else {
				el.removeClassName('selected');
			}
		});
		id.addClassName('shown');
		this.addClassName('selected');
		e.stop();
	});
});