/**************************************************************************************************
DEBUGGING FUNCTIONS

Public Methods:
	Debug()			- constructor
	Add(message)	- add a message to the debug window
	Clear()			- clears the debug window
	Enable()		- opens the debug window
	Disable()		- closes the debug window
	IsEnabled()		- Returns true or false to indicate whether debugging is enabled.
	InWindow(t/f)	- Set to true to open the debug window in a separate window.  If false, opens the 
					  debug info in a dynamic window at the bottom of the current web page.  Defaults
					  to false.
	AttachTo(object)- Set what object to attach the debug window to, if it's not a separate window.
					  Defaults to "document.body".
*/

function Debug() {
	//Our DIV objects that will hold all the debug info.
	this._wrapper = null;
	this._debugBox = null;
	
	//A reference to the window, if InWindow is true.
	this._window = null;
	
	//Defines whether or not we've already created our debug window.
	this._windowCreated = false;
	
	//Holds all the debug messages that come in through Add.
	this._data = '';
	
	//Default to debugging within the current window.
	this._inWindow = false;
	
	//Set what object to attach the debug window to.
	this._attachTo = document.body;
	
	//Turn debugging off by default.
	this.Disable();
}


/*
Adds the given text into a dynamically-created text box.  Use for debugging.
Parameters:
	txt - the text to display in the debug box.
*/
Debug.prototype.Add = function(txt) {
	txt = txt.replace(/\n/g, "<br />");
	
	this._data += txt;
	
	if (this._enabled && this._debugBox) {
		this._debugBox.innerHTML = this._data;
		this._debugBox.scrollTop = this._debugBox.scrollHeight;
	}
}

//Clears the debug text box.
Debug.prototype.Clear = function() {
	this._data = '';
	
	if (this._enabled && this._debugBox)
		this._debugBox.innerHTML = '';
}

//Closes the debug text box.
Debug.prototype.Close = function() {
	this.Disable();
}

//Enables debugging and shows the debugging window.
Debug.prototype.Enable = function() {
	this._enabled = true;
	this.Open();
}

//Disables debugging and hides the debugging window.
Debug.prototype.Disable = function() {
	this._enabled = false;
	
	//Remove the debug window if we have one.
	if (this._windowCreated) {
		if (this._inWindow) {
			if (this._window) {
				this._window.close();
				this._window = null;
			}
		}
		else {
			if (this._wrapper)
				//this._wrapper.style.display = 'none';
				this._attachTo.removeChild(this._wrapper);
		}
		
		this._windowCreated = false;
	}
}

//Returns true or false to indicate whether or not debugging is enabled.
Debug.prototype.IsEnabled = function() {
	return this._enabled;
}


//Disables debugging and hides the debugging window.
Debug.prototype.InWindow = function(bOnOff) {
	this._inWindow = bOnOff;
	this.Reset();
}

//Set what object the debug window is appended to.  Defaults to "document.body".
Debug.prototype.AttachTo = function(object) {
	this._attachTo = object;
	this.Reset();
	
}


/*
PRIVATE METHODS AND PROPERTIES
*/

//Closes and then re-opens the debug window, assuming it's open already.
Debug.prototype.Reset = function() {
	//If we are currently enabled, disable the current window and reopen the new one.
	if (this._enabled) {
		this.Disable();
		this.Enable();
	}
}

//Dynamically adds the debug text box to the end of the page.
Debug.prototype.Open = function() {
	//If we've already created the debug window, return.
	if (this._windowCreated) {
		return;
	}
	
	this._windowCreated = true;
	
	//Create our wrapper object that will hold all the debug info.
	this._wrapper = document.createElement("DIV");
	this._wrapper.style.clear = 'both';
	
	//Clear and Close buttons.
	var clear = document.createElement("BUTTON");
	clear.innerHTML = 'Clear';
	clear.style.font = '8pt Verdana';
	
	var close = document.createElement("BUTTON");
	close.innerHTML = 'Close';
	close.style.font = '8pt Verdana';
	
	//A workaround to get the Clear and Close methods to work in a separate window.
	var dObject = this;
	clear.onmouseup = function() {
		dObject.Clear();
	}
	
	close.onmouseup = function() {
		dObject.Close();
	}
	
	//Title section.
	var title = document.createElement("DIV");
	title.style.border = '2px solid red';
	title.style.borderBottom = '0';
	title.style.backgroundColor = '#F0F0F0';
	title.style.font = 'bold 8pt Verdana';
	title.style.marginTop = '5px';
	title.innerHTML = '&nbsp;DEBUG WINDOW&nbsp;';
	title.appendChild(clear);
	title.appendChild(close);
	
	//Debug box
	this._debugBox = document.createElement("DIV");
	this._debugBox.style.overflowY = 'auto';
	this._debugBox.style.height = '250px';
	this._debugBox.style.font = '10pt Courier New';
	this._debugBox.style.border = '2px solid red';
	this._debugBox.style.backgroundColor = '#F0F0F0';
	this._debugBox.innerHTML = this._data;
	
	//Add to the wrapper.
	this._wrapper.appendChild(title);
	this._wrapper.appendChild(this._debugBox);
	
	if (this._inWindow) {
		//alert('Opening Debug Window');
		try {
			this._window = window.open('/Tools/js/DebugBlank.htm', '_blank', 'width=500, height=300');
		}
		catch(e) {}
		
		this.AddWrapper();
	}
	else {
		//alert('Opening Debug');
		this._attachTo.appendChild(this._wrapper);
	}
}


//Used to add the wrapper DIV to the floating window.  Will call itself multiple times to ensure 
//that it gets added.
Debug.prototype.AddWrapper = function() {
	if (this._window && this._window.addBody) {
		this._window.addBody(this._wrapper);
	}
	else {
		var dObject = this;
		
		setTimeout(
			function() {
				dObject.AddWrapper()
			}
			, 1000
		);
	}
}
