var Background = false;
var VisibleLayers = 0;
var GlobalHideLaysers = true;

var LayersOnResizeFunction = function(event) {
/*
	if ($('layers_background') == null) return false;
	b = window.getScroll();
	s = window.getSize();
	if (hcHeight <= s.y)
		b.x += 17;
	$('layers_background').setStyle('width', intWidth+5+b.x);
	if (intHeight > hcHeight) {
		h = intHeight;
	} else
		h = hcHeight;
	$('layers_background').setStyle('height', h);
			
	$$('.layer').each(function(item, index) {
		item.setStyle('left', b.x + Math.round((intWidth-item.getStyle('width').toInt())/2));
		item.setStyle('top', b.y + Math.round((intHeight-item.getStyle('height').toInt())/2));
	});
*/	
}
window.addEvent('resize', LayersOnResizeFunction);
window.addEvent('scroll', LayersOnResizeFunction);


function HideLayers() {
	bg = $('layers_background');
	if (bg != null && GlobalHideLaysers) {
		tween = bg.get('tween', { duration: 500});
		tween.start('opacity', 0);
		tween.addEvent('complete', function(event){
			this.dispose();
			//this.destroy();
			Background = false;
		}.bind(bg));
		$$('.layer').each(function(item, index){
			var tw = item.get('tween', { duration: 500});
			tw.start('opacity', 0);
			tw.addEvent('complete', function(event){
				var tid = this.get('TargetId');
				var target = $(tid);
				//target.dispose();
				target.setStyle('display', 'none');
				var p = this.get('TargetParent');
				//target.set('id', tid);
				//alert(target.get('id'));
				//alert(target.get('id'));
				if ($(p) != null)
					$(p).grab(target, 'bottom');
				this.empty();
				this.dispose();
				this.destroy();
			}.bind(item));
		});
		VisibleLayers = 0;
	}
}
var LayersEscFunction = function(event){
	if (event.key == 'esc')
		HideLayers();
}
if (!Browser.Engine.trident)
	window.addEvent('keydown', LayersEscFunction);
	
	
function HideLayer() {
	if (this != null && GlobalHideLaysers) {
		VisibleLayers--;
		if (VisibleLayers <= 0) {
			HideLayers();
		} else {
			var tw = this.get('tween', { duration: 500});
			tw.start('opacity', 0);
			tw.addEvent('complete', function(event){
				this.dispose();
				this.destroy();
			}.bind(this));
		}	
	}
}

	
var CreateEmptyLayer = function(id, width, height) {
	var s = window.getScrollSize();
	var b = window.getScroll();
	var div = new Element('div',{'id': id,
						  		 'class': 'layer',
							 	 'styles': {
									 'position': 'absolute',
	  							 	 'width': 0,
									 'height': 0,
									 'left': b.x + Math.round(intWidth/2),
									 'top': b.y + Math.round(intHeight/2),
									 'z-index': 201
									}
								});
	div.set('LayerWidth', width);
	div.set('LayerHeight', height);
	
	button_close = new Element('div',{'class': 'layer_close', 'styles': {'left':width-19,'top':8}   });
	div.grab(button_close);
	button_close.addEvent('click', HideLayer.bind(div));
	VisibleLayers++;
	
	$$('body')[0].grab(div, 'top');
	var fx = new Fx.Morph(div, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
	fx.start({
	    'width': width,
		'left': b.x + Math.round((intWidth-width)/2)
	});
	fx.addEvent('complete', function(event){
		var fx = new Fx.Morph(this, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
		fx.start({
	    	'height': height,
			'top': b.y + Math.round((intHeight-height)/2)
		});
	}.bind(div));
	return div;
}

var TargetToLayer = function(id, width, height) {
	var target = $(id);
	if(target == null) return false;
	var parent = target.getParent();//alert(parent.get('id'));

	d = $(id).getDimensions();
	if (!$chk(width))
		width = d.width;
	if (!$chk(height))
		height = d.height;
	layer = CreateEmptyLayer('layer_'+id, width, height);
	if (layer == null) return false;
	layer.set('TargetParent', parent.get('id'));
	layer.grab(target, 'bottom');
	target.setStyle('display', 'inline-block');
	layer.set('TargetId', target.get('id'));
	//target.setProperty('id', '');
	return layer;
}
 
		
function AddLayer(id) {
	LayersShowBackground(function(){
		//CreateEmptyLayer('layer0', 500, 400);
		TargetToLayer(this);
	}.bind(id));
}

function MessageLayer(text) {
	LayersShowBackground(function(){
		l = CreateEmptyLayer('layer0', 200, 100);
		l.setStyle('z-index', 300);
		l.set('text', text);
	});
}

function LayersShowBackground(callback) {
	if (!Background) {
		Background = true;
		var s = window.getScrollSize();
		var b = window.getScroll();
		var h = hcHeight;
		
		if (Browser.Engine.webkit)
			s.y += b.y;
		
		if (hcHeight <= intHeight) {
			b.x += 17;
			h = intHeight;
		}
		
		
		
		var bg = new Element('div', {'class': 'layers_background',
							 		 'id': 'layers_background',
							 		 'opacity': 0,
									 'styles': {
		  							 	 'width': intWidth+b.x+5,
										 'height': h + 100
										}
									});
		$$('body')[0].grab(bg, 'top');
		
		var tween = bg.get('tween', { duration: 500});
		tween.start('opacity', .75);
		tween.addEvent('complete', function(event){
			callback();
		}.bind(bg));	
		
		if (Browser.Engine.trident)
			bg.addEvent('keydown', LayersEscFunction);
		bg.addEvent('click', HideLayers);	
		
	} else {
		callback();
	}
}
