﻿jQuery.fn.extend({
    dialog: function() {
        var defaults = {
            width: 400,
            height: 200,
            filter: 0.7,
            nav_bg: "icon_close.jpg",
            title: 'My Dialog'
        };

        if (arguments.length != 0) {
            $.each(arguments[0], function(key, value) {
                defaults[key] = value;
            });
        }
        this.css({
            width: defaults.width + "px",
			'min-height': defaults.height + "px",
            '_height': defaults.height + "px",
            position: 'absolute',
            border: '2px solid #000000',
            display: 'none',
            padding: '0px 0px 15px 0px',
            'background-color': '#FFFFFF',
            'z-index': 501
        });



        $(this.selector + ">div").css('float', 'left');

        if ($('#divScreen').length == 0) {
            $('body').append('<div id="divScreen"></div>');
            $('#divScreen').css({
                filter: "alpha(opacity=" + (defaults.filter * 100) + ")",
                opacity: defaults.filter,
                position: 'absolute',
                left: '0px',
                top: '0px',
                width: $(document).width() + "px",
                height: $(document).height() + "px",
                display: 'none',
                'background-color': '#000000',
                'z-index': 500
            });
        }
        if ($.browser.msie && $.browser.version == 6) {
            this.css({
                position: 'absolute'
            });
            $('#divScreen').css({
                position: 'absolute'
            });
        }
        if ($('#closeBar').length == 0) {
            this.prepend('<div id="closeBar"><div id="dialogHeader">' + defaults.title + '</div><a href="JavaScript:void(0)">关闭</a></div>');
            $("#closeBar").css({
                width: '100%',
                'border-bottom': '1px solid #DDDDDD',
                margin: '0px 0px 5px 0px',
                padding: '5px 0px',
                'background': 'url(' + defaults.nav_bg + ') repeat-x',
                'text-align': 'right',
                float: 'left',
                clear: 'both'
            });
            $("#closeBar>a").css({
                margin: '0px 10px 0px 0px',
                color: '#000000',
                'font-size': '13px'
            });
            $("#dialogHeader").css({
                float: 'left',
                'font-family': 'Arial',
                'font-size': '13px',
                'margin-left': '15px',
				'display':'inline'

            });

            var self = this;
            $("#closeBar>a").click(function() {
                self.closeDialog();
            });
        }


        this.alignDialog();
        this.divScreenClick_WindowResize();

    },
    alignDialog: function() {
        var w = $(window).width();
        var h = $(window).height();

        var width = this.width();
        var height = this.height();

        var sh = $(window).scrollTop();

        this.css({
            left: (w - width) / 2 + "px",
            top: (h - height) / 2 + sh + "px"
        });
    },
    openDialog: function() {
        $("#divScreen").show();
        this.fadeIn(300);
        var self = this;
        $(window).scroll(function() {
            self.alignDialog();

        });
        //if ($.browser.msie) {
        //   $('body').css('overflow', 'hidden');
        //}
    },
    closeDialog: function() {
        $("#divScreen").hide();
        this.hide();
        //if ($.browser.msie) {
        //  $('body').css('overflow', 'auto');
        //}
        $(window).unbind('scroll')
    },
    divScreenClick_WindowResize: function() {
        var self = this;
        $('#divScreen').click(function() {
            self.closeDialog();
        });

        $(window).resize(function() {
            self.alignDialog();
        });

    }
});

