﻿/**
* Container Class (Prototype) for the dropDownMenu
*
* @param idOrElement     String|HTMLElement  root Node of the menu (ul)
* @param name            String              name of the variable that stores the result
*                                            of this constructor function
* @param defaultSelected String              The ID of the default selected element
* @param customConfigFunction  Function            optional config function to override the default settings
*                                            for an example see Menu.prototype.config
*/
var Tabs = Class.create();
Tabs.prototype = {

    initialize: function(idOrElement, name, defaultSelected, customConfigFunction) {

        this.name = name;
        this.type = "menu";
        this.closeDelayTimer = null;
        this.closingMenuItem = null;
        this.selectedItem = defaultSelected;
        this.locked = false;

        this.config();
        if (typeof customConfigFunction == "function") {
            this.customConfig = customConfigFunction;
            this.customConfig();
        }
        this.rootContainer = new TabsContainer(idOrElement, this);

        this.blur();
        /*for (var x = 0; x < this.rootContainer.menuItems.length; x++) {
        var menuItem = this.rootContainer.menuItems[x];
        if ($(menuItem.element).identify() == this.selectedItem) {
        //$(menuItem.element).setStyle({ backgroundColor: "silver" });
        $(menuItem.element).addClassName("tabFocus");
        var tabDiv = $(menuItem.element).readAttribute('divid');
        if (tabDiv != null) {
        $(tabDiv).show();
        }

                var onchangeCode = $(menuItem.element).readAttribute('select');
        if (onchangeCode != null) {
        eval(onchangeCode);
        }
        }
        }*/
        this.setFocus(defaultSelected);

    },

    config: function() {
        this.collapseBorders = true;
        this.quickCollapse = true;
        this.closeDelayTime = 200;
    },

    blur: function() {
        for (var x = 0; x < this.rootContainer.menuItems.length; x++) {
            var menuItem = this.rootContainer.menuItems[x];
            menuItem.blur();
        }
    },

    switchTo: function(tab) {
        this.blur();
        this.setFocus(tab);
    },

    setFocus: function(tab) {
        this.selectedItem = $(tab).identify();

        if ($(tab).hasClassName("tabBlur")) {
            $(tab).removeClassName("tabBlur");
        }
        $(tab).addClassName("tabFocus");
        //$(tab).setStyle({ backgroundColor: "silver" });
        var tabDiv = $(tab).readAttribute('divid');
        if (tabDiv != null) {
            $(tabDiv).show();
        }

        var onchangeCode = $(tab).readAttribute('select');
        if (onchangeCode != null) {
            eval(onchangeCode);
        }
    },

    lock: function() {
        this.locked = true;
    },

    unlock: function() {
        this.locked = false;
    }
}

var TabsContainer = Class.create();
TabsContainer.prototype = {
    initialize: function(idOrElement, parent) {
        this.type = "menuContainer";
        this.menuItems = [];
        this.init(idOrElement, parent);
    },

    init: function(idOrElement, parent) {
        this.element = $(idOrElement);
        this.parent = parent;
        this.parentMenu = (this.type == "menuContainer") ? ((parent) ? parent.parent : null) : parent;
        this.root = parent instanceof Tabs ? parent : parent.root;
        this.id = this.element.id;

        if (this.type == "menuContainer") {
            if (this.element.hasClassName("level1")) this.menuType = "horizontal";
            else if (this.element.hasClassName("level2")) this.menuType = "dropdown";
            else this.menuType = "flyout";

            if (this.menuType == "flyout" || this.menuType == "dropdown") {
                this.isOpen = false;
                Element.setStyle(this.element, {
                    position: "absolute",
                    top: "0px",
                    left: "0px",
                    visibility: "hidden"
                });
            } else {
                this.isOpen = true;
            }
        } else {
            this.isOpen = this.parentMenu.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 == "menuContainer") {
                    if (node.tagName.toLowerCase() == "li") {
                        this.menuItems.push(new TabsItem(node, this));
                    }
                } else {
                    if (node.tagName.toLowerCase() == "ul") {
                        this.subMenu = new TabsContainer(node, this);
                    }
                }
                
                //support for buttons (float right)
                var type = $(node).readAttribute("type");
                if(type !=null){
                    if(type == "button"){
                        $(node).setStyle({ cssFloat: 'right', marginLeft: '10px', marginRight:'10px' })
                    } else if (type == "right") {
                        $(node).setStyle({ cssFloat: 'right', marginLeft: '10px', marginRight: '10px' })
                    }
                }
                
                
            }
        }
    },

    getBorders: function(element) {
        var ltrb = ["Left", "Top", "Right", "Bottom"];
        var result = {};
        for (var i = 0; i < ltrb.length; ++i) {
            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;
    },

    open: function() {
        if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
        this.parentMenu.closeAll(this);
        this.isOpen = true;
        if (this.menuType == "dropdown") {
            Element.setStyle(this.element, {
                left: (Position.positionedOffset(this.parent.element)[0]) + "px",
                top: (Position.positionedOffset(this.parent.element)[1] + Element.getHeight(this.parent.element)) + "px"
            });

        } else if (this.menuType == "flyout") {
            var parentMenuBorders = this.parentMenu ? this.parentMenu.getBorders() : new Object();
            var thisBorders = this.getBorders();
            if (
	      (Position.positionedOffset(this.parentMenu.element)[0] + this.parentMenu.element.offsetWidth + this.element.offsetWidth + 20) >
	      (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
	    ) {
                Element.setStyle(this.element, {
                    left: (-this.element.offsetWidth - (this.root.collapseBorders ? 0 : parentMenuBorders["left"])) + "px"
                });
            } else {
                Element.setStyle(this.element, {
                    left: (this.parentMenu.element.offsetWidth - parentMenuBorders["left"] - (this.root.collapseBorders ? Math.min(parentMenuBorders["right"], thisBorders["left"]) : 0)) + "px"
                });
            }
            Element.setStyle(this.element, {
                top: (this.parent.element.offsetTop - parentMenuBorders["top"] - this.menuItems[0].element.offsetTop) + "px"
            });
        }
        Element.setStyle(this.element, { visibility: "visible" });
    },

    close: function() {
        Element.setStyle(this.element, { visibility: "hidden" });
        this.isOpen = false;
        this.closeAll();
    },

    closeAll: function(trigger) {
        for (var i = 0; i < this.menuItems.length; ++i) {
            this.menuItems[i].closeItem(trigger);
        }
    },

    blurAll: function() {
        for (var i = 0; i < this.menuItems.length; ++i) {
            this.menuItems[i].blur();
        }
    }
}


var TabsItem = Class.create();

Object.extend(Object.extend(TabsItem.prototype, TabsContainer.prototype), {
    initialize: function(idOrElement, parent) {
        var menuItem = this;
        this.type = "menuItem";
        this.subMenu;
        this.init(idOrElement, parent);
        if (this.subMenu) {
            this.element.onmouseover = function() {
                menuItem.subMenu.open();
            }
        } else {
            if (this.root.quickCollapse) {
                this.element.onmouseover = function() {
                    menuItem.parentMenu.closeAll();
                }
            }
        }
        var linkTag = this.element.getElementsByTagName("A")[0];
        if (linkTag) {
            linkTag.onfocus = this.element.onmouseover;
            this.link = linkTag;
            this.text = linkTag.text;
        }
        if (this.subMenu) {
            this.element.onmouseout = function() {
                if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
                if (menuItem.root.closeDelayTimer) window.clearTimeout(menuItem.root.closeDelayTimer);
                eval(menuItem.root.name + ".closingMenuItem = menuItem");
                menuItem.root.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close()", menuItem.root.closeDelayTime);
            }
        }


        //bind click event
        this.mouseClickBoundListener = this.clickItem.bindAsEventListener(this);
        Event.observe(this.element, "click", this.mouseClickBoundListener);

    },

    openItem: function() {
        this.isOpen = true;
        if (this.subMenu) { this.subMenu.open(); }
    },

    closeItem: function(trigger) {
        this.isOpen = false;
        if (this.subMenu) {
            if (this.subMenu != trigger) this.subMenu.close();
        }
    },

    clickItem: function(event) {
        var tab = $(Event.element(event)).parentNode;


        if (tab.nodeName && tab.nodeName == "UL") {
            return;
        }
        // if the tabs are locked then don't allow this function to run. (unless the tab is a button)
        var tabType = tab.readAttribute("type");
        if ((!tabType || (tabType != "button" && tabType != "ddbutton")) && this.root.locked) {
            var onTabLockCode = $(this.root.rootContainer.element).readAttribute("onlock");
            if (onTabLockCode != null) {
                eval(onTabLockCode);
            }
            return;
        }

        if (this.root.selectedItem && this.root.selectedItem != tab.identify()) {
            if (!tabType || (tabType != "button" && tabType != "ddbutton")) {   //if this isn't a button then do this
                this.root.selectedItem = tab.identify();
                this.root.blur();
            }


            if (!tabType || (tabType != "button" && tabType != "ddbutton")) {
                if ($(this.root.selectedItem).hasClassName("tabBlur")) {
                    $(this.root.selectedItem).removeClassName("tabBlur");
                }
                $(this.root.selectedItem).addClassName("tabFocus");
                //$(this.root.selectedItem).setStyle({ backgroundColor: "silver" });
                var tabDiv = tab.readAttribute('divid');
                if (tabDiv != null) {
                    $(tabDiv).show();

                    // this allows iframes to reload so that they can be re-sized according to their content.
                    var iframes = $(tabDiv).select('iframe');
                    for (var j = 0; j < iframes.length; j++) {
                        if (iframes[j].getWidth() == 0) {
                            iframes[j].src = iframes[j].src;
                        }
                    }
                }
            }


            var onchangeCode = tab.readAttribute('select');
            if (onchangeCode != null) {
                eval(onchangeCode);
            }
        }
    },

    blur: function() {
        var skipHide = false;
        if (this.root.selectedItem != null && $(this.root.selectedItem) != null) {
            var tabType = $(this.root.selectedItem).readAttribute("type");
            if (tabType && (tabType == "button" || tabType == "ddbutton")) {
                skipHide = true;
            }
        }


        if (!skipHide) {
            if ($(this.element).hasClassName("tabFocus")) {
                $(this.element).removeClassName("tabFocus");
            }
            $(this.element).addClassName("tabBlur");
            //$(this.element).setStyle({ backgroundColor: "white" });
            var tabDiv = $(this.element).readAttribute('divid');
            if (tabDiv != null) {
                $(tabDiv).hide();
            }

            if (this.subMenu) {
                for (var x = 0; x < this.subMenu.menuItems.length; x++) {
                    this.subMenu.menuItems[x].blur();
                }
            }
        }
    }
});

