// JavaScript Document
// Slide Menu 

$.fn.slidemenu = function(options) {
	this.each(function(){
		// set defaults
		var slidemenu = this;
	
		slidemenu.options = {  // set options
			homepage : '/index.html',
			hightlightcurrent : true
		};
		slidemenu.options = $.extend(slidemenu.options,options);	// load options and defualts

		

 		slidemenu.highlightcurrent = function(obj, homepage) {  // function to highlight current menu
			// Check the home link against the path and set the navigation accordingly.
			var path = location.pathname;
	
			if (path == homepage || path == "/") {
				// Note that the jQuery selector matches *only* the home link
				var $nav = $("#" + obj.id + ' a[@href="' + homepage + '"]');

			} else {
				var $nav = $("#" + obj.id + ' a[@href$="' + path + '"]');
			}

    		// Add the active class to the current path and activate it's subnavigation
			$nav.addClass('active')
	
			if($nav.parent().hasClass("submenu")) {
				$nav.parent().toggleClass("openMenu").find("ul").show();
			} 
			else {
				$nav.parents("ul").show().parent().toggleClass("openMenu")	
			}
							 	
	
			// If the active class has subnavigation, show it
			
 		};

		slidemenu.init = function(obj) {
			$("#" + obj.id + " > li > a").find("+ ul").hide().parent().addClass("submenu");  // close all menus on init
				
			if(slidemenu.options.hightlightcurrent) 
				slidemenu.highlightcurrent(obj, slidemenu.options.homepage)
				
			
			$("#" + obj.id + " > li > a").click(function() {  // Expand or collapse on click 										 
	       		if ($(this).parent().children().length > 1)     {
   	      			$(this).find("+ ul").slideToggle();
       	  			$(this).parent().toggleClass("openMenu");
					return false;
        		}	
			});
		};
		slidemenu.init(this);  // init menus
	});
}
