﻿//dimScreen()
//by Brandon Goldman
jQuery.extend({
    //dims the screen
    dimScreen: function(containerId, speed, opacity, callback) {
        var container = $('#' + containerId);
        var overlayPosition = 'fixed';
        
        if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
            overlayPosition = 'absolute';
            container = document.body;
        }

        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;
        }

        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        //container.css('height', '100%');
        return jQuery('<div></div>').attr({
                id: '__dimScreen'
                ,fade_opacity: opacity
                ,speed: speed
                ,containerId: containerId
            }).css({
            background: '#000'
            ,height: '100%'
            ,left: '0px'
            ,opacity: 0
            ,position: overlayPosition
            ,top: '0px'
            ,width: '100%'
            ,zIndex: 999
        //}).appendTo(document.body).fadeTo(speed, opacity, callback);
        }).appendTo(container).fadeTo(speed, opacity, callback);                
    },
    
    //stops current dimming of the screen
    dimScreenStop: function(callback) {        
        var x = jQuery('#__dimScreen');
        var containerId = x.attr('containerId');
        var opacity = x.attr('fade_opacity');
        var speed = x.attr('speed');
        var container = $('#' + containerId);
        //container.css('height', 'auto');
        x.fadeOut(speed, function() {
            x.remove();
            if(typeof callback == 'function') callback();
        });                
    }
});
