(function($) { //create closure
/* sidebar_toggle adapted from menu_toggle_adder */
$.fn.menu_toggle = function(options){
var defaults = {	
animate_toggle: 'yes',
highlight_selected_link: 'no',
toggle: '/pix/common/toggle.gif',
toggled: '/pix/common/toggled.gif',
toggle_selected: '/pix/common/toggle.gif',
toggled_selected: '/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;
				});	
			}
	});
	/*-- Add selected class to selected parent li's --*/	           
                $("#sidebarWrapper li:has(h3.menu_selected)").addClass('menu_selected_holder');
                $("#sidebarWrapper li ul li:has(h3.menu_selected)").removeClass('menu_selected_holder');
}
//end of closure
})(jQuery);