/*******************************************************************************************

	Tiered Tree Menu
	
	Author: Asa Sherrill <asa.sherrill@definition6.com>
	Company: Definition 6
	Date: July 19, 2007

	Required Structure:
	
	1. Wrap the menu with a div, give the div an ID name
	2. Each LI must be of the following structures
	
		a. No Children
		<li><img src="#"><a href="#">Node Label</a></li>
		
		b. Children
		<li><img src="#"><a href="#">
			<ul>
			</ul>
		</li>

	
	Required Setup:
	
	1. For each tier, declareTier(). The first call will indicate the first tier;
	the Nth call will indicate the Nth tier. 
	
	2. Following both the html structure as well as the declareTier() calls, call
	the populate method with the id of the div wrapper as its only parameter.
	
	
*********************************************************************************************/
var arr_ClosedImages = new Array();
var arr_OpenedImages = new Array();
var arr_StaticImages = new Array();
var str_ImgPath = "images/";
var str_TopBorderClass = "LI_Top";
var str_BottomBorderClass = "LI_Bottom";
var str_OneLIClass = "LI_Single";
var str_SelectorClass = "SelectedItem";
var obj_SelectedLI = "none";

function declareImagePath ( newPath ) {
// default is images/	
	str_ImgPath = newPath;
}

function declareTier ( closedImageSrc, openImageSrc, staticImageSrc ) {
/* PRE: For each tier of the menu, this method will need to be called. 
 * 		
 *		closedImage: If the tier has a child - this image is used when the child is closed
 *		openImage: when the child is open
 *		staticImage: when there is no child
 */ 
 
	arr_ClosedImages[arr_ClosedImages.length] = str_ImgPath + closedImageSrc;
	arr_OpenedImages[arr_OpenedImages.length] = str_ImgPath + openImageSrc;
	arr_StaticImages[arr_StaticImages.length] = str_ImgPath + staticImageSrc;
	
}

function declareSelectorId ( idname ) {
// 	PRE: the id applied to the LI of the selected element
//  POST: the program will leave the selected visible, as well as its direct child (if available)
	
	str_SelectorId = idname;	
	
}

function declareBorderClasses ( topBorder, bottomBorder, oneLI ) {
// PRE: two classnames
// POST: topBorder - applied to the first LI in each UL, bottomBorder - applied to the last
	
	str_TopBorderClass = topBorder;
	str_BottomBorderClass = bottomBorder;
	str_OneLIClass = oneLI;
		
}


function populate( parent_div ) {
// PRE: the id of the div containing the tier Unordered List
// POST: scans for the first UL and begins spidering via the Visit() function.
	

	var mainObj = document.getElementById ( parent_div );
	var counter = 0;	
	while ( mainObj.childNodes[counter].tagName != "UL" ) counter ++;
	visit ( mainObj.childNodes[counter], 0 );
	
	if ( obj_SelectedLI.tagName == "LI" ) {
		openTo ( obj_SelectedLI );	
	}

		
}

function openTo ( selectedLI ) {
// PRE:	the object LI to open to
// POST: recurses UP through parent UL's, opening each.

	if ( selectedLI.tagName != "DIV" && selectedLI.childNodes.length > 0) {
		// Get the LI's Image Tag 
		
		var counter = 0;
		while ( selectedLI.childNodes[counter].tagName != "IMG" ) counter ++;
		var imgObj = selectedLI.childNodes[counter];
		
	
			
		if ( imgObj.value != "N" ) {
			imgObj.src =  arr_OpenedImages[ imgObj.tier ];
			imgObj.value = "O";
		}
		
		if ( selectedLI.parentNode.tagName == "UL" ) {
			selectedLI.parentNode.style.display = "Block";
			openTo ( selectedLI.parentNode.parentNode );
		}
	}
}

function visit ( headUL, tier ) {
// PRE: a UL node that should be spidered - UL nodes within LI 
//      nodes beneath this primary node will recurse back into this function.
//		Additionally, the function requires a Tier which indicates which images to
//		use. (Tier images should be set up before hand).
// POST: Prepares the menu's imagery as well as behavior 
	
	var firstLI = -1;
	var lastLI = -1;
	
	for ( var x = 0; x < headUL.childNodes.length; x ++ ) {
		if ( headUL.childNodes[x].tagName == "LI" ) {
			
			if ( firstLI < 0 ) { firstLI = x; }
			lastLI = x;
			
			var imgObj;
			var parent = false;
			var bln_IsSelected = false;
		
	
			for ( var q = 0; q < headUL.childNodes[x].childNodes.length; q ++ ) {
				
				if ( headUL.childNodes[x].childNodes[q].tagName == "A" ) {
					
					//alert ( "Checking Href:" + window.location.href + " VS. " + headUL.childNodes[x].childNodes[q].href);
					var domain = 'http://' + window.location.host;
					var aLink = headUL.childNodes[x].childNodes[q].href;
					var dLink = jNavPageNameRef;

					aLink = aLink.substr(domain.length, aLink.length-domain.length);
					
					if ( aLink.indexOf(dLink)>-1 ) { 
						obj_SelectedLI = headUL.childNodes[x];
						bln_IsSelected = true;
						headUL.childNodes[x].childNodes[q].className = str_SelectorClass;
					}
					
				
				} else if ( headUL.childNodes[x].childNodes[q].tagName == "IMG" ) {
				// Capture the Img Tag.. it will be modified later depending on whether or not there is a child list
					
					imgObj = headUL.childNodes[x].childNodes[q];
					
				
				} else if ( headUL.childNodes[x].childNodes[q].tagName == "UL" ) {
				// This implies that the node has a child - it should be labelled as such	
				// and this node should be passed back into visit with the tier incremented by 1	
					
					if ( bln_IsSelected ) {
						headUL.childNodes[x].childNodes[q].style.display="block";
					} else { 
						headUL.childNodes[x].childNodes[q].style.display="none";
					}
					
					if ( tier == arr_ClosedImages.length - 1 ) {
						visit ( headUL.childNodes[x].childNodes[q], tier ); //recurse
					} else {
						visit ( headUL.childNodes[x].childNodes[q], tier + 1 ); //recurse
					}
					
					parent = true; // label this as a parent node
					
				}
			}
			
			
			if ( parent ) {
			// Indicated previously that this element has a child menu, set the image, 
			// value, tier, and behavior accordingly
			/*
				value: O (Open), C (Closed), N (No Child)
			*/
			
				if ( bln_IsSelected ) {
					
					imgObj.value = "O";	
					imgObj.src = arr_OpenedImages[tier];
					
				} else { 
				
					imgObj.value = "C";
					imgObj.src = arr_ClosedImages[tier];
					
				}
				
				imgObj.tier = tier
				imgObj.onclick = tier_clicked;
				
			} else {
			// No child, requires only an image and indication that it is not clickable 
			
				imgObj.src = arr_StaticImages[tier];
				imgObj.value = "N";
				imgObj.style.cursor = "auto";
				imgObj.tier = tier;
			}	
		}
	}
	
	if ( firstLI != lastLI ) {
		
		headUL.childNodes[firstLI].className = str_TopBorderClass;
 		headUL.childNodes[lastLI].className = str_BottomBorderClass;
		
	} else { 
	
		headUL.childNodes[firstLI].className = str_OneLIClass;
	
	}
	
	
}

function breakOutContentPage ( hrefString ) {

	var tmpArr = hrefString.split("#");
	var tmpPages = tmpArr[1].split("$");

	return tmpPages[1];
}


function tier_clicked () {
// PRE: an img is clicked that is part of an LI that has a child UL
// POST: the child UL is toggled open or closed depending on its current state

	// Scale across siblings until the UL is found, set its display value accordingly
	var parentObj = this.parentNode;
	var counter = 0;
	
	while ( parentObj.childNodes[counter].tagName != "UL" ) counter ++;

	if ( this.value == "C" ) {
	// The element is closed - open it
	
		openElement ( parentObj.childNodes[counter] );
		this.src =  arr_OpenedImages[ this.tier ];
		this.value = "O";
		
	} else if ( this.value == "O" ) {
	// The element is open - close it
	
		closeElement ( parentObj.childNodes[counter] );
		this.src =  arr_ClosedImages[ this.tier ];
		this.value = "C";
		
	}
}



function openElement ( elementObj ) {
// PRE: any block level element
// POST: the element is opened using the prescribed algorithm below

	elementObj.style.display="block";
	//new Effect.BlindDown( elementObj, {duration:.7} );
}


function closeElement ( elementObj ) {
// PRE: any block level element
// POST: the element is closed using the prescribed algorithm below	
	
	elementObj.style.display="none";	
	//new Effect.BlindUp( elementObj, {duration:.7} );
	
}

// TTT 5/21/02
// MB 3/20/03 - edited to use jLanguage
function popwindow(url, sizex, sizey)
{
	win1=window.open('','client','width='+sizex+',height='+sizey+',location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,screenX=1,screenY=1,top=50,left=50');
	win1.location.href = url + '&llang='+jLanguage;
	win1.focus();
}

function popFlash(url, sizex, sizey, ver, bg)
{
	var winX = sizex + 0;
	var winY = sizey + 35;
	win1=window.open('','client','width='+winX+',height='+winY+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,screenX=1,screenY=1,top=50,left=50');
	win1.location.href = '/common/display/popframes.asp?swf=' + url + '&llang='+jLanguage + '&fX=' + sizex + '&fY=' + sizey + '&ver=' + ver + '&bg=' + bg;
	win1.focus();
}