function grayOutController()
{
	var self = this;

        self.gray = false;
        self.isGray = false;
        self.isGraying = false;
        self.opacity_level = 6;

        self.init = function()
        {
                // the gray-out div
                self.gray = create('div');
		
		self.gray.style.position = "absolute";
		self.gray.style.top = "0";
		self.gray.style.left = "0"; 
		self.gray.style.zIndex = "1"; 
		self.gray.style.width = "100%"; 
		self.gray.style.height = "100%"; 
		self.gray.style.backgroundColor = "#000"; 
		self.gray.style.margin = "0";
		self.gray.style.padding = "0";
		self.gray.style.filter = "alpha(opacity=0)";
		self.gray.style.opacity = "0.0";

                self.gray.style.display = "none";
                document.body.appendChild( self.gray );
	}

	self.positionGray = function()
	{
		// must toggle the gray off for a moment to make sure we're getting the dimension of the page and not the gray 
		//   (only a problem if the page is being resized smaller)
		self.gray.style.display = "none";
		var dims = getPageDims();
		self.gray.style.display = "block";

		self.gray.style.top = '0px';	
		self.gray.style.height = dims.y +'px';
		self.gray.style.width = dims.x +'px';
	};

        // based on ibox ( http://www.ibegin.com/blog/p_ibox.html )
        self.grayOut = function( bState )
        {
                if( (bState===true && self.isGray===true) || self.isGraying===true )
                        { return; }

                if( bState===false)
                {
                        self.gray.style.display = "none";
                        self.isGray = false;
			window.onresize = null;
			window.onscroll = null;
                        return;
                }
		else
		{
			try {
				window.onresize = self.positionGray;
				window.onscroll = self.positionGray;
			} catch(e) {};
		}

                self.isGraying = true;
                self.isGray = false;
                self.gray.style.opacity = 0;
                self.gray.style.filter = 'alpha(opacity=0)';

                for (var i=0;i<=self.opacity_level;i++)
                        { setTimeout('setOpacity('+i+')',50*i); } // from quirksmode.org

                setOpacity = function(value)
                {
                        self.gray.style.opacity = value/10;
                        self.gray.style.filter = 'alpha(opacity=' + value*10 + ')';

                        if (value == self.opacity_level)
                                { self.isGray = true; self.isGraying = false; }
                };

		self.positionGray();
                self.gray.style.display = "";
        };

	self.init();
}

var theGray = false;

addWindowOnload( function() {
	theGray = new grayOutController();
} );

