﻿var m_blogArchivesImgDir = _getPath("/App_Themes/Default/Images/");

function init() {

    //Check to see if we can use the DOM
    if (!document.getElementById) return;
    if (!document.getElementsByTagName) return;
    if (!document.createElement) return;
    
    
    //Get all the UL's in the Navigation
    var navigation = document.getElementById('nav');
    if (!navigation) return;
    var navSub = navigation.getElementsByTagName('ul');
    
    //Go through all the Sub Nav's - give them a hidden class, inject in the toogle graphic
    for (i = 0; i < navSub.length; i++) {
        //Create the Image to inject in
        var toggleImage = document.createElement('img');
        var firstMonth = $(navSub[i]).find(".firstMonth").length > 0;

        toggleImage.setAttribute('src', m_blogArchivesImgDir + (firstMonth ? "BlogArchivesContract.gif" : "BlogArchivesExpand.gif"));
        toggleImage.setAttribute('class', "blogExpand");
        toggleImage.style.cursor = "pointer";
        toggleImage.onclick = function() {
            toggleNav(this);
        }

        //Get the Parent of the UL, and insert the Image before the first child
        navSub[i].parentNode.insertBefore(toggleImage, navSub[i].parentNode.firstChild);

        //Hide the Sub Navigation using a CSS Class and assign a class to the parent for styling
        //navSub[i].style.display = "none";
        navSub[i].parentNode.className = "expandable";
    }
    if (navSub.length > 1) {
        var expandLink = document.createElement('li');
        expandLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='expandAll'>Expand All</a>"

        var collapseLink = document.createElement('li');
        collapseLink.innerHTML = "<a href='#' onclick='toggleNav(this)' id='collapseAll'>Collapse All</a>"

        //Add them to the Bottom of the Navigation
        navigation.appendChild(expandLink);
        navigation.appendChild(collapseLink);
    }
}

function toggleNav(whichOne) {
    if (whichOne.getAttribute('id') == "expandAll") {
        var navigation = document.getElementById('nav');
        var navigationULs = navigation.getElementsByTagName('ul');
        var allImages = navigation.getElementsByTagName('img');
        for (i = 0; i < navigationULs.length; i++) {
            navigationULs[i].style.display = "block";
            allImages[i].setAttribute('src', m_blogArchivesImgDir + "BlogArchivesContract.gif");

            var navigationLIs = navigationULs[i].getElementsByTagName('li');
            for (z = 0; z < navigationLIs.length; z++)
                navigationLIs[z].style.display = "block";
        }
    }
    else if (whichOne.getAttribute('id') == "collapseAll") {
        var navigation = document.getElementById('nav');
        var navigationULs = navigation.getElementsByTagName('ul');
        var allImages = navigation.getElementsByTagName('img');
        for (i = 0; i < navigationULs.length; i++) {
            navigationULs[i].style.display = "none";
            allImages[i].setAttribute('src', m_blogArchivesImgDir + "BlogArchivesExpand.gif");
        }
    }
    else {
        var theParent = whichOne.parentNode;
        var theParentULs = theParent.getElementsByTagName('ul');
        var theParentImage = theParent.getElementsByTagName('img');

        //Grab just the first UL and the first toggle image so that sub-sub UL navs/image don't expand too
        for (i = 0; i < theParentULs.length; i++) {
        
            if (theParentULs[i].style.display == "")
            {
                if ($(theParentULs[i]).find(".firstMonth").length > 0)
                    theParentULs[i].style.display = "block";
            }
        
            if (theParentULs[i].style.display == "none" || theParentULs[i].style.display == "") {
                theParentULs[i].style.display = "block";
                theParentImage[i].setAttribute('src', m_blogArchivesImgDir + "BlogArchivesContract.gif");
                
                var navigationLIs = theParentULs[i].getElementsByTagName('li');
                for (z = 0; z < navigationLIs.length; z++)
                    navigationLIs[z].style.display = "block";
            }
            else {
                theParentULs[i].style.display = "none";
                theParentImage[i].setAttribute('src', m_blogArchivesImgDir + "BlogArchivesExpand.gif");
            }
        }
    }
}

window.onload = init;

function _getPath(path) {
    var wt_virtual = m_virtualDirectory;

    if (wt_virtual.substr(wt_virtual.length - 1, 1) == "/") { wt_virtual = wt_virtual.substr(0, wt_virtual.length - 1) };

    return wt_virtual + path;
}
