// navtree.js
// Mutant II+ navigation tree code
// Copyright Doug Ashbaugh 2007-2010  All Rights Reserved
var tcNavTree = new Object();
var savedULs = new Object();

tcNavTree.imgOpenFolder = "../i/folder_open.gif"; //set image path to "OPEN" folder image
tcNavTree.imgClosedFolder = "../i/folder_closed.gif"; //set image path to "CLOSED" folder image
tcNavTree.createTree = function (strNavTreeDivId, boolPersistEnabled, intPersistDays)
{
    var ultags = document.getElementById(strNavTreeDivId).getElementsByTagName("ul")
    if (typeof savedULs[strNavTreeDivId] == "undefined") savedULs[strNavTreeDivId] = (boolPersistEnabled == true && tcNavTree.getCookie(strNavTreeDivId) != "") ? tcNavTree.getCookie(strNavTreeDivId).split(",") : ""
    for (var i = 0; i < ultags.length; i++)
    tcNavTree.buildSubTree(strNavTreeDivId, ultags[i], i)
    if (boolPersistEnabled == true)
    { //if enable persist feature
        var durationdays = (typeof intPersistDays == "undefined") ? 1 : parseInt(intPersistDays)
        tcNavTree.dotask(window, function ()
        {
            tcNavTree.rememberstate(strNavTreeDivId, durationdays)
        }, "unload") //save opened UL indexes on body unload
    }
}

tcNavTree.buildSubTree = function (strNavTreeDivId, ulelement, index)
{
    //alert(ulelement.getAttribute("navState"));
    ulelement.parentNode.className = "subNavTree"
    if (typeof savedULs[strNavTreeDivId] == "object")
    { //if cookie exists (savedULs[strNavTreeDivId] is an array versus "" string)
        //alert("cookie exists for strNavTreeDivId: " + strNavTreeDivId + " and index: " + index);
        if (tcNavTree.searcharray(savedULs[strNavTreeDivId], index))
        {
            ulelement.setAttribute("navState", "OPEN")
            ulelement.style.display = "block"
            ulelement.parentNode.style.backgroundImage = "url(" + tcNavTree.imgOpenFolder + ")"
        }
        else ulelement.setAttribute("navState", "CLOSED")
    } //end cookie persist code
    else if (ulelement.getAttribute("navState") == null || ulelement.getAttribute("navState") == "CLOSED") //if no cookie and UL has NO rel attribute explicted added by user
    ulelement.setAttribute("navState", "CLOSED")
    else if (ulelement.getAttribute("navState") == "OPEN") //else if no cookie and this UL has an explicit rel value of "OPEN"
    tcNavTree.expandSubTree(strNavTreeDivId, ulelement) //expand this UL plus all parent ULs (so the most inner UL is revealed!)
    ulelement.parentNode.onclick = function (e)
    {
        tcNavTree.doHourglassCursor();
        var submenu = this.getElementsByTagName("ul")[0]
        if (submenu.getAttribute("navState") == "CLOSED")
        {
            submenu.style.display = "block"
            submenu.setAttribute("navState", "OPEN")
            ulelement.parentNode.style.backgroundImage = "url(" + tcNavTree.imgOpenFolder + ")"
        }
        else if (submenu.getAttribute("navState") == "OPEN")
        {
            submenu.style.display = "none"
            submenu.setAttribute("navState", "CLOSED")
            ulelement.parentNode.style.backgroundImage = "url(" + tcNavTree.imgClosedFolder + ")"
        }
        tcNavTree.preventpropagate(e);
        tcNavTree.doNormalCursor();
    }
    ulelement.onclick = function (e)
    {
        tcNavTree.doHourglassCursor();
        tcNavTree.preventpropagate(e);
        tcNavTree.doNormalCursor();
    }
}

tcNavTree.expandSubTree = function (strNavTreeDivId, ulelement)
{ //expand a UL element and any of its parent ULs
    tcNavTree.doHourglassCursor();
    var rootnode = document.getElementById(strNavTreeDivId);
    var currentnode = ulelement;
    currentnode.style.display = "block"
    currentnode.parentNode.style.backgroundImage = "url(" + tcNavTree.imgOpenFolder + ")"
    while (currentnode != rootnode)
    {
        if (currentnode.tagName == "UL")
        { //if parent node is a UL, expand it too
            currentnode.style.display = "block"
            currentnode.setAttribute("navState", "OPEN") //indicate it's open
            currentnode.parentNode.style.backgroundImage = "url(" + tcNavTree.imgOpenFolder + ")"
        }
        currentnode = currentnode.parentNode
    }
    tcNavTree.doNormalCursor();
}

tcNavTree.forceTree = function (strNavTreeDivId, action)
{ //expand or contract all UL elements
    //alert("forceTree");
    var ultags = document.getElementById(strNavTreeDivId).getElementsByTagName("ul");
    for (var i = 0; i < ultags.length; i++)
    {
        ultags[i].style.display = (action == "VIEWALL") ? "block" : "none"
        var relvalue = (action == "VIEWALL") ? "OPEN" : "CLOSED"
        ultags[i].setAttribute("navState", relvalue)
        ultags[i].parentNode.style.backgroundImage = (action == "VIEWALL") ? "url(" + tcNavTree.imgOpenFolder + ")" : "url(" + tcNavTree.imgClosedFolder + ")"
    }
}

tcNavTree.rememberstate = function (strNavTreeDivId, durationdays)
{ //store index of opened ULs relative to other ULs in Tree into cookie
    var ultags = document.getElementById(strNavTreeDivId).getElementsByTagName("ul")
    var openuls = new Array()
    for (var i = 0; i < ultags.length; i++)
    {
        if (ultags[i].getAttribute("navState") == "OPEN") openuls[openuls.length] = i //save the index of the opened UL (relative to the entire list of ULs) as an array element
    }
    if (openuls.length == 0) //if there are no opened ULs to save/persist
    openuls[0] = "none open" //set array value to string to simply indicate all ULs should persist with state being closed
    tcNavTree.setCookie(strNavTreeDivId, openuls.join(","), durationdays) //populate cookie with value strNavTreeDivId=1,2,3 etc (where 1,2... are the indexes of the opened ULs)
}

tcNavTree.getCookie = function (Name)
{ //get cookie value
    var re = new RegExp(Name + "=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

tcNavTree.setCookie = function (name, value, days)
{ //set cookei value
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days))
    document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/";
}

tcNavTree.searcharray = function (thearray, value)
{ //searches an array for the entered value. If found, delete value from array
    var isfound = false
    for (var i = 0; i < thearray.length; i++)
    {
        if (thearray[i] == value)
        {
            isfound = true
            thearray.shift() //delete this element from array for efficiency sake
            break
        }
    }
    return isfound
}

tcNavTree.preventpropagate = function (e)
{ //prevent action from bubbling upwards
    if (typeof e != "undefined") e.stopPropagation()
    else event.cancelBubble = true
}

tcNavTree.dotask = function (target, functionref, tasktype)
{ //assign a function to execute to an event handler (ie: onunload)
    var tasktype = (window.addEventListener) ? tasktype : "on" + tasktype
    if (target.addEventListener) target.addEventListener(tasktype, functionref, false)
    else if (target.attachEvent) target.attachEvent(tasktype, functionref)
}

tcNavTree.doHourglassCursor = function ()
{
    document.body.style.cursor = 'wait';
}
tcNavTree.doNormalCursor = function ()
{
    document.body.style.cursor = 'hand';
    document.body.style.cursor = 'pointer';
}
//EOF
