(function($) {
/* sidebar_toggle adapted from menu_toggle_adder */
$.fn.menu_toggle = function(options){
var defaults = {	
animate_toggle: 'yes',
highlight_selected_link: 'no',
toggle: '/pix/common/arrows_small_right.gif',
toggled: '/pix/common/arrows_small_down.gif',
toggle_selected: '/pix/common/arrows_small_right.gif',
toggled_selected: '/pix/common/arrows_small_down.gif'	
};
 // $('#sidebarWrapper li ul.sbSubCat').css('display','none');
 // $('#sidebarWrapper li img.toggler').css('display','inline');
var opts = $.extend(defaults, options);
var sidebarItems = document.getElementById('sidebarWrapper');
if(!sidebarItems) return;
	/*-- if a h3 has no a tag, add empty a tag for accessibility. --*/	
sidebarItems = $(sidebarItems.getElementsByTagName('li'));
	sidebarItems.find("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' --*/	
	sidebarItems.find("a:has('img.toggler')").each(function(){
	              var $this = $(this);
	              var $section;
	              var toggler_parent = $this.parent('div').attr('class');
	              if (toggler_parent == 'titleStyle'){
	              $section = $this.parent('div.titleStyle');
	              defaults.toggle= '/pix/common/arrow_big_right.png';
                             defaults.toggled= '/pix/common/arrow_big_down.png';
                             defaults.toggle_selected= '/pix/common/arrow_big_right.png';
                             defaults.toggled_selected= '/pix/common/arrow_big_down.png';	
	              } else {
	               $section = $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(){
				$section.nextAll("ul").slideUp("fast");
				$section.nextAll("a.facetReset").slideUp("fast");
				$this.children("img.toggler").eq("0").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$section.nextAll("ul").slideDown("fast");
				$section.nextAll("a.facetReset").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(){
					$section.nextAll("ul").slideDown("fast");	
					$section.nextAll("a.facetReset").slideDown("fast");
					$this.children("img.toggler").eq("0").attr("src", defaults.toggled);
					return false;
				}, function(){
					$section.nextAll("ul").slideUp("fast");
					$section.nextAll("a.facetReset").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(){
				$section.nextAll("ul").hide();
				$section.nextAll("a.facetReset").hide();
				$this.children("img.toggler").eq("0").attr("src", defaults.toggle_selected);
				return false;
			}, function(){
				$section.nextAll("ul").show();
				$section.nextAll("a.facetReset").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(){
					$section.nextAll("ul").show();
					$section.nextAll("a.facetReset").show();
					$this.children("img.toggler").eq("0").attr("src", defaults.toggled);
					return false;
				}, function(){
					$section.nextAll("ul").hide();
					$section.nextAll("a.facetReset").hide();
					$this.children("img.toggler").eq("0").attr("src", defaults.toggle);
					return false;
				});	
			}
	});
	sidebarItems.find("h3:has('a.linkStyle2'), 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 --*/	           
                sidebarItems.filter(":has(h3.menu_selected)").addClass('menu_selected_holder');
                sidebarItems.find("ul li:has(h3.menu_selected)").removeClass('menu_selected_holder');
}
})(jQuery);

