/*
로그인 등에 사용할 범용 레이어 객체
2010.03.26
Layer.load('팝업으로 사용할 컨테이너 ID'); 혹은
Layer.load('ajax로 불러올 URL', 'ajax'); 로 불러오면 팝업이 뜬다.
종료는 Layer.close(); 로 처리한다.
*/
var _Layer = function() {
	this.background = null;
	this.container = null;
	this.parent = null;
};

_Layer.prototype.init = function() {
	this.background = document.createElement('div');	
	this.background.style.cssText = 'position:absolute;left:0;top:0;background:#000;z-index:9999';

	var background = $(this.background);
	background.css('opacity', 0.3);

	this.container = document.createElement('div');
	this.container.style.cssText = 'position:absolute;z-index:10000';
};

// 사용 설명서
// container > ajax 인 경우 파일 경로
//	              > element 인 경우 container 의 id (string)
_Layer.prototype.load = function(container, mode,func) {
	if(mode == 'ajax')
		this.loadAjax(container,func);
	else
		this.loadElement(container);
};

// 위의 load 를 사용하지 않고 바로 이 부분을 호출해도 된다
// 함수 정보 :: this.parent 는 element 시 부모의 정보를 가지고 있어서 layer를 닫을 때 돌려준다.
// ajax string 이 담겨있으면 ajax로 불러온 정보이므로 그냥 날려도 된다.
_Layer.prototype.loadElement = function(container_id) {
	var content = document.getElementById(container_id);
	this.parent = content.parentNode;

	this.parent.removeChild(content);
	this.container.appendChild(content);

	this.loadFinal();
};
_Layer.prototype.loadAjax = function(file_path,func) {
	var self = this;

	this.parent = 'ajax';
	$(this.container).load(file_path, null, function() { self.loadedAjax(); func();});
};
_Layer.prototype.loadedAjax = function() {
	this.loadFinal();
};

_Layer.prototype.loadFinal = function() {
	var background = $(this.background), win = $(window), container = $(this.container);
	
	var scr_width = win.width(), scr_height = win.height();
	var doc_width = $(document).width(), doc_height = $(document).height();
	var width = (scr_width > doc_width) ? scr_width : doc_width;
	var height = (scr_height > doc_height) ? scr_height : doc_height;

	container.css('display', 'none');
	$(this.container.firstChild).css('display', 'block');
	
	document.body.appendChild(this.background);
	document.body.appendChild(this.container);

	background.css('width', width);
	background.css('height', height);
	container.css('left', (scr_width - container.width()) / 2 + win.scrollLeft());
	container.css('top', (scr_height - container.height()) / 2 + win.scrollTop());
	
	container.css('display', 'block');
};


_Layer.prototype.close = function() {
	document.body.removeChild(this.background);
	document.body.removeChild(this.container);
	
	var content = this.container.firstChild;
	$(content).css('display', 'none');
	
	this.container.removeChild(content);
	if(this.parent == 'ajax') {}
	else {
		this.parent.appendChild(content);
	}
	content = null;
	this.parent = null;
};





// 객체 생성
var Layer = new _Layer();
Layer.init();
