/*
 * nav.js
 * 2009-01-14
 * Brown University
 * @author John Pennypacker <John_Pennypacker@brown.edu>
 */


$(document).ready(function(){
	enhanceNav(".navigation");
	$('.search_input').css({'height': 16});
	$('.search_input').css({'paddingTop': 5});
	$('.search_input').css({'paddingBottom': 5});
});


/*
 * Adds a toggle icon to navigation that has subnavigation.
 *
 * @param	string		selector the CSS selector of the navigation list
 */
function enhanceNav(selector) {
	// add icons to nav
	//$(selector+" :has(ul:not('.current'))").prepend(makeIcon('closed'));
	$(selector+" :has(ul.current)").prepend(makeIcon('open'));

	// add eventListeners to icons
	$(selector+" li:has(ul) .parentlist").bind("click", function() {toggleSubList($(this));});
}

/*
 * Toggle navigation sub menus
 *
 * @param	element		el the jQuery object of the element that starts the toggle action
 */
function toggleSubList(el) {

	if($(el).parent("li:has(ul)").children("ul").eq(0).is(":hidden")) {
		$(el).parent("li:has(ul)").children("ul").eq(0).slideDown("fast");
		$(el).attr({src: iconOpen});
	} else {
		$(el).parent("li:has(ul)").children("ul").eq(0).slideUp("fast");
		$(el).attr({src: iconClosed});
	}
}

/*
 * Returns the HTML string that is the toggle icon
 *
 * @param	string		status set status to "open" to display an open icon instead of a closed one
 * @return	string
 */
function makeIcon(status) {
	if(status=="open") {
		src = iconOpen;
	} else {
		src = iconClosed;
	}
	return '<img src="'+ src +'" alt="Toggle sub-items" class="parentlist" />';
}

//show plus and minus icons for the Macintosh-impaired
if(navigator.userAgent.indexOf("Windows") != -1) {
	var iconOpen = basehref +'/img/icon-list-open.png';
	var iconClosed = basehref +'/img/icon-list-closed.png';
} else {
	var iconOpen = basehref +'/img/icon-list-open-arrow.png';
	var iconClosed = basehref +'/img/icon-list-closed-arrow.png';
}