﻿// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_OpenWindow(template, args)
{	
  var ua = navigator.userAgent;
	var isOpera = ( ua.indexOf('Opera') != -1 );
	
	// Does the caller support inline
	if (isOpera)
	{
		pro_windows.selectedWindow = null;
		return;
	}

	var url, resizable, scrollbars;

	url = template['file'];

	if (!(width = parseInt(template['width'])))
		width = 320;

	if (!(height = parseInt(template['height'])))
		height = 200;

	if (!(minWidth = parseInt(template['minWidth'])))
		minWidth = 100;

	if (!(minHeight = parseInt(template['minHeight'])))
		minHeight = 100;
		
	// dislevel ----------------------------------------------------------------------------	
	var _gp = pro_GetViewPort(window);
	
	if (!(_top = parseInt(template['top'])))
		_top = parseInt(_gp.height / 5.0) - parseInt(height / 5.0) + _gp.top;

	if (!(_left = parseInt(template['left'])))
		_left = parseInt(_gp.width / 2.0) - parseInt(width / 2.0);
		
	if (!(modal = template['modal']))
		modal = 'yes';
		
	_title = template['title'];
	// -------------------------------------------------------------------------------------

	resizable = (args && args['resizable']) ? args['resizable'] : "no";
	scrollbars = (args && args['scrollbars']) ? args['scrollbars'] : "no";

	height += 18;

	// Replace all args as variables in URL
	for (var name in args)
	{
		if (typeof(args[name]) == 'function')
			continue;

		url = pro_ReplaceVar(url, name, escape(args[name]));
	}

	// dislevel ----------------------------------------------------------------------------
	pro_windows.open(_title, url, pro_windows.idCounter++, "modal=" + modal + ",width=" + width+ ",height=" + height + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",statusbar=" + resizable + ",left=" + _left + ",top=" + _top + ",minWidth=" + minWidth + ",minHeight=" + minHeight );
	// -------------------------------------------------------------------------------------
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_ReplaceVar(h, r, v)
{
  return h.replace(new RegExp('{\\\$' + r + '}', 'g'), v);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_GetAbsPosition(n, cn)
{
	var l = 0, t = 0;

	while (n && n != cn)
	{
		l += n.offsetLeft;
		t += n.offsetTop;
		n = n.offsetParent;
	}

	return {absLeft : l, absTop : t};
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_GetViewPort(w)
{
	var d = w.document, m = d.compatMode == 'CSS1Compat', b = d.body, de = d.documentElement;

	return {
		left : w.pageXOffset || (m ? de.scrollLeft : b.scrollLeft),
		top : w.pageYOffset || (m ? de.scrollTop : b.scrollTop),
		width : w.innerWidth || (m ? de.clientWidth : b.clientWidth),
		height : w.innerHeight || (m ? de.clientHeight : b.clientHeight)
	};
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_CloseWindow(win)
{
	var n, w;
	for (n in pro_windows.windows)
	{
		w = pro_windows.windows[n];
		if (typeof(w) == 'function') continue;
		if (win.name == w.id + '_iframe')
		{
			w.close();
		}
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function pro_SetWindowTitle(win_ref, title)
{
	for (var n in pro_windows.windows)
	{
		var win = pro_windows.windows[n];
		if (typeof(win) == 'function')
			continue;

		if (win_ref.name == win.id + "_iframe")
			window.frames[win.id + "_iframe"].document.getElementById(win.id + '_title').innerHTML = title;
	}
}


// ---------------------------------------------------------------------------------------
// pro_Windows classes below
// Windows handler
// ---------------------------------------------------------------------------------------
function pro_Windows()
{
	this.settings = new Array();
	this.windows = new Array();
	
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
	this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
	this.isMac = navigator.userAgent.indexOf('Mac') != -1;
	this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
	
	this.action = "none";
	this.selectedWindow = null;
	this.lastSelectedWindow = null;
	this.zindex = 1001;
	
	this.mouseDownScreenX = 0;
	this.mouseDownScreenY = 0;
	this.mouseDownLayerX = 0;
	this.mouseDownLayerY = 0;
	this.mouseDownWidth = 0;
	this.mouseDownHeight = 0;
	this.idCounter = 0;
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.init = function(settings)
{
	this.settings = settings;

	if (this.isMSIE)
		this.addEvent(document, "mousemove", pro_windows.eventDispatcher);
	else
		this.addEvent(window, "mousemove", pro_windows.eventDispatcher);

	this.addEvent(document, "mouseup", pro_windows.eventDispatcher);

	this.addEvent(window, "resize", pro_windows.eventDispatcher);
	this.addEvent(document, "scroll", pro_windows.eventDispatcher);

	this.doc = document;
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.getBounds = function()
{
	if (!this.bounds)
	{
		var vp = pro_GetViewPort(window);
		var top, left, bottom, right, docEl = this.doc.documentElement;

		top    = vp.top;
		left   = vp.left;
		bottom = vp.height + top - 2;
		right  = vp.width  + left - 22; // TODO this number is platform dependant
		// x1, y1, x2, y2
		this.bounds = [left, top, right, bottom];
	}
	return this.bounds;
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.clampBoxPosition = function(x, y, w, h, minW, minH)
{
	var bounds = this.getBounds();
	
	// 2007.09.21 김동호 주석처리 - 왜 연산하는지 이유를 모르겠슴.
	//x = Math.max(bounds[0], Math.min(bounds[2], x + w) - w);
	//y = Math.max(bounds[1], Math.min(bounds[3], y + h) - h);

	return this.clampBoxSize(x, y, w, h, minW, minH);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.clampBoxSize = function(x, y, w, h, minW, minH)
{
	var bounds = this.getBounds();

	return [
		x, y,
		Math.max(minW, Math.min(bounds[2], x + w) - x),
		Math.max(minH, Math.min(bounds[3], y + h) - y)
	];
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.getParam = function(name, default_value)
{
	var value = null;

	value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];

	// Fix bool values
	if (value == "true" || value == "false")
		return (value == "true");

	return value;
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.eventDispatcher = function(e)
{
	e = typeof(e) == "undefined" ? window.event : e;

	if (pro_windows.selectedWindow == null)
		return;

	// Switch focus
	if (pro_windows.isGecko && e.type == "mousedown")
	{
		var elm = e.currentTarget;

		for (var n in pro_windows.windows) {
			var win = pro_windows.windows[n];

			if (win.headElement == elm || win.resizeElement == elm)
			{
				win.focus();
				break;
			}
		}
	}

	switch (e.type)
	{
		case "mousemove":
			pro_windows.selectedWindow.onMouseMove(e);
			break;

		case "mouseup":
			pro_windows.selectedWindow.onMouseUp(e);
			break;

		case "mousedown":
			pro_windows.selectedWindow.onMouseDown(e);
			break;

		case "focus":
			pro_windows.selectedWindow.onFocus(e);
			break;
		case "scroll":
		case "resize":
			if (pro_windows.clampUpdateTimeout)
				clearTimeout(pro_windows.clampUpdateTimeout);
			pro_windows.clampEventType = e.type;
			pro_windows.clampUpdateTimeout =
				setTimeout(function () {pro_windows.updateClamping()}, 100);
			break;
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.updateClamping = function ()
{
	var clamp, oversize, etype = pro_windows.clampEventType;

	this.bounds = null; // Recalc window bounds on resize/scroll
	this.clampUpdateTimeout = null;

	for (var n in this.windows)
	{
		win = this.windows[n];
		if (typeof(win) == 'function' || ! win.winElement) continue;

		clamp = pro_windows.clampBoxPosition(
			win.left, win.top,
			win.winElement.scrollWidth,
			win.winElement.scrollHeight,
			win.features.minWidth,
			win.features.minHeight
		);
		oversize = (
			clamp[2] != win.winElement.scrollWidth ||
			clamp[3] != win.winElement.scrollHeight
		) ? true : false;

		if (!oversize || win.features.resizable == "yes" || etype != "scroll")
			win.moveTo(clamp[0], clamp[1]);
		if (oversize && win.features.resizable == "yes")
			win.resizeTo(clamp[2], clamp[3]);
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.addEvent = function(obj, name, handler)
{
	if (this.isMSIE)
		obj.attachEvent("on" + name, handler);
	else
		obj.addEventListener(name, handler, false);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.cancelEvent = function(e)
{
	if (this.isMSIE)
	{
		e.returnValue = false;
		e.cancelBubble = true;
	} else
		e.preventDefault();
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.parseFeatures = function(opts)
{
	// Cleanup the options
	opts = opts.toLowerCase();
	opts = opts.replace(/;/g, ",");
	opts = opts.replace(/[^0-9a-z=,]/g, "");

	var optionChunks = opts.split(',');
	var options = new Array();

	options['left'] = "10";
	options['top'] = "10";
	options['width'] = "300";
	options['height'] = "300";
	options['minwidth'] = "100";
	options['minheight'] = "100";
	options['resizable'] = "no";
	options['minimizable'] = "no";
	options['maximizable'] = "no";
	options['close'] = "yes";
	options['movable'] = "yes";
	options['statusbar'] = "no";
	options['scrollbars'] = "no";
	options['modal'] = "yes";

	if (opts == "")
		return options;

	for (var i=0; i<optionChunks.length; i++)
	{
		var parts = optionChunks[i].split('=');

		if (parts.length == 2)
			options[parts[0]] = parts[1];
	}

	options['left'] = parseInt(options['left']);
	options['top'] = parseInt(options['top']);
	options['width'] = parseInt(options['width']);
	options['height'] = parseInt(options['height']);
	options['minWidth'] = parseInt(options['minwidth']);
	options['minHeight'] = parseInt(options['minheight']);

	return options;
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.open = function(title, url, name, features)
{
	this.lastSelectedWindow = this.selectedWindow;

	var win = new pro_Window();
	var winDiv, html = "", id;
	var imgPath = this.getParam("images_path");

	features = this.parseFeatures(features);

	// Clamp specified dimensions
	var clamp = pro_windows.clampBoxPosition(
		features['left'], features['top'],
		features['width'], features['height'],
		features['minWidth'], features['minHeight']
	);

	features['left'] = clamp[0];
	features['top'] = clamp[1];
    
	if (features['resizable'] == "yes")
	{
		features['width'] = clamp[2];
		features['height'] = clamp[3];
	}

	// Create div
	id = "mvWindow_" + name;
	win.deltaHeight = 18;

	if (features['statusbar'] == "yes")
	{
		win.deltaHeight += 13;

		if (this.isMSIE)
			win.deltaHeight += 1;
	}

	width = parseInt(features['width']);
	height = parseInt(features['height'])-win.deltaHeight;

	if (this.isMSIE)
		width -= 2;

	// Setup first part of window
	win.id = id;
	win.url = url;
	win.name = name;
	win.features = features;
	this.windows[name] = win;

	iframeWidth = width;
	iframeHeight = height;

	/*
	// Create inner content
	html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  html += '<html xmlns="http://www.w3.org/1999/xhtml">';
  html += '<head>';
  html += '<title>Wrapper iframe</title>';
  html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
  html += '<link href="' + this.getParam("css_file") + '" rel="stylesheet" type="text/css"/>';
  html += '</head>';
  html += '<body onload="parent.pro_windows.onLoad(\'' + name + '\');">';
  html += '<div id="' + id + '_container" class="container">';
  html += '  <div id="' + id + '_head" class="top" onmousedown="parent.pro_windows.windows[\'' + name + '\'].focus();">';
  html += '    <div id="' + id + '_title" class="topTitle" onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;"></div>';
  html += '    <div class="topR"><a href="javascript:parent.pro_windows.windows[\'' + name + '\'].close();" target="_self" onmousedown="return false;"><img alt="닫기" src="' + imgPath + '/close.gif" width="65" height="23" border="0" /></a></div>'; // 12x 12
  html += '    <div class="bodyClear"></div>';
  html += '  </div>';
  html += '  <div id="' + id + '_body" class="body">';
  html += '    <div class="bodyR">';
  html += '      <div class="pad" style="height: ' + height + 'px;">';
  html += '        <iframe id="' + id + '_iframe" name="' + id + '_iframe" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe" scrolling="' + features['scrollbars'] + '" allowTransparency="true"></iframe>';
  html += '      </div>';
  html += '    </div>';
  html += '    <div class="bodyClear"></div>';
  html += '  </div>';
  html += '  <div class="footerS">';
  html += '    <div class="footerSR"></div>';
  html += '  </div>';
  html += '</div>';
  html += '</body>';
	html += '</html>';
	*/
	
	html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  html += '<html xmlns="http://www.w3.org/1999/xhtml">';
  html += '<head>';
  html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  html += '<title>Wrapper iframe</title>';
  html += '<link href="' + this.getParam("css_file") + '" rel="stylesheet" type="text/css"/>';
  html += '</head>';
  html += '<body onload="parent.pro_windows.onLoad(\'' + name + '\');">';
  html += '<div id="' + id + '_container">';
  html += '  <div id="' + id + '_head" style="height: 28px; cursor: move;" onmousedown="parent.pro_windows.windows[\'' + name + '\'].focus();">';
  html += '    <!--close 버튼 부분 시작-->';
  html += '    <div class="close"><a href="javascript:parent.pro_windows.windows[\'' + name + '\'].close();" target="_self" onmousedown="return false;"><img alt="닫기" title="닫기" src="/ipopup2/img/close.gif" width="100" height="28" border="0" /></a></div>';
  html += '    <!--close 버튼 부분 끝-->';
  html += '  </div>';
  html += '  <div class="cb"></div>';
  html += '  <div class="topL"><img alt="border" title="border" src="/ipopup2/img/topR.gif" width="8" height="8" class="fR" /></div>';
  html += '  <!--컨텐츠 들어가는 부분 시작-->';
  html += '  <div class="contents">';
  html += '    <iframe id="' + id + '_iframe" name="' + id + '_iframe" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe" scrolling="' + features['scrollbars'] + '"></iframe>';
  html += '  </div>';
  html += '  <!--컨텐츠 들어가는 부분 끝-->';
  html += '  <div class="botL"></div>';
  html += '</div>';
  html += '</body>';
  html += '</html>';
          

	// Create iframe
	this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
}


// ---------------------------------------------------------------------------------------
// Blocks the document events by placing a image over the whole document
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.setDocumentLock = function(state)
{
	var elm = document.getElementById('mcWindowEventBlocker');

	if (state)
	{
		if (elm == null)
		{
			elm = document.createElement("div");

			elm.id = "mcWindowEventBlocker";
			elm.style.position = "absolute";
			elm.style.left = "0";
			elm.style.top = "0";
			// dislevel ------------------------------------------------------------------------
			//elm.style.backgroundColor = "transparent";
			// dislevel ------------------------------------------------------------------------

			document.body.appendChild(elm);
		}
		
		var _gp = pro_GetViewPort(window);

		elm.style.display = "none";

		var imgPath = this.getParam("images_path");
		var width = document.body.clientWidth;
		var height = document.body.clientHeight;
		
		elm.style.width = width;
		elm.style.height = height;
		elm.innerHTML = '<img src="' + imgPath + '/spacer.gif" width="' + width + '" height="' + height + '" />';

		elm.style.zIndex = pro_windows.zindex-1;
		elm.style.display = "block";
		
		elm.style.backgroundColor = '#000000';
		elm.style.MozOpacity = 0.4;
		elm.style.opacity = 0.4;
		elm.style.filter = 'alpha(opacity=' + 40 + ')';
	}
	else if (elm != null)
	{
		if (pro_windows.windows.length == 0)
			elm.parentNode.removeChild(elm);
		else
			elm.style.zIndex = pro_windows.zindex-1;
	}
}


// ---------------------------------------------------------------------------------------
// Gets called when wrapper iframe is initialized
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.onLoad = function(name)
{
	var win = pro_windows.windows[name];
	var id = "mvWindow_" + name;
	var wrapperIframe = window.frames[id + "_iframe"].frames[0];
	var wrapperDoc = window.frames[id + "_iframe"].document;
	var doc = window.frames[id + "_iframe"].document;
	var winDiv = document.getElementById("mvWindow_" + name + "_div");
	var realIframe = window.frames[id + "_iframe"].frames[0];

	// Set window data
	win.id = "mvWindow_" + name;
	win.winElement = winDiv;
	win.bodyElement = doc.getElementById(id + '_body');
	win.iframeElement = doc.getElementById(id + '_iframe');
	win.headElement = doc.getElementById(id + '_head');
	win.titleElement = doc.getElementById(id + '_title');
	win.resizeElement = doc.getElementById(id + '_resize');
	win.containerElement = doc.getElementById(id + '_container');
	win.left = win.features['left'];
	win.top = win.features['top'];
	win.frame = window.frames[id + '_iframe'].frames[0];
	win.wrapperFrame = window.frames[id + '_iframe'];
	win.wrapperIFrameElement = document.getElementById(id + "_iframe");

	// Add event handlers
	pro_windows.addEvent(win.headElement, "mousedown", pro_windows.eventDispatcher);

	if (win.resizeElement != null)
		pro_windows.addEvent(win.resizeElement, "mousedown", pro_windows.eventDispatcher);

	if (pro_windows.isMSIE)
	{
		pro_windows.addEvent(realIframe.document, "mousemove", pro_windows.eventDispatcher);
		pro_windows.addEvent(realIframe.document, "mouseup", pro_windows.eventDispatcher);
	}
	else
	{
		pro_windows.addEvent(realIframe, "mousemove", pro_windows.eventDispatcher);
		pro_windows.addEvent(realIframe, "mouseup", pro_windows.eventDispatcher);
		pro_windows.addEvent(realIframe, "focus", pro_windows.eventDispatcher);
	}

	for (var i=0; i<window.frames.length; i++)
	{
		if (!window.frames[i]._hasMouseHandlers)
		{
			if (pro_windows.isMSIE)
			{
				pro_windows.addEvent(window.frames[i].document, "mousemove", pro_windows.eventDispatcher);
				pro_windows.addEvent(window.frames[i].document, "mouseup", pro_windows.eventDispatcher);
			}
			else
			{
				pro_windows.addEvent(window.frames[i], "mousemove", pro_windows.eventDispatcher);
				pro_windows.addEvent(window.frames[i], "mouseup", pro_windows.eventDispatcher);
			}

			window.frames[i]._hasMouseHandlers = true;
		}
	}

	if (pro_windows.isMSIE)
	{
		pro_windows.addEvent(win.frame.document, "mousemove", pro_windows.eventDispatcher);
		pro_windows.addEvent(win.frame.document, "mouseup", pro_windows.eventDispatcher);
	}
	else
	{
		pro_windows.addEvent(win.frame, "mousemove", pro_windows.eventDispatcher);
		pro_windows.addEvent(win.frame, "mouseup", pro_windows.eventDispatcher);
		pro_windows.addEvent(win.frame, "focus", pro_windows.eventDispatcher);
	}

	// Dispatch open window event
	var func = this.getParam("on_open_window", "");
	if (func != "")
		eval(func + "(win);");

	win.focus();

	if (win.features['modal'] == "yes")
		pro_windows.setDocumentLock(true);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Windows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html)
{
	var iframe = document.createElement("iframe");
	var div = document.createElement("div"), doc;

	// dislevel ----------------------------------------------------------------------------
	width = parseInt(width)+14;
	height = parseInt(height)+28;
	// -------------------------------------------------------------------------------------

	// Create wrapper div
	div.setAttribute("id", id_prefix + "_div");
	div.setAttribute("width", width);
	div.setAttribute("height", (height));
	div.style.position = "absolute";

	div.style.left = left + "px";
	div.style.top = top + "px";
	div.style.width = width + "px";
	div.style.height = (height) + "px";
	// dislevel ----------------------------------------------------------------------------
	//div.style.backgroundColor = "transparent";
	// dislevel ----------------------------------------------------------------------------
	div.style.display = "none";

	if (this.isGecko)
	{
		iframeWidth = width + 2;
		iframeHeight = height + 2;
	}
	else
	{
		iframeWidth = width;
		iframeHeight = height + 1;
	}

	// Create iframe
	iframe.setAttribute("id", id_prefix + "_iframe");
	iframe.setAttribute("name", id_prefix + "_iframe");
	iframe.setAttribute("border", "0");
	iframe.setAttribute("frameBorder", "0");
	iframe.setAttribute("marginWidth", "0");
	iframe.setAttribute("marginHeight", "0");
	iframe.setAttribute("leftMargin", "0");
	iframe.setAttribute("topMargin", "0");
	iframe.setAttribute("width", iframeWidth);
	iframe.setAttribute("height", iframeHeight);
	// iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
	// dislevel ----------------------------------------------------------------------------
	iframe.setAttribute("allowtransparency", "true");
	// dislevel ----------------------------------------------------------------------------
	iframe.setAttribute("scrolling", "no");
	iframe.style.width = iframeWidth + "px";
	iframe.style.height = iframeHeight + "px";
	// dislevel ----------------------------------------------------------------------------
	//iframe.style.backgroundColor = "transparent";
	// dislevel ----------------------------------------------------------------------------
	div.appendChild(iframe);

	document.body.appendChild(div);

	// Fixed MSIE 5.0 issue
	div.innerHTML = div.innerHTML;

	if (this.isSafari)
	{
		// Give Safari some time to setup
		window.setTimeout(function()
		{
			var doc = window.frames[id_prefix + '_iframe'].document;
			doc.open();
			doc.write(html);
			doc.close();
		}, 10);
	}
	else
	{
		doc = window.frames[id_prefix + '_iframe'].window.document;
		doc.open();
		doc.write(html);
		doc.close();
	}

	div.style.display = "block";

	return div;
}


// ---------------------------------------------------------------------------------------
// Window instance
// ---------------------------------------------------------------------------------------
function pro_Window()
{
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.focus = function()
{
	if (this != pro_windows.selectedWindow)
	{
		this.winElement.style.zIndex = ++pro_windows.zindex;
		pro_windows.lastSelectedWindow = pro_windows.selectedWindow;
		pro_windows.selectedWindow = this;
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.minimize = function()
{
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.maximize = function()
{
	if (this.restoreSize)
	{
		this.moveTo(this.restoreSize[0], this.restoreSize[1]);
		this.resizeTo(this.restoreSize[2], this.restoreSize[3]);
		this.updateClamping();
		this.restoreSize = null;
	}
	else
	{
		var bounds = pro_windows.getBounds();
		this.restoreSize = [
			this.left, this.top,
			this.winElement.scrollWidth,
			this.winElement.scrollHeight
		];
		this.moveTo(bounds[0], bounds[1]);
		this.resizeTo(
			bounds[2] - bounds[0],
			bounds[3] - bounds[1]
		);
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.startResize = function()
{
	pro_windows.action = "resize";
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.startMove = function(e)
{
	pro_windows.action = "move";
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.close = function()
{
	if (pro_windows.lastSelectedWindow != null)
		pro_windows.lastSelectedWindow.focus();

	var pro_windowsNew = new Array();
	for (var n in pro_windows.windows)
	{
		var win = pro_windows.windows[n];
		if (typeof(win) == 'function')
			continue;

		if (win.name != this.name)
			pro_windowsNew[n] = win;
	}

	pro_windows.windows = pro_windowsNew;

	// alert(pro_windows.doc.getElementById(this.id + "_iframe"));

	var e = pro_windows.doc.getElementById(this.id + "_iframe");
	e.parentNode.removeChild(e);

	var e = pro_windows.doc.getElementById(this.id + "_div");
	e.parentNode.removeChild(e);

	pro_windows.setDocumentLock(false);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.onMouseMove = function(e)
{
	var clamp;
	// Calculate real X, Y
	var dx = e.screenX - pro_windows.mouseDownScreenX;
	var dy = e.screenY - pro_windows.mouseDownScreenY;

	switch (pro_windows.action)
	{
		case "resize":
			clamp = pro_windows.clampBoxSize(
				this.left, this.top,
				pro_windows.mouseDownWidth + (e.screenX - pro_windows.mouseDownScreenX),
				pro_windows.mouseDownHeight + (e.screenY - pro_windows.mouseDownScreenY),
				this.features.minWidth, this.features.minHeight
			);

			this.resizeTo(clamp[2], clamp[3]);

			pro_windows.cancelEvent(e);
			break;

		case "move":
			this.left = pro_windows.mouseDownLayerX + (e.screenX - pro_windows.mouseDownScreenX);
			this.top = pro_windows.mouseDownLayerY + (e.screenY - pro_windows.mouseDownScreenY);
			this.updateClamping();

			pro_windows.cancelEvent(e);
			break;
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.moveTo = function (x, y)
{
	this.left = x;
	this.top = y;

	this.winElement.style.left = this.left + "px";
	this.winElement.style.top = this.top + "px";
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.resizeTo = function (width, height)
{
	this.wrapperIFrameElement.style.width = (width+2) + 'px';
	this.wrapperIFrameElement.style.height = (height+2) + 'px';
	this.wrapperIFrameElement.width = width+2;
	this.wrapperIFrameElement.height = height+2;
	this.winElement.style.width = width + 'px';
	this.winElement.style.height = height + 'px';

	height = height - this.deltaHeight;

	this.containerElement.style.width = width + 'px';
	this.iframeElement.style.width = width + 'px';
	this.iframeElement.style.height = height + 'px';
	this.bodyElement.style.width = width + 'px';
	this.bodyElement.style.height = height + 'px';
	this.headElement.style.width = width + 'px';
	//this.statusElement.style.width = width + 'px';
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.updateClamping = function ()
{
	var clamp, oversize;

	clamp = pro_windows.clampBoxPosition(
		this.left, this.top,
		this.winElement.scrollWidth,
		this.winElement.scrollHeight,
		this.features.minWidth, this.features.minHeight
	);
	oversize = (
		clamp[2] != this.winElement.scrollWidth ||
		clamp[3] != this.winElement.scrollHeight
	) ? true : false;

	this.moveTo(clamp[0], clamp[1]);
	if (this.features.resizable == "yes" && oversize)
		this.resizeTo(clamp[2], clamp[3]);
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
function debug(msg)
{
	document.getElementById('debug').value += msg + "\n";
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.onMouseUp = function(e)
{
	pro_windows.action = "none";
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.onFocus = function(e)
{
	// Gecko only handler
	var winRef = e.currentTarget;

	for (var n in pro_windows.windows) {
		var win = pro_windows.windows[n];
		if (typeof(win) == 'function')
			continue;

		if (winRef.name == win.id + "_iframe") {
			win.focus();
			return;
		}
	}
}


// ---------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
pro_Window.prototype.onMouseDown = function(e)
{
	var elm = pro_windows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;

	pro_windows.mouseDownScreenX = e.screenX;
	pro_windows.mouseDownScreenY = e.screenY;
	pro_windows.mouseDownLayerX = this.left;
	pro_windows.mouseDownLayerY = this.top;
	pro_windows.mouseDownWidth = parseInt(this.winElement.style.width);
	pro_windows.mouseDownHeight = parseInt(this.winElement.style.height);

	if (this.resizeElement != null && elm == this.resizeElement.firstChild)
		this.startResize(e);
	else
		this.startMove(e);

	pro_windows.cancelEvent(e);
}


// ---------------------------------------------------------------------------------------
// Global instance
// ---------------------------------------------------------------------------------------
var pro_windows = new pro_Windows();


// ---------------------------------------------------------------------------------------
// Initialize windows
// ---------------------------------------------------------------------------------------
pro_windows.init({
	images_path : "/ipopup2/img",
	css_file : "/ipopup2/fpopup.css"
});
