﻿
function WebHtmlEditorWindow(id)
{
	this.IsIE = (null != document.all) && (window.opera == null);	
	this.Id = id;
	this.Width = 0;
	this.Height = 0;  
	this.OnClientClose = null;
	this.ContentWindow = null;
	this.ContentTD = null;
	this.ContentWrapperTable = null;
	this.Caption = null;
	this.X = 0;
	this.Y = 0;
	this.ShowContentWhenMoving = true;
	this.borderWidth = 0;	
	this.CanMove = true;
	this.CanResize = true;	
	this.DragMode = "";
	this.IsModal = false;
	this.Container = null;
	this.Parent = null;
	this.Argument = null;
	this.ReturnValue = null;
	this.ExitCode = null;
	this.ZIndex = 0;
	this.AdjustPosInterval = -1;
	this.CallbackFunc = null;
	this.OnLoadFunc = null;
	this.Param = null;	
	this.ModalSetCapture = false;	
	this.UseWebHtmlEditorWindow = true;
	this.Window = null;
	this.InnerHTML = null;
}

WebHtmlEditorWindow.prototype.OnLoad = function()
{
	if (this.Window && "" != this.Window.document.title && this.GetCaption()=="")
	{
		this.SetCaption(this.Window.document.title);
	}

	if (this.OnLoadFunc)
	{
		this.OnLoadFunc();
	}
}

WebHtmlEditorWindow.prototype.SetCapture = function(bContainerCapture)
{
	if (this.UseWebHtmlEditorWindow)
	{
		if (null != bContainerCapture)
		{
			this.bContainerCapture = bContainerCapture;
		}
		else if (null != this.bContainerCapture)
		{
			bContainerCapture = this.bContainerCapture;
		}
		else
		{
			bContainerCapture = false;
		}

		if (this.ModalSetCapture && this.IsIE)
		{
			this.ContentWrapperTable.setCapture(bContainerCapture);
		}
	}	
}

WebHtmlEditorWindow.prototype.ReleaseCapture = function()
{
	if (this.UseWebHtmlEditorWindow)
	{
		if (this.ModalSetCapture && this.IsIE)
		{
			if (this.ContentWrapperTable)
				this.ContentWrapperTable.releaseCapture();
		}
	}
}

WebHtmlEditorWindow.prototype.SetZIndex = function(zIndex)
{
	this.ZIndex = zIndex;
	if (this.ContentWrapperTable)
	{
		this.ContentWrapperTable.style.zIndex = this.ZIndex;
	}
}

WebHtmlEditorWindow.prototype.ToggleContent = function()
{
	return;	
	if (this.UseWebHtmlEditorWindow && this.IsIE)
	{	
		var displayStyle = "";	
		if (parseInt(this.Height) == parseInt(this.ContentWrapperTable.style["height"]))
		{
			this.SetHeight(0);
			displayStyle = "none";
		}
		else
		{
			this.SetHeight(this.Height);
			displayStyle = "inline";
		}
		
		this.ContentWindow.style.display = displayStyle;
	}
}

WebHtmlEditorWindow.prototype.IsVisible = function()
{
	if (this.ContentWrapperTable)
	{
		return this.ContentWrapperTable.style.display == "block";
	}	
	return false;
}

WebHtmlEditorWindow.prototype.ShowWindow = function(show, x, y)
{
	if (null == show)
	{
		show = true;
	}	
	var displayStyle = show ? "block" : "none";
	
	if (this.ContentWrapperTable)
	{
		this.ContentWrapperTable.style.display = displayStyle;
	}	
	
	if (null != this.ContentWindow && show)
	{
		this.ContentWindow.style.display = displayStyle;
		if (null != x && null != y)
		{
			x += 10;
			if (this.ContentWrapperTable)
			{
				this.ContentWrapperTable.style.left = x + 'px';
				this.ContentWrapperTable.style.top = y + 'px';		
			}
		}								
		for (var i = 0; i < this.ContentWindow.childNodes.length; i++)
		{			
			try
			{
				this.ContentWindow.childNodes[i].style.display = displayStyle;												
			}
			catch (ex)
			{			
			}
		}
	}
	if (this.Parent)
	{
		this.Parent.OnShowWindow(this, show);
	}
}

WebHtmlEditorWindow.prototype.Init =  function(contentElem, show, containerElem, modal, zIndex)
{
	this.Initialize(contentElem, show);	
	this.IsModal = modal;
	this.Container = containerElem;	
	this.SetZIndex(zIndex);
}

WebHtmlEditorWindow.prototype.Initialize =  function(contentElem, show)
{
	if (this.Id) 
	{
		var editorId = "WebHtmlEditor";
		this.ContentWrapperTable = getElementById(editorId + "WindowContentWrapper" + this.Id);	 
		this.ContentWindow = getElementById(editorId + "WindowContentWindow" + this.Id);	 
		this.ContentTD = getElementById(editorId + "WindowContentTD" + this.Id);
		this.Caption = getElementById(editorId + "WindowCaption" + this.Id);
		this.borderWidth = this.GetTableBorder(this.ContentWrapperTable);
		if (contentElem) 
		{
			this.SetContentElement(contentElem);	 	 	 	 	 
		}
		
		if (null == show)
		{
			var show = true;
		}
		
		this.ShowWindow(show);
	}
	else 
	{
		alert ("No window Id provided");
	}
};

WebHtmlEditorWindow.prototype.SetContentElement = function (contentElem)
{
	var parentNode = contentElem.parentNode;
	if (parentNode) 
	{
		parentNode.removeChild(contentElem);		
	}
	while (this.ContentWindow && this.ContentWindow.childNodes.length > 0)
	{
		this.ContentWindow.removeChild(this.ContentWindow.childNodes.item(0));		
	}
	
	if (this.ContentWindow)
	{
		this.ContentWindow.appendChild(contentElem);
		this.SetContentWindowSize(contentElem.style.width, null);	
	}
}

WebHtmlEditorWindow.prototype.SetContentWindowSize = function (width, height)
{
	this.Width = width;	
	this.Height = height;
	
	if (!document.all)
	{	    
		if (this.ContentWrapperTable)
		{
			this.ContentWrapperTable.getElementsByTagName("TABLE").item(0).style.width = width + "px";	  
		}
	}	
};

WebHtmlEditorWindow.prototype.SetContentVisible = function (visible)
{
	if (this.ContentWindow)
	{
	    this.ContentWindow.style.visibility = visible ? "visible" : "hidden";
	}
};

WebHtmlEditorWindow.prototype.Close =  function(exitCode)
{	
	if (null != this.OnClientClosing && (this.OnClientClosing(exitCode) == false))
	{
		return;
	}

	this.ShowWindow(false);	
		
	this.ExitCode = exitCode;
	
	if (this.AdjustPosInterval > -1)
	{
		window.clearInterval(this.AdjustPosInterval);
		this.AdjustPosInterval = -1;
	}	
	if (this.IsModal)this.ReleaseCapture();			
	try
	{
		if (this.CallbackFunc)this.CallbackFunc(this.ReturnValue, this.Param);
	}
	catch (ex){}
	
	if (this.Parent)this.Parent.DestroyWindow(this);
		
	if (!this.UseWebHtmlEditorWindow && this.Window)this.Window.close();
};
			
WebHtmlEditorWindow.prototype.ToggleCanMove =  function(oDiv)
{
	if (!this.UseWebHtmlEditorWindow)return;
	
	this.CanMove = !this.CanMove;
	
	oDiv.className = this.CanMove ? "WHEWindowButtonPinOff" : "WHEWindowButtonPinOn";
	
	if (!this.CanMove)
	{
		if (this.IsIE)
		{
			this.TopOffset = parseInt(this.ContentWrapperTable.style.top) - GetScrollTop(document);
			this.StartUpdatePosTimer(100);
		}
		else
		{		
			this.ContentWrapperTable.style.position = "fixed";			
		}
	}
	else
	{
		if (this.IsIE)
		{
			window.clearInterval(this.AdjustPosInterval);		
			this.TopOffset = null;
		}
		else
		{
			this.ContentWrapperTable.style.position = "absolute";
		}
	}
}

WebHtmlEditorWindow.prototype.StartUpdatePosTimer = function(iInterval)
{
	if (!this.UseWebHtmlEditorWindow)return;		
	this.AdjustPosInterval = window.setInterval("UpdateWindowPos('" + this.Id + "')", iInterval);
}

function UpdateWindowPos(wndId)
{
	var wnd = GetWebHtmlEditorWindowManager().LookupWindow(wndId);
	if (wnd)wnd.SetPosition();
}

WebHtmlEditorWindow.prototype.CanDrag = function()
{
	if (!this.UseWebHtmlEditorWindow)return true;		
	return ("move" == this.DragMode && this.CanMove)|| ("size" == this.DragMode && this.CanResize);
}

WebHtmlEditorWindow.prototype.OnMouseOver = function(e)
{	
	switch (e.srcElement.id)
	{
		case "BorderBottom":
			break;			
		case "CornerBottomRight":
			e.srcElement.style.cursor = "se-resize";			
			break;			
		default:
			return;
	}
}

WebHtmlEditorWindow.prototype.OnMouseOut = function(e)
{	
	switch (e.srcElement.id)
	{
		case "BorderBottom":
			break;			
		case "CornerBottomRight":
			e.srcElement.className="WHETableWrapperFooterRight";
			break;			
		default:
			return;
	}
}

WebHtmlEditorWindow.prototype.OnDragStart = function(e)
{   
	this.SetActive(true);	
	if (!this.CanDrag())return;	
	this.X = (e.offsetX == null) ? e.layerX : e.offsetX;
	this.Y = (e.offsetY == null) ? e.layerY : e.offsetY;	
	MousePosX = e.clientX;
	MousePosY = e.clientY;	
	this.SetContentVisible(this.ShowContentWhenMoving);
	WebHtmlEditorWindowDragStart();
};

WebHtmlEditorWindow.prototype.SetActive = function(activate)
{		
	if (!this.UseWebHtmlEditorWindow)return;
			
	if (activate)
	{
		if (null != State_ActiveWindow && State_ActiveWindow != this)State_ActiveWindow.SetActive(false);	
		State_ActiveWindow = this;		
		if (this.Parent)this.Parent.ActivateWindow(this);
	}
	else
	{
		if (this == State_ActiveWindow)	State_ActiveWindow = null;
	}
}

WebHtmlEditorWindow.prototype.HitTest = function(x, y)
{
	var left = parseInt(this.ContentWrapperTable.style.left);
	if (isNaN(left))return false;
		
	var top = parseInt(this.ContentWrapperTable.style.top);
	if (isNaN(top))	return false;		
	return	left <= x && x <= (left + this.Width) && top <= y && y <= (top + this.Height);
}

WebHtmlEditorWindow.prototype.SetPosition = function(left, top)
{
	if (!this.UseWebHtmlEditorWindow)
	{
		if (this.Window)
		{
			this.Window.dialogLeft = left;
			this.Window.dialogTop = top;
		}
	}
	else
	{
		if (!left)left = this.ContentWrapperTable.style.left;			
		if (!top)
		{
			if (this.TopOffset != null)	top = parseInt(this.TopOffset) + GetScrollTop(document);
			else top = this.ContentWrapperTable.style.top;
		}			
		left = parseInt(left);
		top = parseInt(top);		
		if (!isNaN(left) && !isNaN(top))
		{		
			if (left <= 0)left = 0;			
			if (top <= 0)top = 0;			
			this.ContentWrapperTable.style.left = left + 'px';
			this.ContentWrapperTable.style.top = top + 'px';
		}
	}
}

WebHtmlEditorWindow.prototype.GetTableBorder = function(table)
{
	var borderWidth = 0;	
	if (null != table)
	{
		var strBorderWidth = table.style.borderWidth;
		borderWidth = parseInt(strBorderWidth);
		if (isNaN(borderWidth))
		{		
			borderWidth = 0;		
			if ("" != table.className)
			{
				var rule = this.GetCssClass(table.className);
				if (null != rule)
				{
					strBorderWidth = rule.style.borderWidth;
					borderWidth = parseInt(strBorderWidth);
					if (isNaN(borderWidth))
					{		
						borderWidth = 0;
					}
				}
			}
		}
	}
	return borderWidth;
}

WebHtmlEditorWindow.prototype.GetCssClass = function(className)
{
	try
	{
		for (var i = 0; i < document.styleSheets.length; i++)
		{
			try
			{
				var cssHref = document.styleSheets[i].href;		
				var arrRules = null;				
				if (this.IsIE)arrRules = document.styleSheets[i].rules;
				else arrRules = document.styleSheets[i].cssRules;					
				for (var j = 0; j < arrRules.length; j++)
				{
					try
					{	
						var rule = arrRules[j];
						if (rule.selectorText == className || rule.selectorText == "." + className)
						{
							return rule;
						}
					}
					catch (ex)
					{
					}
				}
			}
			catch (ex)
			{
			}
		}
	}
	catch(ex)
	{
	}	
	return null;
}

WebHtmlEditorWindow.prototype.GetWidth = function()
{	
	var width = 0;
	if (!this.UseWebHtmlEditorWindow)
	{
		if (this.Window)width = this.Window.dialogWidth;
	}
	else
	{
		if (this.IsIE)
		{	
			width = parseInt(this.ContentWrapperTable.clientWidth);									
		}
		else
		{
			width = parseInt(this.ContentWrapperTable.scrollWidth);			
		}
		
		if (isNaN(width))
		{
			width = 0;
		}
		
		if (width > 0)
		{
			if (this.IsIE)width += 2 * this.borderWidth;
			else width -= 2 * this.borderWidth;
		}
	}
	return width;
}

WebHtmlEditorWindow.prototype.SetWidth = function(width)
{
	width = parseInt(width);
	if (isNaN(width))return;		
	
	if (!this.UseWebHtmlEditorWindow)
	{		
		if (this.Window)
		{		
			if (this.Window.dialogWidth)
			{				
				this.Window.dialogTop = this.Window.screenTop - 30;
				this.Window.dialogLeft = this.Window.screenLeft - 4;								
				this.Window.dialogWidth = width + "px";
			}
			else
			{
				this.Window.outerWidth = width;
			}
		}
	}
	else
	{
		if (this.IsIE)
		{
			this.ContentWrapperTable.style.width = width + "px";
		}
		else
		{			
			this.ContentWrapperTable.style.width = (width + 2 * this.borderWidth) + "px";
			this.ContentTD.style.width = (width - 2 * this.borderWidth) + "px";			
			this.ContentWindow.style.width = parseInt(this.ContentTD.style.width) + "px";;
			this.ContentWindow.style.overflow = "hidden";
		}
	}
}

WebHtmlEditorWindow.prototype.GetHeight = function()
{
	var height = 0;
	if (!this.UseWebHtmlEditorWindow)
	{
		if (this.Window)height = this.Window.dialogHeight;
	}
	else
	{
		if (this.IsIE)
		{
			height = parseInt(this.ContentWrapperTable.clientHeight);
			if (isNaN(height))height = 0;		
			if (height > 0)height += 2 * this.borderWidth;
		}
		else
		{
			height = this.ContentWrapperTable.scrollHeight;	
		}		
	}		
	return height;
}

WebHtmlEditorWindow.prototype.SetHeight = function(height)
{
	height = parseInt(height);
	if (isNaN(height))return;
	
	if (!this.UseWebHtmlEditorWindow)
	{
		if (this.Window)
		{
			if (this.Window.dialogWidth)
			{
				this.Window.dialogTop = this.Window.screenTop - 30;
				this.Window.dialogLeft = this.Window.screenLeft - 4;				
				this.Window.dialogHeight = height + "px";
			}
			else
			{
				this.Window.outerHeight = height;
			}
		}
	}
	else
	{
		if (this.IsIE)
		{		
			this.ContentWrapperTable.style["height"] = height + "px";
			var iframes = this.ContentWrapperTable.getElementsByTagName("IFRAME");			
			if (iframes && iframes.length > 0)
			{
				var dlgHeight = (height - 50);
				if (dlgHeight > 0)iframes.item(0).style["height"] = dlgHeight + "px";
			}
		}
		else
		{
			var totalHeight = this.borderWidth * 2;			
			for (var i = 0; i < this.ContentWrapperTable.rows.length; i++)
			{
				if (this.ContentTD.parentNode.rowIndex == i)continue;				
				var rowHeight = parseInt(this.ContentWrapperTable.rows[i].scrollHeight);			
				if (!isNaN(rowHeight))totalHeight += parseInt(rowHeight);

			}
						
			this.ContentTD["height"] = (height - totalHeight) + "px";			
			this.ContentWindow.style["height"] = (height - totalHeight) + "px";
			this.ContentWindow.style.overflow = "hidden";			
			this.ContentWrapperTable.style["height"] = height + "px";			
		}
	}
}

WebHtmlEditorWindow.prototype.SetSize = function(width, height)
{
	this.SetWidth(width);
	this.SetHeight(height);
	
	if (width > 0)this.Width = width;
		
	if (height > 0)this.Height = height;
}

WebHtmlEditorWindow.prototype.SetCaption = function(caption)
{
	if (this.Caption)this.Caption.innerHTML = caption;
}

WebHtmlEditorWindow.prototype.GetCaption = function()
{
	if (this.Caption)return this.Caption.innerHTML;	
	else return "";
}

var State_ActiveWindow = null; 
var State_ActiveWindowSpan = null;
var MousePosX = 0;
var MousePosY = 0;

function WebHtmlEditorWindowDragStart()
{   
	if (!State_ActiveWindow.CanDrag())
	{
	    return;
	}
	try
	{
	document.body.setCapture();
	}catch(e){}
	IanFEvent.observe(document.body, "mousemove", WebHtmlEditorWindowDrag);
	IanFEvent.observe(document.body, "mouseup", WebHtmlEditorWindowDragEnd);
		
//	if (document.all && document.body.attachEvent) 
//	{	
//		document.body.setCapture();
//		document.body.attachEvent ("onmousemove", WebHtmlEditorWindowDrag);
//		document.body.attachEvent ("onmouseup", WebHtmlEditorWindowDragEnd);
//	} 
//	else if (document.addEventListener)
//	{
//		document.addEventListener("mousemove", WebHtmlEditorWindowDrag, false);
//		document.addEventListener("mouseup", WebHtmlEditorWindowDragEnd, false);			
//	}	
	WebHtmlEditorWindowInitializeDrag(State_ActiveWindow);
}


function WebHtmlEditorWindowDragEnd()
{	
	if (document.all && document.body.detachEvent)
	{
		document.body.detachEvent ("onmousemove", WebHtmlEditorWindowDrag);
		document.body.detachEvent ("onmouseup", WebHtmlEditorWindowDragEnd);		
		document.body.releaseCapture();
	}
	else if (document.removeEventListener)
	{  
		document.removeEventListener("mousemove", WebHtmlEditorWindowDrag, false);
		document.removeEventListener("mouseup", WebHtmlEditorWindowDragEnd, false);		
	}
	if (State_ActiveWindow.IsModal)State_ActiveWindow.SetCapture();
	
	WebHtmlEditorWindowUnInitializeDrag(State_ActiveWindow);	
	State_ActiveWindow.SetContentVisible(true);
}

function WebHtmlEditorWindowDrag(e)
{	
	if (State_ActiveWindow.CanDrag())
	{
		switch (State_ActiveWindow.DragMode)
		{
			case "move":
				WebHtmlEditorWindowMove(e);							
				break;		
			case "size":
				WebHtmlEditorWindowSize(e);
				break;		
		}
	}
	e.cancelBubble = true;	
	MousePosX = e.clientX;
	MousePosY = e.clientY;	
	return false;
}

function WebHtmlEditorWindowInitializeDrag(targetWindow)
{
	if (!targetWindow)return;

	if (!State_ActiveWindowSpan)
	{
		State_ActiveWindowSpan = document.createElement("SPAN");
		State_ActiveWindowSpan.className = "WHETableWrapperResizeSpan";				
		State_ActiveWindowSpan.style.position = "absolute";	
		State_ActiveWindowSpan.style.zIndex = 50000;	
		State_ActiveWindowSpan.innerHTML = "&nbsp;&nbsp;";	
		document.body.appendChild(State_ActiveWindowSpan);
	}
	
	State_ActiveWindowSpan.style.visibility = "visible";	
	State_ActiveWindowSpan.style.top = parseInt(targetWindow.ContentWrapperTable.style.top) + "px";
	State_ActiveWindowSpan.style.left = parseInt(targetWindow.ContentWrapperTable.style.left) + "px";
	State_ActiveWindowSpan.style.width = targetWindow.GetWidth();
	State_ActiveWindowSpan.style["height"] = targetWindow.GetHeight();
	
	switch (targetWindow.DragMode)
	{
		case "move":
			State_ActiveWindowSpan.style.cursor = "default";
			break;	
		case "size":
			State_ActiveWindowSpan.style.cursor = "se-resize";
			break;		
	}
}

function WebHtmlEditorWindowUnInitializeDrag(targetWindow)
{
	if (State_ActiveWindowSpan)	State_ActiveWindowSpan.style.visibility = "hidden";
	targetWindow.SetPosition(State_ActiveWindowSpan.style.left, State_ActiveWindowSpan.style.top);
	targetWindow.SetSize(State_ActiveWindowSpan.style.width, State_ActiveWindowSpan.style["height"]);
}

function WebHtmlEditorWindowMove(e)
{	
	var WebHtmlEditorWindowX = State_ActiveWindow.X;
	var WebHtmlEditorWindowY = State_ActiveWindow.Y;
	var el = State_ActiveWindowSpan;
	var left = 0;
	var top = 0;
	if (document.all)
	{   
		left = e.clientX * 1 + GetScrollLeft(document) - WebHtmlEditorWindowX;
		top = e.clientY * 1 + GetScrollTop(document) - WebHtmlEditorWindowY;
	}
	else
	{       
		left = e.pageX * 1 - WebHtmlEditorWindowX;
		top = e.pageY * 1 - WebHtmlEditorWindowY;
	}
	
	if (left < 0)left = 0;		
	if (top < 0)top = 0;
	
	el.style.left = left + "px";
	el.style.top = top + "px";	
}

var minWidth = 0xc8;
var minHeight = 0xc8;

function WebHtmlEditorWindowSize(e)
{	
	var offsetX = e.clientX - MousePosX;
	var offsetY = e.clientY - MousePosY;	
	var width = parseInt(State_ActiveWindowSpan.style.width);
	var height = parseInt(State_ActiveWindowSpan.style["height"]);	
	width += offsetX;
	height += offsetY;	
	if (width < minWidth){width = minWidth;}
	if (height < minHeight){height = minHeight;}
	State_ActiveWindowSpan.style.width = width + "px";
	State_ActiveWindowSpan.style["height"] = height + "px";
}	



function GetWebHtmlEditorWindowManager()
{		
	var topWindow = GetParentWindow();	 
	if (!topWindow.radWindowManager)topWindow.radWindowManager = new WebHtmlEditorWindowManager();
	return topWindow.radWindowManager;
}

function GetParentWindow()
{
	var topWindow = null;

	var argumentsContainParentWindow = false;
	try
	{
		if (window.dialogArguments.parentWindow && argumentsContainParentWindow)argumentsContainParentWindow = true;
	}
	catch(ex)
	{
		argumentsContainParentWindow = false;
	}
	if (window.dialogArguments != null && argumentsContainParentWindow) 
	{
		topWindow = window.dialogArguments.parentWindow;	
	}
	else if (window.opener && !document.all && window.isWebHtmlEditorWindow)
	{	// NS
		topWindow = opener;
	}	
	else
	{
		topWindow = window;	
	}
	var stopLoop = false;
	while (topWindow.parent && !stopLoop)
	{
		try
		{	
			topWindowTagName = topWindow.parent.tagName.toUpperCase()
		}
		catch (exception)
		{
			topWindowTagName = "";
		}			
		if (topWindow.parent == topWindow)break;
		try
		{
			if (topWindow.parent.document.domain != window.document.domain)break;
		}
		catch(exc)
		{
			stopLoop = true;
			continue;
		}

		try
		{
			if (topWindow.frameElement != null && (topWindow.frameElement.tagName != "IFRAME" || topWindow.frameElement.name != "wheWindowContent"))break;
		}
		catch(exc)
		{
			alert('in the Exception!');
			stopLoop = true;
		}
		topWindow = topWindow.parent;		
	}		
	return topWindow;
}
function Document_OnFocus(e)
{
	if (!e){e = window.event;}	
	GetWebHtmlEditorWindowManager().ActivateWindow();
}

function Document_OnKeyDown(e)
{
	if (!e){e = window.event;}	
	return GetWebHtmlEditorWindowManager().OnKeyDown(e);
}
function WebHtmlEditorWindowInfo_Model()
{
	this.IsIE = (null != document.all);
	this.ID = null;
	this.Url = "";
	this.InnerHtml = "";
	this.InnerObject = null;
	this.Width = 300;
	this.Height = 200;
	this.Caption = "";
	this.IsVisible = true;
	this.Argument = null;
	this.CallbackFunc = null;
	this.OnLoadFunc = null;
	this.Param = null; 
	this.Resizable = true;
	this.Movable = true;
	this.CloseHide = false; 
	this.UseClassicDialogs = true;
	this.BlankIFrameLocation = "";
}

 
function WebHtmlEditorWindowManager()
{
	this.ChildWindows = new Array();
	this.ActiveWindow = null;
	this.TopWindowZIndex = 10001;
	
	this.ContainerPool = new Array();
	this.IsIE = (null != document.all) && (window.opera == null);
	
	document.body.onfocus = Document_OnFocus;
	IanFEvent.observe(document.body, "keydown", Document_OnKeyDown);
//	if (this.IsIE && document.body.attachEvent)
//	{
//		document.body.attachEvent("onkeydown", Document_OnKeyDown);
//	}
//	else if (document.body.addEventListener)
//	{
//		document.body.addEventListener("keydown", Document_OnKeyDown, false);
//	}
}

WebHtmlEditorWindowManager.prototype.ShowModalWindow = function(windowInfo)
{
	var wnd = this.CreateWindow(true, windowInfo);
	return wnd;
}

WebHtmlEditorWindowManager.prototype.ShowModallessWindow = function(windowInfo)
{
	var wnd = this.CreateWindow(false, windowInfo);
	return wnd;
}

/**************************************/
// WINDOWS OPERATIONS
WebHtmlEditorWindowManager.prototype.CreateWindow = function(bIsModal, windowInfo)	
{
	if (!windowInfo)
		return null;
		
	/********* THIS CODE MAKES MOZILLA USE REGULAR WINDOWS INSTEAD OF WHE WINDOW *********/
	windowInfo.UseClassicDialogs = (this.IsIE && windowInfo.UseClassicDialogs)|| (!this.IsIE && ((null != windowInfo.Url && "" != windowInfo.Url)	|| (null != windowInfo.InnerHtml && "" != windowInfo.InnerHtml && windowInfo.UseClassicDialogs)));
	
	var id = "";		
	if (!windowInfo.ID || windowInfo.ID == "")
	{
		id = this.ChildWindows.length;
	}
	else
	{
		id = windowInfo.ID;
	}
	var caption = "";
	if (null == windowInfo.Caption)
	{
		caption = "[" + id + "]";
	}
	else
	{
		caption = windowInfo.Caption;
	}
	var tempWebHtmlEditorWindow = this.GetWindow(id);	
	tempWebHtmlEditorWindow.Argument = windowInfo.Argument;
	tempWebHtmlEditorWindow.Width = windowInfo.Width;
	tempWebHtmlEditorWindow.Height = windowInfo.Height;
	tempWebHtmlEditorWindow.CallbackFunc = windowInfo.CallbackFunc;
	tempWebHtmlEditorWindow.Param = windowInfo.Param;
	tempWebHtmlEditorWindow.CanResize = windowInfo.Resizable;
	tempWebHtmlEditorWindow.CanMove = windowInfo.Movable;
	tempWebHtmlEditorWindow.OnLoadFunc = windowInfo.OnLoadFunc;
	tempWebHtmlEditorWindow.UseWebHtmlEditorWindow = !windowInfo.UseClassicDialogs;
	if (windowInfo.UseClassicDialogs && this.IsIE)
	{
		var features = "status:no;"
						+ "resizable:yes;"
						+ "center:yes;"
						+ "help:no;"
						+ "minimize:no;"
						+ "maximize:no;"
						+ "scroll:no;"
						+ "border:thin;"
						+ "statusbar:no;"
						+ "dialogWidth:" + windowInfo.Width + "px;"
						+ "dialogHeight:" + windowInfo.Height + "px";
		
		if (windowInfo.InnerHtml && windowInfo.InnerHtml != "")
		{
			tempWebHtmlEditorWindow.InnerHTML = windowInfo.InnerHtml;
		}
	
		if (!windowInfo.Url || "" == windowInfo.Url)
			windowInfo.Url = "javascript:''";		
		
		var dialogArguments = new Object();
		dialogArguments.parentWindow = window;
		dialogArguments.wheWindow = tempWebHtmlEditorWindow;
		
		window.showModalDialog(windowInfo.Url, dialogArguments, features);
	}
	else if (windowInfo.UseClassicDialogs && !this.IsIE)
	{
		if (!windowInfo.Url || windowInfo.Url=="")windowInfo.Url = "javascript:''";		
		window.childWebHtmlEditorWindow = tempWebHtmlEditorWindow;
		tempWebHtmlEditorWindow.Window = window.open(windowInfo.Url
							, "_blank"			
							, "status=no,toolbar=no,location=no,resizable=yes,menubar=no,width=" + windowInfo.Width + ",height=" + windowInfo.Height + ",modal=yes");
	}
	else if (!windowInfo.UseClassicDialogs)
	{
		var container = null;
		if (this.ContainerPool.length > 0)
		{
			container = this.ContainerPool.pop();
		}
		else
		{
			container = document.createElement("SPAN");
			document.body.appendChild(container);
		}
		container.innerHTML = this.BuildWrapperTableHtml(id, windowInfo.Width, windowInfo.Height, caption, bIsModal, windowInfo.CloseHide, windowInfo.BlankIFrameLocation);
		
		var contentElem = (null != windowInfo.InnerObject) ? windowInfo.InnerObject:getElementById("WebHtmlEditorWindowContentFrame" + id);
		tempWebHtmlEditorWindow.Init(contentElem, true, container, bIsModal, ++this.TopWindowZIndex);
							
		var frm = getElementById("WebHtmlEditorWindowContentFrame" + id);	
	
		/*** NS toolbar when toolOnPage = false and content element is DIV ***/
		tempWebHtmlEditorWindow.Window = null != frm ? frm.contentWindow : null;
	
		if (windowInfo.InnerHtml && windowInfo.InnerHtml != "")
		{	
			var doc = frm.contentWindow.document;
			
			doc.open();
			doc.write(windowInfo.InnerHtml);
			doc.close();
		}
		else if (windowInfo.Url && windowInfo.Url != "")		
		{	
			frm.src = windowInfo.Url;
		}
		if (bIsModal)
		{
			this.SetCapture(tempWebHtmlEditorWindow, false);
		}

		var theVisibleWidth = Math.min(window.screen.width, document.body.clientWidth);
		var theVisibleHeight = (document.documentElement && document.documentElement.clientHeight)?document.documentElement.clientHeight:Math.min(window.screen["height"], document.body.clientHeight);
		
		var x = GetScrollLeft(document) + (theVisibleWidth - parseInt(windowInfo.Width)) / 2;
		var y = GetScrollTop(document) + (theVisibleHeight - parseInt(windowInfo.Height)) / 2;

		if (null == windowInfo.IsVisible)
		{
			windowInfo.IsVisible = false;
		}
		
		tempWebHtmlEditorWindow.SetSize(windowInfo.Width, windowInfo.Height);
		tempWebHtmlEditorWindow.ShowWindow(windowInfo.IsVisible, x, y);
	}
	
	return tempWebHtmlEditorWindow;
}

WebHtmlEditorWindowManager.prototype.DestroyWindow = function(childWindow)
{				
	var nextWndToActivate = this.GetPrevWindow(childWindow.Id);
	
	this.UnregisterChild(childWindow);	
	if (nextWndToActivate != childWindow)	
	{
		this.ActivateWindow(nextWndToActivate);
	}
	eval(this.GetWindowVarName(childWindow.Id) + " = null;");
	
	if (childWindow.Container)
	{
		this.ContainerPool.push(childWindow.Container);
	}
}

WebHtmlEditorWindowManager.prototype.GetPrevWindow = function(id)
{
	var bNeedPrev = false;
	var retWnd = null;
	for (var i = this.ChildWindows.length - 1; i >= 0; i--)
	{
		var wnd = this.ChildWindows[i];
		if (wnd && wnd.Id == id)
		{
			bNeedPrev = true;
		}
		else if (wnd && bNeedPrev)
		{
			return wnd;
		}
		else if (null == retWnd)
		{
			retWnd = wnd;
		}
	}
	return retWnd;
}

WebHtmlEditorWindowManager.prototype.CloseWindow = function(id)
{
	var wnd = this.LookupWindow(id);
	if (wnd)
	{
		wnd.Close();
	}
}

WebHtmlEditorWindowManager.prototype.ActivateWindow = function(doc)
{	
	if (!doc) 
	{
		doc = this.ActiveWindow;
	}
	if (doc)
	{
		if (doc.IsModal)
		{
			this.SetCapture(doc, false);
		}
		
		if (doc != this.ActiveWindow)
		{
			if (this.ActiveWindow)
			{
				this.ActiveWindow.SetZIndex(this.TopWindowZIndex - 1);
			}	
			doc.SetZIndex(this.TopWindowZIndex);
		
			this.ActiveWindow = doc;	
		}
	}
}

WebHtmlEditorWindowManager.prototype.RegisterChild = function(childWindow)
{
	this.ChildWindows[this.ChildWindows.length] = childWindow;
}

WebHtmlEditorWindowManager.prototype.UnregisterChild = function(childWindow)
{
	for (var i = 0; i < this.ChildWindows.length; i++)
	{
		var wnd = this.ChildWindows[i];
		if (wnd == childWindow)
		{
			this.ChildWindows[i] = null;
			return;
		}
	}
}

WebHtmlEditorWindowManager.prototype.SetCapture = function(childWindow, bContainerCapture)
{
	try
	{
		if (childWindow)
		{
			childWindow.SetCapture(bContainerCapture);
		}
	}
	catch (ex)
	{
	}
}

WebHtmlEditorWindowManager.prototype.LookupWindow = function(id)
{
	for (var i = 0; i < this.ChildWindows.length; i++)
	{	
		var wnd = this.ChildWindows[i];		
		if (wnd && id == wnd.Id)
		{
			return wnd;
		}
	}
	return null; //this.ChildWindows[id];
}

WebHtmlEditorWindowManager.prototype.LookupWindowByBrowserWindowRef = function(browserWindow)
{
	for (var i = 0; i < this.ChildWindows.length; i++)
	{
		var wheWindow = this.ChildWindows[i];		
		if (null != wheWindow && browserWindow == wheWindow.Window)
		{
			return wheWindow;
		}
	}
	return null;
}

WebHtmlEditorWindowManager.prototype.GetCurrentWindow = function(browserWindow)
{													 
	if (browserWindow.dialogArguments)
	{
		return browserWindow.dialogArguments.wheWindow;
	}
	else if (browserWindow.opener != null && browserWindow.opener.childWebHtmlEditorWindow != null)
	{
		return browserWindow.opener.childWebHtmlEditorWindow;
	}
	else
	{
		return this.LookupWindowByBrowserWindowRef(browserWindow);
	}
}

// If already exists a window with same id - returns it; 
// Otherwise creates a new window and returns it
WebHtmlEditorWindowManager.prototype.GetWindow = function(id)
{
	var wnd = this.LookupWindow(id);
	if (!wnd)
	{
		var varName = this.GetWindowVarName(id);
		eval(varName + " = new WebHtmlEditorWindow('" + id + "');");		
		wnd = eval(varName);
		wnd.Parent = this;
		this.RegisterChild(wnd);
	}	
	return wnd;
}

WebHtmlEditorWindowManager.prototype.GetWindowVarName = function(id)
{
	return "window.wheWindow_" + id;
}

WebHtmlEditorWindowManager.prototype.GetWindowFromPos = function(x, y)
{
	var wnd = null;
	for (var i = 0; i < this.ChildWindows; i++)
	{
		var childWnd = this.ChildWindows[i];
		if (childWnd && childWnd.HitText(x, y))
		{
			if (!wnd || wnd.ZIndex < childWnd.ZIndex)
			{
				wnd = childWnd;
			}
		}
	}	
	return wnd;
}

WebHtmlEditorWindowManager.prototype.OnShowWindow = function(childWindow, visible)
{
	if (visible)
	{
		this.ActiveWindow = childWindow;
	}
	else
	{
		if (this.ActiveWindow == childWindow)
		{
			this.ActiveWindow = null;
		}
	}
}

WebHtmlEditorWindowManager.prototype.OnKeyDown = function(evt)
{
    switch (evt.keyCode)
	{	
		case 27:
			if (this.ActiveWindow)
			{
				this.ActiveWindow.Close();
			}	
			break;		
		default:
			return;
	}
	evt.cancelBubble = true;
	evt.returnValue = false;	
}

WebHtmlEditorWindowManager.prototype.BuildWrapperTableHtml = function(id, width, height, caption, bIsModal, bHide, blankIFrameLocation)	
{
	var url = ((null != blankIFrameLocation && "" != blankIFrameLocation) ? blankIFrameLocation : "javascript:'';");
	
	var closeFunc = bHide ? "ShowWindow(false)" : "Close()";
	
	var html = "";		
	html +=	"		<table border='0' id='WebHtmlEditorWindowContentWrapper" + id + "' class='WHETableWrapper' style='display:block; z-index:1000; position:absolute;' cellspacing='0' cellpadding='0' width='" + width + "px' height='" + height + "px'>\n"
			+ "			<tr>\n"
			+ "				<td width='1' class='WHETableWrapperHeaderLeft' nowrap></td>\n"
			+ "				<td width='100%' class='WHETableWrapperHeaderCenter' nowrap='true' ondblclick='wheWindow_" + id + ".ToggleContent();' onmousedown='wheWindow_" + id + ".DragMode=\"move\"; return wheWindow_" + id + ".OnDragStart(event);' onselectstart='return false;'>\n"
			+ "					<span id='WebHtmlEditorWindowCaption" + id + "' onselectstart='return false;' class='WHEWindowHeader'>" + caption + " - WebHtmlEditor</span>\n"
			+ "				</td>\n";
			
	
	if (!bIsModal)		
	{
		html	+= "		<td width='1' class='WHETableWrapperHeaderCenter' nowrap>\n"
				+ "					<span class='WHEWindowButtonPinOff' id='ButtonPin' onclick='wheWindow_" + id + ".ToggleCanMove(this)'>&nbsp;</span>"
				+ "			</td>\n";						
	}
	
	html += "				<td width='1' class='WHETableWrapperHeaderCenter' nowrap>\n"
			+ "			<span class='WHEWindowButtonClose' id='ButtonClose' onclick='wheWindow_" + id + "." + closeFunc + "'>&nbsp;</span>\n"
			+ "				</td>\n"
			+ "				<td width='1' class='WHETableWrapperHeaderRight' nowrap></td>\n"
			+ "			</tr>\n"
			+ "			<tr>\n"
			+ "				<td id='WebHtmlEditorWindowContentTD" + id + "' colspan='5'>\n"
			+ "					<table border='0' width='100%' height='100%' cellspacing='0' cellpadding='0'>\n"
			+ "						<tr>\n"
			+ "							<td width='1' class='WHETableWrapperBodyLeft' nowrap></td>\n"
			+ "							<td width='100%' class='WHETableWrapperBodyCenter' align='left' valign='top' onselectstart='return false;'>\n"
			+ "								<span id='WebHtmlEditorWindowContentWindow" + id + "' align='center' style='width:100%;height:100%;' >\n"
			+ "									<iframe name='wheWindowContent' frameborder='0px' style='border:0px solid green' id='WebHtmlEditorWindowContentFrame" + id + "' src='" + url + "' scrolling='no' border='no' width='100%' height='100%' ></iframe>\n"
			+ "								</span>\n"
			+ "							</td>\n"
			+ "							<td width='1' class='WHETableWrapperBodyRight' nowrap></td>\n"
			+ "						</tr>\n"
			+ "					</table>\n"
			+ "				</td>\n"
			+ "			</tr>\n"
			+ "			<tr>\n"
			+ "				<td colspan='5' width='100%' height='1' >\n"
			+ "					<table border='0' width='100%' height='1' cellspacing='0' cellpadding='0'>\n"			
			+ "						<tr>\n"
			+ "							<td width='1' class='WHETableWrapperFooterLeft' nowrap>&nbsp;</td>\n"
			+ "							<td colspan='3' id='BorderBottom' width='100%' class='WHETableWrapperFooterCenter' nowrap>&nbsp;</td>		\n"
			+ "							<td width='1' id='CornerBottomRight' class='WHETableWrapperFooterRight' onmousedown='wheWindow_" + id + ".DragMode=\"size\"; return wheWindow_" + id + ".OnDragStart(event);' onselectstart='return false;' nowrap>&nbsp;</td>\n"
			+ "						</tr>\n"
			+ "					</table>\n"
			+ "				</td>\n"
			+ "			</tr>\n"
			+ "		</table>\n";			
	return html;
}



function CloseDlg(returnValue)
{
	if (window.wheWindow)
	{
		window.wheWindow.ReturnValue = returnValue;
		window.wheWindow.Close();
	}
}
function GetScrollTop(oDocument)
{
	if (oDocument.documentElement && oDocument["documentElement"].scrollTop)
	{
		return oDocument["documentElement"].scrollTop;
	}
	else
	{
		return oDocument.body.scrollTop;
	}
}
function GetScrollLeft(oDocument)
{
	if (oDocument.documentElement && oDocument["documentElement"].scrollLeft)
	{
		return oDocument["documentElement"].scrollLeft;
	}
	else
	{
		return oDocument.body.scrollLeft;
	}
}