﻿function toggleBio(bioLinkId){
    var mainContent = document.getElementById("MainContent");
    if (eval(mainContent.hasChildNodes())){
        var i = 0;
        var contentBoxes = mainContent.childNodes;
        var currentContentBox = contentBoxes[i];
        while (currentContentBox != null){
            if (currentContentBox.className != null && currentContentBox.className.indexOf("DynamicContentBox") == 0){
                if (currentContentBox.hasChildNodes()){
                    // Valid DynamicContentBox
                    var j = 0;
                    var speakerBoxes = currentContentBox.childNodes;
                    var currentSpeakerBox = speakerBoxes[j];
                    while (currentSpeakerBox != null){
                        if (currentSpeakerBox.className != null && currentSpeakerBox.className == "SpeakerBox"){
                            if (currentSpeakerBox.hasChildNodes()){
                                // Valid SpeakerBox
                                var toggleThisSpeakerBox = false;
                                var newSpeakerBoxMode = "";
                                var bioLinks = currentSpeakerBox.childNodes;
                                var k = 0;
                                var currentBioLink = bioLinks[k];
                                while (currentBioLink != null){
                                    if (currentBioLink.className != null && currentBioLink.className == "BioLink"){
                                        // Valid BioLink
                                        if (currentBioLink.getAttribute("BioLinkId") == bioLinkId){
                                            if (currentBioLink.getElementsByTagName("img").length > 0){
                                                var bioLinkImg = currentBioLink.getElementsByTagName("img")[0];
                                                if (bioLinkImg != null){
                                                    // This element was clicked!
                                                    toggleThisSpeakerBox = true;
                                                    if (bioLinkImg.getAttribute("src").indexOf("Opened") != -1){
                                                        newSpeakerBoxMode = "closed";
                                                        bioLinkImg.removeAttribute("src");
                                                        bioLinkImg.setAttribute("src", "_images/BiographyClosed.gif");
                                                    }else{
                                                        newSpeakerBoxMode = "opened";
                                                        bioLinkImg.removeAttribute("src");
                                                        bioLinkImg.setAttribute("src", "_images/BiographyOpened.gif");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    k++;
                                    currentBioLink = bioLinks[k];
                                }
                                if (toggleThisSpeakerBox){
                                    toggleAdditionalText(currentSpeakerBox, newSpeakerBoxMode);
                                }
                            }
                        }
                        j++;
                        currentSpeakerBox = speakerBoxes[j];
                    }
                }
            }
            i++;
            currentContentBox = contentBoxes[i];
        }
    }
}

function toggleAdditionalText(element, newMode){
    var i = 0;
    if (element.hasChildNodes()){
        var currentChildElement = element.childNodes[i];
        while (currentChildElement != null){
            if (currentChildElement.className != null){
                if (currentChildElement.className == "AdditionalText"){
                    if (newMode == "closed"){
                        // Hide text
                        currentChildElement.style.display = "none";
                    }else{
                        // Show text
                        currentChildElement.style.display = "";
                    }
                }else if (currentChildElement.className == "Dots"){
                    if (newMode == "closed"){
                        // Show dots
                        currentChildElement.style.display = "";
                    }else{
                        // Hide dots
                        currentChildElement.style.display = "none";
                    }
                }
            }
            toggleAdditionalText(currentChildElement, newMode);
            i++;
            currentChildElement = element.childNodes[i];
        }
    }
}

function init(){
    var i = 0;
    var links = document.getElementsByTagName("a");
    var currentLink = links[i];
    while (currentLink != null){
        if (currentLink.className == "BioLink"){
            // Valid BioLink
            currentLink.setAttribute("BioLinkId", i);
            currentLink.onclick = function() { toggleBio(this.getAttribute("BioLinkId")); };
            toggleBio(i);
        }
        i++;
        currentLink = links[i];
    }
}