// pwlib.js for prototype.js
//	fileinfo: dir=web/js

//---- ポップアップ ----
// ベース
var PopupWindow		= null;
var PopupObserver	= null;
var BackupDiv		= null;
var BackupIframe	= null;

// オーバーレイエフェクト
Windows.overlayShowEffectOptions = null;
Windows.overlayHideEffectOptions = null;

// ESCで閉じる
WindowCloseKey.init(27);


//---- ベース ----
function PopupDiv(div, title, win, opts) {
	ClosePopup();

	var s  = $(div).style;  BackupDiv = s.display;
	var wo = initWin(win);
	wo.title = title;
	var w = PopupWindow = new Window(wo);
	w.setContent(div, true, true);
	s.display = "block";
	w.showCenter(true, 20);

	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, 20);

	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 && opts.div) w.setContent(opts.div, true, true);
	else w.setContent(iframe, true, true);
	w.showCenter(true, 20);

	var po = PopupObserver = initObs(opts, { iframe: iframe });
	Windows.addObserver(po);
	return;
}

function ClosePopup() {
	if (PopupWindow != null) PopupWindow.close();
	return;
}

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, "top"			, 20);
	initValue(w, "minWidth"		, 520);
	initValue(w, "minHeight"	, 470);
	initValue(w, "destroyOnClose", true);
//	initValue(w, "closeCallback", function(win) {
//		if (win == PopupWindow) { PopupWindow = null; }
//		try { return o.onClose(); }
//		catch (e) { return true; }			// 閉じる
//		}
//	});
	return w;
}

function initObs(o, info) {
	var b = (o && o.obs) ? o.obs : { };
	if (!b.onDestroy) {
		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) GetElement("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 FitPopup(w,h) {
	if (PopupWindow == null) return;
return;
	var size = PopupWindow.getSize();
	if (w == "") w = size.width;
	if (h == "") h = size.height;
	PopupWindow.setSize(w, h);
	return;
}


//---- ユーティリティ ----
function Confirm(id, note, yurl, nurl, ylab, nlab, opts) {
	var p = $(id);
	if (note && (note != "")) SetHTML(id + "_r", note);
	if (!nurl) nurl = "js:ClosePopup()";
	if (!ylab) ylab = "はい";
	if (!nlab) nlab = "いいえ";
	SetLink(id + "_y", ylab, yurl);
	SetLink(id + "_n", nlab, nurl);
	PopupDiv(id, "確認", { minWidth:250, minHeight:100 });
	return;
}

function Alert(id, note, url, lab, opts) {
	var p = $(id);
	if (note && (note != "")) SetHTML(id + "_r", note);
	if (!url) url = "js:ClosePopup()";
	if (!lab) lab = "OK";
	if (!opts) opts = { minWidth:250, minHeight:100 };
	SetLink(id + "_ok", lab, url);
	PopupDiv(id, "確認", opts);
	return;
}

// end of pwlib.js
