//DD Tab Menu- Script rewritten April 27th, 07: http://www.dynamicdrive.com
//**Updated Feb 23rd, 08): Adds ability for menu to revert back to default selected tab when mouse moves out of menu

//Only 2 configuration variables below:

var enableFolderSelect = true

function tabmenu(enable) {
    tabmenu.enableFolderSelect = enable
    tabmenu.prototype.disabletablinks = false //Disable hyperlinks in 1st level tabs with sub contents (true or false)?
    tabmenu.prototype.snap2original = [false, 300] //Should tab revert back to default selected when mouse moves out of menu? ([true/false, delay_millisec]
    tabmenu.prototype.currentpageurl = window.location.href.replace("http://" + window.location.host, "")
}

tabmenu.prototype.addEvent = 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)
}

tabmenu.prototype.init = function(tabid, dselected) {
    var menuitems = document.getElementById(tabid).getElementsByTagName("a")
    this[tabid + "-menuitems"] = menuitems
    for (var x = 0; x < menuitems.length; x++) {
        if (menuitems[x].getAttribute("AccessKey")) {
            this[tabid + "-menuitems"][x].hasSubContent = true
            if (tabmenu.prototype.disabletablinks)
                menuitems[x].onclick = function() { return false }
            if (tabmenu.prototype.snap2original[0] == true) {
                var submenu = document.getElementById(menuitems[x].getAttribute("AccessKey"))
                menuitems[x].onmouseout = function(e) { revert2default(submenu, tabid, e) }
                submenu.onmouseover = function() { clearrevert2default(tabid) }
                submenu.onmouseout = function(e) { revert2default(this, tabid, e) }
            }
        }
        else //for items without a submenu, add onMouseout effect
            menuitems[x].onmouseout = function(e) { this.className = ""; if (tabmenu.prototype.snap2original[0] == true) tabmenu.prototype.revert2default(this, tabid, e) }
        menuitems[x].onmouseover = function() { tabmenu.prototype.showsubmenu(tabid, this) }
        if (dselected == "auto" && typeof setalready == "undefined" && tabmenu.prototype.isSelected(menuitems[x].href)) {
            tabmenu.prototype.showsubmenu(tabid, menuitems[x])
            this[tabid + "-dselected"] = menuitems[x]
            var setalready = true
        }
        else if (parseInt(dselected) == x) {
            tabmenu.prototype.showsubmenu(tabid, menuitems[x])
            this[tabid + "-dselected"] = menuitems[x]
        }
    }
}

tabmenu.prototype.definemenu = function(tabid, dselected) {
    this[tabid + "-menuitems"] = null
    this[tabid + "-dselected"] = -1
    tabmenu.prototype.addEvent(window, function() { tabmenu.prototype.init(tabid, dselected) }, "load")
}

tabmenu.prototype.showsubmenu = function showsubmenu(tabid, targetitem) {
    if (targetitem != null) {
        var menuitems = this[tabid + "-menuitems"]
        tabmenu.prototype.clearrevert2default(tabid)
        for (i = 0; i < menuitems.length; i++) {
            menuitems[i].className = ""
            if (typeof menuitems[i].hasSubContent != "undefined") {
                document.getElementById(menuitems[i].getAttribute("AccessKey")).style.display = "none"
            }
        }

        targetitem.className = "current"

        if (typeof targetitem.hasSubContent != "undefined") {
            document.getElementById(targetitem.getAttribute("AccessKey")).style.display = "block"
        }
    }
}

tabmenu.prototype.isSelected = function(menuurl) {
    var menuurl = menuurl.replace("http://" + window.location.host, "")

    var path = window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/") + 1)

    //alert("isSelected" + tabmenu.prototype.enableFolderSelect)

    if (menuurl.endsWith("/")) {
        return menuurl.startsWith(path) || (this.currentpageurl == menuurl)
    }
    
    return (this.currentpageurl == menuurl)
    


}

tabmenu.prototype.isContained = function(m, e) {
    var e = window.event || e
    var c = e.relatedTarget || ((e.type == "mouseover") ? e.fromElement : e.toElement)
    while (c && c != m) try { c = c.parentNode } catch (e) { c = m }
    if (c == m)
        return true
    else
        return false
}

tabmenu.prototype.revert2default = function(outobj, tabid, e) {
    if (!isContained(outobj, tabid, e)) {
        window["hidetimer_" + tabid] = setTimeout(function() {
        tabmenu.prototype.showsubmenu(tabid, this[tabid + "-dselected"])
        }, snap2original[1])
    }
}

tabmenu.prototype.clearrevert2default = function(tabid) {
    if (typeof window["hidetimer_" + tabid] != "undefined")
        clearTimeout(window["hidetimer_" + tabid])
}







