// JavaScript Document

function highlightNav(){
	theNav = document.getElementById("header-nav");	
	theLinks = theNav.getElementsByTagName("a");
	currentFolder = document.location.href.split('/')[3];
	for(i=0; i<theLinks.length;i++){
		linkFolder = theLinks[i].href.split('/')[3];
		if(linkFolder==currentFolder){
			theLinks[i].className="mmhide_active";
		}
	}
}


function cleanURL(dirtyURL){
	while( dirtyURL[dirtyURL.length-1]=="default.aspx" || dirtyURL[dirtyURL.length-1]=="#" || dirtyURL[dirtyURL.length-1]=="" ){		
		dirtyURL.pop();
	}
	return dirtyURL;
}


function highlightSecondaryNav(){	
	var theDiv = document.getElementById('secondary-nav');
	if(theDiv){
		var theItems = theDiv.getElementsByTagName('li');
		var curr_link=0;
		var curr_item=0;
		var match_found=false;
		var sub_match_found=false;
		var activeItem;
		var activeSubLink;
		
		for (curr_item = 0; curr_item < theItems.length; curr_item++)
		{
			
					currentpath=window.location.href;
					curr_array=cleanURL(currentpath.split("/"));
					theLink=theItems[curr_item].getElementsByTagName('a')[0];
					if(theLink){
						newpath=theLink.href;
						new_array=cleanURL(newpath.split("/"));
						
						//alert((new_array.length-curr_array.length));
						//alert(curr_array[new_array.length-1]+"  :  "+new_array[new_array.length-1]);
							
						//SEE IF THE LINKS MATCH AND MAKE SURE IT IS NOT A # LINK
						var i=0;
						var dir_lvl=0;
						//if(curr_array.length>4) dir_lvl=3;
						
						for(i=(curr_array.length-1);i>dir_lvl;i--){	
							
							if(curr_array[i]==new_array[new_array.length-1] && newpath.lastIndexOf('#')<0){
								//alert(theItems[curr_item].parentNode.parentNode.tagName);
								
								if(curr_array[i-1]==new_array[new_array.length-2]){	
								
									if(theItems[curr_item].parentNode.parentNode=="[object HTMLDivElement]" || theItems[curr_item].parentNode.parentNode.tagName=="DIV"){
										//set highest level in secondary nav
										activeItem=theItems[curr_item];
										match_found=true;
									}
									else{
										//set sub link in secondary nav
											activeSubLink=theLink;
											sub_match_found=true;
										
									}
									
									break;
								
								}
								
							}
							
						}
					
					}
					
						
		}
		// CHANGE STYLES OF THE MATCHING LINKS
		if(activeItem){
			activeItem.className="mmhide_active";
		
			theSubList=activeItem.getElementsByTagName('ul')[0];
				
			//make sub links of active Item visible
			if(theSubList!=null){
				theSubList.style.display="block";
			}
			
			if(sub_match_found){
				activeSubLink.className="mmhide_active";
			}
		}
	}
	
}


function setImage(newPath){
	//enlargedImage = document.getElementById('enlargedImage');	
	popupContainer = document.getElementById('popup');	
	popupContent = document.getElementById('popup-content');
	newImage = document.getElementById('popupPhoto');
	
	containerHeight = document.body.scrollHeight;
	
	if(window.scrollMaxY){
		containerHeight+=window.scrollMaxY	
	}
	
	hideLink = "<a href='javascript:hidePopup()' class='hide'>Close</a>";
	
	popupContent.innerHTML = "<img src='"+ newPath +"' onload='positionImage(this);'/>"+hideLink;
	
	popupContainer.style.display="block";
	
	popupContainer.style.height=containerHeight +"px";
	
}

function positionImage(imageToPosition){
	
	offset = document.documentElement.scrollTop;
	popTop = (document.body.clientHeight - imageToPosition.height)/2.5;
	popLeft = (document.body.clientWidth - imageToPosition.width)/2;

	
	document.getElementById('popup-content').style.top = (popTop+offset).toString() +"px";
	document.getElementById('popup-content').style.left = popLeft.toString() +"px";	
}

function hidePopup(){
	popupContainer = document.getElementById('popup');	
	popupContainer.style.display="none";

}
