// prototype-window lib
//	ライブラリ
// global vars
var PopupWindow		= null;
var PopupObserver	= null;
var BackupDiv		= null;
var BackupIframe	= null;

// overlay effect setup
Windows.overlayShowEffectOptions = null;
Windows.overlayHideEffectOptions = null;

// public functions
function PopupDiv(div, title, win, opts) {
	ClosePopup();

	BackupDiv = $(div).style.display;
	var wo = initWin(win);
	wo.title = title;
	var w = PopupWindow = new Window(wo);
	w.setContent(div, true, true);
	w.showCenter(true);

	var po = PopupObserver = initObs(opts, { div: div });
	Windows.addObserver(po);
	return;
}

function PopupHTML(url, title, win, opts) {
	ClosePopup();

	var wo = initWin(win);
	var q  = url.indexOf("?", 0);
	wo.url = (q == -1) ? url : url + "&cache=" + (new Date()).getTime();
	wo.title = title;
	var w = PopupWindow = new Window(wo);
	w.showCenter(true);

	var po = PopupObserver = initObs(opts, { });
	Windows.addObserver(po);
	return;
}

function PopupForm(iframe, title, win, opts) {
	ClosePopup();

	BackupIframe = $(iframe).src;
	var wo = initWin(win);
	wo.title = title;
	var w = PopupWindow = new Window(wo);

	if ((opts != null) && (opts.div != null)) {
		w.setContent(opts.div, true, true);
	}
	else {
		w.setContent(iframe, true, true);
	}
	w.showCenter(true);

	var po = PopupObserver = initObs(opts, { iframe: iframe });
	Windows.addObserver(po);
	return;
}

function ClosePopup() {
	if (PopupWindow != null) PopupWindow.close();
	return;
}

// private functions
function initWin(o) {
	var w = (o != null) ? o : { };
	initValue(w, "minimizable"	, false);
	initValue(w, "maximizable"	, false);
	initValue(w, "resizable"	, true );
	initValue(w, "hideEffect"	, Element.hide);
	initValue(w, "showEffect"	, Element.show);
	initValue(w, "opacity"		, 1);
	initValue(w, "minWidth"		, 300);
	initValue(w, "destroyOnClose", true);
	return w;
}

function initObs(o, info) {
	var b = ((o != null) && (o.obs)) ? o.obs : { };
	if (b.onDestroy == null) {
		b.onDestroy = function(evt, win) {
			if (win == PopupWindow) {
				var div		= info.div;
				var iframe	= info.iframe;
				var id		= (div	  != null) ? div
							: (iframe != null) ? iframe
							: null;
				if (div != null) $("container").appendChild($(id));
				PopupWindow = null;
				Windows.removeObserver(PopupObserver);
				PopupObserver = null;
				if (div != null) {
					$(div).style.display = BackupDiv;
					BackupDiv = null;
				}
				else if (iframe != null) {
					$(iframe).src = BackupIframe;
					BackupIframe = null;
				}
			}
			try {
				o.onDestroyEnd();
			}
			catch (e) { ;}
			return;
		}
	}
	return b;
}

function initValue(o, name, def) {
	var x = o[name];
	if (x == null) o[name] = def;
	return;
}

function LoadHTML(url, id, opts) {
	if (id == null) id = "load";
	var on_complete_end = (opts != null) ? opts.onCompleteEnd : null;
	var p = $(id);
	var q = url.indexOf("?", 0);
	var curl = (q == -1)
			 ? url
			 : url + "&cache=" + (new Date()).getTime();
	new Ajax.Updater(id, curl, {
		method: "get",
		onComplete : function evalResponse(req) {
			var x = req.responseText;
			if (x.substring(0, 1) == "+") {
				p.innerHTML = "";
				eval(x.substring(1));
			}
			if (on_complete_end != null) {
				on_complete_end(req);
			}
			return;
		}
	});
	return void(0);
}

function LoadForm(formid, id) {
	if (formid == null) formid = "form";
	if (id == null) id = "load";
	var form = $(formid);
	new Ajax.Updater(id, form.action, {
		method		: form.method,
		parameters	: Form.serialize(form),
		onComplete	: function evalResponse(req) {
			var x = req.responseText;
			if (x.substring(0, 1) == "+") {
				p.innerHTML = "";
				eval(x.substring(1));
			}
			return;
		}
	});
	return void(0);
}

