/**
 *	Nested Accordion Menu
 *	Requires jQuery 
 *
 *	PageBuilder Plus System Scripts
 *	Copyright (c) 2011 Virtual Design House (marketingease.com, pagebuilderplus.com)
 **/
$(document).ready(function(){
	
	$('#menu').find('ul').hide();
	
	$('#menu a').each(function(){
		// check if the location.href matches the html of any links
		if (location.href.match($(this).html().replace(/ /g,'-').replace(/&amp;/,'and').toLowerCase())) {
			// if the current link is a directory and has a ul show it
			if ($(this).next()) {
				$(this).next().show();
			}
			// make sure this is not a directory and add the class 'active' to it
			if ($(this).parent().find('ul').length === 0) {
				$(this).addClass('active');
			}
		}
		// add padding-left to each submenu link according to its depth into the menu
		//$(this).css('padding-left', ($(this).parents('ul').size() - 1) * 10); // no left margin for top level menu
		$(this).css('padding-left', $(this).parents('ul').size() * 10);
	});
	
	// add 'submenu' class to all li's links that contain an ul
	$('#menu li:has(ul)').find('a:first').addClass('submenu').css('padding-right', '20px').append('<span></span>');
	$('.submenu span').css({'position':'absolute','top':'50%','right':'10px','margin-top':'-2px','border':'4px solid transparent'});
	
	$('.submenu').click(function(){
		// if submenu is already visible, hide all child submenus within that li
		if ($(this).next().is(':visible')) {
			$(this).parent().find('ul').slideUp();
		} else {
			// if is top level menu, hide all submenu's and then show the current ul
			if ($(this).parent().parent().attr('id') == 'menu') {
				$('#menu ul:visible').slideUp();
				$(this).next().slideDown();
			} else {
				$(this).next().slideDown();
			}
		}
	});
	
});
