﻿(function($) {
    $.fn.winmenu = function(options) {
        var me = this;
        var showing = false;
        var element = null;
        var elm_shadow = null;
        var elm_subCont = [];
        var needFixPng = false;
        options = $.extend({
            menuData: [],
            title: null,
            clsBtn: true,
            showline: true,
            shadowOffset: 4,
            dragable: false,
            Opacity: 1,
            placeIn: document.body,
            dynamicLoad: false,
            click2cls: true,
            autoCls: true,
            autoActive: true,
            btn: null,
            displace: [0, 0],
            ableExpandSubmenu: true,
            noneInfo: '空',
            zIndex: 999999,
            allInOne: false,
            onShow: null,
            onHide: null,
            sDivCss: 'menu-div',
            sOutCss: 'menu-item',
            sOverCss: 'menu-over',
            sDownCss: 'menu-down',
            sDisableCss: 'menu-disabled',
            sActiveCss: 'menu-active',
            Lang: {
                close: '关闭',
                loading: '读取中...'
            }
        }, options);
        var isChild = function(element, ancestor) {
            if (element.compareDocumentPosition)
                return (element.compareDocumentPosition(ancestor) & 8) === 8;

            if (ancestor.contains)
                return ancestor.contains(element) && ancestor !== element;

            while (element = element.parentNode)
                if (element == ancestor) return true;

            return false;
        };
        var testHandleHover = function(event) {
            var node = event.target, type = event.type, currentTarget = event.currentTarget;
            if (currentTarget && currentTarget.tagName) {
                if (type === 'load' || type === 'error' || (type === 'click' && currentTarget.tagName.toLowerCase() === 'input' && currentTarget.type === 'radio'))
                    node = currentTarget;
            }
            if (node.nodeType == 3)
                node = node.parentNode;
            return node;
        };
        var checkMouse = function(e) {
            var p = testHandleHover(e);
            if (p != options.btn && element && !isChild(p, element)) {
                hide();
            }
        }
        var eventCancelBubble = function(event) {
            event.cancelBubble = true;
            if (event) {
                event.stopPropagation();
            }
        }
        buildWin();
        if (options.autoCls) {
            $(options.btn).bind('click', eventCancelBubble);
            $(document).bind('click', checkMouse);
        }
        function buildWin() {
            needFixPng = checkNeedFixPng();
            element = document.createElement("div")
            element.className = options.sDivCss;
            var divWin = document.createElement('table');
            divWin.cellSpacing = '0';
            divWin.cellPadding = '0';
            divWin.style.border = '0px';
            divWin.style.borderCollapse = "separate";
            //divWin.className = options.sDivCss;
            // $(divWin).css
            $(options.menuData).each(function(sm) {
            var _tr = divWin.insertRow(0);
                var _td = document.createElement("td");
                var _subCont = document.createElement("div");
                _subCont.className = "menu-content";
                _subCont.appendChild(document.createTextNode(options.Lang.loading));
                _td.appendChild(_subCont);
                _tr.appendChild(_td);
                elm_subCont.push(_subCont);
                var _td = document.createElement("td");
                //_td.style.cssText = "height:5px;background: url(http://www.p5w.net/images/ShadowRight.png) no-repeat left top";
                if (needFixPng) {
                    _td.className = "menu-right pngfix";
                } else {
                    _td.className = "menu-right";
                }
                _tr.appendChild(_td);

                var _tr = divWin.insertRow(1);
                var _td = document.createElement("td");
                //_td.style.cssText = "height:5px;background: url(http://www.p5w.net/images/ShadowBottom.png) no-repeat left top";
                if (needFixPng) {
                    _td.className = "menu-bottom pngfix";
                }
                else {
                    _td.className = "menu-bottom";
                }
                _tr.appendChild(_td);

                var _td = document.createElement("td");
                //_td.style.cssText = "height :5px; width:5px;background:url(http://www.p5w.net/images/ShadowRightBottom.png) no-repeat left top";
                if (needFixPng) {
                    _td.className = "menu-right-bottom pngfix";
                }
                else {
                    _td.className = "menu-right-bottom";
                }
                _tr.appendChild(_td);

            });
            element.appendChild(divWin);
            options.placeIn.appendChild(element);
            if (options.showline) {
                var divShadow = document.createElement('div');
                elm_shadow = divShadow;
                divShadow.style.cssText = "POSITION: absolute;FONT-SIZE: 1px; OVERFLOW: hidden;background :#FFFFFF; ";
                $(elm_shadow).hide();
                options.placeIn.appendChild(divShadow);
            }
            $(element).hide();
            buildSubContent(0);
        }
        function buildSubContent(index) {
            if (isNaN(index)) { return; }
            if (!options.menuData[index] || !elm_subCont[index]) {
                return;
            }
            if (elm_subCont[index].style.display != 'none' && elm_subCont[index].innerHTML == options.Lang.loading) {
                doBuildSubContent(index);
            }
            if (index < options.menuData.length - 1) {
                setTimeout(me.buildSubContent(index + 1), 10);
            }
        }
        function doBuildSubContent(index) {
            if (options.menuData[index].data.constructor == Array) {
                var divSubContent = getSubContent(index);
            } else if (options.menuData[index].data.constructor == Function) {
                var divSubContent = options.menuData[index].data(index);
            }
            if (elm_subCont[index].style.display == 'none') {
                $(divSubContent).hide();
            }
            if (divSubContent.innerHTML == '') {
                divSubContent.innerHTML = options.noneInfo;
            }
            elm_subCont[index].parentNode.insertBefore(divSubContent, elm_subCont[index]);
            $(elm_subCont[index]).remove();
            elm_subCont[index] = divSubContent;
        }
        function getSubContent(index) {
            sm = options.menuData[index];
            var divSubContent = document.createElement('div');
            divSubContent.className = 'menu-content';
            if (sm.isDivCssEx) {
                $(divSubContent).addClass(sm.isDivCssEx);
            }
            $(sm.data).each(function(i, si) {
                var si = { text: si.text || '', title: si.title || '', action: si.action || null, value: (si.value || !isNaN(si.value)) ? si.value : null, active: si.active || false, disabled: si.disabled || false };
                var divSI = document.createElement('div');
                divSI.className = options.sOutCss;
                if (si.active) {
                    $(divSI).addClass(options.sActiveCss);
                }
                if (si.disabled) { $(divSI).addClass(options.sDisableCss) };

                divSI.innerHTML = si.text;
                if (si.text) {
                    divSI.title = (si.title || si.text);
                }
                var G = $(divSI);
                G.bind('mouseover', function(e) {
                    if (si.disabled) { return; }
                    $(divSI).addClass(options.sOverCss);
                });
                G.bind('mouseout', function(e) {
                    if (si.disabled) { return; }
                    $(divSI).removeClass(options.sOverCss);
                    $(divSI).removeClass(options.sDownCss);
                });
                G.bind('mousedown', function(e) {
                    if (si.disabled) { return; }
                    $(divSI).addClass(options.sDownCss);
                });
                G.bind('mouseup', function(e) {
                    if (si.disabled) { return; }
                    $(divSI).removeClass(options.sDownCss);
                });
                G.bind('click', function(e) {
                    if (si.disabled) { return; }
                    if (typeof si.action == "function") {
                        (si.action)(si.value);
                    }
                    if (options.click2cls) {
                        me.hide();
                    }
                });
                divSubContent.appendChild(divSI);
            });
            return divSubContent;
        }
        var hide = this.hide = function() {

            // if (options.autoCls) {
            //     $(options.btn).unbind('click', eventCancelBubble);
            //     $(document).unbind('click', checkMouse);
            // }
            // $(element).remove();
            // if (options.showline) {
            //     $(elm_shadow).remove();
            // }
            $(element).hide();
            if (options.showline) {
                $(elm_shadow).hide();
            }
            $(options.btn).removeClass("loginName-down");
        }
        var popup = this.popup = function() {
            // if (showing) { return; }
            if (!element) {
                buildWin();
            }
            element.style.position = 'absolute';
            var pos = $(options.btn).offset();
            element.style.left = pos.left + (options.displace[0] || 0) + 'px';
            element.style.top = (pos.top + (options.displace[1] || 0) + parseInt(options.btn.offsetHeight) - 3) + 'px';
            element.style.zIndex = options.zIndex;
            if (options.showline) {
                elm_shadow.style.left = parseInt(element.style.left) + 1 + 'px';
                elm_shadow.style.top = parseInt(element.style.top) + 'px';
                elm_shadow.style.zIndex = options.zIndex + 1;
                elm_shadow.style.width = $(options.btn).width() + 20 + 'px';
                elm_shadow.style.height = '2px';
                $(elm_shadow).show();
            }
            $(options.btn).addClass("loginName-down");
            $(element).show();
            //$(element).slideDown(1000);
            //showing = true;
        }
        function checkNeedFixPng() {
            var _b = $.browser;
            return _b.msie && parseFloat(_b.version) < 7.0 ? true : false;
        };
        return this;
    };
})(jQuery);
