// JS file for handling toggling of subnav elements
function ApplyStyle(){
    if(document.getElementById("treeNav") != null)
    {
	    var tree_ul_array = document.getElementById("treeNav").getElementsByTagName('ul');
	    lenUL = tree_ul_array.length;
	    for(i=0;i<lenUL;i++)
	        tree_ul_array[i].className = '';
	    
	    var tree_li_array = document.getElementById("treeNav").getElementsByTagName('li');
	    var lenLI = tree_li_array.length;
	    for(i=0;i<lenLI;i++)
	        tree_li_array[i].className = '';
	        
        var tree_a_array = document.getElementById("treeNav").getElementsByTagName('a');
        var len_a = tree_a_array.length;
        for(i=0;i<len_a;i++){
            if(tree_a_array[i].className == 'CMSListMenuLinkHighlighted')
                tree_a_array[i].className = 'highlighted';
            else
                tree_a_array[i].className = '';	 
        }                   
    }
} 

function initNav() {
	if (document.getElementById && document.getElementsByTagName) {
	    var li_array = document.getElementById("mainNav").childNodes;
	    
	    
	    for(var j = 0; j < li_array.length; j++)
	    {
	        if(li_array[j].tagName == "LI") { //check for li tags
	            //set up top level functionality
					var mainLink = li_array[j].getElementsByTagName("A")[0];
	                mainLink.onclick = function(){
	                    return showChildList(this);
	            }
	            
	            //for each li check for second level children
	            if (li_array[j].getElementsByTagName("UL").length > 0) {
	               var childUL = getChildByTag(li_array[j],"UL");
	               doSecondLevel(childUL);
	            }
	            else
	            {
	            	mainLink.onclick = followLinkOnly; 
	            }	            
	        }
	    }		
		 SetNavState();
	}
}

function doSecondLevel(pEl)
{
    var li_array = pEl.getElementsByTagName("LI");
    
    for( var i = 0; i < li_array.length; i++ ) {
			// bind li tags and set class names
			if (li_array[i].getElementsByTagName("UL").length > 0) { // li has subnav
				//var newClassName = (li_array[i].className.match("active") ? "less" : "MORE"); // if li has subnav and it's not active, add the "less" class, otherwise if it has subnav add the "more" class
				var newClassName = "MORE";
				li_array[i].className = (li_array[i].className ? li_array[i].className + " " + newClassName : newClassName); // if there's already a class name, append a space and then the new class name
				li_array[i].onclick = preventBubbleUp;
			}
			else
			{
			    li_array[i].className = "NONE";
			}
			// bind anchors
			var theLink = li_array[i].getElementsByTagName("A")[0];
			theLink.onclick = followLinkOnly;
		}
}
function showChildList(pEl)
{
    	//hide all second level nav (UL/LI/UL)
   	hideSecondLevel();
    	//get the parent LI
    var parentLI = getGroupParent(pEl,"LI");
	 toggleMenuClass(parentLI);

    //return false;
}

function hideSecondLevel()
{
    var aryMain = document.getElementById("mainNav").getElementsByTagName("LI");
    for (var i=0;i<aryMain.length;i++) {
//	 		aryMain[i].className = aryMain[i].className.replace("less", "MORE");
        //aryMain[i].style.display = "none";
    }
	 //alert("done");
    return false;    
}

function getChildByTag(pNode, theTagName) {
    var theChildren = pNode.childNodes; 
    for (i=0; i<theChildren.length; i++) { 
        if (theChildren[i].tagName == theTagName) { 
            return theChildren[i];
        }
    }
}

function doSubMenu(pEl) {
	var parent = getGroupParent(pEl, "LI");
	
	if (!parent) parent = document.getElementById("mainNav");
	var li_array = parent.getElementsByTagName("LI");
	for (var i = 0; i < li_array.length; i++) {
//		if (isMatch(li_array[i].className, "less") || li_array[i] == pEl) toggleMenuClass(li_array[i]);
	}
	return false;
}
// prevent onclick event from propogating to the A's parent LI
function followLinkOnly(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	//if you want to pass a reference to the element clicked use target for mozilla and srcElement for IE
	//var theEl = e.target;
	//if (e.srcElement) theEl = e.srcElement;
}

function dontFollowLink() {
    return false;
}

function getGroupParent(pNode,strTagName) {
	var groupParent = pNode.parentNode;
	while (groupParent.tagName != strTagName.toUpperCase() && groupParent.tagName != "BODY") {
		groupParent = groupParent.parentNode;
	}
	if (groupParent.tagName == "BODY") {
		return false;
	} else {
		return groupParent;
	}
}

// prevent onclick event from propagating to the Li's parent LI
function preventBubbleUp(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	//call doSubMenu, pass a reference the LI clicked
	var theEl = e.target;
	if (e.srcElement) theEl = e.srcElement;
	doSubMenu(theEl);
}

function toggleMenuClass(pEl) {
//	if (isMatch(pEl.className, "less")) {
		pEl.className = pEl.className.replace("less", "MORE");
//	} else {
//		pEl.className = pEl.className.replace("MORE", "less");
//	}
}
//matching function
function isMatch(pEl,pMatch){
	if ((pEl.match(pMatch))==pMatch) {
		return true;
	} else {
		return false
	}	
}
//sets correct state for pages after a postback/menu reload
function SetSelectedClass()
{

    //alert('set selected');
    //We're looking for the highlighted class in the nav UL set and will move UP through the node hierarchy to make sure the full
    //path is set to selected and visible. if there is no highlighted class found we have to use the current url to find the element
    var subNav = document.getElementById("mainNav");
    var highlightedAry = getElementsByClassName(subNav,"LI","highlighted");
    var pEl;
    if(highlightedAry.length > 0) //found it
	 {
	 		pEl = highlightedAry[0];
			//alert(pEl);
	 }
	 else //get the element by alias
	 {
		var sPath = window.location.pathname;
		//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	 }
    for( var i = 0; i < highlightedAry.length; i++ ) {
		// get the parent UL and set its display=block
		//alert(highlightedAry[i].id);
		var parent = getGroupParent(highlightedAry[i],"LI");
		//alert(parent);
		toggleMenuClass(parent);			
	}	
}

function SetNavState()
{
	var sPath = window.location.pathname;
	//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
	var sPage = sPath.substring(sPath.indexOf('/') + 1);
	
	var sPage = sPage.replace('.aspx','');
	var sPage = sPage.replace('TWW','');
	
	//alert(sPage);
	
	var aryTest = sPage.split('/');
	//alert(aryTest.length);
	var nav = document.getElementById("mainNav");
	var ancAry = nav.getElementsByTagName("A");
	
	for(var i=0; i<aryTest.length; i++)
	{
	    if(aryTest[i] != "")
	    {
		
		    var alias = aryTest[i] + ".aspx";
		    for(var j=0; j < ancAry.length; j++)
		    {
			    if(ancAry[j].href.indexOf(alias) > 0)
			    {
				    var parent = getGroupParent(ancAry[j],"LI");
				    //alert(parent);
    				
				    toggleMenuClass(parent);		
				    //alert("found one!");
				    ancAry[j].className = "highlighted";
				    break;
			    }
		    }
		}
		//alert(aryTest[i]);
	}
	//alert(aryTest.length);
}

function getElementsByClassName(oElm, strTagName, strClassName){ 
        //alert(oElm);
          var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName); 
          var arrReturnElements = new Array(); 
          strClassName = strClassName.replace(/\-/g, "\\-"); 
          var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)"); 
          var oElement; 
          
          for(var i=0; i<arrElements.length; i++){ 
               oElement = arrElements[i];       
               if(oRegExp.test(oElement.className)){ 
                    arrReturnElements.push(oElement); 
               }    
          } 
          return (arrReturnElements) 
}

function updatePhoto(imgPath,caption)
{
    var topsection = document.getElementById("middleColumn");
    var imgMain = document.getElementById("imgChange").getElementsByTagName("IMG")[0];
    var arySpan = getElementsByClassName(topsection,"SPAN","tertiaryHead");
    var capMain = arySpan[0];
    imgMain.src = imgPath;
    capMain.innerHTML = caption;     
}

function getNextNode(pNode) {
    var nextSib = pNode.nextSibling;
    while (nextSib.nodeType != 1) {
        nextSib = nextSib.nextSibling;
    } 
    return nextSib;
}

function getPrevNode(pNode) {
    var prevSib = pNode.previousSibling;
    while (prevSib.nodeType != 1) {
        prevSib = prevSib.previousSibling;
    } 
    return prevSib;
}

//function to toggle prompt for an input box from within its value property
function togglePrompt(pEl,pVal)
{
    if(pEl.value == pVal)
    {
        pEl.value = '';
    }
    else if(pEl.value == '')
    {
        pEl.value = pVal;
    }
}

function playPodcast(pId)
{
    var baseUrl = 'http://neptune.tankdesign.com/TWW/';
    var newWin = window.open(baseUrl + 'mediaPlayer.aspx?fileID=' + pId,'Audio Player','height=20,width=470,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no');   
}

function checkPodcast()
{
    //alert('foo');
    if(document.getElementById("emtpyPodcastList"))
    {
        
        var vidDiv = document.getElementById("h4Video");
        vidDiv.className="topLevel videoCalloutOrange";
        var podList = document.getElementById("podcastList");
        podList.style.display = "none";
        document.getElementById("dvPodcast").style.display="none";
        //recheck for NO videos - very rare but could happen
        if(document.getElementById("emtpyVideoList"))
        {
            vidDiv.style.display = "none";
            var vidList = document.getElementById("videoList");
            vidList.style.display = "none";
            var articleDiv = document.getElementById("dvRelatedArticles");
            articleDiv.className = "topLevel article";
        }
    }
}