/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% WindowObject %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
*/
	/**
	 *	WindowObject
	 *	This class provides a fake window inside the document
	 *
	 *	@param width The width of the window
	 *	@param height The height of the window
	 *	@param title The title of the window
	 *
	 *	@constructor
	 */
	function WindowObject(width, height, title) {
		this.i_width = width;
		this.i_height = height;
		this.i_title = title;
		this.i_top = 0;
		this.i_left = 0;
	}
	
	/**
	 *	The height of the title bar
	 */
	WindowObject.titleHeight = 18;

	/**
	 *	Get/Set the top position of this window
	 *
	 *	@param top (Optional) The new top position of this window
	 *
	 *	@return the current top position of this window
	 */
	WindowObject.prototype.top = function(top) {
		if (top != undefined) {
			this.i_top = top;
			if (this.i_window != undefined) {
				this.i_window.style.top = this.top() + "px";
			}
		}
		return this.i_top;
	}


	/**
	 *	Get/Set the left position of this window
	 *
	 *	@param left (Optional) The new left position of this window
	 *
	 *	@return the current left position of this window
	 */
	WindowObject.prototype.left = function(left) {
		if (left != undefined) {
			this.i_left = left;
			if (this.i_window != undefined) {
				this.i_window.style.left = this.left() + "px";
			}
		}
		return this.i_left;
	}

	/**
	 *	Get/Set the width of this window
	 *
	 *	@param width (Optional) The new width of this window
	 *
	 *	@return the current width of this window
	 */
	WindowObject.prototype.width = function(width) {
		if (width != undefined) {
			this.i_width = width;
			if (this.i_window != undefined) {
				this.i_window.style.width = this.width() + "px";
				this.i_window_title.style.width = (this.width() ) + "px";
				this.i_window_content.style.width = (this.width() ) + "px";
			}
		}
		return this.i_width;
	}
	
	/**
	 *	Get/Set the height of this window
	 *
	 *	@param height (Optional) The new height of this window
	 *
	 *	@return the current height of this window
	 */
	WindowObject.prototype.height = function(height) {
		if (height != undefined) {
			this.i_height = height;
			if (this.i_window != undefined) {
				this.i_window.style.height = this.height() + "px";
				this.i_window_title.style.height = WindowObject.titleHeight + "px";
				this.i_window_content.style.height = (this.height() - WindowObject.titleHeight - 1) + "px";
			}
		}
		return this.i_height;
	}
	
	/**
	 *	Get/Set the title text of this window
	 *
	 *	@param width (Optional) The new title text of this window
	 *
	 *	@return the current title text of this window
	 */
	WindowObject.prototype.title = function(title) {
		if (title != undefined) {
			this.i_title = title;
			if (this.i_window != undefined) {
				this.i_window_title.innerHTML = "&nbsp;" + title;
			}
		}
		return this.i_title;
	}
	
	/**
	 *	Get/Set the content of this window
	 *
	 *	@param content (Optional) The new content DIV
	 *
	 *	@return the current content DIV
	 */
	WindowObject.prototype.windowContent = function(content) {
		if (content != undefined) {
			var oldc = this.i_content;
			this.i_content = content;
			if (this.i_window != undefined) {
				if (oldc != undefined) {
					this.i_window_content.removeChild(oldc);
				}
				this.i_window_content.appendChild(content);
			}
		}
		return this.i_content;
	}
	
	/**
	 *	Get/Set if the window is visible
	 *
	 *	@param state (Optional) The new visible state of this window
	 *
	 *	@return the current visible state of this window
	 */
	WindowObject.prototype.visible = function(state) {
		if (state != undefined) {
			this.i_visible = state;
			if (state == true && this.i_init != true) {
				document.body.appendChild(this.getWindow());
				this.i_init = true;
			}
			if (this.i_window != undefined) {
				this.i_window.style.display = (state ? "" : "none");
			}
		}
		return this.i_visible;
	}
	
	/**
	 *	Center the window
	 */
	WindowObject.prototype.center = function() {
		this.left(Math.floor((Environment.browserWidth() - this.width()) / 2));
		this.top(Math.floor((Environment.browserHeight() - this.height()) / 2));
	}
	
	/**
	 *	Handle when the mouse is depressed on the title bar
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 */
	WindowObject.prototype.handleMouseDown = function(e) {
		this.i_mm = EventControl.listen(document.body, "onmousemove", this.handleMouseMove, this);
		this.i_mu = EventControl.listen(document.body, "onmouseup", this.handleMouseUp, this);
		this.i_start_x = Environment.getCursorX() - this.left();
		this.i_start_y = Environment.getCursorY() - this.top();
	}
	
	/**
	 *	Handle when the mouse moves during a drag
	 *
	 *	@private
	 *
	 *	@param e The event that triggered this
	 */
	WindowObject.prototype.handleMouseMove = function(e) {
		this.left(Environment.getCursorX() - this.i_start_x);
		this.top(Environment.getCursorY() - this.i_start_y);
	}
	
	/**
	 *	Handler for when the user releases the mouse
	 *
	 *	@private
	 *
	 *	@param e the event that triggered this
	 */
	WindowObject.prototype.handleMouseUp = function(e) {
		this.i_mm.destroy();
		this.i_mu.destroy();
	}
	
	
	
	/**
	 *	Get the DIV that contains this window
	 *
	 *	@return the DIV that contains this window
	 */
	WindowObject.prototype.getWindow = function() {
		if (this.i_window == undefined) {
			this.i_window = document.createElement('DIV');
			this.i_window.className = "WindowObject";
			this.i_window.style.width = this.width() + "px";
			this.i_window.style.height = this.height() + "px";
			this.i_window.style.left = this.left() + "px";
			this.i_window.style.top = this.top() + "px";
			this.i_window.style.display = (this.visible() ? "" : "none");
			
				this.i_window_title = document.createElement('DIV');
				this.i_window_title.className = "WindowObject_title";
				this.i_window_title.style.width = (this.width()) + "px";
				this.i_window_title.style.height = WindowObject.titleHeight + "px";
				this.i_window_title.innerHTML = "&nbsp;" + this.title();
				this.i_window_title.style.lineHeight = WindowObject.titleHeight + "px";
				EventControl.listen(this.i_window_title, "onmousedown", this.handleMouseDown, this);
				this.i_window.appendChild(this.i_window_title);
				
				this.i_window_content = document.createElement('DIV');
				this.i_window_content.className = "WindowObject_content";
				this.i_window_content.style.width = (this.width()) + "px";
				this.i_window_content.style.height = (this.height() - WindowObject.titleHeight - 1) + "px";
				this.i_window.appendChild(this.i_window_content);
					
					if (this.i_content != undefined) {
						this.i_window_content.appendChild(this.i_content);
					}
		}
		return this.i_window;
	}
