$(document).ready(function(){
	fixMenuHeights();
	setupSidebar();
});

function fixMenuHeights(){
	var x = $('nav > ul > li > a').each(function(){
        var span = $(this).children('span');
        if (span.height() > 20 ){
            span.css('marginTop', 3);
        }else{
            span.css('marginTop', 13);
        }
    });
	
	if ($('.edit-buttons').size() > 0){
		$('nav').css({'margin-bottom':'35px'});
	}
}

function setupSidebar(){
	$('#SubNav ul').siblings('a').hide(); 				// hide anchors that are siblings to ul's (parent page)
	var selected = $('#SubNav li.selected');  			// find selected item
	if (selected.length == 0){
		$('#SubNav > ul > li').hide();
		$('#SubNav > ul').css('min-height',0);
	}else{
		selected.siblings('li').hide();  // hide siblings of selected item
		selected.parents('li').siblings('li').hide();  // hide all siblings of all parent LIs
		//selected.show().parents('ul').children('li > a').hide(); // hide all main anchors of ULs
		$('ul > li > ul',selected).hide();  // hide nested children
		$('ul a',selected).show();  // show anchor of selected
		if ($('ul > li',selected).length == 0 && selected.parents('ul').length > 1 ){
			selected.siblings('li').show().children('a').show().add('a',selected).css('padding-left','10px');
			selected.siblings('li').children('ul').hide();
		}else if ($('ul > li',selected).length == 0){
			//selected.hide();
		}
		$(selected).show().children('a').show();
		var parent = $(selected).parents('li:eq(0)');
		
		if (parent.length > 0){
		  parent.children('a').show(); // show parent
		} //else{  alert('siblings!');  selected.siblings('li').css('padding-left','33px');}

		$("ul > li > a",selected).css('padding-left','10px'); // indent children
		/*
		var directions = $('#SubNav .menu17 .iFOL125');
		if (directions.length > 0){
			$('#SubNav .iFOL16 > a').show();
			$('#SubNav .iFOL16 ul').hide();
		}
		
		var contact = $('#SubNav .menu17 .iFOL16.selected');
		
		if (contact.length > 0){
			contact.children('a').hide();
			$('ul', contact).show();
		}
		*/
	}
}
