--- C:\Users\Thomas Gorgolione\Desktop\imagemenu_old\imagemenu.module	2007-03-20 07:44:07.000000000 -0400
+++ C:\Users\Thomas Gorgolione\Desktop\imagemenu\imagemenu.module	2009-06-20 22:46:26.726468800 -0400
@@ -1,6 +1,9 @@
 <?php
 // $Id: imagemenu.module,v 1.5 2007/03/20 11:44:07 pobster Exp $
 
+// global variable to determine what menu is active (usable in showing menu blocks)]
+$imagemenu_isactive = array();
+
 /**
  * Implementation of hook_help
  */
@@ -211,6 +214,7 @@
   return;
 }
 
+
 function imagemenu_add_item($mid = 0) {
   // if there's no menu id set then check to see if there's any base menus to add an item to
   if (!$mid || !is_numeric($mid)) {
@@ -239,16 +243,26 @@
     '#required' => TRUE,
   );
   $form['imagepath'] = array('#type' => 'textfield',
-    '#title' => t('Image Path'),
+    '#title' => t('Inactive (normal) Image Path'),
     '#default_value' => $item['imagepath'],
-    '#description' => t('The path to the image.'),
+    '#description' => t('The path to an image to display when a menu item is not active.'),
     '#required' => TRUE,
   );
+  $form['activeimagepath'] = array('#type' => 'textfield',
+    '#title' => t('Active Image Path'),
+    '#default_value' => $item['activeimagepath'],
+    '#description' => t('Optional. The path to an image to display when a menu item is active.'),
+  );
   $form['mouseover'] = array('#type' => 'textfield',
-    '#title' => t('Image Path'),
+    '#title' => t('Mouseover Image Path'),
     '#default_value' => $item['mouseover'],
     '#description' => t('Optional. The path to an image to display on mouseover.'),
   );
+  $form['activemouseover'] = array('#type' => 'textfield',
+    '#title' => t('Active Mouseover Image Path'),
+    '#default_value' => $item['activemouseover'],
+    '#description' => t('Optional. If required, a mouseover for the active menu item.'),
+  );
   $form['path'] = array('#type' => 'textfield',
     '#title' => t('Path'),
     '#default_value' => $item['path'],
@@ -287,20 +301,24 @@
 function imagemenu_add_item_form_validate($form_id, $form) {
   $check = is_readable($form['imagepath']);
   if (!$check) form_set_error('imagepath', t('File not found.'));
+  $check = is_readable($form['activeimagepath']);
+  if (!$check && $form['activeimagepath']) form_set_error('activeimagepath', t('File not found.'));
   $check = is_readable($form['mouseover']);
   if (!$check && $form['mouseover']) form_set_error('mouseover', t('File not found.'));
+  $check = is_readable($form['activemouseover']);
+  if (!$check && $form['activemouseover']) form_set_error('activemouseover', t('File not found.'));
 }
 
 function imagemenu_add_item_form_submit($form_id, $form) {
   if ($form['path'] == 'node') $form['path'] = '<front>';
   if (!$form['mid']) {
     $mid = db_next_id('imagemenu');
-    db_query("INSERT INTO {imagemenu} (mid, pid, path, imagepath, mouseover, title, alt, weight, type) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d)", $mid, $form['pid'], $form['path'], $form['imagepath'], $form['mouseover'], $form['title'], $form['alt'], $form['weight'], $form['expanded']);
+    db_query("INSERT INTO {imagemenu} (mid, pid, path, imagepath, activeimagepath, mouseover, activemouseover, title, alt, weight, type) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", $mid, $form['pid'], $form['path'], $form['imagepath'], $form['activeimagepath'], $form['mouseover'], $form['activemouseover'], $form['title'], $form['alt'], $form['weight'], $form['expanded']);
     drupal_set_message(t('Menu item successfully added.'));
     watchdog('imagemenu', t('imagemenu: added new item to menu \'<em>%menu</em>\'', array('%menu' => $form['pid'])), WATCHDOG_NOTICE);
   }
   else {
-    db_query("UPDATE {imagemenu} SET pid = %d, path = '%s', imagepath = '%s', mouseover = '%s', title = '%s', alt = '%s', weight = %d, type = %d WHERE mid = %d", $form['pid'], $form['path'], $form['imagepath'], $form['mouseover'], $form['title'], $form['alt'],  $form['weight'], $form['expanded'], $form['mid']);
+    db_query("UPDATE {imagemenu} SET pid = %d, path = '%s', imagepath = '%s', activeimagepath = '%s', mouseover = '%s', activemouseover = '%s', title = '%s', alt = '%s', weight = %d, type = %d WHERE mid = %d", $form['pid'], $form['path'], $form['imagepath'], $form['activeimagepath'], $form['mouseover'], $form['activemouseover'], $form['title'], $form['alt'],  $form['weight'], $form['expanded'], $form['mid']);
     drupal_set_message(t('Menu item successfully updated.'));
     watchdog('imagemenu', t('imagemenu: updated item in menu \'<em>%menu</em>\'', array('%menu' => $form['pid'])), WATCHDOG_NOTICE);
   }
@@ -381,12 +399,245 @@
   return $item->title;
 }
 
-function imagemenu_fetch_item($item, $prefix = '') {
-  $script = $item['mouseover'] ? ' onMouseOver="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$item['mouseover'].'\'" onMouseOut="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$item['imagepath'].'\'"' : '';
-  $output = '<a href="'.url($item['path']).'"><img src="'.base_path().$item['imagepath'].'" alt="'.$item['alt'].'" title="'.$item['title'].'" name="imagemenu_'.$prefix.'_'.$item['mid'].'"'.$script.'></a>';
+function imagemenu_fetch_item($item, $prefix = '', $active = FALSE) {
+  // test to see if the menu item path is a taxonomy page, or in a standard menu
+  // if so test to see if the current path is within the vocab, term, or menu that corresponds to the menu path 
+  global $imagemenu_isactive;
+
+  if ($active == FALSE && (variable_get('imagemenu_taxonomy', '0') == 1)) {
+  	  // make sure we get the real paths to perform tests on before we do anything (we won't be able to use arg())
+  	  $full_path = drupal_lookup_path("alias", $_GET["q"]) ? drupal_lookup_path("alias", $_GET["q"]) : $_GET["q"];
+	  $full_menu_path = drupal_lookup_path("alias", $item['path']) ? drupal_lookup_path("alias", $item['path']) : $item['path'];
+  	  $path_args = explode('/', $full_path);
+  	  $menu_path_args = explode('/', $full_menu_path);
+  	  
+	  // is node or term page within specific vocabulary?
+	  if ($menu_path_args[0] == "taxonomy" && $menu_path_args[1] == "vocabulary" && isset($menu_path_args[2]) && is_numeric($menu_path_args[2])) {
+	     // if we're in a taxonomy term, see if the term belongs to the vocab
+	  	 if ($path_args[0] == "taxonomy" && $path_args[1] == "term") {
+	        $term = taxonomy_get_term($path_args[2]);
+			$active = ($term->vid == $menu_path_args[2]) ? TRUE : $active;
+	 	 }
+		 
+		 // if we're in a node, see if the node belongs to any terms in the vocab. if so, we can
+		 // say that the node is associated to a vocabulary
+		 else if ($path_args[0] == "node" && (isset($path_args[1]) && is_numeric($path_args[1]))) {
+		 	if (count(taxonomy_node_get_terms_by_vocabulary($path_args[1], $menu_path_args[2])) > 0)
+				$active = TRUE;
+		 }
+	  }
+	  
+	  // is node or subterm within specific term?
+	  if ($menu_path_args[0] == "taxonomy" && $menu_path_args[1] == "term") {
+	  	 
+		 // if we're in a node, test for node type
+	     if ($path_args[0] == "node" && (isset($path_args[1]) && is_numeric($path_args[1]))) {
+		 	$terms = taxonomy_node_get_terms($path_args[1]);
+			foreach ($terms as $term) {
+		 		$active = ($term->tid == $menu_path_args[2]) ? TRUE : $active; // going up hiererchy
+				if (active != TRUE) $active = find_master_term_asc($menu_path_args[2], $term->tid) ? TRUE : $active;
+			}
+		 }
+		
+		 else if ($path_args[0] == "taxonomy" && $path_args[1] == "term") 
+		 	$active = find_master_term_asc($menu_path_args[2], $path_args[3]) ? TRUE : $active; // going up hierarchy
+	  }
+	  
+	  // is node a blog post for a specific user?
+	  if ($menu_path_args[0] == "blog" && isset($menu_path_args[1]) && is_numeric($menu_path_args[1])) {
+	     if ($path_args[0] == "node" && (isset($path_args[1]) && is_numeric($path_args[1]))) {
+		 	$this_node = node_load($path_args[1]);
+			$active = (($this_node->type == "blog") && ($this_node->uid == $menu_path_args[1])) ? TRUE : $active;
+		 }
+	  }
+	  
+	  // is node a blog post in general?
+	  if ($menu_path_args[0] == "blog" && !isset($menu_path_args[1])) {
+	     if ($path_args[0] == "node" && (isset($path_args[1]) && is_numeric($path_args[1]))) {
+		 	$this_node = node_load($path_args[1]);
+			$active = ($this_node->type == "blog") ? TRUE : $active;
+		 }
+	  }
+	  
+	  // is node a subitem in a menu?
+	  if ($menu_path_args[0] == "node" && (isset($menu_path_args[1]) && is_numeric($menu_path_args[1]))) {
+	  	 
+		 $menu_mids = db_query("SELECT * FROM {menu} WHERE path='%s'", $full_menu_path);
+	     $path_mids = db_query("SELECT * FROM {menu} WHERE path='%s'", $full_path);
+		 $path_mids_array = array();
+		
+		 while ($path_mid = db_fetch_object($path_mids))
+			$path_mids_array[] = $path_mid;
+			
+		 if((db_num_rows($menu_mids) > 0) && (db_num_rows($path_mids) > 0)) {
+		 	while ($menu_mid = db_fetch_object($menu_mids)) {
+				foreach ($path_mids_array as $path_mid) {
+					$active = (menu_in_active_trail_in_submenu($path_mid->mid, $menu_mid->mid)) ? TRUE : $active;
+				}
+			}
+		 }
+	  }
+	  
+	  // CATEGORY MODULE: is node a category within a category/container, or is the node a page associated with a parent category?
+	  if (($menu_path_args[0] == "node") && isset($menu_path_args[1]) && is_numeric($menu_path_args[1]) && module_exists("category")) {
+		 // if the menu item is a category or a container
+		 if(db_num_rows(db_query("SELECT cid FROM {category} WHERE cid=%d", $menu_path_args[1])) > 0) {
+		 	
+		 	// is page a node
+			if($path_args[0] == "node" && isset($path_args[1]) && is_numeric($path_args[1])) {
+				 
+				$cat_data = db_query("SELECT cid FROM {category} WHERE cid=%d", $path_args[1]);
+				$assigned_menus = db_query("SELECT cid FROM {category_node} WHERE nid=%d", $path_args[1]);
+				
+				// is the page a category/container nodetype?
+				if(db_num_rows($cat_data) > 0) {
+					if ($page_cat == $menu_path_args[1])
+						$active = TRUE; // if the page_cat is already assigned to the menu, then do nothing else
+					
+					else 
+						$active = (find_master_cat_asc($menu_path_args[1], $path_args[1])) ? TRUE : $active; // otherwise find it in the hierarchy
+				}
+				
+				else if (db_num_rows($assigned_menus) > 0) { // if not, does it have parent categories assigned to them, and what are they?
+					while ($cat_obj = db_fetch_object($assigned_cat)) { // run through the hierarchy and see if one of them, or the parent is the menu cat
+						if ($cat_obj->cid == $menu_path_args[1])
+							$active = TRUE;
+							
+						else
+							$active = (find_master_cat_asc($menu_path_args[1], $cat_obj->cid)) ? TRUE : $active;
+					}
+				}
+			}
+		 }
+	  }
+	  
+	  // is node the root forum page?
+	  if ($menu_path_args[0] == "forum" && !isset($menu_path_args[1])) {
+		 
+	  	 // if a page is a forum
+	  	 if ($path_args[0] == "forum" && (isset($path_args[1]) && is_numeric($path_args[1])))
+		 	$active = TRUE;
+		 
+		 else if ($path_args[0] == "node" && isset($path_args[1]) && is_numeric($path_args[1])) {
+		 	// if a page is a forum topic
+			
+		 	$this_node = node_load($path_args[1]);
+			$active = ($this_node->type == "forum") ? TRUE : $active;
+		 }
+	  }
+	  
+	  // is node a subitem in a forum?
+	  if ($menu_path_args[0] == "forum" && isset($menu_path_args[1]) && is_numeric($menu_path_args[1])) {
+		 // if we're in a node, test for node type
+	     if ($path_args[0] == "node" && isset($path_args[1]) && is_numeric($path_args[1])) {
+		 	// forums work like taxonomy, since it uses the taxonomy system.  Therefore, we can
+			// do searches like we do taxonomy
+		 	$terms = taxonomy_node_get_terms($path_args[1]);
+			foreach ($terms as $term) {
+		 		$active = ($term->tid == $menu_path_args[1]) ? TRUE : $active; //going up hierarchy
+				if ($active != TRUE) {
+					$active = find_master_term_asc($menu_path_args[1], $term->tid) ? TRUE : $active;
+				}
+			}
+		 }
+		 
+		 // if we're in a sub-forum
+		 else if ($path_args[0] == "forum" && isset($path_args[1]) && is_numeric($path_args[1])) {
+		 	// are we in a subforum of a forum?
+			$forums = forum_get_forums($menu_path_args[1]);
+			foreach ($forums as $forum) {
+		 		$active = ($forum->tid == $path_args[1]) ? TRUE : $active; //going down hierarchy
+				if (active != TRUE) 
+					$active = find_master_term_desc($path_args[1], $forum->tid) ? TRUE : $active;
+			}
+		 }
+	  }
+  }
+  
+  if ($prefix != "form") {
+ 	$imagemenu_isactive[$prefix][$item['mid']] = $active;
+  }
+
+  $image_path = $active && $item['activeimagepath'] ? $item['activeimagepath'] : $item['imagepath'];  
+  $mouse_over_img = $active && $item['activemouseover'] ? $item['activemouseover'] : $item['mouseover'];
+  
+  $script = $mouse_over_img ? ' onMouseOver="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$mouse_over_img.'\'" onMouseOut="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$image_path.'\'" onfocus="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$mouse_over_img.'\'" onblur="document.imagemenu_'.$prefix.'_'.$item['mid'].'.src=\''.base_path().$image_path.'\'"' : '';
+  
+  $output = '<a href="'.url($item['path']).'"><img src="'.base_path().$image_path.'" alt="'.$item['alt'].'" title="'.$item['title'].'" name="imagemenu_'.$prefix.'_'.$item['mid'].'"'.$script.'></a>';
   return $output;
 }
 
+// recurse through terms until we find a match (the taxonomy specified in the menu) or fail trying
+function find_master_term_desc($needle, $haystack) {
+	$terms = db_query("SELECT tid FROM {term_hierarchy} WHERE parent='%s'", $haystack);
+	
+	if (db_num_rows($terms) == 0) {
+		return FALSE;
+	}
+	
+	while($term = db_fetch_object($terms)) {
+		if ($needle == $term->tid)
+			return TRUE;
+		
+		else {
+			if(find_master_term_desc($needle, $term->tid)) {
+				return TRUE;
+			}
+		}
+	}
+	
+	return FALSE;
+}
+
+// recurse through terms until we find a match (the taxonomy specified in the menu) or fail trying
+function find_master_term_asc($needle, $haystack) {
+	
+	$terms = db_query("SELECT parent FROM {term_hierarchy} WHERE tid='%s'", $haystack);
+	
+	if (db_num_rows($terms) == 0) {
+		return FALSE;
+	}
+	
+	while($term = db_fetch_object($terms)) {
+		if ($term->parent != 0) {
+			if ($needle == $term->parent)
+				return TRUE;
+			
+			else {
+				if(find_master_term_asc($needle, $term->parent)) {
+					return TRUE;
+				}
+			}
+		}
+	}
+	
+	return FALSE;
+}
+
+// recurse through terms until we find a match (the taxonomy specified in the menu) or fail trying
+function find_master_cat_asc($master_cat, $start_cat) {
+	$cats = db_query("SELECT parent FROM {category_hierarchy} WHERE cid=%d", $start_cat);
+	
+	if (db_num_rows($cats) == 0) {
+		return FALSE;
+	}
+	
+	while($cat = db_fetch_object($cats)) {
+		if ($cat->parent != 0) {
+			if ($master_cat == $cat->parent)
+				return TRUE;
+			
+			else {
+				if(find_master_cat_asc($master_cat, $cat->parent)) {
+					return TRUE;
+				}
+			}
+		}
+	}
+	
+	return FALSE;
+}
+
 function imagemenu_display($mid, $full = FALSE) {
   if (!$mid || !is_numeric($mid)) return FALSE;
   switch (variable_get('imagemenu_layout', 'vertical')) {
@@ -423,6 +674,11 @@
     '#default_value' => variable_get('imagemenu_layout', 'vertical'),
     '#description' => t('The orientation of the menu.'),
   );
+  $form['admin_settings']['imagemenu_taxonomy'] = array('#type' => 'checkbox',
+    '#title' => t('Menuing and Taxonomy influence'),
+    '#default_value' => variable_get('imagemenu_taxonomy', '0'),
+    '#description' => t('If selected, taxonomy and standard menuing will determine whether an active image is shown.'),
+  );
   return system_settings_form($form);
 }
 
@@ -579,23 +835,27 @@
                      WHERE pid = %d
                      ORDER BY weight, title', $pid);
   while ($item = db_fetch_object($query)) {
-    $output[$item->mid] = array('mid' => $item->mid, 'title' => $item->title, 'alt' => $item->alt, 'mouseover' => $item->mouseover, 'description' => $item->description, 'path' => $item->path, 'imagepath' => $item->imagepath, 'enabled' => $item->enabled, 'type' => $item->type, 'depth' => $depth);
+    $output[$item->mid] = array('mid' => $item->mid, 'title' => $item->title, 'alt' => $item->alt, 'mouseover' => $item->mouseover, 'activemouseover' => $item->activemouseover, 'description' => $item->description, 'path' => $item->path, 'imagepath' => $item->imagepath, 'activeimagepath' => $item->activeimagepath, 'enabled' => $item->enabled, 'type' => $item->type, 'depth' => $depth);
   }
   return $output;
 }
 
 function imagemenu_build_table_vertical($mid, $full = FALSE) {
   $menus = imagemenu_fetch_rows($mid, $full);
+  $visible = key(imagemenu_show_visible_tree($mid));
   foreach ($menus as $menu) {
-    $output[] = array(array('data' => imagemenu_fetch_item($menu, $mid)));
+  	$active = $menu['mid'] == $visible ? TRUE : FALSE;
+    $output[] = array(array('data' => imagemenu_fetch_item($menu, $mid, $active)));
   }
   return $output;
 }
 
 function imagemenu_build_table_horizontal($mid, $full = FALSE) {
   $menus = imagemenu_fetch_rows($mid, $full);
+  $visible = key(imagemenu_show_visible_tree($mid));
   foreach ($menus as $menu) {
-    $output[$menu['depth']][] = array('data' => imagemenu_fetch_item($menu, $mid));
+  	$active = $menu['mid'] == $visible ? TRUE : FALSE;
+    $output[$menu['depth']][] = array('data' => imagemenu_fetch_item($menu, $mid, $active));
   }
   return $output;
 }
@@ -604,7 +864,7 @@
   $path = $_GET['q'];
   $output = array();
   $mids = array();
-  if ($path == 'node' || !$path) $path = '<front>';
+  if ($path == 'node' || !$path || $path == 'front_page') $path = '<front>';
   $query = db_query("SELECT mid FROM {imagemenu} WHERE path = '%s' ORDER BY mid", $path);
   while ($tmp = db_fetch_object($query)) {
     $mids[] = $tmp->mid;
