var dimmed = false;
var $floater;
var buttons = new Object;
var handlingFloater = false;
var $btn = null;
var stopClose = false;
var callback = function() { };

$(document).ready(function() {
    $(document).setupDimmer();
});

;(function($) {
$.fn.extend({
    setupDimmer: function(options) {    
        jQuery('<div></div>').attr({id: 'dimmer'}).addClass("dimmer").appendTo(document.body);
		$(".dimmer").fadeTo(1, 0);

        document.onkeyup = function(e) { 	
		    if (e == null) { // ie
			    keycode = event.keyCode;
		    } else { // mozilla
			    keycode = e.which;
		    }
    		
		    if(keycode == 27) { // close
		        if (!stopClose)
		            $(document).hideFloater();
		    }
		}	
		
		$(".floating_window .close").click(function() {
		    $(document).hideFloater();
		});
		
		$(".dimmer").fadeTo(1, 0);
    
        $(".dimmer").click(function() {
            $(document).hideFloater();
        });
    },
    showFloater: function(darkness, handleFloater, btn, offCallback, preventClose) {
        stopClose = preventClose;
        
        if (btn != null)
        {
            $btn = btn;
            callback = offCallback;
        }
        else
        {
            $btn = null;
            callback = function() { };
        }
        $floater = $(this);
        
        if (handleFloater)
        {
            var floatingWidth = $floater.width();
            var floatingHeight = $floater.height();
            
            $floater.css("marginLeft", "-" + (floatingWidth / 2) + "px");
            $floater.css("marginTop", "-" + (floatingHeight / 2) + "px");
        
            handlingFloater = true;
        }
        else
        {
            handlingFloater = false;
        }
   
        if (!darkness)
            darkness = 0.9;
            
        $("#dimmer").show();
        $("#dimmer").fadeTo(500, darkness);
        
        if (handleFloater)
            $floater.fadeIn(500);
        dimmed = true;
    },
    hideFloater: function(ignoreBtn) {
        if (!stopClose)
        {
            if (!ignoreBtn && $btn != null)
                $btn.simClick(true);
                
            
            $("#dimmer").fadeTo(500, 0, function() { $(this).hide(); callback(); });
            dimmed = false;
           
            if (handlingFloater)
            {
                $floater.hide();
            }
        }
    }
});


})(jQuery);