/***************************************************************************
# File: lib_debugger.js - JavaScript Debugger
# Version: 1.2.3
***************************************************************************/

/**** Window Error Functions ***
if(window.bjs_debugger_flag != undefined)
{
	ob_win_bjs = new JS_Debug();
	window.onerror = ob_win_bjs.ThrowScriptError;
}
*/


/************ JavaScript Debugger ************/
function JS_Debug()
{
	if(this.new_document == undefined)
	{
		this.current_year = '';
		this.current_month = '';
		this.current_date = '';
		this.current_hour = '';
		this.current_minute = '';
		this.current_second = '';
		this.current_milliseconds = '';
		this.am_pm = '';

		window_options = 'toolbar=no,location=no,resizable=yes,scrollbars=yes,menubar=no,width=600,height=500';
		new_window = window.open('', 'newWin', window_options);
		
		this.new_document = new_window.document;
		
		this.new_document_begin = '<html><head><title>Bourbon JavaScript Debugger</title><style type="text/css" media="screen">@import "' + base_directory_css + 'js_debugger.css";</style></head><body>';
		this.new_document_content = '<h1>Bourbon JavaScript Debugger &raquo; ' + this.GetDateNow() + ' ' + this.GetTimeNow() + '</h1><div id="bjs_debug_box"></div>';
		this.new_document_end = '</body></html>';
		
		this.new_document.write(this.new_document_begin + this.new_document_content + this.new_document_end);
		this.new_document.close();
	}

	this.bjs_debug_box_id = this.new_document.getElementById("bjs_debug_box");
	
	return this;
}


// Method -- Add an item to the debug window
JS_Debug.prototype.AddItem = function(in_line)
{
	ob_win_bjs.bjs_debug_box_id.innerHTML = ob_win_bjs.bjs_debug_box_id.innerHTML.replace(new RegExp('<ol>', 'gi'), '');
	ob_win_bjs.bjs_debug_box_id.innerHTML = ob_win_bjs.bjs_debug_box_id.innerHTML.replace(new RegExp('</ol>', 'gi'), '');
	ob_win_bjs.bjs_debug_box_id.innerHTML = ob_win_bjs.bjs_debug_box_id.innerHTML.replace(new RegExp(' id="current_li"', 'gi'), '');
	
	in_line = in_line.replace(new RegExp('<', 'gi'), '&lt;');
	in_line = in_line.replace(new RegExp('>', 'gi'), '&gt;');

	bjs_debug_box_html  = "<ol>";
	bjs_debug_box_html += '<li id="current_li"><span id="debugg_time">' + ob_win_bjs.GetTimeNow() + '</span> ' + in_line + "</li>";
	bjs_debug_box_html += ob_win_bjs.bjs_debug_box_id.innerHTML;
	bjs_debug_box_html += "</ol>";
	
	ob_win_bjs.bjs_debug_box_id.innerHTML = bjs_debug_box_html;
}


// Method -- Get the current date
JS_Debug.prototype.GetDateNow = function()
{
	this.obj_date = new Date();

	this.current_year = this.obj_date.getFullYear();
	this.current_month = this.obj_date.getMonth();
	this.current_month++;
	this.current_date = this.obj_date.getDate();
	
	return this.current_year + "-" + this.current_month + "-" + this.current_date;
}


// Method -- Get the current time
JS_Debug.prototype.GetTimeNow = function()
{
	this.obj_date = new Date();

	this.current_hour = this.obj_date.getHours();
	
	if(this.current_hour < 12)
		this.am_pm = "am";
	else
		this.am_pm = "pm";
	
	if(this.current_hour == 0)
		this.current_hour = 12;
	
	if(this.current_hour > 12)
		this.current_hour = this.current_hour - 12;
	
	this.current_minute = this.obj_date.getMinutes();
	this.current_minute = this.current_minute + '';
	if(this.current_minute.length == 1)
		this.current_minute = "0" + this.current_minute;
	
	this.current_second = this.obj_date.getSeconds();
	this.current_second = this.current_second + '';
	if(this.current_second.length == 1)
		this.current_second = "0" + this.current_second;
	
	this.current_milliseconds = this.obj_date.getMilliseconds();
	this.current_milliseconds = this.current_second + '';
	if(this.current_milliseconds.length == 1)
		this.current_milliseconds = "0" + this.current_milliseconds;
	
	return this.current_hour + ':' + this.current_minute + ':' + this.current_second + ':' + this.current_milliseconds + " " + this.am_pm;
}


// Method -- Throw a JavaScript error
JS_Debug.prototype.ThrowScriptError = function(in_msg, in_url, in_line)
{
	ob_win_bjs.AddItem("~~ JavaScript Error: " + in_msg + " ~~ Line: " + in_line + " ~~ URL: " + in_url);

	return true;
}

