function MenuUnselectAllMenus(event) {
	if( ! document.popupmenus ) return;
	if( ! event ) {	/*	IE requirement	*/
		event = window.event;
		event.target = event.srcElement;
	}
	if( event.target.menuItem || ( event.target.parentNode && event.target.parentNode.menuItem ) ) return;
	for( var i = 0 ; i < document.popupmenus.length ; i++ ) {
		document.popupmenus[i].setSelectedIndex(-1);
	}
}

if( ! document.__msh_onclick__ ) document.__msh_onclick__ = [];
document.__msh_onclick__.push( MenuUnselectAllMenus );
document.onclick = function(event) {
	for( var i = 0 ; i < document.__msh_onclick__.length ; i++ ) {
		window.funcRef = document.__msh_onclick__[i];
		window.funcRef(event);
	}
}

function MenuRegisterMenu( menu ) {
	if( ! document.popupmenus ) document.popupmenus = [ menu ];
	else document.popupmenus.push( menu );
	menu.node.onmouseover = MenuUnselectAllMenus;
}
function MenuPAFSetup( formId , menuData , installPath ) {
	var form = document.getElementById( formId );
	form.propertiesPanel = getChildrenWithId(form,"propertiesPanel")[0];
	form.url = getChildrenWithId(form,"MenuItemURL")[0];
	form.menuItemTarget = getChildrenWithId(form,"MenuItemTarget")[0];
	form.text = getChildrenWithId(form,"MenuItemText")[0];
	form.role = getChildrenWithId(form,"MenuItemRole")[0];
	form.icon = getChildrenWithId(form,"MenuItemIcon")[0];
	form.menuName = getChildrenWithId(form,"MenuName")[0];
	form.menuType = getChildrenWithId(form,"MenuType")[0];
	form.menuCssClassName = getChildrenWithId(form,"MenuCssClassName")[0];

	form.menuItemTarget.onchange = function() {
		form.currentMenuItem.setTarget( this.value );
	}
	form.text.onkeyup = function() {
		this.form.currentMenuItem.setText( this.value );
		if( this.form.currentMenuItem.getSubmenu() ) this.form.currentMenuItem.getSubmenu().name = this.value;
	}
	form.role.onchange = function() {
		if( form.currentMenuItem.getRole() == "submenu" && ! window.confirm( "Zmiana typu spowoduje usunięcie submenu ! Czy kontynuować ?" ) ) return;
		this.form.currentMenuItem.setRole( this.value );
	}
	form.url.onkeyup = function() {
		this.form.currentMenuItem.setUrl( this.value );
	}
	form.url.onchange = function() {
		this.onkeyup();
	}
	form.icon.onkeyup = function() {
		this.form.currentMenuItem.setIconPath( this.value );
	}
	form.menuName.onkeyup = function() {
		this.form.currentMenu.name = this.value;
	}
	form.menuType.onchange = function() {
		this.form.currentMenu.setType( this.value );
	}
	form.menuCssClassName.onchange = function() {
		this.form.currentMenu.setClassName( this.value );
	}

	form.rollURLSelector = function() {
		var windowInnerHeight = ( isIE ) ? document.documentElement.clientHeight : window.innerHeight;
		var windowInnerWidth = ( isIE ) ? document.documentElement.clientWidth : window.innerWidth;
		this.urlSelector.style.top = Math.max( 0 , windowInnerHeight/2 - this.urlSelector.offsetHeight/2 ) + "px";
		this.urlSelector.style.left = this.propertiesPanel.offsetLeft + this.propertiesPanel.offsetWidth + 5 + "px";
		this.urlSelector.style.visibility = (this.urlSelector.style.visibility!="visible")?"visible":"hidden";
		if( this.urlSelector.style.visibility == "visible" && this.urlSelector.moduleList.count() == 0 )
			form.urlSelector.loadModuleList();
		//DONE	2006-11-01 załadowanie modułów przy pokazaniu selektora URL jeśli nie ma żadnych
	}
	form.urlSelector = getChildrenWithId(form,"MenuPAFURLSelector")[0];
	form.urlSelector.rollModuleList = function() {
		if( this.moduleList.offsetHeight < this.moduleList.scrollHeight ) {
			this.moduleList.prevScrollTop = this.moduleList.scrollTop;
			this.moduleList.scrollTop = 0;
			this.moduleList.style.height = this.moduleList.scrollHeight + "px";
		} else {
			this.moduleList.style.height = "";	//CSS will take care for that
			this.moduleList.scrollTop = this.moduleList.prevScrollTop;
		}
		form.rollURLSelector();
		form.rollURLSelector();
	}
	form.urlSelector.loadModuleList = function() {
		this.moduleList.load();
	}
	form.urlSelector.moduleList = getChildrenWithId(form.urlSelector,"moduleList")[0];
	form.urlSelector.moduleList.urlSelector = form.urlSelector;
	form.urlSelector.getClassId = function() {
		return getChildrenWithId( this , "class_id")[0].value;
	}
	form.urlSelector.moduleList.load = function() {
		while( this.childNodes.length > 0 ) this.removeChild(this.firstChild);
		var ajax = new AJAX(installPath+"/ajax.php?action=getModuleList&class_id="+this.urlSelector.getClassId());
		ajax.target = this;
		ajax.onload = function() {
			var doc = this.http_request.responseXML;
			if( ! doc ) return alert("Problem z ładowaniem poprzez moduł AJAX !");
			var o = doc.getElementsByTagName("modulelist");
			if( o[0] && o[0].childNodes ) for( var i = 0 ; i < o[0].childNodes.length ; i++ ) {
				var module = o[0].childNodes[i];
				var node = document.createElement("div");
				node.innerHTML = module.name + " - " + module.section;
				node.setAttribute("moduleId", module.id);
				node.setAttribute("action", module.action);
				this.target.appendChild(node);
				node.form = form;
				node.onclick = function() {
					var form = getForm( this );
					if( this.getAttribute( "action" ) )
						form.url.value = "?action="+this.getAttribute("action");
					else
						form.url.value = "?action=ModuleDisplay&data[Module][id]="+this.getAttribute("moduleId");
					form.url.onchange();
					form.rollURLSelector();
				}
			}
		}

	}
	form.urlSelector.moduleList.count = function() {
		return this.childNodes.length;
	}
	form.topmenu = new MenuPort( ( menuData ) ? menuData.type : "vertical" );
	getChildrenWithId(form,"workspace")[0].appendChild( form.topmenu.node );
	if( menuData ) form.topmenu.setClassName( menuData.className );
	else form.topmenu.setClassName( "menu" );


	/**
	 * Set up custom actions for editing purpose
	 */
	form.topmenu.onnewitem = function() {
		if( ! this.selectedItem.getText() ) this.selectedItem.setText("menuItem");
		if( ! this.selectedItem.getUrl() ) this.selectedItem.setUrl("#");
		form.currentMenuItem = this.selectedItem;
		form.currentMenu = this;
		form.text.value = form.currentMenuItem.getText();
		form.role.value = form.currentMenuItem.getRole();
		if( form.currentMenuItem.getUrl() ) form.url.value = form.currentMenuItem.getUrl();
		if( form.currentMenuItem.getIconPath() ) form.icon.value = form.currentMenuItem.getIconPath();
		else form.icon.value = "";
		form.menuName.value = this.name;
		form.menuType.value = this.getType();
		this.select(this.selectedItem);
		
		this.selectedItem.onchange = function() {
			this.node.onclick = function(event) {
				if( this.menuItem.onclick ) this.menuItem.onclick(event);
			};
		};
		this.selectedItem.node.onclick = function(event) {
			if( this.menuItem.onclick ) this.menuItem.onclick(event);
		};
		this.selectedItem.onclick = function() {
			form.currentMenuItem = this;
			form.currentMenu = this.parent;
			form.text.value = this.getText();
			form.role.value = this.getRole();
			if( form.currentMenuItem.getUrl() ) form.url.value = form.currentMenuItem.getUrl();
			if( form.currentMenuItem.getIconPath() ) form.icon.value = form.currentMenuItem.getIconPath();
			else form.icon.value = "";
			form.menuName.value = this.parent.name;
			form.menuType.value = this.getType();
			form.menuCssClassName.value = this.parent.getClassName();
			form.menuItemTarget.value = this.getTarget();
		}
	};
	form.topmenu.onremoveitem = function() {
		this.selectedItem.onclick();
	}
	if( menuData == null ) form.topmenu.newItem( { text: "menuItem" } );
	else form.topmenu.load( menuData );
	form.topmenu.show();

	/**
	 *	This function must be created after loading, cause it overrides name from data to load set.
	 */
	MenuItemPort.prototype.oncreatesubmenu = function() {
		if( this.getSubmenu() ) this.getSubmenu().name = this.getText();
	}

	form.onsubmit = function() {
		if( ! window.confirm( "Czy napewno zapisać zmiany ?" ) ) return false;
		var nodes = MenuPAFGetNodes( this.topmenu );
		for( var i = 0 ; i < nodes.length ; i++ ) {
			form.appendChild( document.createTextNode(nodes[i].name + " : " + nodes[i].value ) );
			nodes[i].type = "hidden";
			form.appendChild( nodes[i] );
			form.appendChild( document.createElement("br") );
		}
		//return false;
	}
}
function MenuPAFGetNodes( menu ) {
	/**
	 *	This function creates HTMLInputElements that will be inserted into form, based on menu structure
	 *	Data will be used to save menu in MySQL
	 */
	var  nodes = [];
	//menu name
	var node = document.createElement( 'input' );
	node.name = "data[Menu]["+menu.id+"][name]";
	node.value = menu.name;
	nodes.push( node );
	//menu type
	node = document.createElement( 'input' );
	node.name = "data[Menu]["+menu.id+"][type]";
	node.value = menu.getType();
	nodes.push( node );
	//menu category
	node = document.createElement( 'input' );
	node.name = "data[Menu]["+menu.id+"][category]";
	node.value = ( menu.getParent() ) ? 'submenu' : 'topmenu';
	nodes.push( node );
	//menu cssClassName
	node = document.createElement( 'input' );
	node.name = "data[Menu]["+menu.id+"][cssClassName]";
	node.value = menu.getClassName();
	nodes.push( node );
	for( var i = 0 ; i < menu.items.length ; i++ ) {
		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][text]";
		node.value = menu.items[i].getText();
		nodes.push( node);

		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][role]";
		node.value = menu.items[i].getRole();
		nodes.push( node);

		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][url]";
		node.value = menu.items[i].getUrl();
		nodes.push( node);

		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][target]";
		node.value = menu.items[i].getTarget();
		nodes.push(node);


		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][icon]";
		node.value = menu.items[i].getIconPath();
		nodes.push( node );

		node = document.createElement( 'input' );
		node.name = "data[Menu]["+menu.id+"][MenuItem]["+i+"][submenu_id]";
		if( menu.items[i].getSubmenu() ) node.value = menu.items[i].getSubmenu().id;
		nodes.push( node );
		if( menu.items[i].getSubmenu() ) nodes = nodes.concat( MenuPAFGetNodes( menu.items[i].getSubmenu() ) );
	}
	return nodes;
}

/*{{{TODO-DONE
	//TODO 2006-04-11 add JS function for hiding module types in MenuPrintAddForm()	//DONE 2006-04-11
}}}*/
