<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 10/10/2009	ELL
// Revised: 11/10/2009	ELL
// Added menu item classes and styles for individual item control.
// Revised: 1/8/2011	ELL
// Added menu type to specify whether CSS or JavaScript menu is being added.
// Revised: 11/20/2011	ELL
// Fixed menu item styles table description to have menu number right after "menu".
// Revised: 2/7/2012	ELL
// Added table prefix handling.
// 
// 
	include ("check_session.php");
	
	$description = $_POST['description'];
	$menu_type = $_POST['menu_type'];
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "menus` ORDER BY `menu_name` DESC";
	$result = mysql_query($query, $bwb_link);
	$row_cnt = mysql_num_rows($result);
	if ($row_cnt < 1)
		$suffix = 1;
	else
	{
		$row = mysql_fetch_array($result);
		$menu_name = $row["menu_name"];
		
		$index = array();
		$i = 1;
		$index[0] = substr($menu_name, strlen("menu"));
		while ($row = mysql_fetch_array($result))
		{
			$menu_name = $row["menu_name"];
			$index[$i] = substr($menu_name, strlen("menu"));
			$i++;
		}
		sort($index);
		$suffix = $index[$i-1] + 1;
	}

	$new_menu = substr($menu_name, 0, 4) . $suffix;
	$style_description = "CSS Menu " . $suffix . " Styles";
	$item_style_description = "CSS Menu " . $suffix . " Item Styles";
	
	// Add to menus table.
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "menus` SET `menu_name` = '$new_menu', `description` = '$description', `menu_type` = $menu_type";
	$result = mysql_query($query, $bwb_link);
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` WHERE `name` LIKE 'menu%' ORDER BY `name` DESC";
	$result = mysql_query($query, $bwb_link);
	$row = mysql_fetch_array($result);
	$sort_order = $row["sort_order"];
	$sort_order++;
	
	// Add to style sheet list.
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$new_menu', `description` = '$style_description', show_class_editor = 0, show_custom_styles = 0, `sort_order` = $sort_order";
	$result = mysql_query($query, $bwb_link);
	$sort_order++;
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '" . $new_menu . "_item', `description` = '$item_style_description', show_class_editor = 0, show_custom_styles = 0, `sort_order` = $sort_order";
	$result = mysql_query($query, $bwb_link);
	
	// Create new menu classes table.
	$query = "CREATE TABLE " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SELECT * FROM " . BWB_TABLE_PREFIX . "menu1_classes";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes` ADD PRIMARY KEY (`class`)";
	$result = mysql_query($query, $bwb_link);
	
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes`";
	$result = mysql_query($query, $bwb_link);
		
	// Insert new menu classes.
	// CSS menu classes
	$new_class = ".menu_div" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Menu Position', sort_order=150";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_ul" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level UL', sort_order=160";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_li" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level LI', sort_order=170";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_a" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level A', sort_order=180";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_a" . $suffix . " A:link";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level A Link', sort_order=190";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_a" . $suffix . " A:hover";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level A Hover', sort_order=200";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_a" . $suffix . " A:visited";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level A Visited', sort_order=210";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".top_level_a" . $suffix . " A:active";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level A Active', sort_order=220";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_ul" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 UL', sort_order=230";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_li" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 LI', sort_order=240";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_a" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 A', sort_order=250";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_a" . $suffix . " A:link";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 A Link', sort_order=260";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_a" . $suffix . " A:hover";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 A Hover', sort_order=270";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_a" . $suffix . " A:visited";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 A Visited', sort_order=280";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_1_a" . $suffix . " A:active";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 A Active', sort_order=290";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_ul" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 UL', sort_order=300";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_li" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 LI', sort_order=310";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_a" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 A', sort_order=320";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_a" . $suffix . " A:link";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 A Link', sort_order=330";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_a" . $suffix . " A:hover";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 A Hover', sort_order=340";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_a" . $suffix . " A:visited";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "" . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 A Visited', sort_order=350";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".sub_level_2_a" . $suffix . " A:active";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 A Active', sort_order=360";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Menu Font', sort_order=100";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . " li:hover, .nav" . $suffix . " ul li a:hover";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Hover Attributes', sort_order=28";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".nav" . $suffix . " a";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='URL Attributes', sort_order=110";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . ", .nav" . $suffix . " ul";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Menu Attributes, Horizontal On/Off ', sort_order=10";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . ">li";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Top Level Item Size and Padding', sort_order=40";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . " li>ul";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Menu Size and Position', sort_order=50";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . " li:hover>ul";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Show Sub Menu on Hover', sort_order=60";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".nav" . $suffix . " ul li a";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Menu Padding', sort_order=70";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . " li:active, .nav" . $suffix . " ul li a:active";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Active Attributes', sort_order=90";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = "ul.nav" . $suffix . ", .nav" . $suffix . " ul, .nav" . $suffix . " ul li a";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Menu Attributes, Mouse Out', sort_order=80";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".nav" . $suffix . " a:link";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Link Attributes', sort_order=120";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".nav" . $suffix . " a:visited";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Link Visited Attributes', sort_order=130";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".nav" . $suffix . " a:hover";
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Link Hover Attributes', sort_order=140";
	$result2 = mysql_query($query2, $bwb_link);
	// JavaScript menu classes
	$new_class = ".m0l0iout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Main Level, Mouse Out', sort_order=-150";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l0iover" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Main Level, Mouse Over', sort_order=-140";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l0oout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Main Level Background, Mouse Out', sort_order=-130";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l0oover" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Main Level Background, Mouse Over', sort_order=-120";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l1iout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1, Mouse Out', sort_order=-110";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l1iover" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1, Mouse Over', sort_order=-100";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l1oout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 Background, Mouse Out', sort_order=-90";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l1oover" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 1 Background, Mouse Over', sort_order=-80";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l2iout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2, Mouse Out', sort_order=-70";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l2iover" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2, Mouse Out', sort_order=-60";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l2oout" . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 Background, Mouse Out', sort_order=-50";
	$result2 = mysql_query($query2, $bwb_link);
	$new_class = ".m0l2oover " . $suffix;
	$query2 = "INSERT INTO " . BWB_TABLE_PREFIX . "menu" . $suffix . "_classes SET `class` = '" . $new_class . "', description='Sub Level 2 Background, Mouse Out', sort_order=-40";
	$result2 = mysql_query($query2, $bwb_link);
	
	// Create new menu css table.
	$query = "CREATE TABLE " . BWB_TABLE_PREFIX . "menu" . $suffix . "_css SELECT * FROM " . BWB_TABLE_PREFIX . "site_css";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_css` ADD PRIMARY KEY (`class`)";
	$result = mysql_query($query, $bwb_link);
	
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_css`";
	$result = mysql_query($query, $bwb_link);
		
	// Create new menu items table.
	$query = "CREATE TABLE " . BWB_TABLE_PREFIX . "menu" . $suffix . "_items SELECT * FROM " . BWB_TABLE_PREFIX . "menu1_items";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_items` ADD PRIMARY KEY (`ID`)";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_items` CHANGE `ID` `ID` INT( 11 ) NOT NULL AUTO_INCREMENT";
	$result = mysql_query($query, $bwb_link);
	
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_items`";
	$result = mysql_query($query, $bwb_link);
	
	// Create new menu item class table.
	$query = "CREATE TABLE " . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_classes SELECT * FROM " . BWB_TABLE_PREFIX . "menu1_item_classes";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_classes` ADD PRIMARY KEY (`class`)";
	$result = mysql_query($query, $bwb_link);
	
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_classes`";
	$result = mysql_query($query, $bwb_link);
	
	// Create new menu item css table.
	$query = "CREATE TABLE " . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_css SELECT * FROM " . BWB_TABLE_PREFIX . "site_css";
	$result = mysql_query($query, $bwb_link);
	
	$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_css` ADD PRIMARY KEY (`class`)";
	$result = mysql_query($query, $bwb_link);
	
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "menu" . $suffix . "_item_css`";
	$result = mysql_query($query, $bwb_link);

	header("Location: manage_menu.php?menu=$new_menu");
	
?>
