﻿/// <reference path="../Script/Lib.js" />

var Window = new Class();
Window.prototype = 
{
	content		: null,
	useMask		: null,
	opacity 	: null,
	width		: null,
	height		: null,
	isOpen		: null,
	onopen		: null,
	onclose		: null,
	centerScreen: null,
	usingForm	: null,
	background	: null,

	init : function()
	{
		var cons = 
		[
			["DIV", { ID: "WIN", className: "CILA_WIN" }, [
				["TABLE", { cellSpacing: 0, cellPadding: 0 }, [
					["TR", {}, [
						["TD", {}, [["DIV", { className: "CILA_WIN_TopLeft"}]]],
						["TD", {}, [["DIV", { ID: "BAR", className: "CILA_WIN_TopCenter"}]]],
						["TD", {}, [["DIV", { className: "CILA_WIN_TopRight"}]]]
					]],
					["TR", {}, [
						["TD", { className : "CILA_WIN_CenterLeft"}],
						["TD", { }, [
							["DIV", {ID: "CENTER", className: "CILA_WIN_Center" }, [
								["DIV", {ID : "CONTENT"}]
							]]
						]],
						["TD", { className : "CILA_WIN_CenterRight"}]
					]],
					["TR", {}, [
						["TD", {}, [["DIV", { className: "CILA_WIN_BottomLeft"}]]],
						["TD", {}, [["DIV", { className: "CILA_WIN_BottomCenter"}]]],
						["TD", {}, [["DIV", { className: "CILA_WIN_BottomRight"}]]]
					]]
				]],
				["DIV", {className : "CILA_WIN_ButtonZone"}, [
					//["INPUT", {type : "button", className : "CILA_WIN_Button_Minimize", onclick : null}],
					//["INPUT", {type : "button", className : "CILA_WIN_Button_Maximize", onclick : null}],
					//["INPUT", {type : "button", className : "CILA_WIN_Button_Restore", onclick : null}],
					["INPUT", {type : "button", className : "CILA_WIN_Button_Close", onclick : this.close.handle(this)}]
				]]
			]]
		];
		this.ctrl = create(cons, document.body);
		if (this.width)
		{
			this.ctrl.WIN.style.width = this.width + "px";
			this.ctrl.CENTER.style.width = "100%";
		}
		if (this.height)
			this.ctrl.CENTER.style.height = this.height + "px";
		if (this.content)
		{
			var p = this.content.parentNode;
			if (p) p.removeChild(this.content);
			this.ctrl.CONTENT.appendChild(this.content);
		}
		this.popup = new PopupMenu
		({
			menu		: this.ctrl.WIN,
			moved		: true,
			moveObj		: this.ctrl.BAR,
			useMask		: this.useMask,
			resetPos	: false,
			autoClose	: false,
			background	: this.background,
			usingForm	: this.usingForm,
			opacity		: this.opacity,
			onopen		: this._onopen.handle(this),
			onclose		: this._onclose.handle(this),
			centerScreen: this.centerScreen
		});
	},
	
	open : function()
	{
		this.isOpen	= true;
		this.popup.show();
	},
	
	close : function()
	{
		this.isOpen	= false;
		this.popup.hide();
	},
	
	_onclose : function(){if (this.onclose) this.onclose();},
	_onopen : function(){if (this.onopen) this.onopen();}
}

inherit(Window, {
	
	init : function()
	{
		//alert
		var cons = 
		[
			["DIV", {ID : "CONTENT"}, [
				["DIV", {className : "CILA_WIN_Alert_Msg"}, [
					["INPUT", {type : "button", disabled : true, className : "CILA_WIN_AlertIcon"}],
					["SPAN", {ID : "TITLE"}]
				]],
				["DIV", {className : "CILA_WIN_Alert_IconZone"}, [
					["INPUT", {ID : "INPUT", type : "button", value : "OK", className : "CILA_WIN_Button"}]	
				]]
			]]
		];
		cons = create(cons, document.body);
		Window.alertPopup = new Window
		({
			content		: cons.CONTENT,
			useMask		: false,
			opacity		: 0.1,
			centerScreen: true,
			_title		: cons.TITLE,
			onopen		: function(input){this.focus()}.handle(cons.INPUT)
		});
		cons.INPUT.onclick = function(){Window.alertPopup.close();};
		//confirm
		cons = 
		[
			["DIV", {ID : "CONTENT"}, [
				["DIV", {className : "CILA_WIN_Alert_Msg"}, [
					["INPUT", {type : "button", disabled : true, className : "CILA_WIN_AlertIcon"}],
					["SPAN", {ID : "TITLE"}]
				]],
				["DIV", {className : "CILA_WIN_Alert_IconZone"}, [
					["INPUT", {ID : "INPUTOK", type : "button", value : "OK", className : "CILA_WIN_Button"}],
					["INPUT", {ID : "INPUTCANCEL", type : "button", value : "Cancel", className : "CILA_WIN_Button"}]	
				]]
			]]
		];
		cons = create(cons, document.body);
		Window.confirmPopup = new Window
		({
			content		: cons.CONTENT,
			useMask		: false,
			opacity		: 0.1,
			centerScreen: true,
			_title		: cons.TITLE,
			onopen		: function(input){this.focus()}.handle(cons.INPUTOK)
		});
		cons.INPUTCANCEL.onclick = function()
		{
			Window.confirmPopup.close();
		};
		cons.INPUTOK.onclick = function()
		{
			Window.confirmPopup.close();
			if (Window.confirmPopup._func) Window.confirmPopup._func();
		};
		//prompt
		cons = 
		[
			["DIV", {ID : "CONTENT"}, [
				["DIV", {className : "CILA_WIN_Alert_Msg"}, [
					["TABLE", {}, [
						["TR", {}, [
							["TD", {}, [
								["INPUT", {type : "button", disabled : true, className : "CILA_WIN_QuestionIcon"}]
							]],
							["TD", {}, [
								["SPAN", {ID : "TITLE"}],
								["BR"],
								["INPUT", {ID : "TEXT", type : "text", className : "CILA_WIN_Textbox"}]
							]]
						]]
					]]
				]],
				["DIV", {className : "CILA_WIN_Alert_IconZone"}, [
					["INPUT", {ID : "INPUTOK", type : "button", value : "OK", className : "CILA_WIN_Button"}],
					["INPUT", {ID : "INPUTCANCEL", type : "button", value : "Cancel", className : "CILA_WIN_Button"}]	
				]]
			]]
		];
		cons = create(cons, document.body);
		Window.promptPopup = new Window
		({
			content		: cons.CONTENT,
			useMask		: false,
			opacity		: 0.1,
			centerScreen: true,
			_title		: cons.TITLE,
			_text		: cons.TEXT,
			onopen		: function(input)
			{
				this.focus();
				if (!Window.promptPopup._value) Window.promptPopup._value = "";
				Window.promptPopup._text.value = Window.promptPopup._value;
			}.handle(cons.INPUTOK)
		});
		cons.INPUTCANCEL.onclick = function()
		{
			Window.promptPopup.close();
		};
		cons.INPUTOK.onclick = function()
		{
			Window.promptPopup.close();
			if (Window.promptPopup._func) Window.promptPopup._func(Window.promptPopup._text.value);
		};
		
		Window.init = null;
	},
	
	alert : function(str, func)
	{
		if (Window.init) Window.init();
		if (Window.alertPopup.isOpen)
			return;
		Window.alertPopup._title.innerHTML = str;
		Window.alertPopup.onclose = func;
		Window.alertPopup.open();
	},
	
	confirm : function(str, func)
	{
		if (Window.init) Window.init();
		if (Window.confirmPopup.isOpen)
			return;
		Window.confirmPopup._title.innerHTML = str;
		Window.confirmPopup._func = func;
		Window.confirmPopup.open();
	},
	
	prompt : function(str, value, func)
	{
		if (Window.init) Window.init();
		if (Window.promptPopup.isOpen)
			return;
		Window.promptPopup._title.innerHTML = str;
		Window.promptPopup._value = value;
		Window.promptPopup._func = func;
		Window.promptPopup.open();
	}
});
