﻿// JScript 文件
var Menu = Class.create();
Menu.prototype = {
    initialize: function(count, options) {
        this.Count = count;

        this.SetOptions(options);
        this.MenuPrefix = this.options.menuPrefix;
        this.SubmenuPrefix = this.options.submenuPrefix;
        this.ActiveMenuCSS = this.options.activeMenuCSS;
        this.MenuCSS = this.options.menuCSS;
        this.IconPrefix = this.options.iconPrefix;
        this.ExtendIcon = this.options.extentIcon;
        this.ContractIcon = this.options.contractIcon;
    },
    SetOptions: function(options) {
        this.options = {
            //默认值
            menuPrefix: '',     //主菜单前缀
            submenuPrefix: '',  //子菜单前缀
            menuCSS: '',        //菜单项样式
            activeMenuCSS: '',  //活动菜单项样式
            iconPrefix: '',     //菜单图标前缀
            extentIcon: '',      //菜单展开时图标
            contractIcon: ''     //菜单收缩时图标
        };
        Object.extend(this.options, options || {});
    },
    SwitchTo: function(index, callback) {
        for (var i = 0; i < this.Count; i++) {
            var menu = document.getElementById(this.MenuPrefix + i);
            var icon = document.getElementById(this.IconPrefix + i);
            var submenu = document.getElementById(this.SubmenuPrefix + i);
            if (i == index) {
                if (!isNull(menu))
                    menu.className = this.ActiveMenuCSS;

                if (!isNull(icon) && this.ExtendIcon != '')
                    icon.src = this.ExtendIcon;

                if (!isNull(submenu))
                    submenu.style.display = 'block';
            }
            else {
                if (!isNull(menu))
                    menu.className = this.MenuCSS;

                if (!isNull(icon) && this.ContractIcon != '')
                    icon.src = this.ContractIcon;

                if (!isNull(submenu))
                    submenu.style.display = 'none';
            }
        }

        if (typeof (callback) == 'function') {
            callback.apply();
        }
    }
}

var Tab = Class.create();
Tab.prototype = {
    initialize: function(count, options) {
        this.Count = count;

        this.SetOptions(options);
        this.TabPrefix = this.options.tabPrefix;
        this.STabPrefix = this.options.sTabPrefix;
        this.BodyPrefix = this.options.bodyPrefix;
        this.TabCSS = this.options.tabCSS;
        this.ActiveTabCSS = this.options.activeTabCSS;
    },
    SetOptions: function(options) {
        this.options = {
            //默认值
            tabPrefix: '', //主Tab前缀
            sTabPrefix: '', //副Tab前缀
            bodyPrefix: '', //Tab Body前缀
            tabCSS: '',
            activeTabCSS: ''
        };
        Object.extend(this.options, options || {});
    },
    SwitchTo: function(index, callback) {
        for (var i = 0; i < this.Count; i++) {
            var tab = document.getElementById(this.TabPrefix + i);
            var sTab = document.getElementById(this.STabPrefix + i);
            var body = document.getElementById(this.BodyPrefix + i);

            if (i == index) {
                if (!isNull(tab))
                    tab.className = this.ActiveTabCSS;

                if (!isNull(sTab))
                    sTab.style.display = 'block';

                if (!isNull(body))
                    body.style.display = 'block';
            }
            else {
                if (!isNull(tab))
                    tab.className = this.TabCSS;

                if (!isNull(sTab))
                    sTab.style.display = 'none';

                if (!isNull(body))
                    body.style.display = 'none';
            }
        }

        if (typeof (callback) == 'function') {
            callback.apply();
        }
    }
}

var DropMenu = Class.create();
DropMenu.prototype = {
    initialize: function(parent, menu, items, command, options) {
        var oThis = this;

        this.parent = parent;
        this.id = parent.id;
        this.menu = menu;
        this.items = items;
        this.command = command;

        this._fCK = function(e) { oThis.select(window.event || e); };
        this._fMV = function(e) { oThis.mouseOver(window.event || e); };
        this._fMT = function(e) { oThis.mouseOut(window.event || e); };
        this._fMD = function(e) { oThis.mouseDown(window.event || e); };

        this.setOptions(options);
        this.width = this.options.width;
        this.height = this.options.height;
        this.css = this.options.css;
        this.scroll = this.options.scroll;
    },
    setOptions: function(options) {
        this.options = {
            //默认值
            width: 120,
            height: 180,
            css: '',
            scroll: ''
        };
        Object.extend(this.options, options || {});
    },
    ResetMenu: function(items) {
        if (items != null)
            this.items = items;

        this.menu.innerHTML = '';
        for (var i = 0; i < this.items.length; i++) {
            var text = this.items[i].text;
            var value = this.items[i].value;

            var item = document.createElement('div');
            this.menu.appendChild(item);
            item.value = value;
            item.text = text;
            item.innerHTML = text;

            addEventHandler(item, 'mouseover', this._fMV);
            addEventHandler(item, 'mouseout', this._fMT);
            addEventHandler(item, 'click', this._fCK);
        }
    },
    DropDown: function() {
        this.ResetMenu();
        this.menu.style.display = 'block';

        var rect = this.getRect();
        this.menu.style.top = rect.top + 'px';
        this.menu.style.left = rect.left + 'px';
        this.menu.style.width = rect.width + 'px';

        if (rect.height > this.height) {
            this.menu.style.height = this.height + 'px';
            this.menu.className = this.scroll;
        }
        else {
            this.menu.style.height = rect.height + 'px';
            this.menu.className = this.css;
        }

        addEventHandler(document.body, 'mousedown', this._fMD);
    },
    getRect: function() {
        var rect = getElementRect(this.parent);
        var docRect = getDocumentRect();

        var t = rect.top + rect.height;
        var h = this.menu.offsetHeight;

        //如果控件超出了窗口底部，根据控件跟窗口顶部和底部的距离确定控件显示位置
        if (t + h > docRect.top + docRect.height) {
            if (rect.top - docRect.top > docRect.top + docRect.height - rect.top + rect.height) {
                t = rect.top - h;
                if (t < 0) {
                    t = 0;
                    h = rect.top;
                }
            }
            else {
                h = docRect.top + docRect.height - rect.top + rect.height;
            }
        }

        return { left: rect.left, top: t, width: this.width, height: h };
    },
    drawIn: function() {
        this.menu.innerHTML = '';
        this.menu.style.display = 'none';
        removeEventHandler(document.body, 'mousedown', this._fMD);
    },
    select: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj)) {
            if (typeof (this.command) == 'function') {
                this.command.call(obj, obj.value, obj.text);
            }
            else if (typeof (this.command) == 'string') {
                eval(this.command + '("' + obj.value + '","' + obj.text + '")');
            }
        }

        this.drawIn();
    },
    mouseOver: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj))
            obj.className = 'active';
    },
    mouseOut: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj))
            obj.className = '';
    },
    mouseDown: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        do {
            if (obj == this.menu)
                return;

            if (obj.tagName.toLowerCase() == "body") {
                this.drawIn();
                return;
            };
            obj = obj.parentNode;
        } while (obj.parentNode);
    }
}

var ComboMenu = Class.create();
ComboMenu.prototype = {
    initialize: function(parent, drop, menu, tip, options) {
        var oThis = this;

        this.parent = parent;
        this.drop = drop;
        this.menu = menu;
        this.tip = tip;

        this.f_click = function(e) { oThis.select(window.event || e); };
        this.f_mouseOver = function(e) { oThis.mouseOver(window.event || e); };
        this.f_mouseOut = function(e) { oThis.mouseOut(window.event || e); };
        this.f_mouseDown = function(e) { oThis.mouseDown(window.event || e); };
        this.f_showMenu = function(e) { oThis.showMenu(window.event || e); };
        this.f_hideMenu = function(e) { oThis.hideMenu(window.event || e); };

        this.setOptions(options);
        this.dropWidth = this.options.dropWidth;
        this.menuWidth = this.options.menuWidth;
        this.css = this.options.css;
        this.scroll = this.options.scroll;

        this.dropItemsCmd = null;
    },
    setOptions: function(options) {
        this.options = {
            //默认值
            dropWidth: 120,
            menuWidth: 240,
            css: '',
            scroll: ''
        };
        Object.extend(this.options, options || {});
    },
    showTip: function() {
        var rect = getElementRect(this.parent);

        this.tip.style.top = rect.top + rect.height + 'px';
        this.tip.style.left = rect.left + 'px';
        this.tip.style.width = this.dropWidth + 'px';
        this.tip.style.display = 'block';
    },
    hideTip: function() {
        this.tip.style.display = 'none';
    },
    DropDown: function(items, command) {
        this.dropItemsCmd = command;
        this.resetDrop(items);

        this.drop.style.display = 'block';
        var rect = this.getDropRect();
        this.drop.style.top = rect.top + 'px';
        this.drop.style.left = rect.left + 'px';
        this.drop.style.width = rect.width + 'px';

        if (rect.height > this.height) {
            this.drop.style.height = this.height + 'px';
            this.drop.className = this.scroll;
        }
        else {
            this.drop.style.height = rect.height + 'px';
            this.drop.className = this.css;
        }

        addEventHandler(document.body, 'mousedown', this.f_mouseDown);
    },
    drawIn: function() {
        this.drop.innerHTML = '';
        this.drop.style.display = 'none';
        removeEventHandler(document.body, 'mousedown', this.f_mouseDown);
    },
    resetDrop: function(items) {
        this.drop.innerHTML = '';
        for (var i = 0; i < items.length; i++) {
            var text = items[i].text;
            var value = items[i].value;

            var item = document.createElement('div');
            this.drop.appendChild(item);
            item.value = value;
            item.text = text;
            item.innerHTML = text;

            addEventHandler(item, 'mouseover', this.f_mouseOver);
            addEventHandler(item, 'mouseout', this.f_mouseOut);
            addEventHandler(item, 'click', this.f_click);
        }
    },
    showMenu: function(e) {
        this.menu.style.display = 'block';

        var rect = this.getMenuRect();
        this.menu.style.top = rect.top + 2 + 'px';
        this.menu.style.left = rect.left + 'px';

        addEventHandler(document.body, 'mousedown', this.f_mouseDown);
    },
    hideMenu: function(e) {
        this.menu.style.display = 'none';
        removeEventHandler(document.body, 'mousedown', this.f_mouseDown);
    },
    getDropRect: function() {
        var rect = getElementRect(this.parent);
        var docRect = getDocumentRect();

        var t = rect.top + rect.height;
        var h = this.drop.offsetHeight;

        //如果控件超出了窗口底部，根据控件跟窗口顶部和底部的距离确定控件显示位置
        if (t + h > docRect.top + docRect.height) {
            if (rect.top - docRect.top > docRect.top + docRect.height - rect.top + rect.height) {
                t = rect.top - h;
                if (t < 0) {
                    t = 0;
                    h = rect.top;
                }
            }
            else {
                h = docRect.top + docRect.height - rect.top + rect.height;
            }
        }

        return { left: rect.left, top: t, width: this.dropWidth, height: h };
    },
    getMenuRect: function() {
        var rect = getElementRect(this.parent);
        var docRect = getDocumentRect();

        var w = this.menu.offsetWidth;
        var l = rect.left + rect.width - w;

        var t = rect.top + rect.height;
        var h = this.menu.offsetHeight;

        //如果控件超出了窗口底部，根据控件跟窗口顶部和底部的距离确定控件显示位置
        if (t + h > docRect.top + docRect.height) {
            if (rect.top - docRect.top > docRect.top + docRect.height - rect.top + rect.height) {
                t = rect.top - h;
                if (t < 0) {
                    t = 0;
                    h = rect.top;
                }
            }
            else {
                h = docRect.top + docRect.height - rect.top + rect.height;
            }
        }

        return { left: l, top: t, width: w, height: h };
    },
    select: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj)) {
            if (typeof (this.dropItemsCmd) == 'function') {
                this.dropItemsCmd.call(obj, obj.value, obj.text);
            }
            else if (typeof (this.dropItemsCmd) == 'string') {
                eval(this.dropItemsCmd + '("' + obj.value + '","' + obj.text + '")');
            }
        }

        this.drawIn();
    },
    mouseOver: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj))
            obj.className = 'active';
    },
    mouseOut: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        if (!isNull(obj))
            obj.className = '';
    },
    mouseDown: function(e) {
        var obj = e.target ? e.target : event.srcElement;
        do {
            if (obj == this.drop || obj == this.menu)
                return;

            if (obj.tagName.toLowerCase() == "body") {
                this.drawIn();
                this.hideMenu();
                return;
            };
            obj = obj.parentNode;
        } while (obj.parentNode);
    }
}