<? 
class Menu	// Copyright by Mateusz Rusin 2004 // 1.07
{
	var $menu;
	var $default_type = 'list';
	var $default_arg_name = null;
	var $default_class = 'menu';
	var $default_icon_class = 'icon';
	var $current_sub_id = 'current_sub';
	var $prev = array ('class' => 'menuprev', 'legend' => '&lt;', 'access_key' => '&lt;');
	var $next = array ('class' => 'menunext', 'legend' => '&gt;', 'access_key' => '&gt;');
	var $current_url;
	var $current_args = array ();
	var $short;
	var $bullet_src = 'bullet.gif';
	var $templates = array 
		(
		0 /* default */	=> array ('current_id' => 'id="current"'),
		'list'					=> array ('menu' => array ("\r\n{indent}<ul>", "\r\n{indent}</ul>"), 'entry' => array ("\r\n\t{indent}<li>", '</li>')),
		'pseudo_list'		=> array ('entry' => array ("\r\n\t{indent}<img src=\"{bullet_src}\" alt=\"\" />", '')),
		'fieldset'			=> array ('submenu' => array ("\r\n{indent}<fieldset>", "\r\n{indent}</fieldset>"), 'entry' => array ("\r\n\t{indent}<legend>", "</legend>")),
		'select'				=> array ('seperate' => true, 'no_link' => true, 'menu' => array ("\r\n{indent}<form><nobr>{prev}<select class=\"{class}\" name=\"{arg_name}\" onChange=\"javascript: submit ()\">", "\r\n{indent}</select>{next}</nobr></form>"), 'entry' => array ("\r\n\t{indent}<option value=\"{value}\"{current}>", "</option>", 'current' => ' selected="selected"'))
		);
	function menu ($menu = null, $default_type = null, $short = false) // Copyright by Mateusz Rusin 2004 // 1.03
	{
		if (!empty ($menu))
			$this -> set_menu ($menu);
		if (!empty ($default_type))
			$this -> default_type = $default_type;
		$this -> set_current_args ($_GET);
		if (!empty ($_SERVER ['REDIRECT_URL']))
			$this -> set_current_url (basename ($_SERVER ['REDIRECT_URL']));
		else
			$this -> set_current_url (basename ($_SERVER ['SCRIPT_NAME']));
		$this -> short = $short;
	}
	function set_menu ($menu) // Copyright by Mateusz Rusin 2004 // 1.0
	{
		$this -> menu = $menu;
	}
	function set_current_url ($current_url) // Copyright by Mateusz Rusin 2004 // 1.0
	{
		$this -> current_url = $current_url;
	}
	function get_current_url () // Copyright by Mateusz Rusin 2004 // 1.0
	{
		return $this -> current_url;
	}
	function set_current_args ($current_args) // Copyright by Mateusz Rusin 2004 // 1.0
	{
		$this -> current_args = $current_args;
	}
	function get_current_args () // Copyright by Mateusz Rusin 2004 // 1.0
	{
		return $this -> current_args;
	}
	function find_current ($menu, &$current_sub) // Copyright by Mateusz Rusin 2004 // 1.02
	{
		$new_sub_menu = array ();
		$current_sub = false;
		//foreach ($menu ['sub'] as $entry)
		for ($i = 0; $i < count ($menu ['sub']); $i++)
		{
			$entry = $menu ['sub'] [$i];
			if (!isset ($entry ['current'])) 
				$entry ['current'] = 
					!empty ($entry ['url']) && empty ($entry ['no_check'] ['url']) || isset ($entry ['args'])
					?															// jeśli łącze
						!empty ($entry ['url']) && empty ($entry ['no_check'] ['url'])
						?															// jeśli adres w menu
							$this -> get_current_url () == $entry ['url']
							?															// jeśli adres jednakowy
								isset ($entry ['args'])
								?															// jeśli zmienne (i adres) w menu
									($this -> get_current_args () == $entry ['args'] ||
									!empty ($entry ['args']) && array_intersect_assoc ($entry ['args'], $this -> get_current_args ()) == $entry ['args'])
									?															// jeśli zmienne (i adres) jednakowe
										true 
									:															// jeśli zmienne niejednakowe (tylko adres) w menu
										false
								:															// jeśli nie ma zmiennych (tylko adres) w menu
									true 
							:															// jeśli adres niejednakowy
								false
						:															// jeśli nie adres (a więc zmienne) w menu
						(
							($this -> get_current_args () == $entry ['args'] ||
							!empty ($entry ['args']) && array_intersect_assoc ($entry ['args'], $this -> get_current_args ()) == $entry ['args'])
							?															// jeśli zmienne jednakowe
								true 
							:															// jeśli zmienne niejednakowe
								false
						)
					:															// jeśli nie łącze
						false;
				/*$xxx = 
					isset ($entry ['url']) && empty ($entry ['no_check'] ['url']) || isset ($entry ['args'])
					?															// jeśli łącze
						isset ($entry ['url']) && empty ($entry ['no_check'] ['url'])
						?															// jeśli adres w menu
							$this -> get_current_url () == $entry ['url']
							?															// jeśli adres jednakowy
								isset ($entry ['args'])
								?															// jeśli zmienne (i adres) w menu
									($this -> get_current_args () == $entry ['args'] ||
									!empty ($entry ['args']) && array_intersect_assoc ($entry ['args'], $this -> get_current_args ()) == $entry ['args'])
									?															// jeśli zmienne (i adres) jednakowe
										'true 1' 
									:															// jeśli zmienne niejednakowe (tylko adres) w menu
										'false 2'
								:															// jeśli nie ma zmiennych (tylko adres) w menu
									'true 3' 
							:															// jeśli adres niejednakowy
								'false 4'
						:															// jeśli nie adres (a więc zmienne) w menu
						(
							($this -> get_current_args () == $entry ['args'] ||
							!empty ($entry ['args']) && array_intersect_assoc ($entry ['args'], $this -> get_current_args ()) == $entry ['args'])
							?															// jeśli zmienne jednakowe
								'true 5' 
							:															// jeśli zmienne niejednakowe
								'false 6'
						)
					:															// jeśli nie łącze
						'false 7';*/
			if (!empty ($entry ['sub']))
			{
				$entry = $this -> find_current ($entry, $current_sub_sub);
				$entry ['current_sub'] = $current_sub_sub;
			}
//echo "<br>\n ".$entry ['args'] ['page'].'=('.$xxx.') ';var_dump ($entry ['current']);
			if ($entry ['current'] || !empty ($entry ['current_sub']))
				$current_sub = true;
			if ($entry ['current'])
				$current_index = $i;
			$new_sub_menu [] = $entry;
		}
		$menu ['current_sub_index'] = isset ($current_index) ? $current_index : null;
		$menu ['sub'] = $new_sub_menu;
		return $menu;
	}
	function render ($menu = null, $type = null, $default_arg_name = null, $short_menu = null) // Copyright by Mateusz Rusin 2004 // 1.07
	{
		if (!isset ($menu))
			$menu = $this -> menu;
		if (!isset ($type))
			$type = !empty ($menu ['type']) ? $menu ['type'] : $this -> default_type;
		if (!isset ($default_arg_name) && isset ($menu ['sub'] [0] ['args']))
			$default_arg_name = key ($menu ['sub'] [0] ['args']);
		if (!isset ($default_arg_name))
			$default_arg_name = $this -> default_arg_name;
		$submenu = '';
		$html_menu = '';
		$indent = '';
		static $level = 0;
		for ($i = 0; $i < $level; $i++)
			$indent .= "\t";
		if (!empty ($this -> templates [$type] ['menu'] [0]))
			$html_menu .= str_replace (array ('{indent}', '{arg_name}', '{class}'), array ($indent, $default_arg_name, isset ($menu ['class']) ? $menu ['class'] : $this -> default_class), $this -> templates [$type] ['menu'] [0]);
		if (!isset ($menu ['sub']))
			$menu ['sub'] = array ();
		foreach ($menu ['sub'] as $entry)
		{
			$short = isset ($short_menu) ? $short_menu : $this -> short;
			if ($short != 2 && !empty ($entry ['current_sub']))
				$short = false;
			if ($short != 2 && !empty ($entry ['current']))
				$short = false;
			if ($level == 0 || !$short || $short == 2)
			{
				/*if (!isset ($default_arg_name) && isset ($entry ['sub'] [0] ['args']))
					$default_arg_name = key ($entry ['sub'] [0] ['args']);*/
				if (!empty ($this -> templates [$type] ['submenu'] [0]))
					$html_menu .= str_replace ('{indent}', $indent, $this -> templates [$type] ['submenu'] [0]);
				if (!empty ($entry ['legend']))
				{
					if (!empty ($this -> templates [$type] ['entry'] [0]))
						$html_menu .= 
							str_replace 
							(
								array ('{indent}', '{current}', '{value}', '{bullet_src}'), 
								array 
									(
										$indent,  
										(
											!empty ($this -> templates [$type] ['entry'] ['current']) && !empty ($entry ['current']) 
												? $this -> templates [$type] ['entry'] ['current'] 
												: ''
										), 
										(
											!empty ($default_arg_name) && !empty ($entry ['args'] [$default_arg_name])
												? $entry ['args'] [$default_arg_name] 
												: ''
										),
										$this -> bullet_src
									),
								$this -> templates [$type] ['entry'] [0]
							);
					$query = '';
					if (isset ($entry ['args']))
					{
						foreach ($entry ['args'] as $arg_name => $arg_value)
							$query .= $arg_name . '=' . $arg_value . '&';
	//					$query = str_replace ('&', '&amp;', rtrim ($query, '&')); // == next 3 lines:
						if ($query [strlen ($query) - 1] == '&')
							$query = substr ($query, 0, -1);
						$query = str_replace ('&', '&amp;', $query);
					}
					if (empty ($this -> templates [$type] ['no_link'])) 
						$html_menu .= '<a class="' . 
							(isset ($entry ['class']) 
								? $entry ['class'] . '"' 
								: $this -> default_class . '"') . 
							(!empty ($entry ['current']) 
								? ' ' . 
								(!empty ($this -> templates [$type] ['current_id']) 
									? $this -> templates [$type] ['current_id'] 
									: (!empty ($this -> templates [0 /*default*/] ['current_id']) ? $this -> templates [0 /*default*/] ['current_id'] : '')) 
								: (!empty ($entry ['current_sub']) 
									? ' id="' . $this -> current_sub_id . '"' 
									: '')) . 
							(!empty ($entry ['id']) 
								? ' id="' . $entry ['id'] . '"' 
								: '') . 
							(!empty ($entry ['title']) 
								? ' title="' . $entry ['title'] . '"' 
								: '') . 
							($this -> access_key ($entry ['legend']) 
								? ' accesskey="' . $this -> access_key ($entry ['legend']) . '"' 
								: '') . 
							(!empty ($entry ['target']) 
								? ' target="' . $entry ['target'] . '"' 
								: '') . 
							(isset ($entry ['url']) || isset ($entry ['args']) 
								? ' href="' . 
								(isset ($entry ['url']) 
									? $entry ['url'] 
									: '') . 
								(!empty ($query) 
									? '?' . $query 
									: '') . '">' 
								: '">') . 
							(!empty ($entry ['icon']) 
								? '<img class="' .
									(isset ($entry ['icon_class']) 
									? $entry ['icon_class'] 
									: $this -> default_icon_class) . 
									'" alt="" src="' . $entry ['icon'] . '" />'
								: '') . 
							$entry ['legend'] . 
						'</a>';
					else
						$html_menu .= $entry ['legend'];
					if (!empty ($this -> templates [$type] ['entry'] [1]))
						$html_menu .= 
							str_replace 
							(
								array ('{indent}', '{current}'), 
								array 
									(
										$indent, 
										(
											(!empty ($this -> templates [$type] ['entry'] ['current']) && !empty ($entry ['current'])) 
												? $this -> templates [$type] ['entry'] ['current'] 
												: ''
										)
									),
								$this -> templates [$type] ['entry'] [1]
							);
				}
				if 
					(
						!empty ($entry ['sub']) 
							&& 
							(
								($short != 2 && empty ($this -> templates [$type] ['seperate']) || !empty ($entry ['current']) || !empty ($entry ['current_sub'])) 
								|| 
								($short == 2 && !empty ($entry ['current_sub']) || !empty ($entry ['current']))
							)
					)
				{
					++ $level;
					$submenu .= 
						$this -> render ($entry, !empty ($entry ['type']) ? $entry ['type'] : null, isset ($default_arg_name) ? $default_arg_name : null, isset ($short) ? $short : null);
					-- $level;
					if (empty ($this -> templates [$type] ['seperate'])) 
					{
						$html_menu .= $submenu;
						$submenu = '';
					}
				}

				if (!empty ($this -> templates [$type] ['submenu'] [1]))
					$html_menu .= str_replace ('{indent}', $indent, $this -> templates [$type] ['submenu'] [1]);
			}
		}
		if (!empty ($this -> templates [$type] ['menu'] [1]))
			$html_menu .= str_replace ('{indent}', $indent, $this -> templates [$type] ['menu'] [1]);
		if (!empty ($this -> templates [$type] ['seperate']) && !empty ($submenu))
			$html_menu .= $submenu;
		if (isset ($menu ['current_sub_index']))
		{
			$query = '';
			if (isset ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['args']))
			{
				foreach ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['args'] as $arg_name => $arg_value)
					$query .= $arg_name . '=' . $arg_value . '&';
				$query = str_replace ('&', '&amp;', rtrim ($query, '&'));
			}
			$html_menu = str_replace ('{next}', $menu ['current_sub_index'] ? 
				'<a class="' . $this -> next ['class'] . '"' . 
					(!empty ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['legend']) 
						? ' title="' . strip_tags ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['legend']) . '"' 
						: '') . 
					' accesskey="' . $this -> next ['access_key'] . '"' .
					(isset ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['url']) || isset ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['args']) 
						? ' href="' . 
						(isset ($menu ['sub'] [$menu ['current_sub_index'] - 1] ['url']) 
							? $menu ['sub'] [$menu ['current_sub_index'] - 1] ['url'] 
							: '') . 
						(!empty ($query) 
							? '?' . $query 
							: '') . '">' 
						: '">') . 
					$this -> next ['legend'] . 
				'</a>' : '', $html_menu);
			$query = '';
			if (isset ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['args']))
			{
				foreach ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['args'] as $arg_name => $arg_value)
					$query .= $arg_name . '=' . $arg_value . '&';
//				$query = str_replace ('&', '&amp;', rtrim ($query, '&'));
				if ($query [strlen ($query) - 1] == '&')
					$query = substr ($query, 0, -1);
				$query = str_replace ('&', '&amp;', $query);
			}
			$html_menu = str_replace ('{prev}', $menu ['current_sub_index'] + 1 < count ($menu ['sub']) ? 
				'<a class="' . $this -> prev ['class'] . '"' . 
					(!empty ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['legend']) 
						? ' title="' . strip_tags ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['legend']) . '"' 
						: '') . 
					' accesskey="' . $this -> prev ['access_key'] . '"' .
					(isset ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['url']) || isset ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['args']) 
						? ' href="' . 
						(isset ($menu ['sub'] [$menu ['current_sub_index'] + 1] ['url']) 
							? $menu ['sub'] [$menu ['current_sub_index'] + 1] ['url'] 
							: '') . 
						(!empty ($query) 
							? '?' . $query 
							: '') . '">' 
						: '">') . 
					$this -> prev ['legend'] . 
				'</a>' : '', $html_menu);
		}
		return $html_menu;
	}
	function get () // Copyright by Mateusz Rusin 2004 // 1.01
	{
//var_dump ($this -> find_current ($this -> menu, $current_sub));
		return $this -> render ($this -> find_current ($this -> menu, $current_sub));
	}
	function show () // Copyright by Mateusz Rusin 2004 // 1.0
	{
		print $this -> get ();
	}
	function access_key ($label)
	{
		return substr (strstr ($label, '<u>'), 3, 1);
	}
}
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
?>