/**
 * Simple Javascript console for IE
 *
 * Inspired by Firebug for Firefox
 *
 * Instructions:
 *	 1. Include this script file in your page's HTML head
 *	 2. From some Javascript on the page, call console.log()
 *
 * Author:								Moxley Stratton (www.moxleystratton.com)
 * License:							 LGPL
 * Original Release Date: 2006-12-04
 * @version 0.2
 */
var IEConsole = {
	HEIGHT: 120,
	INPUT_HEIGHT: 25,
	createContainer: function() {
		container = document.createElement('div');
		container.style.height = IEConsole.HEIGHT + 'px';
		container.style.border = "1px solid #0a0";
		container.appendChild(IEConsole.createOutputArea());
		container.appendChild(IEConsole.createInputBar());
		return container;
	},
	createOutputArea: function() {
		var output = document.createElement('div');
		output.style.height = (IEConsole.HEIGHT - IEConsole.INPUT_HEIGHT) + "px";
		output.style.overflow = 'auto';
		output.style.textAlign = "left";
		IEConsole.outputElement = output;
		return output;
	},
	createInputBar: function() {
		var form = document.createElement('form');
		form.style.borderTop = "1px solid #aaa";
		form.style.height = IEConsole.INPUT_HEIGHT + "px";
		form.style.margin = 0;
		form.action = "javascript:;";
		form.onsubmit = IEConsole.evaluate;
		form.appendChild(IEConsole.createPromptAndInput());
		return form;
	},
	createPromptAndInput: function() {
		var container = document.createElement("div");
		container.id = 'IEConsole_container';
		container.innerHTML = "<table style=\"width: 100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>" +
			"<td width=\"25\" style=\"padding-top: 10px;\">&gt;&gt;&gt;</td>" +
			"<td><input type=\"text\" /></td>" +
			"</tr></table>";
		IEConsole.inputElement = container.getElementsByTagName('input')[0];
		IEConsole.inputElement.style.width = "100%";
		IEConsole.inputElement.style.border = "0";
		IEConsole.inputElement.onkeydown = function(e) {
			if (typeof(event) != "undefined") {
				if (event.keyCode == 38) IEConsole.historyUp();
				else if (event.keyCode == 40) IEConsole.historyDown();
			}
		}
		return container;
	},
	history: [],
	historyPointer: 0,
	historyUp: function() {
		IEConsole.historyPointer--;
		if (IEConsole.historyPointer < 0) return;
		IEConsole.inputElement.value = IEConsole.history[IEConsole.historyPointer];
	},
	historyDown: function() {
		if (IEConsole.historyPointer >= IEConsole.history.length) return;
		IEConsole.historyPointer++;
		if (IEConsole.historyPointer >= IEConsole.history.length) {
			IEConsole.inputElement.value = '';
		}
		else {
			IEConsole.inputElement.value = IEConsole.history[IEConsole.historyPointer];
		}
	},
	evaluate: function() {
		IEConsole.history.push(IEConsole.inputElement.value);
		IEConsole.historyPointer = IEConsole.history.length;
		var result;
		try {
			result = eval(IEConsole.inputElement.value);
			IEConsole.log(IEConsole.formatResult(result));
		}
		catch (e) {
			IEConsole.logError(result);
		}
		IEConsole.inputElement.value = '';
	},
	formatResult: function(result) {
		try {
			if (typeof result == "undefined") {
				return "undefined";
			}
			else if (typeof result == "object") {
				if (result.nodeName) {
					if (result.tagName) {
						return IEConsole.formatElement(result);
					}
					else {
						return "HTML non-element: " + result.nodeName;
					}
				}
				else {
					return "" + result;
				}
			}
			else {
				return "" + result;
			}
		}
		catch (e) {
			return "Failed to format result: " + e.message;
		}
	},
	formatElement: function(element) {
		return "<" + element.tagName.toLowerCase() + (element.childNodes.length > 0 ? ">" : "/>");
	},
	escapeHTML: function(str) {
		str = str.replace("<", "&lt;");
		str = str.replace(">", "&gt;");
		return str;
	},
	log: function(msg) {
		IEConsole.logRaw(IEConsole.escapeHTML(msg));
	},
	logError: function(msg) {
		IEConsole.logRawError(IEConsole.escapeHTML(msg));
	},
	launch: function() {
		if (!IEConsole.block) {
			IEConsole.block = IEConsole.createContainer();
			document.body.insertBefore(IEConsole.block, document.body.childNodes[0]);
			IEConsole.inputElement.focus();
		}
	},
	logRaw: function(msg) {
		msg = "<div style=\"border-bottom: 1px solid #aaa; padding-left: 5px\">" + 
			IEConsole.formatMessageText(msg) + "</div>\n";                                                                                      
		IEConsole.logLine(msg);
	},
	logRawError: function(msg) {
		msg = "<div style=\"border-bottom: 1px solid #aaa; padding-left: 5px" +
			"background-color: #ffc\"><span style=\"color: red\">" +
			IEConsole.formatMessageText(msg) + "</span></div>\n";
		IEConsole.logLine(msg);
	},
	formatMessageText: function(msg) {
		msg = msg.toString().strip();
		if (!msg) msg = "&nbsp;"
		return msg;
	},
	logLine: function(msg) {
		if (!IEConsole.block) {
						IEConsole.block = IEConsole.createContainer();
						document.body.insertBefore(IEConsole.block, document.body.childNodes[0]);
						IEConsole.inputElement.focus();
		}
		IEConsole.outputElement.innerHTML += msg;
		var out = IEConsole.outputElement;
		out.scrollTop = out.scrollHeight - out.clientHeight;
	}
}
if (typeof 'string'.strip != 'function'){
	String.prototype.strip = function(){
		return this.replace(/^\s+/, '').replace(/\s+$/, '');
	}
}
if (typeof console == "undefined") console = {};
if (typeof console.log == "undefined") {
	console.log = function(msg) {
		IEConsole.log(msg);
	}
	console.error = function(msg) {
		IEConsole.logError(msg);
	}
}
