/*
[User Guide]
	[Options]
	type (confirm or alert, default :alert)
	action
	width
	height

	[Boxs]
	loadBox
	alertBox

	[Example]
	<script>
	var objBox = new embox();
	function loadding() {
		objBox.alertBox('Loadding....',{width:'300', height:'100' });
		alert("end");
		objBox.boxClose();
	}
	</script>
	<input type="button" onClick="loadding();" value="alertBox">
	<input type="button" onClick="objBox.alertBox('This is embianBox',{action:'alert(\'hi\')', width:'300', height:'100' });" value="alertBox">
	<input type="button" onClick="objBox.alertBox('This is embianBox',{type:'confirm', action:'loadding()', width:'300', height:'100' });" value="alertBox">
*/
var embox = Class.create();
embox.prototype = {
	initialize: function() {
		window.onresize = this.boxStyle;
	},
	// valiable setting
	valInit: function(msg, options) {
		this.boxInit();
		message = "empty";
		type = "load";
		action = "emBox.boxClose()";
		width = "200px";
		height = "100px";
		if (msg) message = msg;
		if (options) {
			if (options.type) type = options.type;
			if (options.action) action = options.action;
			if (options.width) width = options.width;
			if (options.height) height = options.height;
			if (!options.type && options.action) type = "";
		}
		bw = width;
		bh = height;
	},
	// div layout setting
	boxInit: function() {
		var obj1 = $('backgroundlayer');
		var obj2 = $('alertlayer');

		bodyTag = document.getElementsByTagName("body").item(0);
		this.bodyTag = bodyTag;
		if (!obj1) {
			var div1 = document.createElement('DIV');
			div1.setAttribute('id','backgroundlayer');
			bodyTag.appendChild(div1);
		}
		if (!obj2) {
			var div2 = document.createElement('DIV');
			div2.setAttribute('id','alertlayer');
			bodyTag.appendChild(div2);
		}
		backDiv = $('backgroundlayer');
		alertDiv = $('alertlayer');
	},
	// div layout style
	boxStyle: function() {
		backDiv = $('backgroundlayer');
		if (backDiv) {
			var sw = document.body.scrollWidth; // 스크롤 포함한 전체 화면 넓이
			var sh = document.body.scrollHeight; // 스크롤 포함한 전체 화면 높이
			var iw = window.innerWidth ||
					document.documentElement.clientWidth ||
					document.body.clientWidth ||
					0; // 보여지는 화면 넓이
			var ih = window.innerHeight ||
					document.documentElement.clientHeight ||
					document.body.clientHeight ||
					0; // 보여지는 화면 높이
			var tx = window.pageXOffset ||
					document.body.scrollLeft ||
					0; // 전체 화면중 보여지는 화면의 상단 X좌표
			var ty = window.pageYOffset ||
					document.body.scrollTop ||
					0; // 전체 화면중 보여지는 화면의 상단 Y좌표
			var x = tx+(iw/2)-(bw/2); // 박스의 X좌표
			var y = ty+(ih/2)-(bh/2); // 박스의 Y좌표

			if (sw < iw) sw = iw;
			if (sh < ih) sh = ih;

			backDiv.style.left = "0px";
			backDiv.style.top = "0px";
			backDiv.style.width = sw+"px";
			backDiv.style.height = sh+"px";
			bodyTag.appendChild(backDiv);

			alertDiv.style.left = x+"px";
			alertDiv.style.top = y+"px";
			alertDiv.style.width = bw+"px";
			alertDiv.style.height = bh+"px";
			alertDiv.style.backgroundColor = "#FFF";
			bodyTag.appendChild(alertDiv);

			//alert("all:"+sw+"*"+sh+"\nview:"+iw+"*"+ih+"\ntop:"+tx+"*"+ty+"\nbox:"+x+"*"+y);
		}
	},
	boxAction: function() {
		if (action) {
			eval(action);
		}
		if (action!="emBox.boxClose()") {
			bodyTag.removeChild(backDiv);
			bodyTag.removeChild(alertDiv);
			backDiv = $('backgroundlayer');
			alertDiv = $('alertlayer');
		}
	},
	boxClose: function() {
		bodyTag.removeChild(backDiv);
		bodyTag.removeChild(alertDiv);
		backDiv = $('backgroundlayer');
		alertDiv = $('alertlayer');
	},
	boxMsg: function() {
		this.boxStyle();
		var obj1 = $('msglayer');
		var obj2 = $('btnlayer');
		if (obj1) alertDiv.removeChild(obj1);
		if (obj2) alertDiv.removeChild(obj2);

      var div3 = document.createElement('DIV');
      div3.setAttribute('id','msglayer');
		if (type=="load") {
			div3.style.background = "url(/images/loading.gif) no-repeat left";
		}
      div3.innerHTML = message;
      alertDiv.appendChild(div3);
	},
	boxButton: function() {
		var div3 = document.createElement('DIV');
		div3.setAttribute('id','btnlayer');
		alertDiv.appendChild(div3);
		// OK button
		var div4 = document.createElement('INPUT');
		div4.setAttribute('type','button');
		div4.setAttribute('id','layerokbtn');
		div4.className = 'layerbtn';
		div4.setAttribute('value','확인');
		div4.onclick = this.boxAction;
		div3.appendChild(div4);
		// CANCEL button
		if (type=="confirm") {
			var div5 = document.createElement('INPUT');
			div5.setAttribute('type','button');
			div5.setAttribute('id','layercancelbtn');
			div5.className = 'layerbtn';
			div5.setAttribute('value','취소');
			div5.onclick = this.boxClose;
			div3.appendChild(div5);
		}
		div4.blur();
		div4.focus();
	},
/*
	loadBox: function(msg, options) {
		this.valInit(msg, options);
		this.boxMsg();
	},
*/
	alertBox: function(msg, options) {
		this.valInit(msg, options);
		this.boxMsg();
		if (type!="load") {
			this.boxButton();
		}
	}
}
var emBox = new embox();
