/**
 * Version:   $ $
 * ID:        $Id: debug.js,v 1.3 2007/03/15 12:59:22 billyreisinger Exp $
*/

/**
 * JS Debug Console
 *  Billy Reisinger
 * bookmarklet: 
 * javascript:void(z=document.body.appendChild(document.createElement('script')));void(z.language='javascript');void(z.type='text/javascript');void(z.src='http://content-dev2/breisinger/debug.js');
*/

function Debugger() {
	this.returnBrowserType = function() {
		if(window.navigator.userAgent.toLowerCase().indexOf("opera") != -1) {
			return "op";
		}
		if(window.navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
			return "ie";
		}
		if(window.navigator.userAgent.toLowerCase().indexOf("firefox") != -1) {
			return "ff";
		}
		if(window.navigator.userAgent.toLowerCase().indexOf("safari") != -1) {
			return "sa";
		}
	}
	this.browser = this.returnBrowserType();
	this.lineNumber = 0;
	this.mainBlock;
	this.output = document.getElementById("debugOutput");
	this.input;
	this.domGetElFunctions = {
		id: new Array("document.getElementById","$"),
		className: new Array("getElementsByClassName","$C")
	};
	this.history = new Array();
	this.historyPosition = 0;
	this.printStack = false;
	var line = "**********************************************************************************";
	this.defaultText = "JS Debug Console\nEnter \"debug.help()\" for a list of commands.\n";
	this.tips = [
		"Did you know?\nThe DOM Inspector (ALT-X) will automatically put\n an element with an ID in the input field for you.",
		"Did you know?\nYou can tie this script into your own to debug scripts. Use 'debug.methodName' anywhere\n in your scripts, and pull\n up this window before executing to see\n the results.",
		"Did you know?\nUse debug.stopWatch.start() and debug.stopWatch.stop() to\n time execution speeds! Handy for optimization.",
		"Did you know?\nPress TAB to complete a function, method, or property name.\n If more than one match is found, a list of possible\n matches will appear.",
		"Did you know?\nYou can use debug.show() to show a list of the names\nand types of an object's members.\nOn the other hand, debug.dump will show names and\n_values_ of an object's members.",
		"Whoa ---- you can now tab-complete HTML element ids after typing document.getElementsById(' (or the '$' shorthand if using Prototype).  This also works with class names (i.e. document.getElementsByClassName)"
	]
	this.defaultText += line + "\n" + this.tips[(parseInt((Math.random()*10)%this.tips.length))] + "\n" + line + "\n";
	var _null = "nooutput";
	
	this.print = function(text,clear,suppressLineNumbers) {
		clear = (typeof clear != "undefined") ? clear : false;
		if(this.output == null || document.getElementById("debugParent") == null) {
			this.create();
			this.output = document.getElementById("debugOutput");
			this.mainBlock = document.getElementById("debugParent");
		}
		if(clear) {
			this.output.value = "";
		}
		if(text != "") {
			if(typeof suppressLineNumbers != "undefined" && !suppressLineNumbers) {
				this.output.value += this.lineNumber + ". ";
			}
			this.output.value += text + "\n";
			this.output.scrollTop = this.output.scrollHeight;
			this.lineNumber++;
		}
		return _null;
	}
	
	this.show = function(obj) {
		this.print(line,false,true);
		var out = "";
		this.lineNumber = 0;
		
		for(var p in obj) {
			if(typeof obj[p] == "function") {
				t = obj[p].toString();
				t = t.replace(/[\x0A\x0D]/g,"").replace(/\s+/g,"").replace(/\{.+\}/g,"{ ... }");
				t = t.replace(p,"");
				t = p + ": " + t;
			} else {
				t = p + ": " + typeof obj[p];
			}
			out += ++this.lineNumber + ". " + t + "\n";
		}
		this.print(out,false,true);
		this.print(line,false,true);
		this.output.scrollTop = this.output.scrollHeight;
		return _null;
	}
		
	this.dump = function(obj) {
		if(typeof obj == "string") {
			this.print(obj);
		} else {
			this.print(line,false,true);
			var out = "";
			//object
			if(typeof obj.push == "undefined") {
				for(th in obj) {
					out += ++this.lineNumber + ". " + th + " = " + obj[th] + "\n";
				}
			//array
			} else {
				for(var i = 0; i<obj.length; i++) {
					out += ++this.lineNumber + ". " + obj[i] + "\n";
				}
			}
			this.print(out,false,true);
			this.print(line,false,true);
			this.output.scrollTop = this.output.scrollHeight;
		}		
		return _null;
	}
	
	/*******
	* HISTORY
	*******/
	this.addToHistory = function(input) {
		this.history.push(input);
		this.historyPosition = this.history.length - 1;
	}
	
	this.getPreviousInput = function() {
		if(this.historyPosition < 0) {
			return '';
		}
		
		return this.history[this.historyPosition--];
	}
	this.getNextInput = function() {
		if(this.historyPosition < this.history.length)
			return this.history[this.historyPosition++];
		else
			return '';
	}
	
	/*******
	* EVALUATION
	*******/
	this.cssEvalFlag = false;
	this.evaluate = function(input) {
		if(input == "") return false;
		if(this.cssEvalFlag) {
			this.evalCss(input);
		} else {
			this.evalJs(input);
		}
	}
	/**
	* evaluate 'input' string as javascript
	**/
	this.evalJs = function(input) {
		this.addToHistory(input);
		this.print(">> " + input);
		try {
			//trying to use an unusual variable name on purpose here
			var ttttt;
			if(this.browser == "ie") {
				ttttt = eval(input);
			} else {
				ttttt = window.eval(input);
				}
			if(ttttt != null) {
				if(ttttt.toString() != _null) this.print(ttttt.toString());
			} else {
				this.print("null");
			}
		} catch(e) {
			this.print(e.message);
			if(this.printStack == true) { this.printStackTrace(); }
		} 
	}
	/*******
	* Map input keystrokes
	*******/
	this.assignInputKeyEvent = function(keyCode) {
		
		//Enter key
		if(keyCode == 13) {
			this.evaluate(this.input.value);
			this.input.value = "";
			return false;
		//Up key
		} else if(keyCode == 38) {
			//TODO: check for shift key so that opera can have this feature?
			if(this.browser != "op") {
				this.input.value = this.getPreviousInput();
			}
			return false;
		//Down key
		} else if(keyCode == 40) {
			//TODO: check for shift key so that opera can have this feature?
			if(this.browser != "op") {

				this.input.value = this.getNextInput();
			}
			return false;
		//Tab key
		} else if(keyCode == 9) {
			this.tabComplete();
			return false;
		}
	}
	/*******
	* HTML
	*******/
	this.clear = function() {
		this.output.value = "";
		//this.input.value = "";
		this.input.focus();
		return _null;
	}
	this.cls = this.clear;
		
	this.create = function() {
		var self = this;
		
		//outermost container
		var debugParent = document.createElement("div");
		var windowScrollY = 0;
		
		if (document.documentElement && document.documentElement.scrollTop) {
			windowScrollY = document.documentElement.scrollTop;
		} else if (document.body) {
			windowScrollY = document.body.scrollTop
		} else {
			windowScrollY = window.scrollY;
		}
		debugParent.style.top = windowScrollY + 50 + "px";
		debugParent.id = "debugParent";
		
		//close on ESC key press
		document.onkeydown = function(e) {
			e = (typeof window.event != "undefined") ? window.event : e;
			if (e.keyCode == "27") {
				self.close();
			}
		}
		
		//WRAPPERS FOR TEXTAREAS
		var textareaWrap = document.createElement("div");
		textareaWrap.id = "debugTextareaWrap";
		
		
		//OUTPUT FIELD
		var debugOutput = document.createElement("textarea");
		debugOutput.id = "debugOutput";
		debugOutput.wrap = "off";
		debugOutput.readOnly = "true";
		debugOutput.value = this.defaultText;
		
		//INPUT FIELD
		var inp = document.createElement("textarea");
		inp.id = "debugInput";
		var last = "";
		
		//listen for certain keystrokes, map them 
		inp.onkeydown = function(e) {
			e = (typeof window.event != "undefined") ? window.event : e;
			if(!self.cssEvalFlag) {
				return self.assignInputKeyEvent(e.keyCode);
			} else { 
				if(this.browser == "ie" && window.event.keyCode == 9) {
					inp.value += "\t";
					return false;
				}
			}
		}
		inp.onkeypress = function(e) { 
			e = (typeof window.event != "undefined") ? window.event : e;
			var k = e.keyCode;
			if(!self.cssEvalFlag) {
				if(k==9 || k==13 || k==38 || k==40) {
					if(k!=40 && this.browser != "ie") {
						return false;
					}
				}
			} else {
				
				if(k==9) {
					inp.value += "\t";
					return false;
				}
			}
		}
		
		//DRAG / TITLE BAR
		var dragBut = document.createElement("div");
		dragBut.innerHTML = "JS Debug Console";
		dragBut.id = "debugDragBar";
		dragBut.onmousedown = function(e) {
			e = (typeof window.event != "undefined") ? window.event : e;
			var xplus = (typeof e.layerX == "undefined") ? e.offsetX : e.layerX;
			var yplus = (typeof e.layerY == "undefined") ? e.offsetY : e.layerY;
			document.onmousemove = function(e) {	
				var coords = self.getMouseXY(e);
				document.getElementById("debugParent").style.top = coords.y - yplus + "px";
				document.getElementById("debugParent").style.left = coords.x - xplus + "px";
			}
			return false;
		}
		document.onmouseup = function() {
			document.onmousemove = null;
		};
		//cancel click event to prevent text selection
		dragBut.onclick = function() { return false; }
		
		//expand/shrink console 
		dragBut.ondblclick = function() {
			self.collapseOrExpand();
			return false;
		}
		
		/***********************
		* BUTTONS
		************************/
		//CLEAR BUTTON
		var clearBut = document.createElement("a");
		clearBut.innerHTML = "Clear (Alt-C)";
		clearBut.accessKey = "C";
		clearBut.className = "debugButton";
		clearBut.onclick = function() {
			self.clear();
			return false;
		}
		this.setCrossBrowserAccessKeyFunctionForAnchor(clearBut);
		
		//EVALUATE BUTTON
		var evalBut = document.createElement("a");
		evalBut.value = "Evaluate (Alt-Z)";
		evalBut.innerHTML = "Evaluate (Alt-Z)";
		evalBut.accessKey = "Z";
		evalBut.className = "debugButton";
		evalBut.onclick = function() {
			self.evaluate(inp.value);
			if(!self.cssEvalFlag) {
				inp.value = "";
			}
			inp.focus();
			return false;
		}
		this.setCrossBrowserAccessKeyFunctionForAnchor(evalBut);
		
		
		//CLOSE BUTTON
		var closeBut = document.createElement("a");
		closeBut.innerHTML = "Close (Esc)";
		closeBut.value = "Close (Esc)";
		closeBut.className = "debugButton";
		closeBut.onclick = function() {
			self.close();
		}
				
		//STACK TRACE BUTTON
		var stackBut = document.createElement("a");
		stackBut.innerHTML = "Stack Trace Off";
		stackBut.value = "Stack Trace Off";
		stackBut.className = "debugButton";
		stackBut.onclick = function() {
			self.printStack = self.printStack ? false : true;
			if(self.printStack) {
				self.setButtonVisualActiveState(stackBut,"on");
			} else {
				self.setButtonVisualActiveState(stackBut,"off");
			}
		}
		
		
		//HELP BUTTON
		var helpBut = document.createElement("a");
		helpBut.innerHTML = "Help";
		helpBut.className = "debugButton";
		helpBut.onclick = function() {
			self.help();
		}
		
		//DOM BUTTON
		var domBut = document.createElement("a");
		domBut.innerHTML = "Mouseover DOM Off";
		domBut.title = "Mouseover DOM: toggle to turn on/off inspection of document nodes (Alt-X)";
		domBut.className = "debugButton";
		domBut.accessKey = "X";
		domBut.tabIndex = "4";
		domBut.value = "Mouseover DOM Off";
		this.domActive = false;
		domBut.onclick = function() {
			if(!self.domActive) {
				document.body.onmouseover = function(e) {
					if(typeof e == "undefined") { e = window.event; }
					self.highlightNode(e);
					self.showNodes(e);
				}
				document.body.onmouseout = function(e) {
					if(typeof e == "undefined") { e = window.event; }
					self.unHighlightNode(e);
				}
				self.setButtonVisualActiveState(domBut,"on");
				self.domActive = true;
			} else {
				document.body.onmouseover = function() {}
				self.domActive = false;
				self.setButtonVisualActiveState(domBut,"off");
			}
			return _null;
		}
		this.setCrossBrowserAccessKeyFunctionForAnchor(domBut);
		
		//CSS BUTTON 
		var cssBut = document.createElement("a");
		cssBut.innerHTML = "CSS Input Off";
		cssBut.title = "CSS Input: turn on CSS input to enter arbitrary CSS (Alt-S)";
		cssBut.className = "debugButton";
		
		cssBut.accessKey = "S";
		cssBut.onclick = function() {
			if(!self.cssEvalFlag) {
				self.setButtonVisualActiveState(cssBut,"on");
				self.cssEvalFlag = true;
				inp.className = "cssEntry";
				if(document.getElementById("debugStyleInput") != null) {
					self.styleInputTag.disabled = false;
				}
				if(self.cssEntryHistory.length > 0) {
					inp.value = self.cssEntryHistory[(self.cssEntryHistory.length - 1)];
				} else {
					inp.value = "";
				}
			} else {
				cssBut.innerHTML = "CSS Input Off";
				cssBut.value = "CSS Input Off";
				self.setButtonVisualActiveState(cssBut,"off");
				inp.className = "";
				self.cssEvalFlag = false;
				if(document.getElementById("debugStyleInput") != null) {
					self.styleInputTag.disabled = true;
				}
				inp.value = "";
			}
			inp.focus();
			return _null;
		}
		this.setCrossBrowserAccessKeyFunctionForAnchor(cssBut);
		
		//MINIMIZE BUTTON 
		var transBut = document.createElement("a");
		transBut.accessKey = "Q";
		transBut.className = "debugButton";
		transBut.innerHTML = "Transparent";
		transBut.value = "Transparent";
		transBut.title = "Transparent: make the debug window almost invisible (Alt-Q)";
		this.transparent = false;
		transBut.onclick = function() {
			if(!self.transparent) {
				debugParent.className = "transparentMode";
				self.setButtonVisualActiveState(transBut,"on");
				self.transparent = true;
			} else {
				self.transparent = false;
				debugParent.className = "";
				self.setButtonVisualActiveState(transBut,"off");
			}
		}
		this.setCrossBrowserAccessKeyFunctionForAnchor(transBut);
		
		//RESIZE BUTTON
		var resizeBut = document.createElement("div");
		resizeBut.id = "debugResizeButton";
		this.minDims = { x:100,y:100 };
		resizeBut.onmousedown = function(e) {
			e = (typeof window.event != "undefined") ? window.event : e;
			originalDims = self.getDimensions(textareaWrap);
			originMouseDims = self.getMouseXY(e);
			document.onmousemove = function(e) {
				newMouseDims = self.getMouseXY(e);
				var newWidth = originalDims.x + (newMouseDims.x - originMouseDims.x);
				if(newWidth < self.minDims.x) { newWidth = self.minDims.x; }
				textareaWrap.style.width = newWidth + "px";
				debugParent.style.width = newWidth + "px";
				
				
				var newHeight = originalDims.y + (newMouseDims.y - originMouseDims.y);
				if(newHeight < self.minDims.y) { newHeight = self.minDims.y; }
				textareaWrap.style.height = newHeight + "px";
				debugParent.style.height = newHeight + "px";
			}
			document.onmouseup = function() {
				document.onmousemove = "";
			}
		}
		
		
		var bottomBar = document.createElement("div");
		bottomBar.id = "debugBottomBar";
		
		//append nodes to DOM
		
		debugParent.appendChild(dragBut);
		
		
		bottomBar.appendChild(clearBut);
		bottomBar.appendChild(evalBut);
		bottomBar.appendChild(closeBut);
		bottomBar.appendChild(helpBut);
		bottomBar.appendChild(domBut);
		bottomBar.appendChild(cssBut);
		bottomBar.appendChild(stackBut);
		bottomBar.appendChild(transBut);
		debugParent.appendChild(bottomBar);
		
		debugParent.appendChild(resizeBut);
		document.body.appendChild(debugParent);
		
		//the textareas should be last to get w/h calculated correctly
		textareaWrap.appendChild(debugOutput);
		textareaWrap.appendChild(inp);
		debugParent.appendChild(textareaWrap);
		
		
		
		
		this.bottomBar = document.getElementById("debugBottomBar");
		this.dragBar = document.getElementById("debugDragBar")
		this.output = document.getElementById("debugOutput");
		this.input = document.getElementById("debugInput");
		this.mainBlock = debugParent;
		
		/* When user scrolls page, move debug window, too */ 
		this.addEvent(window,'scroll',function() {
			debugParent.style.top = 50 + self.getXBrowserYOffset() + 'px';
		});
	}
	this.getXBrowserYOffset = function() {
		var y;
		if (self.pageYOffset) // all except Explorer
		{
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
		{
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			y = document.body.scrollTop;
		}
		return y;
	}
	this.setButtonVisualActiveState = function(button,state) {
		if(state == "on") {
			button.style.backgroundColor = "green";
			button.innerHTML = button.innerHTML.replace(/Off/,"On");
			
		} else {
			button.style.backgroundColor = "";
			button.innerHTML = button.innerHTML.replace(/On/,"Off");
		}
	}
	/***
	* Print some useful information
	***/
	this.help = function() {
		this.print(line);
		this.print("JS DEBUG CONSOLE, Billy Reisinger",true);
		this.print(line);
		this.print("METHODS");
		this.print(line);
		this.print("this.cls() - clear console and terminal");
		this.print("this.print(str,clear) - output str to console ~~ str = string ~~ clear = true|false: clear console before output");
		this.print("this.close() - close this console");
		this.print("this.dump(obj) - output object and members to console");
		this.print("this.show(obj) - print out the names and types (only) of all members of obj");
		this.print("this.stopWatch.start() - start timer");
		this.print("this.stopWatch.stop() - end timer and return result in ms");
		this.print("this.printStackTrace() - print the execution stack of the current function");
		this.print("this.kill(HTML Element) - remove an element from the page.");
		this.print("this.getDimensions(HTML Element) - get width, height dimensions of an html element. Returns an object [x,y]");
		this.print(line);
		this.print("KEYSTROKES");
		this.print(line);
		this.print("press up arrow in input field to retrieve last input");
		this.print("press ESC to show/hide console");
		this.print("press Alt-Q to turn on/off Transparent mode, so you can see through the debugger.");
		this.print("press ENTER in input field to enter a command");
		this.print("press TAB to auto-complete input");
		this.print("press Alt-Z to evaluate input");
		this.print("press Alt-X to activate/deactivate DOM inspector");
		this.print("press Alt-C to clear output and input");
		this.print("press Alt-S to turn on/off CSS input mode. In CSS input mode, you can enter arbitrary CSS selectors and rules, as you would normally do in a CSS stylesheet.");
		
		this.print(line);
		
		return _null;
	}
	/***
	* show/hide debugger
	***/
	this.close = function() {
		if(this.mainBlock.style.display == "none") {
			this.mainBlock.style.display = "block";
		} else {
			this.mainBlock.style.display = "none";
		}
	}
	
	/****
	* Cross-browser access key
	****/
	this.tabIndexIndex = 0;
	this.setCrossBrowserAccessKeyFunctionForAnchor = function(el) {
		var self = this;
		el.tabIndex = ++this.tabIndexIndex;
		//IE only focuses on anchors with access keys, but FF fires click. 
		if(this.browser == "ie") {
			el.onfocus = function() {
				//access key is being used; fire button's click event
				if(window.event.altKey) {
					el.onclick();
				}
				self.input.focus();
			}
		}
		
	}
	
	/***
	* TODO: remove this? 
	***/
	this.collapseOrExpand = function() {
		
	}
	
	/***
	* Time execution in ms
	***/
	this.stopWatch = {
		t_start: 0,
		t_end: 0,
		t_total: 0,
		start: function() { 
			t_start = new Date().getTime();
			return t_start;
		},
		stop: function() {
			t_end = new Date().getTime();
			t_total = t_end - t_start;
			return (t_total);
		}
	}
	/***
	* figure out what functions were calling at a current point 
	***/
	this.printStackTrace = function() {
		var c = this.printStackTrace.caller;
		var outputString = "STACK TRACE:\n";
		while(c) {
			var str = String(c);
			str = str.replace(/[\x0A\x0D]+/g, "")
				.replace(/\s+/g," ")
				.replace(/\x7B.+\x7D/, "{ ... }");
			outputString += str + "\n";
			c = c.caller;
		}
		this.print(outputString);
	}
	/***
	* return obj [x,y] representing mouse position on screen in px
	****/
	this.getMouseXY = function(e) {
		// Temporary variables to hold mouse x-y pos.s
		var tempX = 0
		var tempY = 0
		
		// IE
		if (window.event) { 
		    //doctype present in IE6/7
		    if(document.documentElement && document.documentElement.scrollTop) {
			    tempX = window.event.clientX + document.documentElement.scrollLeft;
			    tempY = window.event.clientY + document.documentElement.scrollTop;
		    } else {
			tempX = window.event.clientX + document.body.scrollLeft;
			tempY = window.event.clientY + document.body.scrollTop;
		    }
		} else {  // grab the x-y pos.s if browser is NS
			tempX = e.pageX;
			tempY = e.pageY;
		} 
		
		return {x:tempX,y:tempY};
	}
	/***
	* return an object [x,y] representing object width, height
	***/
	this.getDimensions = function(el) {
		var dims = {}
		if(document.all) {
			dims.x = el.offsetWidth;
			dims.y = el.offsetHeight;
		} else {
			dims.x = parseInt(document.defaultView.getComputedStyle(el,"").getPropertyValue("width"));
			dims.y = parseInt(document.defaultView.getComputedStyle(el,"").getPropertyValue("height"));
		}
		return dims;
	}
	
	/***
	* Try to complete user input by searching this page's scope
	***/
	this.tabComplete = function(e) {
		if(this.cssEvalFlag) { return false; }
		
		e = (typeof window.event != "undefined") ? window.event : e;
		var inputText = this.input.value;
		
		/* see if input is a dom selector function */
		var match = null;
		if(match = this.searchInputForDomGetElFunctions(inputText)) { 
			this.tabCompleteIdOrClass(match.match[0], match.type);
			return false;
		}
		
		//get last word of input 
		var words = inputText.split(/\s+/);
		var lastWord = words[(words.length - 1)];
		
		var numOpeningParens = lastWord.split("(").length - 1;
		var numClosingParens = lastWord.split(")").length - 1;
		
		var scope;
		var sentinel = 0;
		
		var diff = numOpeningParens - numClosingParens;
		
		if(diff > 0) {
			//how many )'s are after the last ( ?
			numClosingParens = lastWord.split("(")[numOpeningParens].split(")").length - 1;
			//now we can figure out how many )'s we care about
			var numRealDanglers = numOpeningParens - numClosingParens;	
			scope = lastWord.split("(").slice(numRealDanglers).join("(");
		} else if (diff < 0) {
			this.print("error: too many closing parentheses");
			return false;
		} else 	{
			scope = lastWord;
		}
		
		scope = scope.split(".");
		var fragment = scope.pop();
		scope = scope.join(".");
		
		if(scope == "") scope = "window";
		
		var members = this.getMembers(scope);
		var results = this.findTextMatchesInArray(members,fragment);
		
		//no match was found
		if(results == false) {
			//this.print("no match");
			
		//several matches have been found
		} else if(typeof results != "string") {
			this.dump(results);
			
		//one match was found
		} else {
			var reggie = new RegExp(fragment + "$");
			this.input.value = this.input.value.replace(reggie,results);
		}
		
		return false;
	}
	/**
	* scan inputText to determine if a dom get el fct was typed in.  If so, return match
	* and type of match (class or id)
	**/
	this.searchInputForDomGetElFunctions = function(inputText) {
		for(var i = 0; i<this.domGetElFunctions.id.length; i++) {
			selfct = new RegExp(this.domGetElFunctions.id[i].replace("\$","\\\$") + "\\\(['\"]\\w*$");
			if(inputText.match(selfct)) {
			    return { match: inputText.match(selfct), type: "id"};
			}
		}
		for(var i = 0; i<this.domGetElFunctions.className.length; i++) {
			selfct = new RegExp(this.domGetElFunctions.className[i].replace("\$","\\\$") + "\\\(['\"]\\w*$");
			if(inputText.match(selfct)) {
			    return { match: inputText.match(selfct), type: "class"};
			}
		}
	}
	this.findTextMatchesInArray = function(arrayToTest,findMe) {
		var resultsArray = new Array();
		var tester = new RegExp("^" + findMe);
		
		for(var i=0;i<arrayToTest.length;i++) {
			if(tester.test(arrayToTest[i])) {
				resultsArray.push(arrayToTest[i]);
			}
		}
		if(resultsArray.length > 1) {
			resultsArray.sort(); 
			return resultsArray;
		} else if (resultsArray.length == 1) {
			return resultsArray[0];
		} else {
			return false;
		}
	}
	/**
	* scan an object and return just the member names
	**/
	this.getMembers = function(context) {
		var members = new Array();
		for(memberName in eval(context)) {
			members.push(memberName);
		}
		return members;
	}
	
	/**
	* try to complete a partial match of a class or id on this page.
	**/
	this.tabCompleteIdOrClass = function(inputText,type) {
	    
	    //parse out query
	    var query = inputText.split("(");
	    query = query[query.length - 1].replace(/\W/g,'');
	    
	    //loop through dom to find els that match query
	    var matches = new Array();
	    
	    var els = document.getElementsByTagName("*");
	    if(type == "id") {
			for(var i = 0; i<els.length; i++) {
				if(els[i].id && els[i].id.indexOf(query) == 0) {
					matches.push(els[i].id);
				}
			}
		
	    } else if (type == "class") {
			for(var i = 0; i<els.length; i++) {
				if(els[i].className && els[i].className != '' && (els[i].className.indexOf(query) == 0 || query == '')) {
					/* tokenize classes into array */
					var classes = els[i].className.split(/\s/);
					for(var ii = 0; ii < classes.length; ii++) {
						/* prevent duplicate entries */
						if(matches.join("***").indexOf(classes[ii]) == -1) {
							matches.push(classes[ii]);
						}
					}
				}
			}
	    }
	    if(matches.length == 1) {
			this.input.value += matches[0].split(query)[1];
	    } else if (matches.length == 0) {
			this.print("no match");
	    } else {
			this.dump(matches.sort());
	    }
	}
	// Calls 'func' on 'eventName' in 'obj'
	this.addEvent = function(obj, eventName, func) {
		if(obj.addEventListener)
			return obj.addEventListener(eventName, func, true);
		else if(obj.attachEvent) {
			obj.attachEvent("on" + eventName, func);
			return true;
		}
		return false;
	}
	this.findElementPosition = function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
	/***
	* highlights the current node being moused over when DOM inspector is on
	***/
	/* this.highlighter = document.createElement('div');
	this.highlighter.className = "debugHighlighter";
	this.highlighter.innerHTML = "Current Element";
	
	document.body.appendChild(this.highlighter); */
	
	this.highlightNode = function(e) {
		/* if(typeof e == "undefined") e = window.event;
		var el = typeof e.target == "undefined" ? e.srcElement : e.target;
		if(el == document.body) return false;
		if(el.nodeType != 1) return false;
		if(el.className.indexOf("debug") != -1 || el.id.indexOf("debug") != -1) {
			return false;
		}
		
		var dims = this.getDimensions(el);
		var pos = this.findElementPosition(el);
		this.highlighter.style.height = dims.y + "px";
		this.highlighter.style.width = dims.x + "px";
		this.highlighter.style.left = pos[0] + "px";
		this.highlighter.style.top = pos[1] + "px";
		el.appendChild(this.highlighter); */
	}
	this.unHighlightNode = function(e) {
		/* var el = typeof e.target == "undefined" ? e.srcElement : e.target;
		if(el.className.indexOf("debug") != -1 || el.id.indexOf("debug") != -1) {
			return false;
		} */
	}
	/****
	* DOM inspection
	****/
	this.currentNode = {};
	this.showNodes = function(e) {
		if(typeof e == "undefined") e = window.event;
		var el = typeof e.target == "undefined" ? e.srcElement : e.target;
		//store first node for later use
		this.currentNode = el;
		
		//see what first node is
		var childMost = this.identifyNode(el,false);
		
		//step through parent nodes
		var out = "";
		var childmostTxt = "childmost..... " + childMost.txt + "\n";
		while(el = el.parentNode) {
			if(el.nodeName.toLowerCase() == "html") {
				out = "parentmost.... <html>\n" + out;
				break;
			}
			out = this.identifyNode(el).txt + "\n" + out;
		}
		out = "**** PRESS ALT-X TO PAUSE / UNPAUSE ****\n" + out;
		out += childmostTxt;
		this.print(out,true);
		if(!this.cssEvalFlag) {
			if(childMost.id != "") {
				if(typeof $ != "undefined") {
					this.input.value = '$("' + childMost.id + '")';
				} else {
					this.input.value = 'document.getElementById("' + childMost.id + '")';
				}
			} else {
				
				this.input.value = "this.currentNode";
			}
		}
	}
	
	/****
	* returns 
	* {
		txt: string <node class="" id="">,
		id: string elementId
	  }
	 ***/
	this.identifyNode = function(el,showDots) { 
		showDots = typeof showDots == "boolean" ? showDots : true;
		
		var out = {
			txt: "",
			id: ""
		};
		
		if(showDots) out.txt += ".............. ";
		out.txt += "<" + el.nodeName.toLowerCase();
		if(el.id != "") {
			out.id = el.id;
			out.txt += ' id="' + el.id + '"';
		}
		if(el.className !="") {
			out.txt +=  ' class="' + el.className + '"';
		}
		out.txt += ">";	
		return out;
	}
	this.kill = function() {
		this.currentNode.parentNode.removeChild(this.currentNode);
	}
	/****
	* Write style rule in stylesheet
	****/
	this.cssEntryHistory = new Array();
	this.insertStyleRule = function(rule) {
		var lastStyleSheetIndex = document.styleSheets.length - 1;
		if(document.getElementById("debugStyleInput") == null) {
			this.styleInputTag = document.createElement("style");
			this.styleInputTag.id = "debugStyleInput";
			this.styleInputTag.type = "text/css";
			document.body.appendChild(this.styleInputTag);
		}
		if(this.browser != "ie") {
			//wow, I can't believe this works in FF. It shouldn't. 
			this.styleInputTag.innerHTML += rule + "\n";
		} else {
			/* in IE, stylesheets are added to the top of the stack */
			var i = 0; 
			/* create array of rules */
			var rulesArray = rule.split("}");
			for(var t = 0; t < rulesArray.length; t++) {
				var ruleSplit = rulesArray[t].split("{");
				//IE wont take multiple selectors in one rule in addRule.
				var selectors = ruleSplit[0].split(",");
				for(var k = 0; k < selectors.length; k++) {
					document.styleSheets[i].addRule(selectors[k],ruleSplit[1].replace(/}/,""));
				}
			}
		}
		return _null;
	}
	this.evalCss = function(input) {
		try {
			this.insertStyleRule(input);
		} catch (e) {
			this.print(e.message);
		}
		this.cssEntryHistory.push(input + "\n");
		this.print(input);
	}
}

/***************
* PROFILER :: Time execution speeds in page scope 
***************/
function Profiler(func,onFinish) {
	this.func = func;
	
	this.time = 0;
	this.defaultOnFinish = function() {
		//debug.print(this.time);
	}
	this.results = new Array();
	this.onFinish = typeof onFinish != "function" ? this.defaultOnFinish : onFinish;
	var self = this;
	/***
	* do statement reps times in a reverse while loop
	***/
	this.reverseWhile = function(reps) {
		this.stopWatch.start();
		while(reps > 0) {
			this.func();
			reps--;
		}
		return this.stopWatch.stop();
	}
	/***
	* do statement reps times in a for loop
	***/
	this.forLoop = function(reps) {
		this.stopWatch.start();
		for(i=0;i<reps;i++) {
			this.func();
		}
		return this.stopWatch.stop();
	}
	/***
	* controller for loop types 
	***/
	this.loop = function(kind,reps) {
		if(!this.results[kind]) {
			this.results[kind] = new Array();
		}
		var repsMemberName = "r_" + reps;
		if(!this.results[kind][repsMemberName]) {
			this.results[kind][repsMemberName] = new Array();
		}
		
		var time = this[kind](reps);		
		this.results[kind][repsMemberName].push(time);
	}
	/***
	* run func only once
	***/
	this.runOnce = function() {
		if(!this.results.runOnce) {
			this.results.runOnce = new Array();
		}
		this.stopWatch.start();
		func();
		this.results.runOnce.push(this.stopWatch.stop());
	}
	/***
	* Time execution in ms
	***/
	this.stopWatch = {
		t_start: 0,
		t_end: 0,
		t_total: 0,
		start: function() { 
			t_start = new Date().getTime();
			return t_start;
		},
		stop: function() {
			t_end = new Date().getTime();
			t_total = t_end - t_start;
			self.time = t_total;
			return t_total;
		}
	}
	/***
	* return average of numeric array
	***/
	this.average = function(arr) {
		var sum = 0;
		for(i=0;i<arr.length;i++) {
			sum += arr[i];
		}
		return sum / arr.length
	}
	/***
	* run func() passes times in type manner (if type is a loop type, do reps iterations)
	***/
	this.multiPass  = function(passes,type,reps) {
	    	if(typeof type == "undefined") type = "runOnce";
		var self = this;
		if(type == "runOnce") {
			if(passes < 1) {
				self.reportProfile(Math.round(this.average(this.results.runOnce)),type,reps);
			} else {
				window.setTimeout(function() {
					self.runOnce();
					self.multiPass(--passes,type);
				},50);
			}
			
		} else {
		
		    if(passes < 1) {
			    var repsMemberName = "r_" + reps;
			    self.reportProfile(Math.round(this.average(this.results[type][repsMemberName])),type,reps);
		    } else {
			    window.setTimeout(function() {
				    self.loop(type,reps);
				    self.multiPass(--passes,type,reps);
			    },50);
		    }
		}
	}
	this.reportProfile = function(avgMs,type,reps) {
		var line = "-------PROFILER----------------------------------------------";
		var str = line + "\n" + this.func + "\n" + line + "\n";
		str += "Type of profile: " + type + "\n";
		
		if(typeof reps != "undefined") {
			str += "Loop iterations: " + reps + "\n";
		}
		
		str += "Average execution time: " + avgMs + "ms" + "\n";
		
		if(type == "runOnce") {
		    howManyTimes = this.results.runOnce.length;
		} else { 
		    repsMemberName = "r_" + reps;
		    howManyTimes = this.results[type][repsMemberName].length;
		}
		str += "Average calculated from " + howManyTimes + " pass(es)\n";
		str += line + "\n";
		debug.print(str);
	}
}

/****
* import stylesheet
****/
zzzazaa=document.body.appendChild(document.createElement('link'));
zzzazaa.type='text/css';
zzzazaa.rel='stylesheet';
/* use your own stylesheet here if you please */
zzzazaa.href='http://www.billyreisinger.com/tools/debug.css';

/*****
* create debugger 
*****/
var debug;
var createDebug = function() {
	//alert(typeof debug);
	if(typeof debug != "function") {
		debug = new Debugger();
		debug.create();
	} else if(typeof debug.mainBlock == "undefined") {
		debug = new Debugger();
		debug.create();
	}
	if(debug.mainBlock.parentNode == null) {
		debug.create();
	}
}
createDebug();
