﻿
/**
* dropDownHeaderMenuNew v0.3
* An easy to implement dropDown HeaderMenuNew for Websites, that may be based on styled list tags
*
* Works for IE 5.5+ PC, Mozilla 1+ all Plattforms, Opera 7+
*
* Copyright (c) 2004 Knallgrau New Medias Solutions GmbH, Vienna - Austria
*
* Written by Matthias Platzer at http://knallgrau.at
* 
* Use it as you need it
* It is distributed under a BSD style license
*/

userAgent = navigator.userAgent.toLowerCase();
isIE = ((userAgent.indexOf("msie") != -1) && (userAgent.indexOf("opera") == -1));
isOpera = (userAgent.indexOf("opera") != -1);
isMac = (userAgent.indexOf("mac") != -1);
isMacIE = (isIE && isMac);
isWinIE = (isIE && !isMac);
isSafari = (userAgent.indexOf("safari") != -1);
isGecko = (navigator.product == "Gecko" && !isSafari);

/**
* Container Class (Prototype) for the dropDownHeaderMenuNew
* 
* @param idOrElement     String|HTMLElement  root Node of the HeaderMenuNew (ul)
* @param name            String              name of the variable that stores the result
*                                            of this constructor function
* @customConfigFunction  Function            optional config function to override the default settings
*                                            for an example see HeaderMenuNew.prototype.config
*/
function HeaderMenuNew(idOrElement, name, customConfigFunction) {
    this.author = "Matthias Platzer AT knallgrau.at";
    this.copyright = "Copyright (c) 2004 Knallgrau New Medias Solutions GmbH, Vienna - Austria";
    this.license = "distributed under a BSD-Style license";

    this.lastUpdate = "2004-11-08";
    this.version = "0.4";

    this.name = name;
    this.type = "HeaderMenuNew";
    this.closeDelayTimer = null;
    this.closingHeaderMenuNewItem = null;

    this.config();
    if (typeof customConfigFunction == "function") {
        this.customConfig = customConfigFunction;
        this.customConfig();
    }

    this.rootContainer = new HeaderMenuNewContainer(idOrElement, this);
}

HeaderMenuNew.prototype.config = function() {
    this.collapseBorders = true;
    this.quickCollapse = true;
    this.closeDelayTime = 500;
};

function HeaderMenuNewContainer(idOrElement, parent) {
    this.type = "HeaderMenuNewContainer";
    this.HeaderMenuNewItems = [];
    this.init(idOrElement, parent);
}

HeaderMenuNewContainer.prototype.init = function(idOrElement, parent) {
    this.element = (typeof idOrElement == "string") ? document.getElementById(idOrElement) : idOrElement;
    this.parent = parent;
    this.parentHeaderMenuNew = (this.type == "HeaderMenuNewContainer") ? ((parent) ? parent.parent : null) : parent;
    this.root = parent instanceof HeaderMenuNew ? parent : parent.root;
    this.id = this.element.id;

    if (this.type == "HeaderMenuNewContainer") {
        if (this.hasClass("dropdown")) this.HeaderMenuNewType = "dropdown";
        else if (this.hasClass("flyout")) this.HeaderMenuNewType = "flyout";
        else if (this.hasClass("horizontal")) this.HeaderMenuNewType = "horizontal";
        else this.HeaderMenuNewType = "standard";
        if (this.HeaderMenuNewType == "flyout" || this.HeaderMenuNewType == "dropdown") {
            this.isOpen = false;
            this.element.style.position = "absolute";
            this.element.style.top = "0px";
            this.element.style.left = "0px";
            this.element.style.visibility = "hidden";
        } else {
            this.isOpen = true;
        }
    } else {
        this.isOpen = this.parentHeaderMenuNew.isOpen;
    }

    var childNodes = this.element.childNodes;
    if (childNodes == null) return;

    for (var i = 0; i < childNodes.length; i++) {
        var node = childNodes[i];
        if (node.nodeType == 1) {
            if (this.type == "HeaderMenuNewContainer") {
                if (node.tagName.toLowerCase() == "li") {
                    this.HeaderMenuNewItems.push(new HeaderMenuNewItem(node, this));
                }

            } else {
                if (node.tagName.toLowerCase() == "ul") {
                    this.subHeaderMenuNew = new HeaderMenuNewContainer(node, this);
                }
            }
        }
    }
};

HeaderMenuNewContainer.prototype.getAbsOffsetTop = function() {
    var offset = 0;
    var ele = this.element;
    var sl = (window.pageYOffset ? window.pageYOffset : document.body.scrollTop);
    do { offset += ele.offsetTop; ele = ele.offsetParent; } while (ele != null && ele.style.position != "relative" && ele.style.position != "absolute" && (ele.style.position != "fixed"))
    return offset;
};

HeaderMenuNewContainer.prototype.getAbsOffsetLeft = function() {
    var offset = 0;
    var ele = this.element;
    var sl = (window.pageXOffset ? window.pageXOffset : document.body.scrollLeft);
    do { offset += ele.offsetLeft; if (ele.style.position == 'fixed') { offset += sl }; ele = ele.offsetParent; } while (ele != null)
    return offset;
};

HeaderMenuNewContainer.prototype.hasClass = function(className) {
    return (" " + this.element.className + " ").indexOf(className) > -1;
};

HeaderMenuNewContainer.prototype.getBorders = function(element) {
    var ltrb = ["Left", "Top", "Right", "Bottom"];
    var result = {};
    for (var i in ltrb) {
        if (this.element.currentStyle)
            var value = parseInt(this.element.currentStyle["border" + ltrb[i] + "Width"]);
        else if (window.getComputedStyle)
            var value = parseInt(window.getComputedStyle(this.element, "").getPropertyValue("border-" + ltrb[i].toLowerCase() + "-width"));
        else
            var value = parseInt(this.element.style["border" + ltrb[i]]);
        result[ltrb[i].toLowerCase()] = isNaN(value) ? 0 : value;
    }
    return result;
};

HeaderMenuNewContainer.prototype.open = function() {
    if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
    this.parentHeaderMenuNew.closeAll(this);
    this.element.style.visibility = "visible";
    this.isOpen = true;
    if (this.HeaderMenuNewType == "dropdown") {
        this.element.style.top = (this.parent.getAbsOffsetTop() + this.parent.element.offsetHeight) + "px";
        this.element.style.left = (this.parent.element.offsetLeft) + "px";
    } else if (this.HeaderMenuNewType == "flyout") {
        var parentHeaderMenuNewBorders = this.parentHeaderMenuNew ? this.parentHeaderMenuNew.getBorders() : new Object();
        var thisBorders = this.getBorders();
        if (
      (this.parentHeaderMenuNew.getAbsOffsetLeft() + this.parentHeaderMenuNew.element.offsetWidth + this.element.offsetWidth + 20) >
      (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
    ) {
            this.element.style.left = (-this.element.offsetWidth - (this.root.collapseBorders ? 0 : parentHeaderMenuNewBorders["left"])) + "px";
        } else {
            this.element.style.left = (this.parentHeaderMenuNew.element.offsetWidth - parentHeaderMenuNewBorders["left"] - (this.root.collapseBorders ? Math.min(parentHeaderMenuNewBorders["right"], thisBorders["left"]) : 0)) + "px";
        }
        this.element.style.top = (this.parent.element.offsetTop - parentHeaderMenuNewBorders["top"] - this.HeaderMenuNewItems[0].element.offsetTop) + "px";
    }
};

HeaderMenuNewContainer.prototype.close = function() {
    this.element.style.visibility = "hidden";
    this.isOpen = false;
    this.closeAll();
};

HeaderMenuNewContainer.prototype.closeAll = function(trigger) {
    for (var i in this.HeaderMenuNewItems) {
        this.HeaderMenuNewItems[i].closeItem(trigger);
    }
};

HeaderMenuNewItem.prototype = HeaderMenuNewContainer.prototype;
function HeaderMenuNewItem(idOrElement, parent) {
    var HeaderMenuNewItem = this;
    this.type = "HeaderMenuNewItem";
    this.subHeaderMenuNew;
    this.init(idOrElement, parent);
    if (this.subHeaderMenuNew) {
        this.element.onmouseover = function() {
            HeaderMenuNewItem.subHeaderMenuNew.open();
        }
    } else {
        if (this.root.quickCollapse) {
            this.element.onmouseover = function() {
                HeaderMenuNewItem.parentHeaderMenuNew.closeAll();
            }
        }
    }
    var linkTag = this.element.getElementsByTagName("A")[0];
    if (linkTag) {
        linkTag.onfocus = this.element.onmouseover;
        this.link = linkTag;
        this.text = linkTag.text;
    }
    if (this.subHeaderMenuNew) {
        this.element.onmouseout = function() {
            if (HeaderMenuNewItem.root.openDelayTimer) window.clearTimeout(HeaderMenuNewItem.root.openDelayTimer);
            if (HeaderMenuNewItem.root.closeDelayTimer) window.clearTimeout(HeaderMenuNewItem.root.closeDelayTimer);
            eval(HeaderMenuNewItem.root.name + ".closingHeaderMenuNewItem = HeaderMenuNewItem");
            HeaderMenuNewItem.root.closeDelayTimer = window.setTimeout(HeaderMenuNewItem.root.name + ".closingHeaderMenuNewItem.subHeaderMenuNew.close()", HeaderMenuNewItem.root.closeDelayTime);
        }
    }
}

HeaderMenuNewItem.prototype.openItem = function() {
    this.isOpen = true;
    if (this.subHeaderMenuNew) { this.subHeaderMenuNew.open(); }
};

HeaderMenuNewItem.prototype.closeItem = function(trigger) {
    this.isOpen = false;
    if (this.subHeaderMenuNew) {
        if (this.subHeaderMenuNew != trigger) this.subHeaderMenuNew.close();
    }
};

// DEBUG

HeaderMenuNew.prototype.toString = function() {
    return "HeaderMenuNew";
}

HeaderMenuNew.prototype.trace = function() {
    return "root";
}

HeaderMenuNewContainer.prototype.toString = function() {
    return "y:" + this.getAbsOffsetTop();
}

HeaderMenuNewContainer.prototype.trace = function() {
    // return this.element.outerHTML.substring(0,40);
    return this;
    return (this.parent ? this.parent.trace() : "x") + " > " + this;
}

function trace(msg) {
    var ele = document.getElementById("trace");
    ele.value = msg + "\n" + ele.value;
}

