﻿var WQDialog_v1 = {
    isNull: true,
    $dialogDiv: null,
    $overlay: null,
    $dialogWindow: null,
    $dialogTitle: null,
    $dialogContent: null,
    $dialogIcon: null,
    $dialogText: null,
    $dialogBtnsPanel: null,

    init: function () {
        if (this.isNull) {
            this.$dialogDiv = $("#WQDialog");
            this.$dialogDiv.append('<div class="dialog_lightOff fixed-top"></div>');
            this.$overlay = $("#WQDialog .dialog_lightOff");

            this.$overlay.append('<div class="dialog_window"></div>');
            this.$dialogWindow = $('#WQDialog .dialog_window');

            this.$dialogWindow.append('<div class="dialog_title"></div><div class="dialog_content"></div>');
            this.$dialogTitle = $('#WQDialog .dialog_title');
            this.$dialogContent = $('#WQDialog .dialog_content');
            this.$dialogTitle.css('height', 0);

            this.$dialogContent.append('<div class="dialog_icon"></div><div class="dialog_tipText"></div><div class="dialog_button_panel"></div>');
            this.$dialogIcon = $('#WQDialog .dialog_icon');
            this.$dialogText = $('#WQDialog .dialog_tipText');
            this.$dialogBtnsPanel = $('#WQDialog .dialog_button_panel');

            this.$dialogWindow.draggable();
            this.$overlay.css('height', $(window).height());

            $(window).resize(
            function () {
                WQDialog_v1.setCenter();
            });

            this.isNull = false;
        }
    },

    clearButtons: function () {
        this.$dialogBtnsPanel.html("");
        this.$dialogBtnsPanel.css(
        {
            'height': 0,
            'padding': 0,
            'margin': 0
        });
    },

    addButton: function (buttonId, buttonText) {
        this.$dialogBtnsPanel.append("<a class='dialog_button' id='" + buttonId + "'>" + buttonText + "</a>");
        var $btn = $('#' + buttonId);

        this.$dialogBtnsPanel.css(
        {
            'padding': '10px 15px',
            'width': WQDialog_v1.getSize(WQDialog_v1.$dialogBtnsPanel.parent()).width - 30,
            'height': ''
        });

        $btn.hover(
        function () {
            $btn.addClass("dialog_button_hover");
        },
        function () {
            $btn.removeClass("dialog_button_hover").removeClass("dialog_button_down");
        });

        $btn.mousedown(
        function () {
            $btn.addClass("dialog_button_down");
        });

        $btn.mouseup(
        function () {
            $btn.removeClass("dialog_button_down");
        });
    },

    setTitle: function (title) {
        this.$dialogTitle.text(title);
        this.$dialogTitle.css({
            'padding': '10px 15px',
            'width': WQDialog_v1.getSize(WQDialog_v1.$dialogTitle.parent()).width - 30,
            'height': ''
        });
    },

    setIcon: function (iconUrl, width, height) {
        if (width == null) {
            width = 42;
        }
        if (height == null) {
            height = 42;
        }
        this.$dialogIcon.css({
            "background-image": "url(" + iconUrl + ")",
            'width': width,
            'height': height,
            'margin': '30px 20px 0 33px'
        });
    },

    setTipText: function (tipText) {
        this.$dialogText.html(tipText);
    },

    setTheme: function (theme) {
        this.$dialogWindow.addClass(theme);
    },

    setOpenClickEvenButton: function (buttonId) {
        $("#" + buttonId).unbind("click");
        $("#" + buttonId).click(
		function () {
		    WQDialog_v1.openDialog();
		});
    },
    setCloseClickEvenButton: function (buttonId) {
        $("#" + buttonId).unbind("click");
        $("#" + buttonId).click(
		function () {
		    WQDialog_v1.closeDialog();
		});
    },
    setCenter: function () {
        var $element = $(".dialog_window");
        var vSize = WQDialog_v1.getSize($element);
        var vWidth = vSize.width;
        var vHeight = vSize.height;

        $element.css(
        {
            width: vWidth,
            left: ($(window).width() - vWidth) / 2,
            top: Math.max(($(window).height() - vHeight) / 2, 0)
        });
        this.$overlay.css('height', $(window).height());
    },
    openDialog: function () {
        if (WQDialog_v1.$overlay.css('display') == 'none') {
            if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) <= 9) {
                WQDialog_v1.$overlay.show();
                WQDialog_v1.setCenter();
            }
            else {
                WQDialog_v1.$overlay.stop().fadeToggle(500);
                WQDialog_v1.setCenter();
            }
        }
    },
    closeDialog: function () {
        if (WQDialog_v1.$overlay.css('display') != 'none') {
            if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) <= 9) {
                WQDialog_v1.$overlay.hide();
            }
            else {
                WQDialog_v1.$overlay.stop().fadeToggle(500);
            }
        }
    },
    getSize: function (element) {
        var $element = $(element);
        var padding_left = isNaN(parseInt($element.css('padding-left'))) ? 0 : parseInt($element.css('padding-left'));
        var padding_right = isNaN(parseInt($element.css('padding-right'))) ? 0 : parseInt($element.css('padding-right'));
        var padding_top = isNaN(parseInt($element.css('padding-top'))) ? 0 : parseInt($element.css('padding-top'));
        var padding_bottom = isNaN(parseInt($element.css('padding-bottom'))) ? 0 : parseInt($element.css('padding-bottom'));
        var margin_left = isNaN(parseInt($element.css('margin-left'))) ? 0 : parseInt($element.css('margin-left'));
        var margin_right = isNaN(parseInt($element.css('margin-right'))) ? 0 : parseInt($element.css('margin-right'));
        var margin_top = isNaN(parseInt($element.css('margin-top'))) ? 0 : parseInt($element.css('margin-top'));
        var margin_bottom = isNaN(parseInt($element.css('margin-bottom'))) ? 0 : parseInt($element.css('margin-bottom'));

        var vWidth = $element.width() + padding_left + padding_right + margin_left + margin_right;
        var vHeight = $element.height() + padding_top + padding_bottom + margin_top + margin_bottom;
        return { width: vWidth, height: vHeight };
    }
};

var WQDialog = {
    num: 0,

    lock: new Array(),

    loadCss: function (file) {
        var head = document.getElementsByTagName("head").item(0);
        var cssTag = document.createElement("link");
        cssTag.href = file;
        cssTag.rel = "stylesheet";
        cssTag.type = "text/css";
        head.appendChild(cssTag);
    },

    setCustomDialog: function (obj) { //element, bind, binds, border
        this.loadCss('../Content/WQDialog.css');
        this.num++;
        this.lock.push(false);

        var $body = $('body');
        if ($body.css('position') == 'static') {
            $body.css('position', 'relative');
        }
        if ($body.css('float') == 'none') {
            $body.css('float', 'left');
        }
        $body.append('<div class="WQ_overlay fixed-top" id="WQ_overlay_' + (this.num - 1) + '" style="display:none"></div>');
        var $overlay = $('#WQ_overlay_' + (this.num - 1));

        if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) < 8) {
            $overlay.css({
                'position': 'absolute',
                'z-index': 9999
            });
        }
        $overlay.css('height', $(window).height());
        $overlay.append('<div class="WQ_customDialog"></div>')
        var $customDialog = $overlay.children('.WQ_customDialog');

        if (obj.border == true) {
            $customDialog.append('<div class="WQ_dialogBorder"></div>');
            var $border = $customDialog.children('.WQ_dialogBorder');
            $border.append('<div class="btnClose borderButton">x</div>');
            $('.WQ_dialogBorder .btnClose').click(
            function () {
                WQDialog.closeDialog(WQDialog.num - 1);
            });
        }

        var $element = $(obj.element);
        $body.append($element);

        var padding_left = isNaN(parseInt($element.css('padding-left'))) ? 0 : parseInt($element.css('padding-left'));
        var padding_right = isNaN(parseInt($element.css('padding-right'))) ? 0 : parseInt($element.css('padding-right'));
        var padding_top = isNaN(parseInt($element.css('padding-top'))) ? 0 : parseInt($element.css('padding-top'));
        var padding_bottom = isNaN(parseInt($element.css('padding-bottom'))) ? 0 : parseInt($element.css('padding-bottom'));
        var margin_left = isNaN(parseInt($element.css('margin-left'))) ? 0 : parseInt($element.css('margin-left'));
        var margin_right = isNaN(parseInt($element.css('margin-right'))) ? 0 : parseInt($element.css('margin-right'));
        var margin_top = isNaN(parseInt($element.css('margin-top'))) ? 0 : parseInt($element.css('margin-top'));
        var margin_bottom = isNaN(parseInt($element.css('margin-bottom'))) ? 0 : parseInt($element.css('margin-bottom'));

        var vWidth = $element.width() + padding_left + padding_right + margin_left + margin_right;
        var vHeight = $element.height() + padding_top + padding_bottom + margin_top + margin_bottom;

        $customDialog.css(
        {
            width: vWidth,
            left: ($(window).width() - vWidth) / 2,
            top: Math.max(($(window).height() - vHeight) / 2, 0)
        });
        $(window).resize(function () {
            $customDialog.css(
        {
            left: ($(window).width() - vWidth) / 2,
            top: Math.max(($(window).height() - vHeight) / 2, 0)
        });
            $overlay.css('height', $(window).height());
        });
        $customDialog.append($element).draggable();
        $element.show();

        if (obj.bind != null) {
            $(obj.bind[0]).bind(obj.bind[1], function () {
                WQDialog.openDialog(WQDialog.num - 1);
            });
        }
        else if (obj.binds != null) {
            for (var i in obj.binds) {
                $(obj.binds[i][0]).bind(obj.binds[i][1], function () {
                    WQDialog.openDialog(WQDialog.num - 1);
                });
            }
        }
        return this.num;
    },

    setLoginDialog: function (obj) { //element, bind, binds, border
        this.loadCss('../Content/WQDialog.css');
        this.num++;
        this.lock.push(false);

        var $body = $('body');
        if ($body.css('position') == 'static') {
            $body.css('position', 'relative');
        }
        if ($body.css('float') == 'none') {
            $body.css('float', 'left');
        }
        $body.append('<div class="WQ_overlay fixed-top" id="WQ_overlay_' + (this.num - 1) + '" style="display:none"></div>');
        var $overlay = $('#WQ_overlay_' + (this.num - 1));

        if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) < 8) {
            $overlay.css({
                'position': 'absolute',
                'z-index': 9999
            });
        }
        $overlay.css('height', $(window).height());
        $overlay.append('<form class="wq_login_dialog" id="wq_login_dialog" style="width:512px;height:363px;" action="/Account/Logon" method="post"></form>')
        var $loginDialog = $overlay.children('#wq_login_dialog');

        var htmlCloseBtn = '<div class="wq_login_dialog_close_btn" id="wq_login_dialog_close_btn"></div>';
        var htmlForm = '<div class="wq_login_dialog_form">' +
            '<label for="wq_login_dialog_form_username">登录邮箱</label>' +
            '<input type="text" id="wq_login_dialog_form_username" name="UserName"/>' +
            '<label for="wq_login_dialog_form_password">密码</label>' +
            '<input type="password" id="wq_login_dialog_form_password" name="Password"/>' +
            '<a style="margin-top:15px;" href="/Account/ForgotPassword">忘记密码？</a>' +
            '<input type="hidden" name="RememberMe" id="wq_login_dialog_form_remember" value="false"/>' +
            '<div><div class="wq_login_dialog_form_checkbox"></div><span>记住我的登入状态？</span></div>' +
            '<div class="wq_login_dialog_form_btn_panel">' +
            '<input type="submit" value="登录" class="wq_login_dialog_form_btn"/>' +
            '<a style="width:107px" href="/Account/Register"><input type="button" value="注册" class="wq_login_dialog_form_btn" style="float:right;" id="wq_login_dialog_form_register"/></a>' +
            '</div>' +
            '</div>';

        $loginDialog.append(htmlCloseBtn + htmlForm);
        $('#wq_login_dialog_close_btn').click(
        function () {
            WQDialog.closeDialog(WQDialog.num - 1);
        });

        var vWidth = 512;
        var vHeight = 363;

        $loginDialog.css(
        {
            left: ($(window).width() - vWidth) / 2,
            top: Math.max(($(window).height() - vHeight) / 2, 0)
        });

        $(window).resize(function () {
            $loginDialog.css(
        {
            left: ($(window).width() - vWidth) / 2,
            top: Math.max(($(window).height() - vHeight) / 2, 0)
        });
            $overlay.css('height', $(window).height());
        });
        $loginDialog.draggable();

        if (obj.bind != null) {
            $(obj.bind[0]).bind(obj.bind[1], function () {
                WQDialog.openDialog(WQDialog.num - 1);
            });
        }
        else if (obj.binds != null) {
            for (var i in obj.binds) {
                $(obj.binds[i][0]).bind(obj.binds[i][1], function () {
                    WQDialog.openDialog(WQDialog.num - 1);
                });
            }
        }
        return this.num;
    },

    setSimpleDialog: function (obj) {   //title, text, ok, cancel, bind, binds
        if (this.num == 0)
            this.loadCss('../Content/WQDialog.css');

        this.num++;
        this.lock.push(false);

        var id = this.num - 1;

        var $body = $('body');
        if ($body.css('position') == 'static') {
            $body.css('position', 'relative');
        }
        if ($body.css('float') == 'none') {
            $body.css('float', 'left');
        }
        $body.append('<div class="WQ_overlay fixed-top" id="WQ_overlay_' + (this.num - 1) + '" style="display:none"></div>');
        var $overlay = $('#WQ_overlay_' + (this.num - 1));

        if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) < 8) {
            $overlay.css({
                'position': 'absolute',
                'z-index': 9999
            });
        }
        $overlay.css('height', $(window).height());

        $overlay.append('<div class="WQ_dialog ignorePosition"></div>');
        var $dialog = $overlay.children('.WQ_dialog');
        this.setDialogGrid({
            rows: 3,
            cols: 7,
            unit_width: 60,
            pos: $dialog
        });

        if (obj.title != null) {
            $dialog.append('<div class="WQ_dialogTitle">' + obj.title + '</div>');
            var $dialogTitle = $dialog.children('.WQ_dialogTitle');
        }

        $dialog.append('<div class="WQ_dialogContent"></div>');
        var $dialogContent = $dialog.children('.WQ_dialogContent');
        $dialogContent.append('<div class="WQ_dialogText">' + obj.text + '</div><div class="WQ_dialogButtonPanel" style="margin-top:20px;"></div>');
        var $dialogButtonsPanel = $dialogContent.children('.WQ_dialogButtonPanel');
        var $dialogText = $dialogContent.children('.WQ_dialogText');
        $dialogButtonsPanel.append('<div class="WQ_dialogBtnOK appButton button" style="width:80px;">确定</div><div class="WQ_dialogBtnCancel appButton button" style="width:80px;">取消</div>');
        var $btnOK = $dialogButtonsPanel.children('.WQ_dialogBtnOK');
        var $btnCancel = $dialogButtonsPanel.children('.WQ_dialogBtnCancel');

        $dialog.draggable();
        $(".WQ_dialogBtn").each(
        function () {
            $(this).hover(
            function () {
                $(this).addClass("WQ_dialogBtn_hover");
            },
            function () {
                $(this).removeClass("WQ_dialogBtn_hover").removeClass("WQ_dialogBtn_down");
            });

            $(this).mousedown(
            function () {
                $(this).addClass("WQ_dialogBtn_down");
            });

            $(this).mouseup(
            function () {
                $(this).removeClass("WQ_dialogBtn_down");
            });
        });

        $dialog.css(
        {
            left: ($(window).width() - 462) / 2,
            top: Math.max(($(window).height() - 232) / 2, 0)
        });
        $(window).resize(function () {
            $dialog.css(
            {
                left: ($(window).width() - 462) / 2,
                top: Math.max(($(window).height() - 232) / 2, 0)
            });
            $overlay.css('height', $(window).height());
        });

        $btnOK.click(obj.ok);
        $btnOK.click(
        function () {
            WQDialog.closeDialog(id);
        });
        if (obj.cancel == null) {
            $btnCancel.click(function () {
                if ($overlay.css('display') != 'none') {
                    WQDialog.closeDialog(id);
                }
            });
        }
        else {
            $btnCancel.click(obj.cancel);
        }

        if (obj.bind != null) {
            $(obj.bind[0]).bind(obj.bind[1], function () {
                WQDialog.openDialog(id);
            });
        }
        else if (obj.binds != null) {
            for (var i in obj.binds) {
                $(obj.binds[i][0]).bind(obj.binds[i][1], function () {
                    WQDialog.openDialog(id);
                });
            }
        }
        return this.num;
    },

    openDialog: function (id) {
        if (id == null) {
            id = WQDialog.num - 1;
        }
        if (WQDialog.lock[id]) {
            return;
        }
        $overlay = $('#WQ_overlay_' + id);
        if ($overlay.css('display') == 'none') {
            if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) <= 9) {
                $overlay.show();
            }
            else {
                $overlay.stop().fadeToggle(200);
            }
        }
    },

    closeDialog: function (id) {
        if (id == null) {
            id = WQDialog.num - 1;
        }
        $overlay = $('#WQ_overlay_' + id);
        if ($overlay.css('display') != 'none') {
            if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) <= 9) {
                $overlay.hide();
            }
            else {
                $overlay.stop().fadeToggle(200);
            }
        }
    },

    setIEFixed: function (obj) {
        if (window.ActiveXObject && window.navigator.appVersion.substr(22, 1) < 8) {
            var $element = $(obj.element);
            var fixed = obj.fixed;
            var top = isNaN(parseInt($element.css('top'))) ? 0 : parseInt($element.css('top'));
            var left = isNaN(parseInt($element.css('left'))) ? 0 : parseInt($element.css('left'));

            $element.css(
            {
                position: 'absolute'
            });
            if (fixed == null || fixed === 'top') {
                $(window).scroll(function () {
                    $element.css('top', document.documentElement.scrollTop);
                });
            }
            else if (fixed === 'left') {
                $(window).scroll(function () {
                    $element.css('left', document.documentElement.scrollLeft + left);
                });
            }
        }
    },

    setDialogGrid: function (obj) {
        var UNIT_WIDTH = obj.unit_width;
        var WIDTH = obj.cols;
        var LINE = obj.rows;
        var $insertPos = obj.pos;

        var width = UNIT_WIDTH * WIDTH;
        var height = UNIT_WIDTH * LINE;
        var grids = '<div class="wq_dialog_grids" style="width:' + width + 'px;height:' + height + 'px">';

        for (var i = 0; i < LINE; i++) {
            for (var j = 0; j < WIDTH; j++) {
                var color = Math.round(Math.random() * 60).toString();
                if (color.length < 2)
                    color = '0' + color;
                color = color + color + color;

                grids += '<div style="background:#' + color + ';width:' + UNIT_WIDTH + 'px;height:' + UNIT_WIDTH + 'px"></div>';
            }
        }
        grids += '</div><div class="wqdialog_grids_gradient0" style="width:' + width + 'px;height:' + height +
            'px"></div><div class="wqdialog_grids_gradient1" style="width:' + width + 'px;height:' + height + 'px"></div>';
        $insertPos.prepend(grids);
    }
}
