$(function(){
	    	//find the current section for determining which colored arrow to use
	    	var results = window.location.toString().split("/",4);	
	    	var CurrentSection = results[3];
	    	
	    	
	    	//set img path for when submenu is hidden
		    var closedImg = "/_layouts/images/Arrow-Right.png";
		    //set img path for when submenu is visible
		    var openedImg = "/_layouts/images/Arrow-Down.png";
		    //set img path for when submenu is hidden
		    var subClosedImg = "/_layouts/images/" + CurrentSection + "-Arrow-Right.png";
		    //set img path for when submenu is visible
		    var subOpenedImg = "/_layouts/images/" + CurrentSection + "-Arrow-Down.png";

		    //expand the menu so it is wide enough
		    $("div.leftContainer").parent("td").attr("style","padding-left:50px;padding-top:12px;width:200px");
			//initialize menus
		    var menuRows = $("[id$='CurrentNav'] > tbody > tr");
		    
		    var menuHd = menuRows.filter("[id!='']:has(+tr[id=''])"); //locate all <tr> that contain an ID and have an adjacent <tr> without an ID
			var menuHdNoDrops = menuRows.filter("[id!='']"); //locate all <tr> elements that are main level but have no sub menues
	    	
			// go through the rows and identify additional sub menu levels
			menuRows.not(menuHd).each(function() {
				// each menuRow may contain an entire level 2 section
				// therefore you can search within the menuRows
				var subMenuRows = $(this).find("table > tbody > tr");
			    var subMenuHd = subMenuRows.filter("[id!='']:has(+tr[id=''])");  //locate all <tr> that contain an ID and have an adjacent <tr> without an ID		
			    
				//create the CSS style for the Level 1 items	
    
			    var subCssInit = {
			        "background-repeat" : "no-repeat",
			        "background-position": "5px 50%",
			        "padding"  :"5px 5px 5px 18px",
			        "height" : "7px"
			        
			    };
			    var subCssClosed = {"background-image": "url('"+ subClosedImg +"')"};
			    var subCssOpen = {"background-image": "url('"+ subOpenedImg +"')"};
			    
			    //apply initial inline style to menu headers
			    subMenuHd.find("td:last").css(subCssInit);
			    //apply open arrow if the hd is the active link
			    subMenuHd.find(":not(:has(a.leftNavSelected))").find("td:last").css(subCssClosed);
			    subMenuHd.has("a.leftNavSelected").find("td:last").css(subCssOpen);
			    //apply the open arrow if the hd's submenu contains the active link
			    subMenuRows.filter("[id!='']:has(+tr[id='']:has(a.leftNavSelected))").find("td:last").css(subCssOpen);
			    subMenuRows.filter("[id!='']:has(+tr[id='']:not(:has(.leftNavSelected)))").find("td:last").css(subCssClosed);

				//add this to apply the correct color scheme to the arrows
				//subMenuHd.find("td:last").addClass("subMenu_" + CurrentSection);

subMenuHd.each(function(){
				
					var Level3Menues = $(this).find("+tr[id='']");
					
					// fantastic, we have all the level 3 menu items!
					// now... do something with them
					
					// wrap the menu in divs for smooth animations
					Level3Menues.children("td").each(function(){
						$(this).wrapInner("<div></div>");
					});
					
					// hide these new divs (if they do not contain the current page selection)
					Level3Menues.filter("[id='']:not(:has(.leftNavSelected)+tr[id=''])").find("td > div:not(:has(a.leftNavSelected))").hide();

					
					// give this header a click event to hide/show the div
					$(this).click(function () {
				    	//grab currently visible submenues
				    	var visibleSubMenues = Level3Menues.find("td > div:visible");
				    	
				    	//apply initial inline style to menu headers to reset the arrow
				    	//subMenuHd.find("td:last").css(subCssInit);
				    
				        var styleElm = $(this).find("td:last")
				        var nextTR = $(this).next("tr[id='']");
				        if (nextTR.children("td").children("div").is(':visible')) {
				            //nextTR.hide();
				            //nextTR.fadeOut("slow");
							nextTR.children("td").children("div").slideUp();
				            styleElm.css(subCssClosed );
				        } else {
				            //nextTR.show();
				            //nextTR.fadeIn('slow');
				            nextTR.children("td").children("div").slideDown();
				            styleElm.css(subCssOpen);
				        }
				        			        
				    });

				});
				
				
			});

			//use this to prevent the animations when clicking a link			
			menuRows.find("a").click(function (event) {
				event.stopImmediatePropagation();
		    });
			
			//create the CSS style for the Level 1 items		    
		    var cssInit = {
		        "background-image" : "none",
		        "background-repeat" : "no-repeat",
		        "background-position" : "5px 50%",
		        "padding"  :"5px 5px 5px 18px"
		        
		    };

		    var cssClosed = {"background-image": "url('"+closedImg+"')"};
		    var cssOpen = {"background-image": "url('"+openedImg+"')"};
		    //put submenu content in DIV tags for smooth animations
		    menuRows.children("td").each(function() {
				$(this).wrapInner("<div></div>");
			});


			//first hide all sub menues that do not contain the active link
		    menuRows.filter("[id='']").not(":has(.leftNavSelected)+tr[id='']").children("td").children("div").not(":has(a.leftNavSelected)").hide();
		    menuRows.filter("[id='']").not(":has(.leftNavSelected)+tr[id='']").not(":has(a.leftNavSelected)").hide();
		    
		    //apply initial inline style to menu headers
		    menuHdNoDrops.find("td:last").css(cssInit);
		    menuHd.find("td:last").css(cssInit);
		    //apply open arrow if the hd is the active link
		    menuHd.find(":not(:has(a.leftNavSelected))").find("td:last").css(cssClosed);
		    menuHd.has("a.leftNavSelected").find("td:last").css(cssOpen);
		    //apply the open arrow if the hd's submenu contains the active link
		    menuRows.filter("[id!='']:has(+tr[id='']:has(a.leftNavSelected))").find("td:last").css(cssOpen);
		    menuRows.filter("[id!='']:has(+tr[id='']:not(:has(.leftNavSelected)))").find("td:last").css(cssClosed);

		    //make the leftNav1's background color transparent to allow the dynamic color to show through
		    menuHd.find("td:last").parents(".leftNav1").css("background-color", "transparent");
		    menuHdNoDrops.find("td:last").parents(".leftNav1").css("background-color", "transparent");
		    //dynamically set the parent div to the theme color
		    menuHd.find("td:last").parents(".leftNav1").parent("div").addClass("siteBackColorDark");
		    menuHdNoDrops.find("td:last").parents(".leftNav1").parent("div").addClass("siteBackColorDark");
		    
		    //create the click event for the accordians
		    menuHd.click(function () {
		    	//grab currently visible submenues
		    	var visibleMenues = menuRows.filter("[id='']").children("td").children("div:visible");
		    	
		        var styleElm = $(this).find("td:last")
		        var nextTR = $(this).next("tr[id='']");
		        if (nextTR.children("td").children("div").is(':visible')) {
		            //nextTR.hide();
		            //nextTR.fadeOut("slow");
					nextTR.children("td").children("div").slideUp(400, function(){nextTR.hide();});
		            styleElm.css(cssClosed);
		        } else {
		            nextTR.show();
		            //nextTR.fadeIn('slow');
		            nextTR.children("td").children("div").slideDown();
		            styleElm.css(cssOpen);
		        }
		        	        
		    });
		    
		    //locate menu items that have visible adjacent sub menues and change their arrow to reflect the open state
			menuHd.find("tr:has(+tr[id='']:visible)").find("td:last").css(cssOpen);
			
			//also adjust active header arrows
			menuHd.find("td:has(a.leftNavSelected)").find("td:last").css(cssOpen);

		  

		});
		
