/*************************************************************************************************************
	
	developer : idw1@naver.com
	date: 2006-07-04

*************************************************************************************************************/

/*************************************************************************************************************
	
	WINDOW
		- show
		- hide

*************************************************************************************************************/

YAHOO.namespace("DP.WINDOW");

WINDOW = function(id, parent_id) {
	WINDOW.superclass.constructor.call(this, id, parent_id);
}

Class(
	WINDOW, OBJECT,

	"init", function(id, parent_id) {
		WINDOW.superclass.init.call(this, id, parent_id);

		this.container = null;
		this.title = null;
		this.client = null;
		this.icon = null;
		this.menu = null;

		this.color = YAHOO.DP.WINDOW.color || "#fff";
		this.icon_color1 = YAHOO.DP.WINDOW.icon_color1 || "#333";
		this.icon_color2 = YAHOO.DP.WINDOW.icon_color2 || "#fff";
		this.title_color = YAHOO.DP.WINDOW.title_color || "#666";
		this.border_color = YAHOO.DP.WINDOW.border_color || "#333";
		this.state = "SHOW";

		this.border = 1;
		this.title_padding_w = 0;
		this.title_padding_h = 0;
		this.width = 0;
		this.height = 0;
		this.title_width = 0;
		this.title_height = 0;
		this.container_width = 0;
		this.container_height = 0;
	},

	"clear", function() {
		delete this.container;
		delete this.title;
		delete this.client;
		delete this.icon;
		delete this.menu;

		delete this.color;
		delete this.icon_color;
		delete this.title_color;
		delete this.border_color;
		delete this.state;

		delete this.border;
		delete this.title_padding_w;
		delete this.title_padding_h;
		delete this.width;
		delete this.height;
		delete this.title_width;
		delete this.title_height;
		delete this.container_width;
		delete this.container_height;

		WINDOW.superclass.clear.call(this);
	},

	"getContainerX", function() {
		var x = YAHOO.util.Dom.getStyle(this.container.id, "left");
		if( x == 0 ) {
			return 0;
		}
		return parseInt(x.substr(0, x.length - 2));
	},

	"getContainerY", function() {
		var y = YAHOO.util.Dom.getStyle(this.container.id, "top");
		if( y == 0 ) {
			return 0;
		}
		return parseInt(y.substr(0, y.length - 2));
	},

	"getContainerWidth", function() {
		return this.container_width;
	},

	"getContainerHeight", function() {
		return this.container_height;
	},

	"destroy", function() {
		if( this.menu ) {
			this.menu.destroy();
		}
		DW_UTIL.safeDestroy(this.client, this.container);
		DW_UTIL.safeDestroy(this.title, this.container);
		DW_UTIL.safeDestroy(this.icon, this.container);
		DW_UTIL.safeDestroy(this.container, this.parent_id);

		WINDOW.superclass.destroy.call(this);
	},

	"get_id", function() {
		return this.get_uid("client");
	},

	"calculateSize", function(w, h) {
		this.title_padding_w = 10;
		this.title_padding_h = 6;

		this.width = w;
		this.height = h;

		if( YAHOO.util.Event.isIE ) {
			this.title_width = this.width;
			this.title_height = 21;

			this.container_width = this.width + this.border * 2;
			this.container_height = this.height + this.title_height + this.border * 2;
		}
		else {
			this.title_width = this.width - this.title_padding_w;
			this.title_height = 21 - this.title_padding_h;

			this.container_width = this.width;
			this.container_height = this.height + this.title_height + this.title_padding_h;
		}
	},

	"setXY", function(x, y) {
		if( !this.container ) {
			return;
		}

		DW_UTIL.setXY(this.container, x, y);
	},

	"setWH", function(w, h) {
		if( !this.container ) {
			return;
		}

		this.calculateSize(w, h);

		DW_UTIL.setWH(this.container, this.container_width, this.container_height);
		DW_UTIL.setW(this.title, this.title_width);
		DW_UTIL.setWH(this.client, this.width, this.height);
		if( this.icon ) {
			DW_UTIL.setXY(this.icon, this.width - 13, 5);
		}
	},

	"createContainer", function(x, y) {
		this.container = DW_UTIL.addDiv(this.get_uid("container"), this.parent_id);
		DW_UTIL.setXYWH(this.container, x, y, this.container_width, this.container_height);
		DW_UTIL.setBackground(this.container, this.color);
		DW_UTIL.setBorder(this.container, this.border, this.border_color);
	},

	"createTitle", function() {
		this.title = DW_UTIL.addDiv(this.get_uid("title"), this.get_uid("container"));
		DW_UTIL.setXYWH(this.title, 0, 0, this.title_width, this.title_height);
		DW_UTIL.setBackground(this.title, this.title_color);
		YAHOO.util.Dom.setStyle(this.title, "font", "bold 8pt verdana");
		YAHOO.util.Dom.setStyle(this.title, "color", "#fff");
		YAHOO.util.Dom.setStyle(this.title, "padding", "3px 5px");
		YAHOO.util.Dom.setStyle(this.title, "cursor", "move");
	},

	"createClient", function() {
		this.client = DW_UTIL.addDiv(this.get_uid("client"), this.get_uid("container"));
		YAHOO.util.Event.isIE ? DW_UTIL.setXYWH(this.client, 0, this.title_height, this.width, this.height) : DW_UTIL.setXYWH(this.client, 0, this.title_height + this.title_padding_h, this.width, this.height);
		DW_UTIL.setBackground(this.client, this.color);
	},

	"createCloseIcon", function() {
		this.icon = DW_UTIL.addDiv(this.get_uid("close_icon"), this.get_uid("container"));
		DW_UTIL.setBackground(this.icon, this.icon_color1);
		DW_UTIL.setBorder(this.icon, 1, this.icon_color2);
		YAHOO.util.Event.isIE ? DW_UTIL.setXYWH(this.icon, this.width - 13, 5, 9, 9) : DW_UTIL.setXYWH(this.icon, this.width - 13, 5, 7, 7);
		YAHOO.util.Dom.setStyle(this.icon, "cursor", "pointer");
		for( var i=0; i<5; i++ ) {
			for( var j=0; j<5; j++ ) {
				if( i == j || i + j == 4 ) {
					DW_UTIL.addPixel(this.icon, i+1, j+1, this.icon_color2);
				}
			}
		}
		YAHOO.util.Event.addListener(this.icon, "click", this.onClose, this, true);
	},

	"createMenuIcon", function() {
		this.menu = new MENU(this.get_uid("menu"));
		this.menu.create();

		this.icon = DW_UTIL.addDiv(this.get_uid("menu_icon"), this.get_uid("container"));
		DW_UTIL.setBackground(this.icon, this.icon_color1);
		DW_UTIL.setBorder(this.icon, 1, this.icon_color2);
		DW_UTIL.setXYWH(this.icon, this.width - 13, 5, 7, 7);
		YAHOO.util.Dom.setStyle(this.icon, "cursor", "pointer");

		DW_UTIL.addPixel(this.icon, 3, 1, this.icon_color2, 1);
		DW_UTIL.addPixel(this.icon, 3, 3, this.icon_color2, 1);
		DW_UTIL.addPixel(this.icon, 3, 4, this.icon_color2, 1);
		DW_UTIL.addPixel(this.icon, 3, 5, this.icon_color2, 1);

		YAHOO.util.Event.addListener(this.icon, "mousedown", this.menu.show, this.menu, true);
	},

	"onClose", function(e) {
		this.close();
		this.hide();
	},

	"close", function() {
	},

	"addMenu", function(item, callback, scope) {
		if( !this.icon || !this.menu ) {
			return;
		}

		this.menu.addItem(item, callback, scope);
	},

	"create", function(x, y, w, h, option) {
		this.calculateSize(w, h);

		this.createContainer(x, y);
		this.createTitle();
		this.createClient();
		if( option == "MENU" ) {
			this.createMenuIcon();
		}
		else if( option == "CLOSE" ) {
			this.createCloseIcon();
		}

//		this.drag_drop = new YAHOO.util.DD(this.get_uid("container"));
//		this.drag_drop.setHandleElId(this.get_uid("title"));
	},

	"enableDragDrop", function() {
		this.drag_drop = new YAHOO.util.DD(this.get_uid("container"));
		this.drag_drop.setHandleElId(this.get_uid("title"));
	},

	"center", function() {
		var x = DW_UTIL.centerX(this.container_width - this.border * 2);
		var y = DW_UTIL.centerY(this.container_height - this.border * 2);
		DW_UTIL.setXY(this.container, x, y);
	},

	"setTitle", function(title) {
		this.title.innerHTML = title;
	},

	"addHTML", function(html) {
		this.client.innerHTML += html;
	},

	"addText", function(text) {
		this.client.innerHTML += text.replace(/ /gi, "&nbsp;") + "<br>";
	},

	"clearText", function() {
		this.client.innerHTML = "";
	},

	"hide", function() {
		if( this.container ) {
			YAHOO.util.Dom.setStyle(this.container, "display", "none");
			this.state = "HIDE";
		}
	},

	"show", function() {
		if( this.container ) {
			YAHOO.util.Dom.setStyle(this.container, "display", "block");
			this.state = "SHOW";
		}
	},

	"toggle", function() {
		if( this.state == "HIDE" ) {
			this.show();
		}
		else if( this.state == "SHOW" ) {
			this.hide();
		}
	}
);

MODAL_WINDOW = function(id, parent_id) {
	MODAL_WINDOW.superclass.constructor.call(this, id, parent_id);
}

Class(
	MODAL_WINDOW, WINDOW,

	"addBackground", function(opacity) {
		this.background = DW_UTIL.addTransparentDiv(this.get_uid("background"), document.body, 0, 0, YAHOO.util.Dom.getClientWidth(), YAHOO.util.Dom.getClientHeight(), opacity);
		YAHOO.util.Dom.setStyle(this.background, "zIndex", "998");
	},

	"create", function(x, y, w, h, option, opacity) {
		option = "CLOSE";
		if( opacity != 0 ) {
			opacity = opacity || 0.5;
		}
		MODAL_WINDOW.superclass.create.call(this, x, y, w, h, option);
		YAHOO.util.Dom.setStyle(this.container, "zIndex", "999");
		this.addBackground(opacity);
	},

	"init", function(id, parent_id) {
		MODAL_WINDOW.superclass.init.call(this, id, parent_id);

		this.background = null;
	},

	"clear", function() {
		delete this.background;

		MODAL_WINDOW.superclass.clear.call(this);
	},

	"destroy", function() {
		DW_UTIL.safeDestroy(this.get_uid("background"));

		MODAL_WINDOW.superclass.destroy.call(this);
	},

	"show", function() {
		MODAL_WINDOW.superclass.show.call(this);
		YAHOO.util.Dom.setStyle(this.background, "display", "block");
	},

	"hide", function() {
		MODAL_WINDOW.superclass.hide.call(this);
		YAHOO.util.Dom.setStyle(this.background, "display", "none");
	}
);

PROMPT = function(id, parent_id) {
	PROMPT.superclass.constructor.call(this, id, parent_id);
}

Class(
	PROMPT, MODAL_WINDOW,

	"create", function(x, y, w, h) {
		var x = 0;
		var y = 0;
		var w = 450;
		var h = 57;
		PROMPT.superclass.create.call(this, x, y, w, h);

		this.text = DW_UTIL.addInput(this.get_uid("text"), this.get_id(), "text", "");
		YAHOO.util.Dom.setStyle(this.text, "font", "9pt courier new");
		YAHOO.util.Dom.setStyle(this.text, "color", "#333");
		YAHOO.util.Dom.setStyle(this.text, "padding", "2px 2px 0px 2px");
		DW_UTIL.setBorder(this.text, 1, "#999");
		DW_UTIL.setXYWH(this.text, 5, 5, 434, 17);

		this.btn_ok = DW_UTIL.addInput(this.get_uid("btn_ok"), this.get_id(), "button", "OK");
		DW_UTIL.setBorder(this.btn_ok, 1, "#999");
		DW_UTIL.setBackground(this.btn_ok, "#eee");
		DW_UTIL.setXYWH(this.btn_ok, w - 210, 31, 100, 21);
		YAHOO.util.Dom.setStyle(this.btn_ok, "font", "8pt verdana");

		this.btn_cancel = DW_UTIL.addInput(this.get_uid("btn_cancel"), this.get_id(), "button", "Cancel");
		DW_UTIL.setBorder(this.btn_cancel, 1, "#999");
		DW_UTIL.setBackground(this.btn_cancel, "#eee");
		DW_UTIL.setXYWH(this.btn_cancel, w - 105, 31, 100, 21);
		YAHOO.util.Dom.setStyle(this.btn_cancel, "font", "8pt verdana");

		this.setTitle("PROMPT");

		YAHOO.util.Event.addListener(this.btn_ok, "click", this.handlerOK, this, true);
		YAHOO.util.Event.addListener(this.btn_cancel, "click", this.handlerCancel, this, true);
	},

	"clear", function() {
		delete this.text;
		delete this.btn_ok;
		delete this.btn_cancel;

		PROMPT.superclass.clear.call(this);
	},

	"destroy", function() {
		PROMPT.superclass.destroy.call(this);
	},

	"getValue", function() {
		if( this.text ) {
			return this.text.value;
		}
		return "";
	},

	"setValue", function(value) {
		if( this.text ) {
			this.text.value = value;
		}
	},

	"handlerOK", function() {
		this.onOK();
		this.destroy();
	},

	"handlerCancel", function() {
		this.onCancel();
		this.destroy();
	},

	"onOK", function() {
	},

	"onCancel", function() {
	}
);

FILE_DIALOG = function(id, parent_id) {
	FILE_DIALOG.superclass.constructor.call(this, id, parent_id);
}

Class(
	FILE_DIALOG, MODAL_WINDOW,

	"create", function() {
		var x = 0;
		var y = 0;
		var w = 450;
		var h = 57;
		FILE_DIALOG.superclass.create.call(this, x, y, w, h);

		this.file_text = DW_UTIL.addInput(this.get_uid("file_text"), this.get_id(), "text", "");
		YAHOO.util.Dom.setStyle(this.file_text, "font", "9pt courier new");
		YAHOO.util.Dom.setStyle(this.file_text, "color", "#333");
		YAHOO.util.Dom.setStyle(this.file_text, "padding", "2px 2px 0px 2px");
		DW_UTIL.setBorder(this.file_text, 1, "#999");
		DW_UTIL.setXYWH(this.file_text, 5, 5, 403, 17);

		this.file_button = DW_UTIL.addInput(this.get_uid("file_button"), this.get_id(), "button", "...");
		DW_UTIL.setBorder(this.file_button, 1, "#999");
		DW_UTIL.setBackground(this.file_button, "#fff");
		DW_UTIL.setXYWH(this.file_button, 420, 5, 25, 21);
		YAHOO.util.Dom.setStyle(this.file_button, "padding", "0px");
		YAHOO.util.Dom.setStyle(this.file_button, "font", "8pt verdana");

		this.file_ctrl = DW_UTIL.addInput(this.get_uid("file_ctrl"), this.get_id(), "file", "");
		YAHOO.util.Dom.setStyle(this.file_ctrl, "display", "none");

		this.btn_ok = DW_UTIL.addInput(this.get_uid("btn_ok"), this.get_id(), "button", "OK");
		DW_UTIL.setBorder(this.btn_ok, 1, "#999");
		DW_UTIL.setBackground(this.btn_ok, "#eee");
		DW_UTIL.setXYWH(this.btn_ok, w - 210, 31, 100, 21);
		YAHOO.util.Dom.setStyle(this.btn_ok, "font", "8pt verdana");

		this.btn_cancel = DW_UTIL.addInput(this.get_uid("btn_cancel"), this.get_id(), "button", "Cancel");
		DW_UTIL.setBorder(this.btn_cancel, 1, "#999");
		DW_UTIL.setBackground(this.btn_cancel, "#eee");
		DW_UTIL.setXYWH(this.btn_cancel, w - 105, 31, 100, 21);
		YAHOO.util.Dom.setStyle(this.btn_cancel, "font", "8pt verdana");

		this.setTitle("FILE DIALOG");

		YAHOO.util.Event.addListener(this.file_button, "click", function(e) {
			this.file_ctrl.click();
		}, this, true);

		YAHOO.util.Event.addListener(this.file_ctrl, "change", function(e) {
			this.file_text.value = this.file_ctrl.value;
		}, this, true);

		YAHOO.util.Event.addListener(this.btn_ok, "click", this.handlerOK, this, true);
		YAHOO.util.Event.addListener(this.btn_cancel, "click", this.handlerCancel, this, true);
	},

	"clear", function() {
		delete this.file_text;
		delete this.file_button;
		delete this.file_ctrl;
		delete this.btn_ok;
		delete this.btn_cancel;

		FILE_DIALOG.superclass.clear.call(this);
	},

	"destroy", function() {
		FILE_DIALOG.superclass.destroy.call(this);
	},

	"getValue", function() {
		return this.file_text.value;
	},

	"setValue", function(value) {
		this.file_text.value = value;
	},

	"handlerOK", function() {
		this.onOK();
		this.destroy();
	},

	"handlerCancel", function() {
		this.onCancel();
		this.destroy();
	},

	"onOK", function() {
	},

	"onCancel", function() {
	}
);

CREDIT = {
	show: function(text) {
		if( !CREDIT.wnd ) {
			text = text || "developer : idw1@naver.com" + "<br>"
						 + " language : javascript    ";

			CREDIT.wnd = new MODAL_WINDOW("about");
			CREDIT.wnd.border = 1;
			CREDIT.wnd.create(10, 10, 300, 60, 0.5);
			CREDIT.wnd.center();
			CREDIT.wnd.setTitle("About");

			YAHOO.util.Dom.setStyle(CREDIT.wnd.get_id(), "font", "9pt verdana");
			YAHOO.util.Dom.setStyle(CREDIT.wnd.get_id(), "textAlign", "center");
			YAHOO.util.Dom.setStyle(CREDIT.wnd.get_id(), "padding", "10px 0");
			//YAHOO.util.Dom.setStyle(CREDIT.wnd.get_id(), "color", "#ffffff");
			YAHOO.util.Dom.setStyle(CREDIT.wnd.get_id(), "lineHeight", "150%");
		}
		else {
			CREDIT.wnd.show();
		}

		CREDIT.wnd.clearText();
		CREDIT.wnd.addText(text.replace(/ /gi, "&nbsp;"));
	},

	setXY: function(x, y) {
		this.wnd.setXY(x, y);
	}
}

CONSOLE = function(id, parent_id) {
	CONSOLE.superclass.constructor.call(this, id, parent_id);
}

YAHOO.namespace("CONSOLE");

Class(
	CONSOLE, WINDOW,

	"create", function(x, y, w, h) {
		var x = 0;
		var y = 0;
		var w = 450;
		var h = 57;
		CONSOLE.superclass.create.call(this, x, y, w, h);

		this.output = DW_UTIL.addInput(this.get_uid("output"), this.get_id(), "text", "");
		this.output.readOnly = true;
		YAHOO.util.Dom.setStyle(this.output, "font", "9pt courier new");
		YAHOO.util.Dom.setStyle(this.output, "color", "#333");
		YAHOO.util.Dom.setStyle(this.output, "padding", "2px 2px 0px 2px");
		DW_UTIL.setBackground(this.output, this.color);
		DW_UTIL.setBorder(this.output, 1, this.color);
		DW_UTIL.setXYWH(this.output, 5, 5, 434, 17);

		this.input = DW_UTIL.addInput(this.get_uid("input"), this.get_id(), "text", "");
		YAHOO.util.Dom.setStyle(this.input, "font", "9pt courier new");
		YAHOO.util.Dom.setStyle(this.input, "color", "#333");
		YAHOO.util.Dom.setStyle(this.input, "padding", "2px 2px 0px 2px");
		DW_UTIL.setBorder(this.input, 1, "#999");
		DW_UTIL.setXYWH(this.input, 5, 31, 434, 17);

		this.setTitle("CONSOLE");

		YAHOO.util.Event.addListener(this.input, "keydown", this.onKey, this, true);
		YAHOO.CONSOLE.commands = new Array();
	},

	"clear", function() {
		delete this.text;
		delete this.btn_ok;
		delete this.btn_cancel;

		CONSOLE.superclass.clear.call(this);
	},

	"destroy", function() {
		CONSOLE.superclass.destroy.call(this);
	},

	"getValue", function() {
		if( this.input ) {
			return this.input.value;
		}
		return "";
	},

	"setValue", function(value) {
		if( this.output ) {
			this.output.value = value;
		}
	},

	"focus", function() {
		this.input.focus();
	},

	"onKey", function(e) {
		var count = YAHOO.CONSOLE.commands.length;

		if( e.keyCode == 13 ) { // enter
			YAHOO.CONSOLE.commands.push(this.getValue());
			YAHOO.CONSOLE.cursor = YAHOO.CONSOLE.commands.length;
			try {
				this.setValue("> " + eval(this.getValue()));
				this.x = null;
			}
			catch( x ) {
				this.setValue("> " + x.description);
				this.x = x;
			}
			this.input.value = "";
		}
		if( e.keyCode == 38 ) { // up
			YAHOO.CONSOLE.cursor--;
			if( YAHOO.CONSOLE.cursor < 0 ) {
				YAHOO.CONSOLE.cursor = count - 1;
			}
			this.input.value = YAHOO.CONSOLE.commands[YAHOO.CONSOLE.cursor];
		}
		if( e.keyCode == 40 ) { // down
			YAHOO.CONSOLE.cursor++;
			if( YAHOO.CONSOLE.cursor > count - 1 ) {
				YAHOO.CONSOLE.cursor = 0;
			}
			this.input.value = YAHOO.CONSOLE.commands[YAHOO.CONSOLE.cursor];
		}
	},

	"clr", function() {
		DW_UTIL.clearArray(YAHOO.CONSOLE.commands);
	}
);

LOGGER = function(id, parent_id) {
	LOGGER.superclass.constructor.call(this, id, parent_id);
}

YAHOO.namespace("CONSOLE");

Class(
	LOGGER, WINDOW,

	"create", function(x, y, w, h) {
		var x = 0;
		var y = 0;
		var w = 450;
		var h = 200;
		LOGGER.superclass.create.call(this, x, y, w, h, "CLOSE");

		this.output = DW_UTIL.addInput(this.get_uid("output"), this.get_id(), "textarea", "");
		this.output.wrap = "off";
		DW_UTIL.setBackground(this.output, "#000");
		DW_UTIL.setBorder(this.output, 0);
		DW_UTIL.setXYWH(this.output, 0, 0, w-10, h-10);
		YAHOO.util.Dom.setStyle(this.output, "overflow", "scroll");
		YAHOO.util.Dom.setStyle(this.output, "font", "9pt courier new");
		YAHOO.util.Dom.setStyle(this.output, "color", "#fff");
		YAHOO.util.Dom.setStyle(this.output, "padding", "5px");

		this.setTitle("LOGGER");

		YAHOO.util.Event.addListener(this.input, "keydown", this.onKey, this, true);
		YAHOO.CONSOLE.commands = new Array();
	},

	"clear", function() {
		delete this.text;
		delete this.btn_ok;
		delete this.btn_cancel;

		CONSOLE.superclass.clear.call(this);
	},

	"destroy", function() {
		CONSOLE.superclass.destroy.call(this);
	},

	"log", function(text) {
		this.output.value += text + "\r\n";
		this.output.style.scrollHeight = 999999;
	},

	"clr", function() {
		this.output.value = "";
	}
);
