
function debugging_Setup() {
	var debug = document.createElement('div');
	
	debug.style.position = 'absolute';
	debug.className = 'debugging_Window';
	debug.debugLog = '';
	document.debug = debug;

	debug.onmouseover = function() { this.setOpacity(100); }
	debug.onmouseout = function() { this.setOpacity(20); }

	debug.setOpacity = function(opacity) {
		this.style.filter = 'alpha(opacity=' + opacity + ')';
		this.style.opacity = opacity + '%';
		this.style.MozOpacity = (opacity/100) + '';
	}

	debug.alert = function(message) {
		this.msg.innerHTML = message.replace(/</gi,"&lt;").replace(/>/gi,"&gt;").replace(/[\n\f\r]/gi, '<br>');
		this.setOpacity(100);
		if(message != '') {
			this.msg.style.display = 'block';
			debug.clearalert.style.display = 'block';
		} else {
			this.msg.style.display = 'none';
			debug.clearalert.style.display = 'none';
		}
		setTimeout(debugging_Resize, 50);
	}

	debug.log = function(message) {
		this.debugLog += '<div>' + message.replace(/</gi,"&lt;").replace(/>/gi,"&gt;").replace(/[\n\f\r]/gi, '<br>') + '</div>';
	}
	
	debug.showLog = function() {
		if(document.debug.debugLog != '') {
			w = window.open();
			w.document.open();
			w.document.writeln('<html><head><title>Debug Log</title>');
			w.document.writeln('<style>div { font-family: arial; font-size: 12px; padding-bottom: 6px; margin-bottom: 6px; border-bottom: solid 1px black; }</style>');
			w.document.writeln('</head><body>' + document.debug.debugLog + '</body></html>');
			w.document.close();
		} else {
			alert('Nothing has been logged!\n\nUse: document.debug.log(\'your message\');');
		}
	}

	debug.addButton = function(name, clickFunction) {
		var b = document.createElement('div');
		b.className = 'debuggingButton';
		b.innerHTML = name;
		b.onclick = clickFunction;
		this.appendChild(b);
		return b;
	}
	
	debug.usage = function() {
		var s = 'USAGE 1\ndocument.debug.alert(\'Your message here\');\n\n';
		s += '  This will show the message inside the debug\n  window (bottom right of your screen).\n\n';
		s += 'USAGE 2\ndocument.debug.log(\'Your log entry here\');\n\n';
		s += '  This will add your message to the debugger log.\n  Clicking "Show Log" will display the entire log in\n';
		s += '  a new window.  The log is cleared as soon as\n  the page is refreshed (i.e. navigation or post\n  back occurs).\n\n';
		s += 'OTHER INFO\nDouble-click the alert window to expand the size\nof the alert window.  Again to minimise.\n\n';
		s += 'EXAMPLE\nFor an example, click "OK" to view the\ndocument.body mouse x and y co-ordinates.  To\nremove example, refresh, or display this alert\nagian, and click cancel.';
		if(confirm(s)) {
			document.body.onmousemove = function(e) {
				//DebugUsageExample
				if(!e) e = event;
				document.debug.alert('Mouse X: ' + (e.pageX?e.pageX:e.clientX) + '\n' +
									 'Mouse Y: ' + (e.pageY?e.pageY:e.clientY));
			}
		} else {
			document.body.onmousemove = null;
			document.debug.alert('');
		}
	}
	
	var msg = document.createElement('div');
	msg.className = 'debuggingMsg';
	debug.appendChild(msg);
	debug.msg = msg;
	
	msg.size = 'small';
	msg.ondblclick = function() {
		if(this.size == 'small') { this.style.width = '240px'; this.style.height = '200px'; }
		else { this.style.width = '120px'; this.style.height = '100px'; }
		this.size = (this.size == 'big') ? 'small' : 'big';
		debugging_Resize();
	}
	
	debug.setOpacity(20);

	debug.clearalert = debug.addButton('Clear Alert', function() { document.debug.alert('') });
	debug.clearalert.style.display = 'none';

	debug.addButton('<nobr>Show HTML</nobr>', ShowDocumentHTML);	
	debug.addButton('Show Log', document.debug.showLog);
	debug.addButton('Usage', document.debug.usage);
	
	document.body.appendChild(debug);
	debugging_Resize();
}

function debugging_Resize() {
	debug = document.debug;
	
	debug.style.left = general_GetWindowWidth() - debug.offsetWidth - 26;
	debug.style.top = general_GetWindowHeight() - debug.offsetHeight - 8 + document.body.scrollTop;
}

function writeDebugStyles() {
	document.writeln('<style>');
	document.writeln('.debugging_Window { background-color: #ccddee; border: solid 1px black; }');
	document.writeln('.debuggingButton { font-size: 11px; text-align: center; background-color: #ccddee; padding: 0px 4px; color: black; cursor: pointer; border-bottom: solid 1px #666666; }');
	document.writeln('.debuggingMsg { height: 100px; display: none; padding: 2px; color: black; font-size: 10px; border-bottom: solid 1px black; width: 120px; overflow: auto; }');
	document.writeln('</style>');
}

var debugging_Loc = document.location.href.substr(document.location.href.indexOf('/')+2);
if(debugging_Loc.substr(0, debugging_Loc.indexOf('/')).indexOf('.') == -1) {
	writeDebugStyles();
	document.write('<script language=JavaScript src="script/formatcode.js"></script>');
	general_AddEvent(window, 'load', debugging_Setup);
	general_AddEvent(window, 'resize', debugging_Resize);
	general_AddEvent(window, 'scroll', debugging_Resize);
}




