// Superfish menu

$(function(){ 

	/* Launch Blank Window */
		$(".target_blank").click(function(){
			var url = $(this).attr("href");
			window.open(url)
			return false
		});
		
	/* Expand the search box */
		$("#keywords").width("115");
	
		$("#keywords").focus(function(){
			$(this).animate({
			    width: '150'
			  }, 500, function() {
			  	// Animation complete.
			  	$(this).closest("form").addClass("focus");
			});
		});
		
		$("#keywords").blur(function(){
			$(this).animate({
			    width: '115'
			  }, 500, function() {
			  	// Animation complete.
			  	$(this).closest("form").removeClass("focus");
			});
		});

	/* Control the main navigation menus */
	    $("nav ul.menu").superfish({
	
			animation:   {'left':'0px',opacity:'show'},  // fade-in and slide-down animation 
			delay:       500,                            // half second delay on mouseout 
			speed:       'fast',                          // faster animation speed 
			onBeforeShow: function(){ this.css('left','20px'); },
			autoArrows:  false                           // disable generation of arrow mark-up 		
	
		}).find('> li > ul').prepend('<span class="top-arrow"></span>');
		
	/* Keep the submenus from extending past the edge of the menu */
		$("nav ul.menu").each(function() {
			var $this = $(this);
			var edge = $(this).width(); // All calcs will be rel to the menu. Its width is our edge.
			$this.children("li").each(function() {
				var $this = $(this);
				var $sub = $this.children("ul");
				var sedge = $sub.width() + $this.position().left; // The submenu edge is the left pos of its container + its width.
				var offset = (sedge > edge) ? sedge - edge + 5 : 0; // only offset the submenu if it is over the edge.
				$sub.css("marginLeft", -offset + "px");
				// offset the arrows position to match...
				var $arrow = $sub.find(".top-arrow");
				var pos = Number($arrow.css("left").replace("px","")) + offset;
				$arrow.css("left", pos+"px");
			});
		});
	
		$('ul.nav > li > a.sf-with-ul').parent('li').addClass('sf-ul'); 
		
	/* Control the hide/show of the sidebar on the subpages */		
		
		$("#side nav > ul li ul:not(:has(li))").remove(); // Quick fix to keep menu items with empty submenues from getting show/hide styling.
		
		sidebar_default = "expanded";
		
		if(sidebar_default == "collapsed")
		{
			$("#side ul ul").hide();
			
			$("#side nav > ul li:has(ul)").each(function(){
				$(this).append("<img src='/assets/images/plus.png' alt='Expand' class='plus-minus' />");
			});
			
		}
		else
		{
			$("#side nav > ul li:has(ul)").each(function(){
				$(this).append("<img src='/assets/images/minus.png' alt='Collpase' class='plus-minus' />");
			});
			
		}
		
		$(".plus-minus").click(function(){
			if($(this).attr("alt") == "Expand")
			{
				$(this).attr("src","/assets/images/minus.png");
				$(this).attr("alt","Collapse");
			}
			else
			{
				$(this).attr("src","/assets/images/plus.png");
				$(this).attr("alt","Expand");	
			}
			
			var parent_li = $(this).parent();
			
			$("ul",parent_li).slideToggle("fast");
			return false;
		});
	
});
