// DESCRIPTION:
//	Dynamic navigation bar for left side of Med Center Templates.
//	Each site requires a leftnav_content.js file specifying the menu items.
// Update: 4/6/2006 Huming Tang
//			Created the third lvl parsing schema.
//			this class should be rewritten using a recursive data structure so the code can be cleaner and easier to 
//			maintain.
// AUTHORS: 
//	Tyler Bittner 4/2004
//	Joanna Honikman 2/2004

var txt = "";
var isSelected=false;
var newWin = "0";

function leftnavBuild( leftnav ) {

	// Get current URL
	var path = window.location.pathname;

	// Ignore to the right of a "#" sign in the URL
	var pathArray = path.split("#");
	path = pathArray[0];
	
	// Ignore the filename in the URL
	var pathDir = path.replace(/(^\/.*\/)(.*$)/, '$1');
	
	//
	// Detect which items/levels to show
	//

	// Process level 0
	for ( var i=0; i < leftnav.length; i++ ) {
		if ( leftnav[i] != null ) {

			if ( leftnav[i].url.valueOf() == pathDir.valueOf() ) {
					
				
				leftnav[i].viz = 1;
				
				// If it has children, set flag to display all one level down.
				if ( leftnav[i][0] ) leftnav[i].isContainer = true;
				// TODO: Else if this is an end-node, display all its peers
				
			}else {
				leftnav[i].viz = 0;
			}
			
		}

//document.write('url: ' + leftnav[i][j].url.valueOf() + ' <br>path:' + path.valueOf() );

		// Process level 1
		for ( var j=0; leftnav[i][j] != null; j++ ) {
			
			leftnav[i][j].secfoc = 0;
			if ( leftnav[i].isContainer ) {
				leftnav[i][j].viz = 1;
			}else if ( leftnav[i][j].url.valueOf() == pathDir.valueOf() ) {
							   
				leftnav[i][j].viz = 1;
				leftnav[i].viz = 1;
				// If it has children, set flag to display all one level down.
				if ( leftnav[i][j][0] ) {
					leftnav[i][j].isContainer = true;
				}
				// Set viz=2 if focus is on this page
				
				
				if ( leftnav[i][j].url.valueOf() == path.valueOf() ) {
					leftnav[i][j].viz = 2;
					leftnav[i][j].secfoc = 1;
				}
				// Display all level 1 under this level 0.
				for ( var x=0; leftnav[i][x] != null; x++) {
					// Don't modify this level 1
					if (x == j) continue;
					leftnav[i][x].viz = 1;
					
				}
				
			}else {
				if (! leftnav[i][j].viz ) {
					leftnav[i][j].viz = 0;
					leftnav[i][j].secfoc = 0;
				}
			}
//document.write('url: ' + leftnav[i][j].url.valueOf() + ' <br>path:' + path.valueOf() + '<br>' );

			if ( leftnav[i][j].url.valueOf() == path.valueOf() ) {
					leftnav[i][j].viz = 2;
					leftnav[i][j].secfoc = 1;
			}
			// Process level 2
			isSelected=false;
			for ( var k=0; leftnav[i][j][k] != null; k++ ) {
				// If it has children, set flag to display all one level down.
				
				if ( leftnav[i][j].isContainer ) {
						leftnav[i][j][k].viz = 1;
				}else if ( leftnav[i][j][k].url.valueOf() == pathDir.valueOf()  ) {
					leftnav[i][j][k].viz = 1;
					leftnav[i][j].secfoc = 1;
					//due to folder issue, folder match should select sibling too
					isSelected=true;							

				}else {
					if (leftnav[i][j][k].viz!=1)
						leftnav[i][j][k].viz = 0;
				}

				// Set viz=2 if focus is on this page
				if (  leftnav[i][j][k].url.valueOf() == path.valueOf() )
				{
					leftnav[i][j][k].viz = 2;
					leftnav[i][j].secfoc = 1;
					isSelected=true;
					if ( leftnav[i][j][k][0] ) {
						leftnav[i][j][k].isContainer = true;
					}
				}
				
				if (isSelected)
				{
					
					selectSiblings(leftnav[i][j],true);
					selectSiblings(leftnav[i],false);
				}
				
				//process level 3
				//here the logic is, if one item is visible, all of its sibling should be visible regardless
				//base on their array location
				isSelected=false;
				for ( var l=0; leftnav[i][j][k][l] != null; l++ ) {
					
					if ( leftnav[i][j][k].isContainer ) {
							leftnav[i][j][k][l].viz = 1;
					}else if ( leftnav[i][j][k][l].url.valueOf() == pathDir.valueOf()  ) {
						leftnav[i][j][k][l].viz = 2;
						leftnav[i][j][k].secfoc = 1;
						//due to folder issue, folder match should select sibling too
						isSelected=true;
						// If it has children, set flag to display all one level down.
						if ( leftnav[i][j][k][l][0] ) {
							leftnav[i][j][k][l].isContainer = true;
						}
						
					}else {
						if (leftnav[i][j][k][l].viz!=1)
							leftnav[i][j][k][l].viz = 0;
					}
	
					// Set viz=2 if focus is on this page
					if (  leftnav[i][j][k][l].url.valueOf() == path.valueOf() ) {
						leftnav[i][j][k][l].viz = 2;
						leftnav[i][j].secfoc = 1;
						isSelected=true;
						
					}
									
				}
				if (isSelected)
				{
					selectSiblings(leftnav[i][j][k],true);
					selectSiblings(leftnav[i][j],true);
					selectSiblings(leftnav[i],false);
				}
				// End 3

			}  // End 2
		}  // End 1
	}  // End 0

	//
	// Print all the items
	//
	for ( var i=0; i < leftnav.length; i++ ) {
		// ** Don't print level 0
		newWin=newWinCheck(leftnav[i]);
		leftnavDisplay( 0, leftnav[i].text, leftnav[i].url, 0,null,null,newWin );

		var j=0;
		while ( leftnav[i][j] != null ) {
		newWin=newWinCheck(leftnav[i][j]);
		leftnavDisplay( 1, leftnav[i][j].text, leftnav[i][j].url, leftnav[i][j].viz,leftnav[i][j].secfoc,null,newWin);

			var k=0;
			while ( leftnav[i][j][k] != null ) {
				newWin=newWinCheck(leftnav[i][j][k]);
				leftnavDisplay( 2, leftnav[i][j][k].text, leftnav[i][j][k].url, leftnav[i][j][k].viz, leftnav[i][j].secfoc,null,newWin);
					var l=0;
					while (( leftnav[i][j][k][l] != null ))
					{
						newWin=newWinCheck(leftnav[i][j][k][l]);
						leftnavDisplay( 3, leftnav[i][j][k][l].text, leftnav[i][j][k][l].url, leftnav[i][j][k][l].viz, leftnav[i][j][k].secfoc,null,newWin);
						l++;
					}
				k++;
			}  // End 2
			j++;
		}  // End 1
	}  // End 0
}

function leftnavDisplay(level, displayText, turl, viz, sectionFocus, onclText, newWin) {

/*
 Values to control visibility of left nav menu items (var viz):
 0 = hide
 1 = show, no focus
 2 = show, with focus (highlighted)
*/

var target="";
if (newWin=="1")
{
	target='target="_blank"';
}

if ( (level == 0) || (level == 1) ) {
	
	if ( viz == 0 ) {
		txt = "";
	}else {
		
		var focusClassStart = '';
		var focusClassEnd = '';
		if (sectionFocus == 1) {
			outerBoxClass = 'onfocus_box';
		}else {
			outerBoxClass = 'offfocus_box';
		}
		if ( viz == 2 ) {
			focusClassStart = '<div class="navleft' + level  + 'focus">';
			focusClassEnd = '</div>';
		}

		txt = '<br><div class="' + outerBoxClass + '"> '  + focusClassStart + '<a ' + target + ' href="'
		+ turl + '" '  + '>' + displayText 
		+ '</a>' + focusClassEnd + '</div>';
	}

} else if (level == 2) {
	
	if (sectionFocus == 1 ) {
			outerBoxId = 'onfocus_box';
	}else {
			outerBoxId = 'offfocus_box';
	}
	if ( viz == 0 ) {
		
		txt = "";
	}else {
		
		var level2indent = '<div class="navleft2inner">';
		var focusClassStart = '';
		var focusClassEnd = '';
		if ( viz == 2 ) {
			focusClassStart = '<div class="navleft' + level  + 'focus">';
			focusClassEnd = '</div>';
		}
		
		txt = '<div class="' + outerBoxId + '"> ' + level2indent + focusClassStart + '<a  ' + target + '  href="' + turl + '"> </a> <a ' + target + '  href="'  + turl + '">'
	    + displayText  + '</a></div>' + focusClassEnd + '</div>';
	}

} //else if (level > 2) { 
	//txt = 'JavaScript Error [Left Nav]: Levels above 3 deep are not currently supported';
//}
else { 
	//txt = 'JavaScript Error [Left Nav]: Unspecified menu level';
	if (sectionFocus == 1 ) {
			outerBoxId = 'onfocus_box';
	}else {
			outerBoxId = 'offfocus_box';
	}
	if ( viz == 0 ) {
		txt = "";
	}else {
		var level2indent = '<div class="navleft2inner">';
		var focusClassStart = '';
		var focusClassEnd = '';
		if ( viz == 2 ) {
			focusClassStart = '<div class="navleft2focus">';
			focusClassEnd = '</div>';
		}
		var spaces=11;
		for (var i=1;i<level;i++)
		{
			spaces-=1;
		}
		
		txt = '<div class="' + outerBoxId + '" > ' +  level2indent + level2indent + focusClassStart +  '<a  ' + target + ' href="' + turl + '"> </a> <a  ' + target + '  href="'  + turl + '">'
	    + displayText  + '</a></div>' + focusClassEnd + '</div>'+'</div>';
	}
}

document.write(txt);

return ""
}	// End function leftnavDisplay()

function createMenuTop() {
	document.write('')
	return ""
}

function createMenuEnd() {
	document.write('')
	return ""
}


//use this helper function to select all siblings in an object array tree
function selectSiblings(ObjectArray,Sfocus)
{
	for ( var l=0;ObjectArray[l]!=null; l++ ) 
	{
		
		if (ObjectArray[l].viz!=2)
			ObjectArray[l].viz=1;
		if (Sfocus)
			ObjectArray[l].secfoc=1;
	}
}


//helper function to find out whether the path is a 
//initialize arrays for open/close settings
//default is 1 (show menu item)

var menuarray = new Array(5)
for ( var i=0; i < menuarray.length; i++ ) {
	menuarray[i] = new Array(5);
	for ( var j=0; j < menuarray[i].length; j++ ) {	
		menuarray[i][j] = new Array(5);
		for ( var k=0; k < menuarray[i][j].length; k++ ) {
			menuarray[i][j][k] = new Array(5);
		}
	}
}

/**
*	This function check whether the item object should be displayed in a new window, if it should,
*	return 1 else return 0;
*
*/
function newWinCheck(object)
{
	if (object.newWindow)
	{
		if (object.newWindow=="1")
		{
			return "1";
		}
	}
	return "0";
}