/*********************************************************************************************
 * All functions to display a layer in front of the website. 
 *	
 * <div id='layer_id'>
 *	<div class='overlay'></div>
 *  <div class='close'></div>
 *	<div class='layer'>Layer Content</div>
 * </div>
 *
 **********************************************************************************************/
	
	var currentLayer;
	
	function fadeInLayer(id){
		currentLayer = Ext.get(id);
		var overlay = currentLayer.child('.overlay');
		var content = currentLayer.child('.layer');
		
		currentLayer.removeClass('dn');
		
		content.show();
		content.setStyle({'visibility':'hidden'});
		
		overlay.on('click',closeLayer);
		if(Ext.isIE6)showAllSelects(false);
		setTimeout(oberserveLayer,50);
	}
	
	function closeLayer(e,dom){
		var layer = Ext.get(dom).parent();
			layer.addClass('dn');
		if(Ext.isIE6)showAllSelects(true);
	}
	
	function fadeOut(obj){
		var el = Ext.get(obj);
		if(Ext.isIE6)showAllSelects(true);
		el.addClass('dn');
	}	
	
	function oberserveLayer(){
		var overlay = currentLayer.child('.overlay');
		var content = currentLayer.child('.layer');
		var close = currentLayer.child('.close');
		var docScroll = Ext.getDoc().getScroll();
		var bodyViewSize = Ext.getBody().getViewSize();
		var ypos = (bodyViewSize.height/2) - content.getComputedHeight()/2;
			ypos = (ypos<50) ? docScroll.top + 50 : docScroll.top + ypos;
		var xpos = (bodyViewSize.width/2) - content.getComputedWidth()/2;
		var cxPos = xpos + content.getComputedWidth() - 23;
		
		close.setStyle({
			'top':(ypos-23)+'px',
			'left':cxPos+'px'
		});
		close.on('click',closeLayer);
		
		content.setStyle({
			'top':ypos+'px',
			'left':xpos+'px',
			'visibility':'visible'
		});
		/*content.animate({
				opacity : {to:1},
			},0.3,function(){},'easeOut','run'
		);*/
		overlay.setStyle({
			'width':Ext.getBody().getWidth()+'px',
			'height':getScreenSize('height')+'px',
			opacity:.8
		});
		currentLayer.removeClass('inv');
	}
	
	function showAllSelects(state){
		var s = Ext.query('select');
		Ext.each(s,function (data, index) {
			(!state) ? Ext.get(data).addClass('inv') : Ext.get(data).removeClass('inv');
		});
	}
	
	function getScreenSize(type){
		if( window.innerHeight && window.scrollMaxY!='undefined' )  {// Firefox;
			pageWidth = window.innerWidth + window.scrollMaxX;
			pageHeight = window.innerHeight + window.scrollMaxY;
			
		}
		else if(document.body.scrollHeight > document.body.offsetHeight ){	// all but Explorer Mac
			pageWidth = document.body.scrollWidth;
			pageHeight = document.body.scrollHeight;
		}
		else {// works in Explorer 6 Strict, Mozilla (not FF) and Safari
			pageWidth = document.body.offsetWidth + document.body.offsetLeft;
			pageHeight = document.body.offsetHeight + document.body.offsetTop; }
		if(type=='height') return pageHeight;
		else return pageWidth;
	}
	
	
/*********************************************************************************************************************
 * Wrapper for Firebug console.log() to avoid errors in browsers without Firebug
 *********************************************************************************************************************/
 
 	function consoleLog(msg){
		try{
			console.log(msg);
		} catch(e){
			//alert(msg);
		}
	}
