/*

   Simple JQuery Accordion menu.
   HTML structure to use:

   <ul id="menu">
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     ...
     ...
   </ul>

*/


 
(function($) {
	
	var $theMenu;

	$.fn.accordion_menu = function(options) {
 
		var defaults = {
			MARKER_CLASS		: ""
		};
		
		// Merge options to defaults 
		opts = $.extend(defaults, options);		
		
		$theMenu = $(this);
		
		$theMenu.find('ul').hide();
		
		// Find all the href's != "*" and remove any MARKER_CLASS's		
		var $hrefObjects = $theMenu.find('a[href!="#"]');					// find all href's != "*"
		$hrefObjects.removeClass(opts.MARKER_CLASS);						// remove the MARKER_CLASS
		
		// Get the www address and extract the last field
		var dirs = window.location.pathname.split( '/' );					// break up the address into an array
		var webPage = dirs[dirs.length - 1];								// get the last element in the array
		var liIndex = -1;
				
		// Search thru the href objects for a webPage match
		if (webPage != "") {
			$hrefObjects.each(function(index) {
				var hrefString = $(this).attr('href');							// get the href string for each li
				if (hrefString.indexOf(webPage) != -1) {						// is there a match with the webPage
					$(this).addClass(opts.MARKER_CLASS);
					liIndex = index;											// yes. save the index
					return false;												// and break out of the each loop
				}
			});
		}
		
		// If a match was found then find the parent and show the children
		if (liIndex != -1) {												// if a match was found
			var $parent = $hrefObjects.eq(liIndex).parents('ul:eq(0)');		// then get the parent
			$parent.show();													// and show immediately
		}		
		
		  
		$theMenu.find('li a').click(function() {
			var $checkElement = $(this).next();
			if(($checkElement.is('ul')) && ($checkElement.is(':visible'))) {
				return false;
			}
				
			if(($checkElement.is('ul')) && (!$checkElement.is(':visible'))) {
				$theMenu.find('ul:visible').slideUp('normal');
				$checkElement.slideDown('normal');
				return false;
			}
		});


		// --- Public Function ---
		
		// This function will search for all ul's that are visible and collapse then
		this.collapse = function () {
			$theMenu.find('ul:visible').slideUp('normal');
		}
		
		// This function will remove all submenus.
		// Without any submenus the <a> associated with the top level ul is used
		this.removeSubmenu = function() {
			$theMenu.find('li ul').each(function() {
				$(this).remove();				
			});
			
		}
		
		// This function will open one of the submenus.
		// There is no anumation associated with the interface
		this.showSubmenu = function(item) {
			$theMenu.find('li ul').each (function(index) {
				if (item == index) {
					$(this).show();
					return;
				}
			});
		}
		
		// Return reference to this plugin
		// Required to access the public functions
		return this;
	};
	

  
}) (jQuery);
 
 
 
