// 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;

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

// 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, 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 != null) && (opts.div != null)) {
		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;
}

// 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, "top"			, 20);
	initValue(w, "minWidth"		, 520);
	initValue(w, "minHeight"	, 470);
	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 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;
}
