/*
 * Polymorph CMS
 * Copyright (c) 2008 Mario Smeritschnig, Ingo Smeritschnig, Polymorph OG
 *
 * * * BEGIN LICENCE * * *
 *
 * This file is part of Polymorph CMS. Polymorph CMS is a proprietary Software.
 * 
 * The software publisher (Polymorph OG) grants a license to use one copy of the
 * software to each customer who purchased a Website based on Polymorph CMS, but 
 * the ownership of this copy remains with the software publisher.
 * It is strongly prohibited to copy, modify, publish, redistribute or reverse
 * engineer any parts of the source code. It is also prohibited to use any 
 * portions (functions, classes, methods, includes) of the source code for any
 * purpose.
 * 
 * Polymorph CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *
 * * * END LICENCE * * *
 *
 */

	var close_timeout;
	
	document.observe("dom:loaded", function() {
		$$(".nav_button").each(function(b) {											
			b.observe("mouseover", function(event) {
				b.mouse_is_over = true;
				window.clearTimeout(close_timeout);
				submenu_clicked(b.id);		  
			});
			b.observe("mouseout", function() {
				b.mouse_is_over = false;
				window.clearTimeout(close_timeout);
				close_timeout = window.setTimeout(close_all_submenues, 1000);
			});
		});
		
		$$(".submenu").each(function(b) {											
			b.observe("mouseover", function(event) {
				Event.stop(event);	 
				b.mouse_is_over = true;
				window.clearTimeout(close_timeout);
			});
		});
	});
	

	document.observe("click", close_all_submenues);
	var current_submenu_id;
	function submenu_clicked(id) {
		var sm = $(id+"_submenu");
			
		// slide up all but the clicked one
		$$(".submenu,.submenu_klein").each(function(m){
			if(m!=sm && m.menu_open  && m.menu_open === true) {
				m.menu_open = false;
				Effect.SlideUp(m, {duration:0.1, queue: 'end'});
			}
		});
		
		if(current_submenu_id != id){
			current_submenu_id = id;
			sm.menu_open = true;
			// slide down the clicked one if its closed
			Effect.SlideDown(sm, {duration:0.2, queue: 'end'});			
		}
	}
	
	
	function close_all_submenues() {
		$$(".submenu,.submenu_klein").each(function(m){
			var b = m.up();
			var mo = b.mouse_is_over && b.mouse_is_over === true;
			if(m.menu_open  && m.menu_open === true && !mo) {
				m.menu_open = false;
				Effect.SlideUp(m, {duration:0.1, queue: 'end'});
			}
		});
		current_submenu_id = "";
	}