


/*=====================================================================
Function for Subnav open / close Animations
=====================================================================*/
Event.observe( window, "load", setupRightNav );
			
function setupRightNav() {
	
	if( !$('subnavwrapper') ) return;
	
	$$('#colRt > #subnavwrapper > ul > li.hasChild > b').zip(
		$$('#colRt > #subnavwrapper > ul > li.hasChild > p') ).each( function( pair ) {
			var b = pair[0];
			var p = pair[1];
			var li = $(b.parentNode)
			
			var div = new Element('div', { 'style': 'overflow: hidden' });
			
			if( !li.hasClassName( 'open' ) ) {
				div.setStyle({ visibility: 'hidden', position: 'absolute' });				
			}
			else
			{
				b.title = "Close";
			}

			p.wrap(div);
			p.setStyle({'display': 'block'});
			
			var calculated_height = div.getHeight();
						
			if( !li.hasClassName( 'open' ) ) {
				div.setStyle({height: '0px', visibility: 'visible', position: 'static'});
			}
			
			div.effect = new Effect.Style( div, 'height', { start: 0, end: calculated_height, start_options: { duration: 1.3, transition: Effect.Transitions.spring },
				rewind_options: { transition:  Effect.Transitions.sinoidal, duration: .3 },
				beforeStart: function() {
					if( div.effect.effect_state == 'from' ) {
						li.removeClassName('open');
						b.title = "More";
					} else {
						li.addClassName('open');
						b.title = "Close";
					}
				} } );
	
			if( li.hasClassName( 'open' ) ) {
				div.effect.finish_effect();
			}
		
			b.observe( 'click', function(e) {
				if( e.element() != this )
					return;
					
				div.effect.toggle_effect()
			});
	});
	
	$('subnavwrapper').observe( 'click', function(e) {
			var a = e.element();
			if( !a.match('a') )
				a = a.up('a');
			if( !a )
				return;
			
			var li = a.up('li');
			var ul = li.up('ul');
			
			ul.select('li.hasChild').each( function(cur_li) {
					if( cur_li == li )
						cur_li.down('div').effect.start_effect();
					else
						cur_li.down('div').effect.rewind_effect();
				});
		});
}

// locates an anchor node by href 
function findAnchorByHref(children, requestUri, showHiddenLinks) {

	for (var i = 0; i < children.length; i++) {
		var e = children[i];
		if (e.nodeName.toUpperCase() == 'A') {
			var href = e.readAttribute('href');
			if (href && href == requestUri) {
				
				// make sure the href is visible. if not, we need to return a visible parent node.
				// the name attribute of the hidden href contains its parent's href.
				if (e.visible() || showHiddenLinks) {
					return e;
				} else {
					var parentHref = e.readAttribute('name');
					return findAnchorByHref(children, parentHref, showHiddenLinks);
				}
			} 
		}
	}
	
	return null;

}

// highlight the currently-selected page in the sub nav widget
function highlightSubNavLink(requestUri) {
	
	var div = $('subnavwrapper');
	if (div) {
		
		var children = div.descendants();
		var e = findAnchorByHref(children, requestUri);
		if (e) {
			e.addClassName('selected');
		}
		
	}
		
}

// auto-expand a parent entry in the subnav, if a child page is being viewed
function autoExpandSubNavParent(requestUri) {

	var parents = $$('li.hasChild');
	for (var i = 0; i < parents.length; i++) {
		var parent = parents[i];
		var e = findAnchorByHref(parent.descendants(), requestUri);
		if (e) {
			parent.addClassName('open');
			break;
		}
	}

}

