// KJSLib PopupPane
if (typeof __KJSLib_PopupPane == "undefined")
{
var __KJSLib_PopupPane = true;
//--- start of include guard

var kPopup = {
	open : function(owner, x, y, flags, contentClass, contentCreateArgs, dropScrollDuration, dropScrollStepCount)
	{
		var popup = kPopupPane.own(owner);
		var containerElement = popup.getContainerElement();
		contentCreateArgs.unshift(containerElement);
		var contentElement = contentClass.create.apply(contentClass, contentCreateArgs);

//alert("[" + String(x) + ":" + String(y) + "][" + String(contentElement.offsetWidth) + ":" + String(contentElement.offsetHeight) + "]");

		popup.show((flags & kPopup.CAPTURE_MOUSE) != 0);
		popup.__private.totalHeight = contentElement.offsetHeight;

		if ((flags & kPopup.ADJUST_FIT_Y) != 0)
		{
			var docBody = popup.ownerDocument.body;
//alert("-->" + y + ":" + popup.__private.totalHeight + ":" + docBody.clientHeight + ":" + docBody.scrollTop);
			if (y + popup.__private.totalHeight > docBody.clientHeight + docBody.scrollTop)
			{
				y = docBody.clientHeight + docBody.scrollTop - popup.__private.totalHeight;
			}
		}
		popup.setPosition(x, y);

		var initialHeight;
		if (dropScrollDuration && typeof dropScrollDuration == "number" && dropScrollDuration > 0)
		{
			if (!dropScrollStepCount || typeof dropScrollStepCount != "number" || dropScrollStepCount <= 0)
			{
				dropScrollStepCount = popup.__private.totalHeight / 10;
			}
			popup.__private.dropScrollIncrement = popup.__private.totalHeight / dropScrollStepCount;
			popup.__private.curScrollHeight = popup.__private.dropScrollIncrement;
			popup.__private.dropScrollInterval = dropScrollDuration / dropScrollStepCount;
			initialHeight = popup.__private.curScrollHeight;
		}
		else
		{
			popup.__private.dropScrollInterval = null;
			initialHeight = popup.__private.totalHeight;
		}

		//alert("-->" + contentElement.offsetWidth + ":" + initialHeight);
		popup.setSize(contentElement.offsetWidth, initialHeight);

		if (dropScrollDuration && typeof dropScrollDuration == "number" && dropScrollDuration > 0)
		{
			kPopup.__scrollStep.__popup = popup;
			popup.getParentWindow().setTimeout(kPopup.__scrollStep, popup.__private.dropScrollInterval);
		}
		return popup;
	},
	// flag constants
	CAPTURE_MOUSE : 0x0001,
	ADJUST_FIT_X : 0x0002,
	ADJUST_FIT_Y : 0x0004,

	__scrollStep : function()
	{
		var popup = kPopup.__scrollStep.__popup;
		if (popup.__private.curScrollHeight < popup.__private.totalHeight)
		{
			popup.__private.curScrollHeight += popup.__private.dropScrollIncrement;
			if (popup.__private.curScrollHeight > popup.__private.totalHeight)
			{
				popup.__private.curScrollHeight = popup.__private.totalHeight;
			}
			popup.style.height = popup.__private.curScrollHeight;
			popup.getParentWindow().setTimeout(kPopup.__scrollStep, popup.__private.dropScrollInterval);
		}
	}
}


var kPopupPane = {
	own : function(owner)
	{
		if (this.__pane == null)
		{
			this.__initialize();
			kBindable.bind.call(this, this.__pane);
		}
		else
		{
			this.clear();
		}
		this.__pane.__private = {
			owner : owner
		}
		return this.__pane;
	},
	clear : function()
	{
		if (this.__pane != null)
		{
			if (this.__pane.isVisible())
			{
				this.__pane.hide();
			}
			this.__pane.clearContent();
			this.__pane.clearCSS();
			this.__pane.__private.owner = null;
		}
	},
	getPaneElement : function()
	{
		return this.__pane;
	},
	__pane : null,
	isIE : (navigator.appName == "Microsoft Internet Explorer"),

	prototype : {
		getOwner : function()
		{
			return this.__private.owner;
		},
		isVisible : function()
		{
			return this.style.display != "none";
		},
		setPosition : function(x, y)
		{
			this.style.left = x;
			this.style.top = y;
		},
		setSize : function(w, h)
		{
			this.style.width = w;
			this.style.height = h;
		},
		setContent : function(content)
		{
			this.getContainerElement().innerHTML = content;
		},
		clearContent : function()
		{
			this.getContainerElement().innerHTML = "";
		},
		show : function(captureMouse)
		{
			if ("none" == this.style.display)
			{
				this.style.display = "block";
				this.getContainerElement().focus();
				if (captureMouse)
				{
					this.mouseCaptured = true;
					this.getContainerElement().onclick = function() { this.releaseCapture(); }
					this.getContainerElement().onlosecapture = function() { kPopupPane.clear(); }
					this.getContainerElement().setCapture(false);
				}
			}
		},
		hide : function()
		{
			if ("none" != this.style.display)
			{
				if (this.__private.owner && this.__private.owner.onpopupclose)
				{
					this.__private.owner.onpopupclose(this);
				}
				this.style.display = "none";
				if (this.mouseCaptured)
				{
					this.getContainerElement().onclick = null;
					this.getContainerElement().onlosecapture = null;
					this.getContainerElement().releaseCapture();
				}
			}
		}
	}
}

if (kPopupPane.isIE)
{
	kPopupPane.__initialize = function()
	{
		kPopupPane.__pane = window.frames["__kPopupPane"].frameElement;
		var popupHTML = [];
		popupHTML.push("<html>");
		popupHTML.push("<head id=\"__kPopupHead\">");
		popupHTML.push("</head>");
		popupHTML.push("<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">");
		popupHTML.push("</body>");
		popupHTML.push("</html>");
		kPopupPane.__pane.contentWindow.document.open();
		kPopupPane.__pane.contentWindow.document.write(popupHTML.join("\n"));
		kPopupPane.__pane.contentWindow.document.close();

		kPopupPane.__pane.contentWindow.document.body.getPopupPane = function() { return this.ownerDocument.parentWindow.frameElement; }
	}

	kPopupPane.prototype.inheritCSSRules = function(selectorTexts)
	{
		rules = [];
		var ss = this.__private.owner.ownerDocument.styleSheets;
		for (var i = 0; i < ss.length; ++i)
		{
			var r = ss[i].rules;
			for (var j = 0; j < r.length; ++j)
			{
				var selector = r[j].selectorText;
				var k;
				for (k = 0; k < selectorTexts.length; ++k)
				{
					if (selector == selectorTexts[k])
					{
						var rule = {
							selector : selector,
							rule : r[j].style.cssText
						}
						rules.push(rule);
						//curSS.addRule(selector, r[j].style.cssText);
					}
				}
			}
		}
		if (rules.length > 0)
		{
			this.setCSS(rules);
		}
	}
	kPopupPane.prototype.setCSS = function(css)
	{
		if (this.contentWindow.document.getElementById("__kPopupStyle") == null)
		{
			var headElement = this.contentWindow.document.getElementById("__kPopupHead");
			var styleElement = this.contentWindow.document.createElement("STYLE");
			styleElement.id = "__kPopupStyle";
			styleElement.title = "__kPopupStyle";
			styleElement.type = "text/css";
			headElement.appendChild(styleElement);
		}
		var sSheet = null;
		var sSheets = this.contentWindow.document.styleSheets;
		var i;
		for (i = 0; i < sSheets.length; ++i)
		{
			if (sSheets[i].title == "__kPopupStyle")
			{
				sSheet = sSheets[i];
				break;
			}
		}
		if (sSheet != null)
		{
			for (i = 0; i < css.length; ++i)
			{
				sSheet.addRule(css[i].selector, css[i].rule);
			}
		}
	}
	kPopupPane.prototype.clearCSS = function()
	{
		var headElement = this.contentWindow.document.getElementById("__kPopupHead");
		var styleElement = this.contentWindow.document.getElementById("__kPopupStyle");
		if (headElement != null && styleElement != null)
		{
			headElement.removeChild(styleElement);
		}
	}
	kPopupPane.prototype.getContainerElement = function()
	{
		return this.contentWindow.document.body;
	}
	kPopupPane.prototype.getParentWindow = function()
	{
		return this.contentWindow;
	}
	kPopupPane.prototype.getElementById = function(id)
	{
		return this.contentWindow.document.getElementById(id);
	}
	document.write("<iframe style='display:none;position:absolute;z-index=100' id='__kPopupPane' name='__kPopupPane' marginheight='0' marginwidth='0' noresize frameborder='0' scrolling='no' src='javascript:\"\"'></iframe>");
}
else
{
	kPopupPane.__initialize = function()
	{
		kPopupPane.__pane = document.getElementById("__kPopupPane");
		kMouseCapturePrototype.bind(kPopupPane.__pane);

		kPopupPane.__pane.getPopupPane = function() { return this; }
	}

	kPopupPane.prototype.inheritCSSRules = function(selectorTexts)
	{
		// do nothing here: since we are using a <div>, all owner document style rules are already available
	}
	kPopupPane.prototype.setCSS = function(css)
	{
		if (document.getElementById("__kPopupStyle") == null)
		{
			var headElement = document.getElementsByTagName("HEAD")[0];
			styleElement = document.createElement("STYLE");
			styleElement.id = "__kPopupStyle";
			styleElement.title = "__kPopupStyle";
			styleElement.type = "text/css";
			headElement.appendChild(styleElement);
		}
		var sSheet = null;
		var sSheets = document.styleSheets;
		var i;
		for (i = 0; i < sSheets.length; ++i)
		{
			if (sSheets[i].title == "__kPopupStyle")
			{
				sSheet = sSheets[i];
				break;
			}
		}
		if (sSheet != null)
		{
			for (i = 0; i < css.length; ++i)
			{
				sSheet.insertRule(css[i].selector + "{" + css[i].rule + "}", i);
			}
		}
	}
	kPopupPane.prototype.clearCSS = function()
	{
		var styleElement = document.getElementById("__kPopupStyle");
		if (styleElement != null)
		{
			var headElement = styleElement.parentNode;
			headElement.removeChild(styleElement);
		}
	}
	kPopupPane.prototype.getContainerElement = function()
	{
		return this;
	}
	kPopupPane.prototype.getParentWindow = function()
	{
		return this.ownerDocument.defaultView;
	}
	kPopupPane.prototype.getElementById = function(id)
	{
		return document.getElementById(id);
	}
	document.write("<div style='display:none;position:absolute;overflow:hidden' id='__kPopupPane' name='__kPopupPane'></div>");
}

//--- end of include guard
}
