Window.prototype = new Layer();
Window.prototype.constructor = Window;
Window.superclass = Layer.prototype;

function Window(id, idContentMask, idContent, width, height, frame)
{
	this.SetId(id);
	this.SetWidth(width);
	this.SetHeight(height);
	
	this._frame = frame;
	this._doc = frame.document;
	this._wndWidth = width;
	this._wndHeight = height;
	this._wndTitleElem = null; //Elem that holds window text
	
	//Constants
	this.TITLEBAR_HEIGHT = 15;
	this.SCROLL_WIDTH = 15;

	//Create elements
	this._windowElem = this.prepareWindowElem(id);
	this._contentMaskElem = this.prepareContentMask(idContentMask);
	this._contentElem = this.prepareContent(idContent);
	this._titlebarElem = null;
	this._titlebarXElem = null;
	this._iFrameElem = null;
	this._iFrameOverLayer = null;	
	
	//IFrame extra
	//When moving a window with an iframe appended firefox 1.0 gecko:1.7.5 (dont know about mozilla or netscape)
	//creates artifacts around the window. The solution is to remove the iframe while moving.
	//This var keeps track on if it is appended.
	//Note: every time the iframe is appended to the tree the onload event for the iframe source is fired.
	this._bIFrameElemAppended = false;
	
	//Create scroller
	this._scroller = this.createScroller();
	
	//Vars used when moving window
	this._isMouseDown = false;	
	this._mouseDownPosX = null;
	this._mouseDownPosY = null;
	this._defaultZIndex = null;
	var _this = this;
	this._funcPtrOnMouseMoveTitlebar = function(e) { _this.OnMouseMoveTitlebar(e); };
	this._funcPtrOnMouseUpTitlebar = function(e) { _this.OnMouseUpTitlebar(e); };
	this._funcPtrOnSelectStart = function() { return false; };	
}

Window.prototype.ClassName = function()
{
	return "Window";
}

Window.prototype.GetElem = function()
{
	return this._windowElem;
}

Window.prototype.GetTitlebarX = function()
{
	return this._titlebarXElem;
}

Window.prototype.SetDefaultZIndex = function(zIndex)
{
	this._defaultZIndex = zIndex;
}

Window.prototype.prepareWindowElem = function(id)
{
	var windowElem = this._doc.getElementById(id);
	windowElem.style.position = "absolute";
	windowElem.style.backgroundColor = "transparent"; //Already default
	
	var tableElem = this._doc.createElement("table");
	tableElem.setAttribute("width", "100%");
	tableElem.setAttribute("cellpadding", "0");
	tableElem.setAttribute("cellspacing", "0");
	tableElem.setAttribute("border", "0");
	
	var tableBody = this._doc.createElement("tbody");
	
	var tr1 = this._doc.createElement("tr");
		var td11 = this._doc.createElement("td");
		td11.style.height = this.TITLEBAR_HEIGHT + "px";
		
	var tr2 = this._doc.createElement("tr");
		var td21 = this._doc.createElement("td");
		td21.style.height = this._wndHeight - this.TITLEBAR_HEIGHT + "px";
		td21.style.backgroundColor = "#999999";				

	tableElem.appendChild(tableBody);
	//tableElem.style.height = "102px";
	
	tableBody.appendChild(tr1);
	tr1.appendChild(td11);

	tableBody.appendChild(tr2);
	tr2.appendChild(td21);
		
	windowElem.appendChild(tableElem);
	
	//windowElem.style.MozUserSelect = "none";
	
	return windowElem;
}

Window.prototype.prepareContentMask = function(idContentMask)
{
	var contentMaskElem = this._doc.getElementById(idContentMask);
	if (contentMaskElem == null)
	{
		alert("Window::prepareContentMask: The specified contentMask could not be found");
		return null;
	}
		
	contentMaskElem.style.backgroundColor = "#FFFFFF";	
	contentMaskElem.style.position = "absolute";
	contentMaskElem.style.top = "20px"; 
	contentMaskElem.style.width = this._wndWidth - 25 + "px"; //"275px"; 
	contentMaskElem.style.height = this._wndHeight - 25 + "px"; //"275px"; 
	contentMaskElem.style.left = "5px"; 
	contentMaskElem.style.clip = "rect(0px " + (this._wndWidth - 25) + "px " + (this._wndHeight - 25) + "px 0px)"; 
	//contentMaskElem.style.clip = "rect(0px,275px,275px,0px)"; 
	contentMaskElem.style.overflow = "hidden";
	
	return contentMaskElem;
}

Window.prototype.prepareContent = function(idContent)
{
	var contentElem = this._doc.getElementById(idContent);
	if (contentElem == null)
	{
		alert("Window::prepareContent: The specified 'content' could not be found");
		return null;
	}
	contentElem.style.position = "absolute";
	contentElem.style.top = "0px"; 
	contentElem.style.left = "0px"; 
	contentElem.style.paddingLeft = "2pt";
	contentElem.style.paddingRight = "2pt";	
	contentElem.style.paddingTop = "2pt";	
	return contentElem;
}

Window.prototype.CreateTitlebar = function(wndTitle, bCloseButton)
{
	if (bCloseButton == null)
	{
		bCloseButton = true;		
	}
	
	this._titlebarElem = this.createTitlebar(wndTitle, bCloseButton);
	
	this._titlebarElem.style.MozUserSelect = "none";
	
	this._windowElem.appendChild(this._titlebarElem)
	
	if (bCloseButton)
	{	
		this._titlebarXElem = this._doc.getElementById(this.GetId() + "_titlebarX");
		this._titlebarXElem.style.cursor = "pointer";
	}
}

Window.prototype.createTitlebar = function(wndTitle, bCloseButton)
{
	var titlebarElem = this._doc.createElement("div");
	var	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_Titlebar";
	titlebarElem.setAttributeNode(attr);
	
	var tableElem = this._doc.createElement("table");
	tableElem.setAttribute("width", "100%");
	tableElem.setAttribute("cellpadding", "0");
	tableElem.setAttribute("cellspacing", "0");
	tableElem.setAttribute("border", "0");
	tableElem.style.height = this.TITLEBAR_HEIGHT + "px";
	
	var tableBody = this._doc.createElement("tbody");
	
	var tr1 = this._doc.createElement("tr");
	
		var td11 = this._doc.createElement("td");
		td11.style.width = "15px";
		td11.style.backgroundImage = "url('../images/windows/wnd1/topleft.gif')";
		tr1.appendChild(td11);

		var td12 = this._doc.createElement("td");
		td12.setAttribute("align", "left");
		td12.style.backgroundColor = "#CCCCCC";
		td12.innerHTML = "<i>" + wndTitle + "</i>";
		this._wndTitleElem = td12;
		tr1.appendChild(td12);
		
		if (bCloseButton)
		{
			var td13 = this._doc.createElement("td");
			td13.style.width = "1px"; 
			td13.setAttribute("id", this.GetId() + "_titlebarX");
			td13.setAttribute("align", "right");
			td13.style.backgroundColor = "#CCCCCC";
			td13.innerHTML = "X";
			tr1.appendChild(td13);
		}
		
		var td14 = this._doc.createElement("td");
		td14.style.width = "15px";
		td14.style.backgroundImage = "url('../images/windows/wnd1/topright.gif')";
		tr1.appendChild(td14);

	titlebarElem.appendChild(tableElem);
	tableElem.appendChild(tableBody);
	tableBody.appendChild(tr1);	
		

	titlebarElem.style.cursor = "move";
	titlebarElem.style.height = this.TITLEBAR_HEIGHT + "px";
	titlebarElem.style.position = "absolute";
	titlebarElem.style.top = "0px";
	titlebarElem.style.left = "0px";		
	
	//Define callback functions for moving the window
	var _this = this;
	titlebarElem.addEventListener("mousedown", function(e) { _this.OnMouseDownTitlebar(e, "haha"); }, false);
		
	return titlebarElem;
}

Window.prototype.OnMouseDownTitlebar = function(e, w)
{
	//window.status = "OnMouseDownTitlebar " + e.pageY + " " + e.button + " w: " + w;
	
	//Only permit moving with left mouse button
	if (e.button != 0) //Note: 1 in explorer
		return false;
		
	//this._defaultZIndex = this.GetZIndex();
	this.SetZIndex(1000);
	
	//Need to set these events to the body, since when dragging fast the mouse moves away from the titlebar
	//The events are detached in the onmouseup handler
	this._doc.body.addEventListener('mouseup', this._funcPtrOnMouseUpTitlebar, false ); 
	this._doc.body.addEventListener('mousemove', this._funcPtrOnMouseMoveTitlebar, false );
//	this._doc.body.addEventListener('select', this._funcPtrOnSelectStart );
	this._doc.body.style.cursor = "move";
	
//	this._contentElem.style.user-select = "none";
	//window.onselect = function() { alert("2435"); return false; };
	//this._contentElem.onselect = function() { alert("2435"); };
	//alert(window.onselect);
	
	//if (window.sidebar)
	//{
		//this._doc.onmousedown=disabletext
		//this._doc.onclick=reEnable
	//}
	

	
	this._isMouseDown = true;	

	this._mouseDownPosX = e.layerX;
	this._mouseDownPosY = e.layerY;
			
	return false;  //Important	
}

Window.prototype.OnMouseUpTitlebar = function(e)
{	
	//window.status = "OnMouseUpTitlebar";
	if (e.button != 0) //Note: 1 in explorer
		return false;
			
	this.SetZIndex(this._defaultZIndex);
	this._isMouseDown = false;	
	
	//var _this = this;
	this._doc.body.removeEventListener('mouseup', this._funcPtrOnMouseUpTitlebar, false ); 
	this._doc.body.removeEventListener('mousemove', this._funcPtrOnMouseMoveTitlebar, false );
	//this._doc.body.detachEvent('onselectstart', this._funcPtrOnSelectStart );
	this._doc.body.style.cursor = "default";
	
	if (this._iFrameElem != null)
	{
		this._iFrameOverLayer.SetVisible(false);
		if (!this._bIFrameElemAppended) //see constructor for comment
		{
			this._contentElem.appendChild(this._iFrameElem);
			this._bIFrameElemAppended = true;
		}
	}
}

Window.prototype.OnMouseMoveTitlebar = function(e)
{
	//window.status = e.pageX + "  " + e.layerX;
    if ( this._isMouseDown )
	{
		if (this._iFrameElem != null)
		{
			this._iFrameOverLayer.SetVisible(true);
			if (this._bIFrameElemAppended)	//see constructor for comment
			{				
				this._contentElem.removeChild(this._iFrameElem);
				this._bIFrameElemAppended = false;
			}			
		}
		
		var newY = e.pageY - this._mouseDownPosY;
		var newX = e.pageX - this._mouseDownPosX;
		
		if ( newY < 0 )
			newY = 0;
		if ( newX < 0 )
			newX = 0;
		if ( newY + this.GetHeight() > window.innerHeight )
			newY = window.innerHeight - this.GetHeight();
		if ( newX + this.GetWidth() > window.innerWidth )
			newX = window.innerWidth - this.GetWidth();
			
		
		this.SetTop(newY);
		this.SetLeft(newX);
	}

	return false;  //Important
}


Window.prototype.createScroller = function()
{
	//alert("createscroller");
	var attr = null;
	
	//Create up arrow div
	var divScrollUp = this._doc.createElement("div");
	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_ScrollUp";
	divScrollUp.setAttributeNode(attr);		
	divScrollUp.innerHTML = "<img src=\"../images/scroll/scroll_up.gif\" border=\"0\">";
		
	divScrollUp.style.position = "absolute";
	divScrollUp.style.top = "20px"; 
	divScrollUp.style.width = this.SCROLL_WIDTH + "px";  
	divScrollUp.style.height = "15px"; 
	divScrollUp.style.left = this._wndWidth - 20 + "px"; //"280px"; 
	divScrollUp.style.zIndex = 100;
	divScrollUp.style.visibility = "hidden";
	this._windowElem.appendChild(divScrollUp)
	
	//Create down arrow div
	var divScrollDown = this._doc.createElement("div");
	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_ScrollDown";
	divScrollDown.setAttributeNode(attr);	
	divScrollDown.innerHTML = "<img src=\"../images/scroll/scroll_down.gif\" border=\"0\">";
	
	divScrollDown.style.position = "absolute";
	divScrollDown.style.top = this._wndHeight - 19 + "px"; //"281px"; 
	//divScrollDown.style.width = "15px"; 
	//divScrollDown.style.height = "15px"; 
	divScrollDown.style.left = this._wndWidth - 20 + "px"; //"280px"; 
	divScrollDown.style.zIndex = 40;
	divScrollDown.style.visibility = "hidden";
	this._windowElem.appendChild(divScrollDown)	
	
	//Create the drag div
	var divScrollDrag = this._doc.createElement("div");
	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_ScrollDrag";
	divScrollDrag.setAttributeNode(attr);		
	divScrollDrag.innerHTML = "<img src=\"../images/scroll/scroll_drag.gif\" width=\"15\" height=\"35\" border=\"0\">";
	
	divScrollDrag.style.position = "absolute";
	divScrollDrag.style.top = "56px"; 
	//divScrollDrag.style.width = "15px"; 
	//divScrollDrag.style.height = "15px"; 
	divScrollDrag.style.left = this._wndWidth - 20 + "px"; //"280px"; 
	divScrollDrag.style.zIndex = 30;
	divScrollDrag.style.visibility = "hidden";
	this._windowElem.appendChild(divScrollDrag)	
	
	//Create the track div
	var divScrollTrack = this._doc.createElement("div");
	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_ScrollTrack";
	divScrollTrack.setAttributeNode(attr);	
	
	//divScrollTrack.innerHTML = "<img src=\"../images/scroll/scroll_track.gif\" width=\"15\" height=\"35\" border=\"0\">";
	divScrollTrack.style.backgroundImage = "url('../images/scroll/scroll_track.gif')";
	divScrollTrack.style.position = "absolute";
	divScrollTrack.style.top = "35px"; 
	divScrollTrack.style.width = this.SCROLL_WIDTH + "px"; 
	divScrollTrack.style.height = this._wndHeight - 53 + "px"; //"247px"; 
	divScrollTrack.style.left = this._wndWidth - 20 + "px"; //"280px";
	divScrollTrack.style.zIndex = 20;
	divScrollTrack.style.visibility = "hidden";
	this._windowElem.appendChild(divScrollTrack)		
	
	//Create the to cover the scroll when it is not needed
	var divScrollTrackDim = this._doc.createElement("div");
	attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_ScrollTrackDim";
	divScrollTrackDim.setAttributeNode(attr);		
	//divScrollTrack.innerHTML = "<img src=\"../images/scroll/scroll_track.gif\" width=\"15\" height=\"35\" border=\"0\">";
	divScrollTrackDim.style.backgroundImage = "url('../images/scroll/scr_dim.gif')";
	divScrollTrackDim.style.position = "absolute";
	divScrollTrackDim.style.top = "20px"; 
	divScrollTrackDim.style.width = this.SCROLL_WIDTH + "px"; 
	divScrollTrackDim.style.height = this._wndHeight - 24 + "px"; //"276px"; 
	divScrollTrackDim.style.left = this._wndWidth - 20 + "px"; //"280px"; 
	divScrollTrackDim.style.zIndex = 500;
	divScrollTrackDim.style.visibility = "hidden";
	this._windowElem.appendChild(divScrollTrackDim)		

	
	//Create the scroll	
	var scroller = new Scroller();
	scroller.SetScrollerFrame(this._frame);
	scroller.SetContentFrame(this._frame);
	scroller.SetScrollIds(divScrollUp.id, divScrollDown.id, divScrollDrag.id, divScrollTrack.id, divScrollTrackDim.id);
	scroller.SetContentIds(this._contentMaskElem.id, this._contentElem.id);
	scroller.Init();
	
	//If scroller is not needed, change the width of content layers
	if (!scroller.IsVisible())
	{
		this._contentMaskElem.style.width = this._wndWidth - 25 + this.SCROLL_WIDTH + "px"; //"275px"; 
		this._contentMaskElem.style.clip = "rect(0px " + (this._wndWidth - 25 + this.SCROLL_WIDTH) + "px " + (this._wndHeight - 25) + "px 0px)"; 
	}

	return scroller;
}


Window.prototype.CreateIFrame = function(frameborder)
{
	var overLayer = this._doc.createElement("div");
	var attr = this._doc.createAttribute("id");
	attr.value = this.GetId() + "_OverLayer";
	overLayer.setAttributeNode(attr);
	this._windowElem.appendChild(overLayer);
	overLayer.style.position = "absolute";
	
	/*overLayer.innerHTML = 
		"<table width=\"100%\" border=\"1\">" + 
		"<tr>" +
		"<td align=\"center\">" + 
		"hello" +
		"</td>" +		
		"</tr>" +
		"</table>";*/
	
	this._iFrameOverLayer = new Layer(this.GetId() + "_OverLayer");
	this._iFrameOverLayer.SetTop(this._contentMaskElem.style.top);
	this._iFrameOverLayer.SetLeft(this._contentMaskElem.style.left);
	this._iFrameOverLayer.SetWidth(this._contentMaskElem.style.width);
	this._iFrameOverLayer.SetHeight(this._contentMaskElem.style.height);
	this._iFrameOverLayer.SetZIndex(999);
	this._iFrameOverLayer.SetBGColor("#000077");
	this._iFrameOverLayer.SetVisible(false);
	
	this._iFrameElem = this._doc.createElement("iframe");
	this._iFrameElem.setAttribute("width", this._wndWidth - 16);
	this._iFrameElem.setAttribute("height", this._wndHeight - 30); //Keep this
	this._iFrameElem.setAttribute("scrolling", "no");
	this._iFrameElem.setAttribute("allowtransparency", "yes");
	this._iFrameElem.setAttribute("frameborder", frameborder);
	this._iFrameElem.setAttribute("marginwidth", "0");
	this._iFrameElem.setAttribute("marginheight", "0");	
		
	this._contentElem.appendChild(this._iFrameElem);	
	this._bIFrameElemAppended = true;	
}

Window.prototype.SetSrc = function(src)
{
	if (this._iFrameElem == null)
	{
		alert ("Window:SetSrc: No IFrame has been created. Id: " + this.GetId());
		return;
	}
	
	this.SetWindowText("Loading...");

	//this._iFrameElem.location.href = src; //Does not work
	this._iFrameElem.src = src; //This works??
}

Window.prototype.SetIFrameHeight = function(height)
{
	if (this._iFrameElem == null)
	{
		alert ("Window:SetIFrameHeight: No IFrame has been created. Id: " + this.GetId());
		return;
	}
	
	this._iFrameElem.height = height;
}

Window.prototype.ReloadIFrame = function()
{
	//alert("ReloadIFrame");
	if (this._iFrameElem == null)
	{
		alert ("Window:SetIFrameHeight: No IFrame has been created. Id: " + this.GetId());
		return;
	}
	
	//this._iFrameElem.location.href = this._iFrameElem.location.href;
	this._iFrameElem.src = this._iFrameElem.src;
}

Window.prototype.ReInitScrollbar = function()
{
	//alert ("Window::ReInitScrollbar");
	this._scroller.ReInit();	
	this.setCorrectWidths();
}

Window.prototype.setCorrectWidths = function()
{
	if (this._scroller.IsVisible())
	{
		this._contentMaskElem.style.width = this._wndWidth - 25 + "px"; //"275px"; 		
		this._contentMaskElem.style.clip = "rect(0px " + (this._wndWidth - 25) + "px " + (this._wndHeight - 25) + "px 0px)"; 
	
		if (this._iFrameElem != null)
		{			
			this._iFrameElem.width = this._wndWidth - 16 - this.SCROLL_WIDTH + "px";
		}
	}
	else
	{
		this._contentMaskElem.style.width = this._wndWidth - 25 + this.SCROLL_WIDTH + "px"; //"275px"; 
		this._contentMaskElem.style.clip = "rect(0px " + (this._wndWidth - 25 + this.SCROLL_WIDTH) + "px " + (this._wndHeight - 25) + "px 0px)"; 
		
		if (this._iFrameElem)
		{
			this._iFrameElem.width = this._wndWidth - 16 + "px";
		}
	}
}

Window.prototype.SetWndWidth = function(width)
{	
	this.SetWidth(width);
}

Window.prototype.SetWndHeight = function(height)
{	
	this.SetHeight(height);
}

Window.prototype.SetWindowText = function(text)
{	
	if (this._wndTitleElem == null)
	{
		alert ("Window::SetWindowText: No titlebar created. Id: " + this.GetId());
		return;
	}
	this._wndTitleElem.innerHTML = "<i>" + text + "</i>";
}
