/*
 	Author: SBJ
 
	Requires the mootools javascript library.
*/


var TM = {

	debugEnabled : undefined,
	
	enableDebugConsole : true,
	debugConsoleLoaded : false,

	/*
		Determine if debug information should be suppressed.
	*/
	isDebugEnabled : function() {
		if (typeof(TM.debugEnabled) !== "undefined") {
			return TM.debugEnabled;
		}
	
		TM.debugEnabled = false;
		if ((typeof(document) !== "undefined") && (typeof(document.domain) !== "undefined")) {
			TM.debugEnabled = (document.domain.indexOf(".") === -1);
		}
		
		return TM.debugEnabled;
	},

	/*
		If debug is enabled, prints the supplied string.
	*/
	print : function(s) {
		if (!TM.isDebugEnabled()) {
			return;
		}
			
		if (typeof(console) !== "undefined" && typeof(console.debug) !== "undefined") {
			console.debug(s);
		}
		
		TM.debugConsoleAppend(s);
	},
	
	debug : function(s) {
		TM.print(s);
	},
	
	warn : function(s) {
		TM.print("WARNING: "+s);
		
		TM.debugConsoleNotifyError();
	},
	
	error : function(s) {
		TM.print("ERROR: "+s);
		
		TM.debugConsoleNotifyError();
	},
	
	debugConsoleAppend : function(str) {
		if (typeof(TM.enableDebugConsole) === "undefined" || TM.enableDebugConsole === false) {
			return;
		}
		
		if (typeof(TM.debugConsoleLoaded) === "undefined" || TM.debugConsoleLoaded === false) {
			if (TM.setupDebugConsole() == false) {
				return;
			}
		}
		
		var consoleElem = document.getElementById("TMDebugConsoleTEXT");
		consoleElem.value = consoleElem.value + "\n" + str;
		
		consoleElem.scrollTop = consoleElem.scrollHeight;
	},
	
	debugConsoleNotifyError : function() {
		if (typeof(TM.enableDebugConsole) === "undefined" || TM.enableDebugConsole === false) {
			return;
		}
		
		if (typeof(TM.debugConsoleLoaded) === "undefined" || TM.debugConsoleLoaded === false) {
			if (TM.setupDebugConsole() == false) {
				return;
			}
		}
		
		var consoleElem = document.getElementById("TMDebugConsoleTEXT");
		consoleElem.style.background = "#FF0000";
		
		return;
	},
	
	/**
	 	Setup debug console for output.
	 	Note: Prone to race conditions, 
	*/
	setupDebugConsole : function() {
		if (typeof(TM.enableDebugConsole) === "undefined" || TM.enableDebugConsole === false) {
			return false;
		}
		
		if (typeof(TM.debugConsoleLoaded) === "undefined" || TM.debugConsoleLoaded === true) {
			return false;
		}
		
		// Mark console as loaded
		TM.debugConsoleLoaded = true;
		
		// Log setup execution, might assist in detecting race conditions, and whether or not they pose any problem..
		if (typeof(console) !== "undefined" && typeof(console.debug) !== "undefined") {
			console.debug("Setting up TM JavaScript Debug Console");
		}
		
		var width  = 400;
		
		var consoleWidth  = width;
		var consoleHeight = 100;
		
		var toolbarWidth  = width;
		var toolbarHeight = 15;
		
		var height = consoleHeight + toolbarHeight;
		
		var consoleElementId = "TMDebugConsole";
		var consoleElement = document.createElement("div");
		with (consoleElement) {
			setAttribute("id", consoleElementId);
			style.width  = width+"px";
			style.height = height+"px";
			style.left = "10px";
			style.bottom = "20px";
			style.position = "fixed";
		}
		
		if (typeof(document.body.style.maxHeight) === "undefined") {
			// IE6 and older browsers
			consoleElement.style.position = "absolute";
			
			// Move console window with screen 
			var updatePosition = function() {
				var e = document.getElementById(consoleElementId);
				if (typeof(e) !== "undefined") {
					var scrollTop = document.documentElement.scrollTop;
					var height    = document.documentElement.clientHeight;
					
					e.style.left   = "10px";
					e.style.top    = (scrollTop + height - 29 - parseInt(e.style.height))+"px";
				}
			};
			
			setInterval(updatePosition, 1000);
		}
		
		/* Text area element containing log output */
		var consoleTextAreaId = "TMDebugConsoleTEXT";
		var htmlAreaElement = document.createElement("textarea");
		with (htmlAreaElement) {
			setAttribute("id", consoleTextAreaId);
			style.width  = consoleWidth+"px";
			style.height = consoleHeight+"px";
			readOnly = true;
			value = "TM JavaScript Debug Console";
		}
		
		/* Toolbar div element */
		var toolbarElement = document.createElement("div");
		with (toolbarElement) {
			setAttribute("id", "TMDebugToolbar");
			style.width  	= toolbarWidth+"px";
			style.height 	= toolbarHeight+"px";
			style.position	= "relative";
			style.float 	= "left";
			style.left 		= "0px";
			style.margin  	= "0px 5px 0px 5px";
		}
		
		/* Show/hide console link */
		var consoleVisibilityLinkId  = "TMDebugToolbarVisibility";
		var consoleVisibilityCaption = "&not; Hide Console";
		
		var visibilityToggleElement  = document.createElement("a");
		with (visibilityToggleElement) {
			setAttribute("id", consoleVisibilityLinkId);
			style.position 	= "absolute";
			style.float 	= "left";
			style.left  	= "0px";
			style.cursor 	= "pointer"; 	// Firefox
			style.cursor 	= "hand";		// IE
			innerHTML 		= consoleVisibilityCaption;
		
			/* Show/hide functionality */
			onclick = function(e) {
				var consoleElem = document.getElementById(consoleTextAreaId);
				var buttonLink  = document.getElementById(consoleVisibilityLinkId);
				
				if (typeof(consoleElem) == "undefined" || typeof(buttonLink) == "undefined") {
					// Error
					return;
				}
				
				var displayMode   = "hidden";
				var buttonCaption = "&not; Show Console";
				if (consoleElem.style.visibility == "hidden") {
					displayMode   = "visible";		
					buttonCaption = consoleVisibilityCaption;
				}
				
				consoleElem.style.visibility = displayMode;
				buttonLink.innerHTML = buttonCaption;
				return;
			};	
		}
		
		/* Clear console link */
		var consoleClearLinkId  = "TMDebugToolbarClean";
		var consoleClearCaption = "&not; Clear";
		
		var clearToggleElement  = document.createElement("a");
		with (clearToggleElement) {
			setAttribute("id", consoleClearLinkId);
			style.position 	= "absolute";
			style.float 	= "left";
			style.left  	= "95px";
			style.cursor 	= "pointer"; 	// Firefox
			style.cursor 	= "hand";		// IE
			innerHTML 		= consoleClearCaption;
			
			/* clean console functionality */
			onclick = function(e) {
				var consoleElem = document.getElementById(consoleTextAreaId);
				if (typeof(consoleElem) !== "undefined") {
					consoleElem.value = "";
				}
			};
		}
		
		// Add to toolbar
		toolbarElement.appendChild(visibilityToggleElement);
		toolbarElement.appendChild(clearToggleElement);
		
		// Add to console div
		consoleElement.appendChild(htmlAreaElement);
		consoleElement.appendChild(toolbarElement);
		
		// Add to document
		document.body.appendChild(consoleElement);
		
		return true;
	},
	
	initialize : function() {
		if (TM.isDebugEnabled()) {
			/**
				Hook into javascript errors on page.
				Note: This will not work in webkit browsers -- at least not with chrome currently.
			 */
			window.onerror = function(errMsg, url, lineNumber) {
				TM.error("Javascript execution failed on page\nMessage: "+errMsg+"\nURL: "+url+"\nLine: "+lineNumber);
			};
		}
	}
	
};


window.addEvent("domready", TM.initialize);
