/**
 * This file must be included to use the debugger. The debugger may be pointed to write to a particular element or will open a new
 * window for every write action. There is also the choice to show / hide the debugger.
 * 
 * @version 13-01-2010
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=debugger.js">R. ten Napel, ing.</a>
 **/

/**
 * This static class may be used to debug values to a window or an element.
 **/
var Debugger = new function() {
	this.target = null;
	this.visible = false;
	
	/**
	 * Create a new debugger window.
	 * 
	 * @param options			The options to parse to the new window.
	 * 
	 * @return					The new window.
	 **/
	this.createWindow = function(options) {
		// Set the default options:
		if (options == null) {
			options = "toolbar,width=800,height=600,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes";
		}
		
		return window.open("", "debugWindow", options);
	};
	
	/**
	 * Write a message directly into a new window.
	 * 
	 * @param message			The message to write.
	 * @param options			The new window options.
	 * 
	 * @return					This debugger.
	 **/
	this.writeInWindow = function(message, options) {
		var debugWindow = this.createWindow();
		
		debugWindow.document.write(message);
		debugWindow.document.close();
		
		return this;
	};
	
	/**
	 * Write to the debugger. Multiple arguments are also written to the debugger.
	 * 
	 * @param value				The stuff to write to the debugger.
	 * @param ...				More stuff to write to the debugger.
	 * 
	 * @return					This debugger.
	 **/
	this.write = function(value) {
		var message = "";
		
		// Iterate through all the arguments:
		for (var i = 0; i < arguments.length; i++) {
			var value = arguments[i];
			
			// Add line break:
			if (i > 0) {
				message += "<br />\n<br />\n";
			}
			
			// Detect the value type:
			if (typeof(value) == "string") {
				message += value;
			} else if (typeof(value) == "object") {
				message += GlobalKit.getElementTree(value, 2);
			}
		}
		
		// If the message is not empty:
		if (message != "") {
			// If a target is defined:
			if (this.target != null) {
				$(this.target).append("<b>" + new Date() + "</b><br /><br />" + message + "<hr />");
				this.show();
				
				return this;
			}
			
			// Show message in window if no target is set:
			this.writeInWindow(message);
		}
		
		return this;
	};
	
	/**
	 * Sets the debugger target to print the debugger information to.
	 * 
	 * @param target			The target to set.
	 * 
	 * @return					This debugger.
	 **/
	this.setTarget = function(target) {
		this.target = target;
		
		// Show the target:
		if (target != null && this.isVisible()) {
			this.show();
		}
		
		return this;
	};
	
	/**
	 * Set or retrieve the visibility of the debugger.
	 * 
	 * @param visible			If the debugger must be visible or not.
	 * 
	 * @return					This debugger (when param is set) or true / false when param is null.
	 **/
	this.isVisible = function(visible) {
		// Set value if not null:
		if (visible == null) {
			return this.visible;
		}
		
		this.visible = visible;
		
		return this;
	};
	
	/**
	 * Show the debug window.
	 * 
	 * @return					This debugger.
	 **/
	this.show = function() {
		this.isVisible(true);
		
		// Show the target window:
		if (this.target != null) {
			this.target.show();
		}
		
		return this;
	};
	
	/**
	 * Hide the debug window.
	 * 
	 * @return					This debugger.
	 **/
	this.hide = function() {
		this.isVisible(false);
		
		// Hide the target window:
		if (this.target != null) {
			this.target.hide();
		}
		
		return this;
	};
};