﻿//Accordion navigation; modifies styles applied by spnavigationmenu2010 control
(function($){

	$(function(){
	
		//get navigation menu and make dynamic classes static
		var div = $('.spNavigation').children('div.menu.vertical.menu-vertical');
		
		div.find('ul.dynamic').addClass('static').removeClass('dynamic');	
		
		div.find('li.static.dynamic-children').removeClass('dynamic-children');
		div.find('li.dynamic.dynamic-children').addClass('static').removeClass('dynamic dynamic-children');
		div.find('li.dynamic').addClass('static').removeClass('dynamic');
		
		
		div.find('a.static.dynamic-children').removeClass('dynamic-children');
		div.find('a.dynamic.dynamic-children').removeClass('dynamic dynamic-children');	
		div.find('a.dynamic.menu-item').addClass('static').removeClass('dynamic');
		
		//Find selected item by current URL
		var url = document.URL;
		var selectedLI = div.find('a[href="' + url + '"]').parents('li:first');
		if(selectedLI)
		{
			selectedLI.addClass('selected');
			selectedLI.parents('li').addClass('isParent');
		}		
		
		//Show/Hide "Accordion" on anchor tags
	/*	div.find('a').each(function(){
			if(!$(this).attr('href'))
			{
				$(this).on('click', ShowHide);
			}
		});
	*/	
		//add classes for non-selected parents
		var ul = div.find('ul:first');
		var theLIs = ul.children('li');
		theLIs.each(function(){
			if($(this).children('ul').size() > 0)
			{
				$(this).addClass('isParentNotSelected');
			}
		});
		
		//add classes for selected parents; remove click binding for current selected parent (to prevent hiding contents)
		if(selectedLI)
		{
			var topParentLI = selectedLI.parents('li:last');
			topParentLI.addClass('isParentSelected').removeClass('isParentNotSelected isParent');
			topParentLI.find('a').unbind('click');
		}
		
		//Hide all ULs under the top LI elements
		ul.find('li > ul').hide();
		ul.find('li > a').click(ShowHide);

		//Show all parents of the currently selected item (only happens when on a page with the link in the menu)
		div.find('li.selected').parents().show();

		//show all children of selected item
		div.find('li.selected').children().each(function(){
			ShowChildren($(this));
			adjustments();
		});
				
	});
	
	//SHow all children of element, if any; recursive
	function ShowChildren(element)
	{
		$(element).show();
			if($(element).children().size() > 0)
			{	
				$(element).children().each(function(){
				
					ShowChildren($(this));
				});
			}
			else
				return;
	}
	
	//show hide items based on current state
	function ShowHide()
	{
		var topUL = $('ul.root.static');

		var li = $(this).parent('li');
		
		ul = li.find('ul:first');
	
		//hide all UL not current
		topUL.find('ul').not(ul).each(function(){ 
			//if not root LI, hide UL
			if(!$(this).parent('li').hasClass('selected'))
			{ 
				$(this).hide();
			}
		});

		//show or hide
		ul.toggle();

		//Remove all instances of class
		topUL.find('li').each(function(){
			if($(this).hasClass('ParentExpanded'))
				$(this).removeClass('ParentExpanded');
		});	
		
		//show all children
		ul.children().each(function(){
			$(this).show();
			ShowChildren($(this));
		});
		

		//If not parent of currently selected item, but expanding/closing accordion
		if(ul.is(":visible"))
		{
			ul.parents('li:first').addClass('ParentExpanded');
		}
		
		//show all parents of currently selected LI
		topUL.find('li.isParentSelected').find('li.selected').children().parents('ul').show();
	
		adjustments();
	}
	
function adjustments()
{	
	if($('#pwc-stroller-wrapper'))
	{
		var webpart = $('#pwc-stroller-wrapper').parents('.ms-WPBody:first');
		$('#main-col').append(webpart);
	}
	$('#content-area').css('height','auto');
	$('#left-nav').css('height', 'auto');
	$('#right-col').css('height', 'auto');
	
	var cHeight = $('#content-area').outerHeight();
	var lHeight = $('#left-nav').outerHeight();
	var rHeight = $('#right-col').outerHeight();

	//alert("lHeight: " + lHeight + "; cHeight: " + cHeight + "; rHeight: " + rHeight); 
	if(lHeight >= cHeight && lHeight >= rHeight)
	{
		$('#content-area').outerHeight(lHeight );
		$('#left-nav').outerHeight(lHeight);
		$('#right-col').outerHeight(lHeight);
	}
	
	if(cHeight >= lHeight && cHeight >= rHeight)
	{
		$('#content-area').outerHeight(cHeight );
		$('#left-nav').outerHeight(cHeight );
		$('#right-col').outerHeight(cHeight );
		
	}
	
	if(rHeight >= lHeight && rHeight >= cHeight )
	{
		$('#content-area').outerHeight(rHeight );
		$('#left-nav').outerHeight(rHeight );
		$('#right-col').outerHeight(rHeight );

	}
}

})(jQuery);
