MaskLayerObjects = new Array; 

function MaskLayer(object_id, options)
{
	this.objectId = object_id;
	this.isInit = false;
	
	this.layerObject = null;
	this.iframeObject = null;
	
	this.contentObject = null;
	this.contentOptions = {};
	
	this.curWidth = null;
	this.curHeight = null;
	
	this.options = Object.extend({
    	bgColor: '#cbcbcb',
		opacity: 0.9,
		zIndex: 999,
		className: null,
		appearEffect:null,
		disappearEffect:null				  
    }, options || {});
			
	this.init = function()
	{
		if(this.isInit)
			return false;
		MaskLayerObjects[this.objectId] = this;
		
		this.buildLayerObject();
		this.buildContentObject();
		
		this.isInit = true;
	}	
	
	this.buildLayerObject = function()
	{
		var elems = document.getElementsByTagName('body');
		
		var body = elems[0];
		
		var div = Builder.node('div', {style:'position:absolute;'
									+'display:none;'
									+'z-index:'+this.options.zIndex+';'
									+'width:100%;'
									+'height:0'
									+'padding:0;'
									+'margin:0;'});
		
		if(this.options.className)
			div.className = this.options.className; 
		
		
		var iframe = Builder.node('iframe', {src:'about:blank',
										frameborder:'0',
										scrolling:'no',
										style:'position:absolute;'
									+'display:none;'
									+'z-index:'+this.options.zIndex+';'
									+'width:100%;'
									+'height:0'
									+'padding:0;'
									+'margin:0;'});
									
				
						
		body.style.position = 'relative';
		body.appendChild(iframe);
		body.appendChild(div);		
				
		this.layerObject = $(div);
		this.iframeObject = $(iframe);
		
		this.setupLayerPosition();
		this.setupLayerTransparency();
	}
	
	this.buildContentObject = function()
	{
		var div = Builder.node('div', {style:'width:980px;height:600px;position:absolute;top:0;left:0;display:none;z-index:'+(parseInt(this.options.zIndex)+1)});
		
		this.layerObject.up().appendChild(div);
		
		this.contentObject = $(div);
		
		//link = Builder.node('a', {href:'javascript:;', onclick:'MaskLayerObjects[\''+this.objectId+'\'].hide();'});
		//link.innerHTML = 'bla';		
		//this.contentObject.appendChild(link);
	}
	
	this.setupLayerPosition = function()
	{
		this.layerObject.style.top = '0px';
		this.layerObject.style.left = '0px';
		
		this.iframeObject.style.top = '0px';
		this.iframeObject.style.left = '0px';
				
		
		this.curWidth =  document.documentElement.scrollWidth;
		this.curHeight =  document.documentElement.scrollHeight;
		
		this.layerObject.style.height = this.curHeight+'px';
		this.iframeObject.style.height = this.curHeight+'px';
				
		this.layerObject.style.width = this.curWidth+'px';
		this.iframeObject.style.width = this.curWidth+'px';
	}
	
	this.setupLayerTransparency = function()
	{		
		this.layerObject.style.backgroundColor = this.options.bgColor;
		
		this.layerObject.setOpacity(0);
		this.iframeObject.setOpacity(0);		
	}
	
	this.setContent = function(content, options)
	{
		this.setupContentPosition();
		options = this.getContentOptions(options);
		this.contentObject.innerHTML = content;
		this.contentOptions = options;		
	}
	
	this.setElementContent = function(element, options)
	{
		this.setupContentPosition();
		options = this.getContentOptions(options);
		this.contentObject.appendChild(element);
		
		
		this.contentOptions = options;			
	}
	
	this.setupContentPosition = function()
	{
		if(this.contentObject && this.contentOptions)
		{	if(this.contentOptions.width)
			{
				this.contentObject.style.width = this.contentOptions.width+'px';
				
				var scrollX = getViewportScrollX();
				this.contentObject.style.left = (scrollX+((this.curWidth-scrollX)-this.contentOptions.width)/2)+'px';
			}
		
			if(this.contentOptions.height)
			{
				var scrollY = getViewportScrollY();
				//this.contentObject.style.top = (scrollY+((this.curHeight-scrollY)-this.contentOptions.height)/2)+'px';
				this.contentObject.style.top = (scrollY+50)+'px';
			}
		}
	}	

	this.showWithModuleLoad = function(module_object, force_refresh, options)
	{
		if(this.contentObject.innerHTML == '' || force_refresh)
		{
			var options = this.getContentOptions(options);
			
			module_object.onLoadedHandler = this.onModuleLoaded.bind(this, module_object, options);
			module_object.loadContent(force_refresh);
		}
		else
			this.show();		
	}
	
	this.getContentOptions = function(options)
	{
		if(!options)
			options = {};
		
		if(!options.width)
			options.width = 980;
		
		if(!options.height)
			options.height = 600;
			
		
		if(options.width > this.curWidth)
			options.width = this.curWidth;
			
		if(options.height > this.curHeight)
			options.height = this.curHeight;	
		
		return options;
	}
	
	this.onModuleLoaded = function(module_object, options)
	{
		this.setContent(module_object.content, options);
		
		this.show();
		
	}

	this.show = function()
	{		
		this.setupLayerPosition();
		this.setupContentPosition();
		this.iframeObject.style.display = '';
		
		Effect.Appear(this.layerObject, {from:0, to:this.options.opacity});
		
		if(!this.contentOptions.appearEffect)
			this.contentObject.toggle();
		else
		{
			Effect.Appear(this.contentObject, {from:0, to:1});
		}
		
		Event.observe(window, 'resize', this.onDocumentResized.bind(this));
		Event.observe(window, 'scroll', this.onDocumentResized.bind(this));
		
	}
	
	this.brutalShow = function()
	{
		this.setupLayerPosition();
		this.setupContentPosition();
		
		this.contentObject.toggle();
	}
	
	this.hide = function(destroy_content)
	{
		this.iframeObject.style.display = 'none';
		//this.layerObject.style.display = 'none';
		
		Event.stopObserving(window, 'resize', this.onDocumentResized.bind(this));
		Event.stopObserving(window, 'scroll', this.onDocumentResized.bind(this));
		
		var afterFinish = null;
		
		if(destroy_content)
			afterFinish = this.onHiddenDestroyContent.bind(this);
			
		
		Effect.Fade(this.contentObject, {afterFinish:afterFinish});
		Effect.Fade(this.layerObject);
	}
	
	this.onHiddenDestroyContent = function()
	{
		this.contentObject.innerHTML = '';
	}
	
	this.onDocumentResized = function()
	{
		this.setupLayerPosition();	
		this.setupContentPosition();
	}	
	
	this.init();
}