var plusImg = new Image();
var minusImg = new Image();
plusImg.src = "/img/icons/tri_solid.gif";
minusImg.src = "/img/icons/tri_solid_down.gif";

var plusImgSm = new Image();
var minusImgSm = new Image();
plusImgSm.src = "/img/icons/down_sm2.gif";
minusImgSm.src = "/img/icons/down_sm.gif";

function swap(item) {    
    for (i = 0; i < item.parentNode.childNodes.length; i++) {        
        if (item.parentNode.childNodes[i].nodeName == "UL") {
            if (item.parentNode.childNodes[i].style.display != "none")
                closeItem(item.parentNode.childNodes[i]);
            else {
                openItem(item.parentNode.childNodes[i]);
            }
        } 
        //alert(item.childNodes[i]);
    }
}

function swapOpen(item) {
	// 10/31/2008 D. Moore
	// I don't know why swap uses a slightly different procedure, but I don't want to change
	// openItem and break anything else. So I am creating a method that does the same
	// process of swap, but only does an open, not a close.
	
    for (i = 0; i < item.parentNode.childNodes.length; i++) {        
        if (item.parentNode.childNodes[i].nodeName == "UL") {
            if (item.parentNode.childNodes[i].style.display != "none") {
                //closeItem(item.parentNode.childNodes[i]);
            } else {
                openItem(item.parentNode.childNodes[i]);
            }
        } 
        //alert(item.childNodes[i]);
    }
}

function closeItem(item) {
	// new Effect.BlindUp(item);
	// new Effect.Fade(item);
    item.style.display = "none"; 
    item.parentNode.className = item.parentNode.className.replace("open", "");   
}

function closeItemQuickly(item) {
    item.style.display = "none"; 
    item.parentNode.className = item.parentNode.className.replace("open", "");   
}

function openItem(item) {
	// new Effect.BlindDown(item);
	// new Effect.Appear(item);
    item.style.display = "block";    
    if (item.parentNode.className.indexOf("open") < 0) {
        item.parentNode.className += " open";
    }
    if (item.parentNode.parentNode.id != "PageNavigation") {
        openItem(item.parentNode.parentNode);
    }
}

function urlWithoutAnchor(str) {
	var ret =  str.indexOf("#") > -1 ? str.substring(0,str.indexOf("#")) : str;
	return ret.toLowerCase();
}
function closeChildren(item) {
    for (var i = 0; i < item.childNodes.length; i++) {
        if (item.childNodes[i].nodeName == "LI") {
            for (var j = 0; j < item.childNodes[i].childNodes.length; j++) {
                if (item.childNodes[i].childNodes[j].nodeName == "UL") {
                    closeItemQuickly(item.childNodes[i].childNodes[j]);
                    if (item.childNodes[i].childNodes[j].childNodes.length > 0)
                        closeChildren(item.childNodes[i].childNodes[j]);
                }
            }
        }
    }
    
}

function openUrl(item, url) {
	var parent;
    for (var i = 0; i < item.childNodes.length; i++) {
        if (item.childNodes[i].nodeName == "LI") {
            for (var j = 0; j < item.childNodes[i].childNodes.length; j++) {
                if (item.childNodes[i].childNodes[j].nodeName == "A") {
                    if (item.childNodes[i].childNodes[j].href.toLowerCase() == url.toLowerCase()) {
                        swapOpen(item.childNodes[i].childNodes[j]);
						parent = item.childNodes[i].childNodes[j];
						while (parent = parent.parentNode) {
							if (parent.nodeName == "UL" && parent.id != "PageNavigation") {
								openItem(parent);
							}
						}
                    } 
                    
                } else if (item.childNodes[i].childNodes[j].nodeName == "UL")
                    openUrl(item.childNodes[i].childNodes[j], url);
            }
        }
    }
}

function openMenu() {    
    menu = document.getElementById('PageNavigation');
    if (menu != null) {
        closeChildren(menu);
        openUrl(menu, urlWithoutAnchor(document.location.href))
    } else {
        // Open any legacy leftnav menus
        openOldMenu();
    }
}

function toggleItem( sToggle, sElem, bLargeImg ) {
    menu = document.getElementById('PageNavigation');
    if (menu != null) {
        var nodeFinder = /\w+?(\d+)/g;
        var newId = sToggle.replace(nodeFinder, "$1_");    
        newId = "navItem" + newId.substring(0, newId.length - 1);
        openItem(document.getElementById(newId));    
    } else {
        toggleOldItem(sToggle, sElem, bLargeImg);
    }
}

/*
 * Legacy collapsenav functions
 */

function openOldMenu() {
	var iMainIndex = 1, iCategoryIndex = 1, iContentIndex = 1;
	var bLinkFound = false;
	var mainLink,catLink,contentLink;
	var sLinks = '';
	
	urlWithoutAnchor(document.location.href)
	
	
	//Main Section Loop
	while (document.getElementById('mainSection' + iMainIndex) && !bLinkFound) {
		iCategoryIndex = 1;
		mainLink = document.getElementById('link' + iMainIndex);
		if (mainLink && mainLink.href.toLowerCase() == urlWithoutAnchor(document.location.href)) {
			bLinkFound = true;
			iContentIndex = -1;
			iCategoryIndex= -1;
		}
			
		while (((catLink = document.getElementById('link' + iMainIndex + '_' + iCategoryIndex)) || 
				document.getElementById('con' + iMainIndex + 'cat' + iCategoryIndex + 'Container')) && !bLinkFound) {
			iContentIndex = 1;
			if (catLink) { 
				sLinks += catLink.href + '\n';
			
			}
			if (catLink && catLink.href.toLowerCase() == urlWithoutAnchor(document.location.href)) {
				bLinkFound = true;
				iContentIndex = -1;
			} else {
			
				while ((contentLink = document.getElementById('link' + iMainIndex + '_' + iCategoryIndex + '_' + iContentIndex))  && !bLinkFound) {
					sLinks += '  ' + contentLink.href + '\n';
					//alert(contentLink.href + '\n' + urlWithoutAnchor(document.location.href));
					if (contentLink.href.toLowerCase() == urlWithoutAnchor(document.location.href)) {
						bLinkFound = true;
						
					} else 
						iContentIndex++;
				}
				if (!bLinkFound)
					iCategoryIndex++;
			}
		}	
		if (!bLinkFound)
			iMainIndex++;
	}
	if (bLinkFound) {
		toggleItem('contentButton' + iMainIndex,'content' + iMainIndex + 'Container',true);
		if (iCategoryIndex >= 0) {
			toggleItem('con' + iMainIndex + 'catButton' + iCategoryIndex,'con' + iMainIndex + 'cat' + iCategoryIndex + 'Container');
			if (iContentIndex >= 0) {
				//alert('li' + iMainIndex + '_' + iCategoryIndex + '_' + iContentIndex)
				var oLI = document.getElementById('li' + iMainIndex + '_' + iCategoryIndex + '_' + iContentIndex);
				//if (oLI.className){ 
				//alert(oLI.listStyleImage);
				oLI.className = 'nav3Selected';
				//alert(oLI.style.listStyleImage);
				oLI.style.listStyleImage = "url('/inc/leftnavxml/arrow.gif')";
				//}
			} else {				
				var contentLink = document.getElementById('link' + iMainIndex + '_' + iCategoryIndex);
				if (contentLink)
					contentLink.className = 'categoryLinkBold';				
			}
		}
	}
}

function toggleOldItem( sToggle, sElem, bLargeImg ) {
	var oToggle = document.getElementById(sToggle);
	var oElem = document.getElementById(sElem);
	if (oElem && oToggle) {
		if( oElem.style.display == "block" ) {
			oElem.style.display = "none";
			oToggle.src = (bLargeImg ? plusImg.src : plusImgSm.src);
		}
		else {
			oElem.style.display = "block";
			oToggle.src = (bLargeImg ? minusImg.src : minusImgSm.src);
		}
	}
}

function initMenu(toggleId,menuId) {
	//var oToggle = document.getElementById(toggleId+'toggle');
	var oToggle = document.getElementById(toggleId);
	var oMenu = document.getElementById(menuId);
	oToggle.onclick = function() {
		if( oMenu.style.display == "block" ) {
			oMenu.style.display = "none";
			oToggle.src = plusImg.src;
		}
		else {
			oMenu.style.display = "block";
			oToggle.src = minusImg.src;
		}
		return false;
	}
	oToggle.onmouseover = function() {
		oToggle.style.cursor = 'pointer';
	}
	//oMenu.style.display = "none";
}