/**
 * MENU Interaction Functions
 * Avery Brooks / 2007
 */

/*
function menu_init() {
	menu_scale();
	curObj = document.getElementById('menu');
	// tweenObjects['menu'] = new Tween(curObj.style,'left',Tween.backEaseOut,parseInt(content_div.style.left),10,1,'px');
}

function menu_scale() {
	pageSize = getPageSize();
	$('#menu').height(pageSize[1]);
}

function hideMenu() {
	deb("Hide menu");
	tweenObjects['menu'].continueTo((-1 * $('#menu').width()),2);
	tweenObjects['menu'].onMotionFinished() = function() {}
}

function showMenu() {
	deb("Show menu");
	tweenObjects['menu'].continueTo(168,2);
}

function toggleMenu() {
	// can do this based on x position ... need to build it
	deb("Toggle");
	hideMenu();
}
*/

/**
 * Process Menu -- Give it an object containing UL nested menu
 */
jQuery.fn.processMenuItem = function(level) {

	if (!level) level = 1;

	var $menu = $(this).children();

	$menu.each(function(){

		if ($(this).is("li")) {
			deb ("LI!");
		} else if ($(this).is("ul")) {
			if (level > 1) {
				$(this).css("display","none");
			}
			deb ("UL!");
		} else if ($(this).is("a")) {
			deb("A");
			if ($(this).next().is("ul")) {
				// $(this).append(' <span class="DirClosed"><img src="/Presentation/Client/images/icons/client_more.gif" alt="+" /></span>');
				// $(this).append(' <span class="DirOpened"><img src="/Presentation/Client/images/icons/client_less.gif" alt="-" /></span>');

				$(this).append(' <span class="DirClosed"><img src="/Presentation/Client/images/icons/folder_closed.gif" alt="+" /></span>');
				$(this).append(' <span class="DirOpened"><img src="/Presentation/Client/images/icons/folder_open.gif" alt="-" /></span>');

				$(this).children(".DirOpened").css("display","none");
				$(this).bind("click",$(this).next(),function(e){
					$(e.data).slideToggle();
					$(e.data).prev().children(".DirClosed").toggle();
					$(e.data).prev().children(".DirOpened").toggle();
				});
				deb("found UL next!");
			} else {
				// Hey!  Here you are buddy
			}
		} else {
			deb ("U can't touch this.");
		}
		$(this).processMenuItem(level++);
	});

}


/**
 * Open the Menu to a specific Item (and indicate selection with some CSS)
 */
jQuery.fn.openToItem = function(menuContainerID) {

	if ($(this).is("A")) {
		$(this) = $(this).parent();		
	}

	$(this).children("a").css("color","#f60");

	var curObj = $(this).parent();

	while (curObj != null && ($(curObj).is("UL") || (curObj).is("LI"))) {
		
		$(curObj).prev().children(".DirClosed").toggle();
		$(curObj).prev().children(".DirOpened").toggle();
				
		curObj.show();
		curObj = $(curObj).parent();
	}
}

