function MenuController(openedId,closedId,menuId){
	this.myOpened = document.getElementById(openedId);
	this.myClosed = document.getElementById(closedId);
	this.myMenu = document.getElementById(menuId);
	this.open = openMenu;
	this.afterOpen = afterOpen;
	this.close = closeMenu;
	this.afterClose = afterClose;
	
	createDropListener();
	dropListener.register(this);
}
function openMenu(evt){
	this.myOpened.style.display = 'block';
	this.myClosed.style.display = 'none';
	this.myMenu.style.display = 'block';

	this.afterOpen();
	dropListener.closeOthers(this);
	d4cancelEvent(evt);
}
function closeMenu(){
	this.myOpened.style.display = 'none';
	this.myClosed.style.display = 'block';
	this.myMenu.style.display = 'none';
	this.afterClose();	 
}
function afterOpen(){
	var onOpen = this.myMenu.getAttribute('onMenuOpen');
	if( onOpen ){
		eval(onOpen);
	}
}
function afterClose(){
	var onClose = this.myMenu.getAttribute('onMenuClose');
	if( onClose ){
		eval(onClose);
	}
}

var dropListener;
function createDropListener(){
	if( !dropListener ){
		dropListener = new MenuListener();
	}
}
function MenuListener(){
	this.listeners = new Array();
	this.register = listenerRegister;
	this.closeOthers = listenerCloseOthers;
}
function listenerRegister(listener){
	this.listeners[this.listeners.length] = listener;
} 
function listenerCloseOthers(menu){
	for( i=0; i<this.listeners.length; i++) {
		if( this.listeners[i]!=menu ){
			this.listeners[i].close();
		}
	}
}
d4addEventListener(document, 'click', function closeCustMenus(){
	try {
		dropListener.closeOthers(null);
	} catch (e) {
		//ei haittaa jos ei ole määritelty
	}
});
