//dimScreen()
//by Brandon Goldman
//edited by Vinícius Batista de Souza.
jQuery.extend({
    //dims the screen
    dimScreen: function (speed, opacity, color, callback) {

        var resizeTimer = null;
        $(window).bind('resize', function () {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout("$.resizeDimmer()", 100);
        });


        if (color == undefined || color == '')
            color = "#FFFFFF";

        if (jQuery('#__dimScreen').size() > 0) return;

        if (typeof speed == 'function') {
            callback = speed;
            speed = null;
        }

        if (typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }

        if (speed < 1) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }

        if (opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }

        var isIe = false;

        if ($.browser.msie && $.browser.version >= 8) {
            isIe = true;
        }

        var minusW = 0;
        var minusH = 0;

        if (isIe) {
            //minusH = 4;
            //minusW = 21;
        }


        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        return jQuery('<div></div>').attr({
            id: '__dimScreen'
                , fade_opacity: opacity
                , speed: speed
        }).css({
            background: color
            , height: ($(document).height() - minusH) + 'px'
            , left: '0px'
            , opacity: 0
            , position: 'absolute'
            , top: '0px'
            , width: ($(document).width() - minusW) + 'px'
            , zIndex: 1060
			, overflow: 'hidden'
        }).appendTo(document.body).fadeTo(speed, 0.7, callback);
    },

    //stops current dimming of the screen
    dimScreenStop: function (callback) {
        var x = jQuery('#__dimScreen');
        var opacity = x.attr('fade_opacity');
        var speed = x.attr('speed');
        x.fadeOut(speed, function () {
            x.remove();
            if (typeof callback == 'function') callback();
        });
    },

    resizeDimmer: function () {
        var isIe = false;

        if ($.browser.msie && $.browser.version >= 8) {
            isIe = true;
        }

        var minusW = 0;
        var minusH = 0;

        if (isIe) {
            minusH = 4;
            minusW = 21;
        }

        $('#__dimScreen').css({
        height: ($(document).height() - minusH) + 'px'
                , width: ($(document).width() - minusW) + 'px'
        });
    }
});