var jtk = new JoookToolkit();

function Layer(emtId, clickSrc){
	this.emt = null;
	this.visible = false;
	this.clickSrc = clickSrc;
	this.canceling = false;
	this.onBeforeShow = null;
	this.onShow = null;
	this.onBeforeHide = null;
	this.onHide = null;
	
	this.setOrigin = function(x, y){
		this.emt.style.left = x + "px";
		this.emt.style.top = y + "px";
	}
	
	this.show = function(){
		if(this.onBeforeShow != null && typeof(this.onBeforeShow) == 'function'){
			this.onBeforeShow.call();
		}
		this.canceling = true;
		this.emt.style.display = "block";
		this.visible = true;
		if(this.onShow != null && typeof(this.onShow) == 'function'){
			this.onShow.call();
		}
	}
	
	this.hide = function(){
		if(this.onBeforeHide != null && typeof(this.onBeforeHide) == 'function'){
			this.onBeforeHide.call();
		}
		if(!this.canceling){
			this.emt.style.display = "none";
			this.visible = false;
		}
		this.canceling = false;
		if(this.onHide != null && typeof(this.onHide) == 'function'){
			this.onHide.call();
		}
	}
	
	
	this.attachHandlers = function(){
		var that = this; // Store current object reference for later delegate binding
		
		jtk.addEvent(document.body, "click", function(){
			JTKDelegate.create(that, that.hide());
		});
		
		jtk.addEvent(this.emt, "click", function(){
			JTKDelegate.create(that, that.show());
		});
		
		jtk.addEvent(this.clickSrc, "click", function(){
			JTKDelegate.create(that, that.show());
		});
	}
	
	this.init = function(emtId){
		this.emt = jtk.select("#" + emtId)[0];
		this.emt.style.position = "absolute";
		this.attachHandlers();
	}
	
	this.init(emtId);
}