// set the speed of the animation effects
var duration = 0.2;
var menu_duration = 0.3;

// apply the accordion effect to menu entries
function menu_toggle(element_to_handle,img_down, img_up, url,level,follow_url) {
	var open_menu_item = 0;

	if (follow_url == 1) {
		var old_url = get_pathname(window.location);
	} else {
		var old_url = url;
	}
	// if we clicked on the parent of a submenu, only close the submenu
	// then load the new page
	if ( old_url.length > url.length ) {
		if ( old_url.startsWith(url) ) {
			open_menu_item = $(get_open_menu_item(level+1));
			img_down = $(get_open_menu_item(level+1)+'-down');
			img_up = $(get_open_menu_item(level+1)+'-up');
			new Effect.BlindUp(open_menu_item, {
				duration: duration,
				afterFinish: function() {
					img_down.toggle();
					img_up.toggle();
					window.location = url;
				}
			});
			return false;
		}
	}

	open_menu_item = $(get_open_menu_item(level));

	// handle accordion effect
	//
	// if the clicked element is visible, just close it
	if ( element_to_handle.visible() ) {
		if ( ((element_to_handle == open_menu_item) && (url == old_url)) || (element_to_handle != open_menu_item) ) {
			new Effect.BlindUp(element_to_handle, { 
				duration: duration,
				afterFinish: function() {
					img_down.toggle();
					img_up.toggle();
					if ( url != old_url )
						window.location = url;
				}
			});
		} else {
			if ( url != old_url )
				window.location = url;			
		}
	} else {
		// if the clicked element is invisible and
		// another element is visible, first close
		// the other one, then open the clicked element
		if ( open_menu_item != 0) {
			new Effect.BlindUp(open_menu_item, {
				duration: duration
			});
			new Effect.BlindDown(element_to_handle, {
				duration: duration,
				afterFinish: function() {
					img_down.toggle();
					img_up.toggle();
					if ( url != old_url )
						window.location = url;
				}
			});
			$(get_open_menu_item(level)+'-down').toggle();
			$(get_open_menu_item(level)+'-up').toggle();
		} else {
			// if no other element is visible, just open
			// the clicked element
			new Effect.BlindDown(element_to_handle, {
				duration: duration,
				afterFinish: function() {
					img_down.toggle();
					img_up.toggle();
					if ( url != old_url )
						window.location = url;
				}
			});
		}
	}
	return false;
};

// close all menu entries of given level
function close_all(url,level) {
	var open_menu_item = 0;
	open_menu_item = $(get_open_menu_item(level));
	img_down = $(get_open_menu_item(level)+'-down');
	img_up = $(get_open_menu_item(level)+'-up');
	if ( open_menu_item != 0) {
		new Effect.BlindUp(open_menu_item, {
			duration: duration,
			afterFinish: function() {
				img_down.toggle();
				img_up.toggle();
				window.location = url;
			}
		});
	} else {
		window.location = url;
	}
}


// fade in menu, if necessary
function show_menu() {
	var referrer = get_pathname(document.referrer).sub(/\/([^\/]*).*/,'#{1}');
	var url = get_pathname(document.URL).sub(/\/([^\/]*).*/,'#{1}');
	var length = 0;
	if ( referrer.length < url.length) {
		length = referrer.length;
	} else {
		length = url.length;
	}
	if ( referrer.toLowerCase().substr(0,length) != url.toLowerCase().substr(0,length)) {
		new Effect.Appear($('menu'), {duration: menu_duration});
	} else {
		if ( length == 0 ) {
			new Effect.Appear($('menu'), {duration: menu_duration});
		} else {
			$('menu').show();
		}
	}
}

// get the pathname part of an given url
function get_pathname(url) {
	return new String(url).sub(/[^:]+:\/\/[^:\/]+(:[0-9]+)?(\/.*)/,'#{2}');
}

// get the open menu element of a given menu level
function get_open_menu_item(level) {
	var open_menu_item = 0
	MenuElements[level].each(function(item) {
	  if ( $(item).visible() )
		open_menu_item = item;
	});
	return open_menu_item;
}

