/***********************************************
* Dolphin Tabs Menu- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more
***********************************************/

var dolphintabs = {
    selectedIndex: -1,
    subcontainers: [],
    last_accessed_tab: null,
    revealInterval: null,
    tabItems: null,

    revealsubmenu: function(curtabref) {
        this.hideallsubs()
        if (this.last_accessed_tab != null)
            this.last_accessed_tab.className = ""
        if (curtabref.getAttribute("rel")) //If there's a sub menu defined for this tab item, show it
        {
            var subContainer = document.getElementById(curtabref.getAttribute("rel"));
            subContainer.style.display = "block";
        }
        curtabref.className = "current"
        this.last_accessed_tab = curtabref
    },

    hideallsubs: function() {
        for (var i = 0; i < this.subcontainers.length; i++)
            document.getElementById(this.subcontainers[i]).style.display = "none"
    },

    getTab: function(index) {
        return this.tabItems[index];
    },

    getSelectedTab: function() {
        return this.tabItems[this.selectedIndex];
    },

    getSelectedTabSubContainer: function(selectedTab) {
        return document.getElementById(selectedTab.getAttribute("rel"));
    },

    init: function(menuId, selectedIndex) {
        this.tabItems = document.getElementById(menuId).getElementsByTagName("a");
        for (var i = 0; i < this.tabItems.length; i++) {
            if (this.tabItems[i].getAttribute("rel"))
                this.subcontainers[this.subcontainers.length] = this.tabItems[i].getAttribute("rel") //store id of submenu div of tab menu item
            if (i == selectedIndex) { //if this tab item should be selected by default
                this.selectedIndex = selectedIndex;
                this.tabItems[i].className = "current"
                this.revealsubmenu(this.tabItems[i])
            }
            this.tabItems[i].onmouseover = function() {
                if (dolphintabs.revealInterval != null) {
                    clearTimeout(dolphintabs.revealInterval);
                    dolphintabs.revealInterval = null;
                }
                dolphintabs.revealsubmenu(this);
            }
          if (selectedIndex > -1 && i != selectedIndex) {
                this.tabItems[i].onmouseout = function(event) {
                    fixOnMouseOut(this, event, "dolphintabs.waitForMovement();");
                }
            } 
        } //END FOR LOOP
    },

    waitForMovement: function() {
        dolphintabs.revealInterval = setTimeout("dolphintabs.revealsubmenu(dolphintabs.tabItems[dolphintabs.selectedIndex])", 1500);

        var subContainer = this.getSelectedTabSubContainer(dolphintabs.last_accessed_tab);
        subContainer.onmouseover = function() {
            if (dolphintabs.revealInterval != null) {
                clearTimeout(dolphintabs.revealInterval);
                dolphintabs.revealInterval = null;
            }
        }

        subContainer.onmouseout = function(event) {
            fixOnMouseOut(this, event, "dolphintabs.revealsubmenu(dolphintabs.tabItems[dolphintabs.selectedIndex]);");
        }
    }

}

function is_child_of(parent, child) {
    if (child != null) {
        while (child.parentNode) {
            if ((child = child.parentNode) == parent) {
                return true;
            }
        }
    }
    return false;
}
function fixOnMouseOut(element, event, JavaScript_code) {
    var current_mouse_target = null;
    if (!event) event = window.event;
    if (event.toElement) {
        current_mouse_target = event.toElement;
    } else if (event.relatedTarget) {
        current_mouse_target = event.relatedTarget;
    }
    if (!is_child_of(element, current_mouse_target) && element != current_mouse_target) {
        eval(JavaScript_code);
    }
}
