(function($) { //create closure
/* sidebar_toggle adapted from menu_toggle_adder */
$.fn.menu_toggle = function(options){
var defaults = {	
animate_toggle: 'yes',
highlight_selected_link: 'yes',
toggle: 'http://www.rei.com/pix/common/toggle.gif',
toggled: 'http://www.rei.com/pix/common/toggled.gif',
toggle_selected: 'http://www.rei.com/pix/common/toggle.gif',
toggled_selected: 'http://www.rei.com/pix/common/toggled.gif'	
};
$('#sidebarWrapper li ul.sbSubCat').css('display','none');
$('#sidebarWrapper li img.toggler').css('display','inline');
var opts = $.extend(defaults, options);

           

	/*-- if a h3 has no a tag, add empty a tag for accessibility. --*/	
	$("#sidebarWrapper li h3").parent('li:has(ul)').each(function(){
		var $this = $(this);	
		 $this.children("h3:not(:has('a'))").eq("0").children("span:not(:has('a'))").wrapInner('<a href="#" class="linkStyle2"></a>');                          
                             /* if no span tag use this:    $this.children("h3:not(:has('a'))").eq("0").wrapInner('<a href="#" class="linkStyle2"></a>'); */
	});
	/*-- Find the selected link with the image 'toggle.gif' and change it to 'toggled.gif' --*/	
	$("#sidebarWrapper li a:has('img.toggler')").each(function(){
	              var $this = $(this);
		var toggler_src = $this.children("img.toggler").eq("0").attr("src");
		if (toggler_src == defaults.toggled_selected && defaults.animate_toggle == "yes") {
			$this.toggle(function(){
				$this.nextAll("ul").slideUp("fast");
				$this.children("img.toggler").eq("0").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$this.nextAll("ul").slideDown("fast");
				$this.children("img.toggler").eq("0").attr("src", defaults.toggled_selected);
				return false;
			});
		}
		else if (toggler_src == defaults.toggle && defaults.animate_toggle == "yes") {
				$this.toggle(function(){
					$this.nextAll("ul").slideDown("fast");					
					$this.children("img.toggler").eq("0").attr("src", defaults.toggled);
					return false;
				}, function(){
					$this.nextAll("ul").slideUp("fast");
					$this.children("img.toggler").eq("0").attr("src", defaults.toggle);
					return false;
				});
			}
		/*--- if toggle_selected = no --*/		
	else if (toggler_src == defaults.toggled_selected && defaults.animate_toggle == "no") {
			$this.toggle(function(){
				$this.nextAll("ul").hide();
				$this.children("img.toggler").eq("0").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$this.nextAll("ul").show();
				$this.children("img.toggler").eq("0").attr("src", defaults.toggled_selected);
				return false;
			});
		}
		
		else if (toggler_src == defaults.toggle && defaults.animate_toggle == "no") {
				$this.toggle(function(){
					$this.nextAll("ul").show();
					$this.children("img.toggler").eq("0").attr("src", defaults.toggled);
					return false;
				}, function(){
					$this.nextAll("ul").hide();
					$this.children("img.toggler").eq("0").attr("src", defaults.toggle);
					return false;
				});	
			}
	});
	$("#sidebarWrapper li h3:has('a.linkStyle2'), #sidebarWrapper li h3:has('span:has('a.linkStyle2')')").each(function(){   
		var $this = $(this);						
		var toggler_src = $this.prev("a").children("img").attr("src");				
		if (toggler_src == defaults.toggled_selected && defaults.animate_toggle == "yes") {		
			$this.toggle(function(){
				$this.nextAll("ul").slideUp("fast");
				$this.prev("a").children("img").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$this.nextAll("ul").slideDown("fast");
				$this.prev("a").children("img").attr("src", defaults.toggled_selected);
				return false;
			});
		}		
		else if (toggler_src == defaults.toggle && defaults.animate_toggle == "yes") {			
				$this.toggle(function(){
					$this.nextAll("ul").slideDown("fast");
					$this.prev("a").children("img").attr("src", defaults.toggled);
					return false;
				}, function(){
					$this.nextAll("ul").slideUp("fast");
					$this.prev("a").children("img").attr("src", defaults.toggle);
					return false;
				});
			}			
		/*--- if toggle_selected = no --*/			
		else if (toggler_src == defaults.toggled_selected && defaults.animate_toggle == "no") {		
			$this.toggle(function(){
				$this.nextAll("ul").hide();
				$this.prev("img").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$this.nextAll("ul").show();
				$this.prev("a").children("img").attr("src", defaults.toggled_selected);
				return false;
			});
		}		
		else if (toggler_src == defaults.toggle && defaults.animate_toggle == "no") {			
				$this.toggle(function(){
					$this.nextAll("ul").show();
					$this.prev("a").children("img").attr("src", defaults.toggled);
					return false;
				}, function(){
					$this.nextAll("ul").hide();
					$this.prev("a").children("img").attr("src", defaults.toggle);
					return false;
				});	
			}
	});
	
             if ( defaults.highlight_selected_link == 'yes' ) {	
            
                var page_name_string = location.pathname.toString();
               
                var page_name_array = page_name_string.split('/');
                var page_name = page_name_array[page_name_array.length - 1];	
                /*-- kludge for ie6 --*/	
                        /*if( jQuery.browser.msie && (jQuery.browser.version < 7) ) 
                         {
                            var page_name_array = page_name_string.split('\\');
                            var page_name = page_name_array[page_name_array.length - 1];
                            }*/
            	
            	if (page_name) {            	 
            		$('#sidebarWrapper a[href="' + page_name + '"]').addClass('menu_selected');	
            		
            	}
            	$("#sidebarWrapper li ul li a.menu_selected").parent("li").parent("ul").parent("li").children("h3").children().eq(0).addClass("menu_selected");
            	$("#sidebarWrapper li ul li ul li a.menu_selected").parent("li").parent("ul").parent("li").parent("ul").parent("li").children("h3").children().eq(0).addClass("menu_selected");
            	$("#sidebarWrapper li h3:has('a.menu_selected')").each(function(){   
            		var $this = $(this);						
            		var toggler_src = $this.prev("a").children("img").attr("src");	
            		$this.nextAll("ul").slideDown("fast");
            		$this.prev("a").children("img").attr("src", defaults.toggled);
            		return false;
            		});
            	}
            	
                /*-- Add selected class to selected parent li's --*/	           
                $("#sidebarWrapper li:has(a.menu_selected)").addClass('menu_selected_holder');
                $("#sidebarWrapper li ul li:has(a.menu_selected)").removeClass('menu_selected_holder');

}
//end of closure
})(jQuery);
