<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 10/12/2009	ELL
// Revised: 11/10/2009	ELL
// Added insertion of individual item class for item by item styling.
// Added proportionalizing.
// Revised: 2/9/2012	ELL
// Added table prefix handling.
// Revised: 6/9/2012	ELL
// Modified for use with shopping cart as well.  Uses $use_with_cart to determine if BWB_BASE_DIR and BWB_WEBPAGE_DIR
// need to be prepended to simple webpage links (selected from dropdown list as they are in webpage folder).
// Modified: 10/15/2012	ELL
// Added use of new `name` field for 'name' attribute.  It turns out that & codes in 'name' attribute are affecting
// menu spacing, so need to trap out these codes before saving in `name`.
// 
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "menu_assignments` WHERE `border_area` = '" . $border_area . "'";
	$result = mysql_query($query, $bwb_link);
	if (mysql_num_rows($result) > 0)
	{
		// Get menu index as suffix.
		$row = mysql_fetch_array($result);
		$menu_name = $row["menu_name"];
		/////////// Check for correct type. ///////////////////
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "menus` WHERE `menu_name` = '$menu_name'";
		$result = mysql_query($query, $bwb_link);
		$row = mysql_fetch_array($result);
		$menu_type = $row["menu_type"];
		///////////////////////////////////////////////////////
		if (strlen($menu_name) > 4)
			$suffix = substr($menu_name, 4);
		else
			$suffix = "";
		
		// Get proportional flag.
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "menus` WHERE `menu_name` = '" . $menu_name . "'";
		$result = mysql_query($query, $bwb_link);
		$row = mysql_fetch_array($result);
		$proportional = $row["proportional"];
		$proportional_sub = $row["proportional_sub"];
		
		if ($proportional == 1)	// If proportional, then need to calculate average menu size.
		{
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items WHERE level=0";
			$result = mysql_query($query, $bwb_link);
			$total_menu_items_top = mysql_num_rows($result);
			$total_menu_chars_top = 0;
			while ($row = mysql_fetch_array($result))
			{
				$item_text = $row['item_text'];
				$total_menu_chars_top = $total_menu_chars_top + strlen($item_text);
			}
			if ($total_menu_items_top > 0)
				$ave_menu_size_top = $total_menu_chars_top/$total_menu_items_top;
			else
				$ave_menu_size_top = 20;
			
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_css WHERE `class`='ul.nav" . $suffix . ">li'";
			$result = mysql_query($query, $bwb_link);
			$row = mysql_fetch_array($result);
			$top_level_width = $row['width'];
			$top_level_width_units = $row['width_units'];
			if ($top_level_width_units == 1) // Item width in percent
			{
				$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_css WHERE `class`='ul.nav" . $suffix . "'";
				$result = mysql_query($query, $bwb_link);
				$row = mysql_fetch_array($result);
				$menu_width = $row['width'];
				$menu_width_units = $row['width_units'];
				if ($menu_width == -1)
					$menu_width = 900;	// If not set then assume 900 px.
				elseif ($menu_width_units == 1)	// Menu width in percent
					$menu_width = 900 * $menu_width/100;	// Percent of 900 px.
				$top_level_width = $menu_width * $top_level_width/100;
			}	
		}
		
		if ($proportional_sub == 1)	// If proportional sub menus, then need to calculate average submenu sizes.
		{
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items WHERE level=1";
			$result = mysql_query($query, $bwb_link);
			$total_menu_items_sub_1 = mysql_num_rows($result);
			$total_menu_chars_sub_1 = 0;
			while ($row = mysql_fetch_array($result))
			{
				$item_text = $row['item_text'];
				$total_menu_chars_sub_1 = $total_menu_chars_sub_1 + strlen($item_text);
			}
			if ($ave_menu_size_sub_1 > 0)
				$ave_menu_size_sub_1 = $total_menu_chars_sub_1/$total_menu_items_sub_1;
			else
				$ave_menu_size_sub_1 = 20;
			
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items WHERE level=2";
			$result = mysql_query($query, $bwb_link);
			$total_menu_items_sub_2 = mysql_num_rows($result);
			$total_menu_chars_sub_2 = 0;
			while ($row = mysql_fetch_array($result))
			{
				$item_text = $row['item_text'];
				$total_menu_chars_sub_2 = $total_menu_chars_sub_2 + strlen($item_text);
			}
			if ($ave_menu_size_sub_2 > 0)
				$ave_menu_size_sub_2 = $total_menu_chars_sub_2/$total_menu_items_sub_2;
			else
				$ave_menu_size_sub_2 = 20;
			
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items WHERE level=3";
			$result = mysql_query($query, $bwb_link);
			$total_menu_items_sub_3 = mysql_num_rows($result);
			$total_menu_chars_sub_3 = 0;
			while ($row = mysql_fetch_array($result))
			{
				$item_text = $row['item_text'];
				$total_menu_chars_sub_3 = $total_menu_chars_sub_3 + strlen($item_text);
			}
			if ($ave_menu_size_sub_3 > 0)
				$ave_menu_size_sub_3 = $total_menu_chars_sub_3/$total_menu_items_sub_3;
			else
				$ave_menu_size_sub_3 = 20;
			
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_css WHERE `class`='ul.nav" . $suffix . " li>ul'";
			$result = mysql_query($query, $bwb_link);
			$row = mysql_fetch_array($result);
			$sub_level_width = $row['width'];
			$sub_level_width_units = $row['width_units'];
			if ($sub_level_width_units == 1) // Item width in percent
			{
				$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_css WHERE `class`='ul.nav" . $suffix . "'";
				$result = mysql_query($query, $bwb_link);
				$row = mysql_fetch_array($result);
				$menu_width = $row['width'];
				$menu_width_units = $row['width_units'];
				if ($menu_width == -1)
					$menu_width = 900;	// If not set then assume 900 px.
				elseif ($menu_width_units == 1)	// Menu width in percent
					$menu_width = 900 * $menu_width/100;	// Percent of 900 px.
				$sub_level_width = $menu_width * $sub_level_width/100;
			}	
		}
		
		// Initialize navigation HTML.
		$last_level = 0;
		if ($menu_type == 1)
			echo "\n<noscript>\n";
		echo "\n<div class='menu_div" . $suffix . "'>\n";
		$one_indent = "   ";
		$indent = $one_indent;
		echo $indent . "<ul class='top_level_ul" . $suffix . " nav" . $suffix . "'>\n";
		// Get items and build navigation HTML.
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items ORDER BY sort_order";
		$result = mysql_query($query, $bwb_link);
		$list_item_not_closed = 0;
		$item = 1;
		while ($row = mysql_fetch_array($result))
		{
			$item_text = stripslashes($row['item_text']);
//			$item_text = str_replace('AMPERSAND', '&', $item_text);
//			$item_text = str_replace('SEMICOLON', ';', $item_text);
			$level = $row['level'];
			$URL = $row['URL'];
			$target = $row['target'];
			$name = $row['name'];
			if ($use_with_cart === true)	{
				if (strpos($URL, "/") === false)	{
					if ((BWB_BASE_DIR == "") && (BWB_WEBPAGE_DIR == ""))
						$URL = "/" . $URL;
					elseif ((BWB_BASE_DIR != "") && (BWB_WEBPAGE_DIR == ""))
						$URL = "/" . BWB_BASE_DIR . "/" . $URL;
					elseif ((BWB_BASE_DIR == "") && (BWB_WEBPAGE_DIR != ""))
						$URL = "/" . BWB_WEBPAGE_DIR . "/" . $URL;
					else
						$URL = "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR . "/" . $URL;
				}
			}
			
			if (($proportional == 1) && ($level == 0))	// If proportional, then need to calculate item size.
			{
				$item_length = strlen($item_text);
				if ($item_length < $ave_menu_size_top)
				  $item_width = $top_level_width * (($item_length + ($ave_menu_size_top/$item_length))/$ave_menu_size_top);
				else if ($item_length > $ave_menu_size_top)
				  $item_width = $top_level_width * (($item_length - ($item_length/$ave_menu_size_top))/$ave_menu_size_top);
				else
				  $item_width = $top_level_width;
				$item_width = intval(round($item_width));
			}
			
			if (($proportional_sub == 1) && ($level == 1))	// If proportional, then need to calculate item size.
			{
				$item_length = strlen($item_text);
				if ($item_length < $ave_menu_size_sub_1)
				  $item_width = $sub_level_width * (($item_length + ($ave_menu_size_sub_1/$item_length))/$ave_menu_size_sub_1);
				else if ($item_length > $ave_menu_size_sub_1)
				  $item_width = $sub_level_width * (($item_length - ($item_length/$ave_menu_size_sub_1))/$ave_menu_size_sub_1);
				else
				  $item_width = $sub_level_width;
				$item_width = intval(round($item_width));
			}
			
			if (($proportional_sub == 1) && ($level == 2))	// If proportional, then need to calculate item size.
			{
				$item_length = strlen($item_text);
				if ($item_length < $ave_menu_size_sub_2)
				  $item_width = $sub_level_width * (($item_length + ($ave_menu_size_sub_2/$item_length))/$ave_menu_size_sub_2);
				else if ($item_length > $ave_menu_size_sub_2)
				  $item_width = $sub_level_width * (($item_length - ($item_length/$ave_menu_size_sub_2))/$ave_menu_size_sub_2);
				else
				  $item_width = $sub_level_width;
				$item_width = intval(round($item_width));
			}
			
			if (($proportional_sub == 1) && ($level == 3))	// If proportional, then need to calculate item size.
			{
				$item_length = strlen($item_text);
				if ($item_length < $ave_menu_size_sub_3)
				  $item_width = $sub_level_width * (($item_length + ($ave_menu_size_sub_3/$item_length))/$ave_menu_size_sub_3);
				else if ($item_length > $ave_menu_size_sub_3)
				  $item_width = $sub_level_width * (($item_length - ($item_length/$ave_menu_size_sub_3))/$ave_menu_size_sub_3);
				else
				  $item_width = $sub_level_width;
				$item_width = intval(round($item_width));
			}
			
			if ($level == 0)
			{
				$class_ul = "top_level_ul" . $suffix;
				$class_li = "top_level_li" . $suffix;
				$class_a = "top_level_a" . $suffix;
			}
			elseif ($level == 1)
			{
				$class_ul = "sub_level_1_ul" . $suffix;
				$class_li = "sub_level_1_li" . $suffix;
				$class_a = "sub_level_1_a" . $suffix;
			}
			elseif ($level > 1)
			{
				$class_ul = "sub_level_2_ul" . $suffix;
				$class_li = "sub_level_2_li" . $suffix;
				$class_a = "sub_level_2_a" . $suffix;
			}
				
			if ($level == $last_level)
			{
				if ($list_item_not_closed > 0)
				{
					echo "</li>\n";	// No change in levels, close off list item.
					$list_item_not_closed--;
				}
				$item_class = "menu" . $suffix . "_item" . $item;
				if((($proportional == 1) && ($level == 0)) || (($proportional_sub == 1) && ($level > 0)))
					echo $indent . $one_indent . "<li class='" . $class_li . "' style='width:" . $item_width . "px;'><span class='" . $item_class . "'><a href='"
					. $URL . "' name='" . $name . "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				else
					echo $indent . $one_indent . "<li class='" . $class_li . "'><span class='" . $item_class . "'><a href='" . $URL . "' name='" . $name
					. "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				$list_item_not_closed++;
				$item++;
			}
			elseif ($level > $last_level)	// Going deeper into sub levels, nest next ul inside of li.
			{
				$indent .= $one_indent;
				echo "\n" . $indent . "<ul class='" . $class_ul . "'>\n";
				$item_class = "menu" . $suffix . "_item" . $item;
				if((($proportional == 1) && ($level == 0)) || (($proportional_sub == 1) && ($level > 0)))
					echo $indent . $one_indent . "<li class='" . $class_li . "' style='width:" . $item_width . "px;'><span class='" . $item_class . "'><a href='"
					. $URL . "' name='" . $name . "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				else
					echo $indent . $one_indent . "<li class='" . $class_li . "'><span class='" . $item_class . "'><a href='" . $URL . "' name='" . $name
					. "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				$list_item_not_closed++;
				$item++;
			}
			elseif ($level < $last_level)
			{
				echo "</li>\n";	// Coming out of sub level, close off this list item and one ul is nested in.
				$level_diff = $last_level - $level;
				while ($level_diff > 0)
				{
					echo $indent . "</ul>\n";
					$indent = substr($indent, 3);
					if ($list_item_not_closed > 0)
					{
						echo $indent . $one_indent . "</li>\n";
						$list_item_not_closed--;
					}
					$level_diff--;
				}
				$item_class = "menu" . $suffix . "_item" . $item;
				if((($proportional == 1) && ($level == 0)) || (($proportional_sub == 1) && ($level > 0)))
					echo $indent . $one_indent . "<li class='" . $class_li . "' style='width:" . $item_width . "px;'><span class='" . $item_class . "'><a href='"
					. $URL . "' name='" . $name . "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				else
					echo $indent . $one_indent . "<li class='" . $class_li . "'><span class='" . $item_class . "'><a href='" . $URL . "' name='" . $name
					. "' target='" . $target . "' class='" . $class_a . "'>" . $item_text . "</a></span>";
				$list_item_not_closed++;
				$item++;
			}
			
			$last_level = $level;
		}
		
		echo "</li>\n";		// Close last list item.
		$list_item_not_closed--;
		
		while ($last_level > 0)
		{
			echo $indent . "</ul>\n";
			if ($list_item_not_closed > 0)
			{
				echo $indent . "</li>\n";	// If list item open then close it.
				$list_item_not_closed--;
			}
			$indent = substr($indent, 3);
			$last_level--;
		}
		echo $indent . "</ul>\n";
		echo "</div>\n";
		if ($menu_type == 1)
			echo "\n</noscript>\n";
	}
?>