(function ($) {
    $.fn.jqDrag = function (h) { return i(this, h, 'd'); };
    $.fn.jqResize = function (h) { return i(this, h, 'r'); };
    $.jqDnR = { dnr: {}, e: 0,
        drag: function (v) {
            if (M.k == 'd') E.css({ left: M.X + v.pageX - M.pX, top: M.Y + v.pageY - M.pY });
            else E.css({ width: Math.max(v.pageX - M.pX + M.W, 0), height: Math.max(v.pageY - M.pY + M.H, 0) });
            return false;
        },
        stop: function () { $(document).unbind('mousemove', J.drag).unbind('mouseup', J.stop); }
    };
    var J = $.jqDnR, M = J.dnr, E = J.e,
i = function (e, h, k) {
    return e.each(function () {
        h = (h) ? $(h, e) : e;
        h.bind('mousedown', { e: e, k: k }, function (v) {
            var d = v.data, p = {}; E = d.e;
            // attempt utilization of dimensions plugin to fix IE issues
            if (E.css('position') != 'relative') { try { E.position(p); } catch (e) { } }
            M = { X: p.left || f('left') || 0, Y: p.top || f('top') || 0, W: f('width') || E[0].scrollWidth || 0, H: f('height') || E[0].scrollHeight || 0, pX: v.pageX, pY: v.pageY, k: d.k };
            $(document).mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
            return false;
        });
    });
},
f = function (k) { return parseInt(E.css(k)) || false; };

    $.fn.floatForm = function (options) {
        var defaults = {
            controlTag: '',
            draggable: true
        };
        var options = $.extend(defaults, options);
        if (options.controlTag == '') {
            return false;
        }
        return this.each(function () {
            var obj = $(this);
            var bg;
            if (options.draggable) {
                if ($(this).find(".floatingDialog_handle").length > 0) {
                    $(this).jqDrag(".floatingDialog_handle");
                } else {
                    $(this).jqDrag();
                }
            }
            $(options.controlTag).click(function (event) {
                var yScrolltop;
                var xScrollleft;
                if (self.pageYOffset || self.pageXOffset) {
                    yScrolltop = self.pageYOffset;
                    xScrollleft = self.pageXOffset;
                } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft) {     // Explorer 6 Strict 
                    yScrolltop = document.documentElement.scrollTop;
                    xScrollleft = document.documentElement.scrollLeft;
                } else if (document.body) {// all other Explorers
                    yScrolltop = document.body.scrollTop;
                    xScrollleft = document.body.scrollLeft;
                }
                //obj.css({"left":(event.clientX + xScrollleft - (obj.css("width").replace("px","")/2)) + "px","top":(event.clientY + document.body.scrollTop + yScrolltop  - (obj.css("height").replace("px","")/2))  + "px"})
                //obj.css({ "left": (event.clientX + xScrollleft - (obj.css("width").replace("px", "") / 2)) + "px", "top": (event.clientY + document.body.scrollTop + yScrolltop + 10) + "px" });
                obj.css({ 'top': $(document).scrollTop() + 20, 'left': ($(document).width() - obj.css("width").replace("px","")) / 2 });
                bg = gBGDiv(obj).show();
                bg.click(function () { obj.find('.floatingDialogClose').click(); });
                obj.show();
            });
            $('.floatingDialogClose', this).click(function () {
                obj.hide();
                if (!bg) bg = gBGDiv(obj);
                bg.hide();
            });
        });
    }
    function gBGDiv(o) {
        var bg = $("#jqDnrFloatDivBg");
        if (bg.length <= 0) {
            if (typeof (removeLayer) != "undefined")//需求分析修改：Requirement.aspx页面，为兼容IE6/7，增加移除遮罩层标识；
            {
                bg = $('<div id="jqDnrFloatDivBg"></div>').css({ 'background-color': '#d4e3f6', 'position': 'absolute', 'top': '0px', 'left': '0px', 'opacity': 0, 'z-index': -1 }).appendTo('body');
            }
            else {
                bg = $('<div id="jqDnrFloatDivBg"></div>').css({ 'background-color': '#000000', 'position': 'absolute', 'top': '0px', 'left': '0px', 'opacity': 0.6, 'z-index': Number($(o).css('z-index')) - 1 }).appendTo('body');
            }
        } else { bg.unbind(); }
        bg.css({ 'width': $(document).width(), 'height': $(document).height() });
        return bg;
    }
    window.CloseFloatingDialog = function () {
        $('.floatingDialogClose').click();
    }

})(jQuery);