

// --------------------------------------------
// Use lower case log to use this
//
// For example: log.info("test");
// --------------------------------------------
var ua = navigator.userAgent.toLowerCase(); 
var isIE = (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1);
var isOpera = (ua.indexOf("opera") != -1)
function Logger() { return(function() { this.initialize.apply(this, arguments); }); }
var TheLog = new Logger();
TheLog.prototype =
	{
	initialize: function(title, debug)
		{
		this.title = title;
		this.debug = debug;
		this.opened = false;
		this.enabled = true;
		this.tries = 0;
		this.window = null;
		this.msgs = []
		},

	enable: function(flag)
		{
		this.enabled = flag;
		},
	open: function()
		{
		try {
		if ((this.tries++ < 10) && ((!this.opened) || (!this.window) || (this.window.closed)))
			{
			if (isIE)
				{
				this.window = window.open("", "Debug");
				if (!this.window)
					{
					this.window = window.open("", "Debug2");
					}
				}
			else
				{
				//this.window = window.open("", "" + window.location, "width=1500,height=1000,scrollbars=yes,resizable=yes,status=no,location=no,menubar=no,toolbar=no");
				this.window = window.open("", "Debug", "width=1500,height=1000,scrollbars=yes,resizable=yes,status=no,location=no,menubar=no,toolbar=no");
				}
			if (!this.window)
				{
				return;
				}
			this.doc = this.window.document;
			this.doc.write("<html><head><title>" + this.title + "</title>"
				+ "<style type=\"text/css\"> .debug-line-class { font-size: 1.0em; font-weight: bold; font-family: arial, \"lucida console\", sans-serif; } </style>"
				+ "</head><body></body></html>");
			this.doc.close();
			this.lineNumber = 1;

			// No infinite loops please
			this.debug = false;
			for (var i = 0; i < this.msgs.length; ++i)
				{
				var m = this.msgs[i];
				this.log(m.msg, m.color, m.size);
				}
			this.msgs = [];
			this.debug = true;
			}
		    this.opened = this.doc != null;
		    }
		catch(e) {}

		},
	close: function()
		{
		if (this.window)
			{
			this.window.close();
			this.window = null;
			this.doc = null;
			}
		this.opened = false;
		},

	error: function(msg, error)
		{
		if (error != null)
			{
			for (var parm in error)
				{
				try	{
					msg += "[" + parm + "=" + ("" + error[parm]).truncate(100) + "]";
					}
				catch (e) {}
				}
			msg += " " + error;
			}
		this.log(msg, "#FF0000");
		},
	big: function(msg)
		{
		this.log(msg, "#0000ff", "1.3em");
		},
	green: function(msg)
		{
		this.log(msg, "#008800", "1.3em");
		},
	red: function(msg)
		{
		this.log(msg, "#bb0000", "1.3em");
		},
	blue: function(msg)
		{
		this.log(msg, "#0000ff", "1.1em");
		},
	magenta: function(msg)
		{
		this.log(msg, "#ea00ff", "1.1em");
		},
	info: function(msg)
		{
		this.log(msg);
		},
	date: function(msg)
		{
		this.log("[" + new Date().toString() + "]" + msg);
		},
	html: function(_html)
		{
		if (_html)
			{
			var str = _html.replace(/</g, '&lt;');
			str = str.replace(/>/g, '&gt;');
			// str = str.replace(/\n/g, '<br/>');
			this.info(str);
			}
		else
			{
			this.red("HTML UNDEFINED");
			}
		},
	path: function(e)
		{
		var str = "";
		var hier = [];
		while (e)
			{
			hier.push(e);
			e = e.parentNode;
			}

		var indent = "";
		while (hier.length > 0)
			{
			e = hier.pop();
			str += indent + e.nodeName + "[" + (e.id ? e.id : '') + "]<br/>";
			indent += "..";
			}
		this.info(str);
		},
	dump: function(o)
		{
		this.info(this.dumpObject(o));
		},
	dumpObject: function(o)
		{
		var str = "";
		if (typeof o == 'object')
			{
			str += "object={";
			var comma = "";
			for (var name in o)
				{
				str += comma + name + "=" + o[name];
				comma = ",";
				}
			str += "}";
			}
		else
			{
			str = "Not an object. Is a " + (typeof o);
			}
		return(str);
		},
	dumpTree: function(e, indent, all)
		{
		if (all || typeof e.offsetWidth != "undefined")
			{
			var id = e.id ? e.id : "noid";
			var name = e.nodeName;
			var classname = e.className ? "(" + e.className + ")" : "";
			var value = "";
			if (name == "P")
				{
				value = this._calcTextOfPara(e, "");
				}

			if (value.length > 0)
				{
				value = "=" + value;
				}

			this.log(indent + e.id + classname + "[" + name + value +"] ( " + this.ss(e.offsetWidth) + "  x  " + this.ss(e.offsetHeight) 
				+ (e.style ? ", fontsize:" + Element.getStyle(e, 'fontSize') + ",family:" + Element.getStyle(e, 'fontFamily') + ",weight:" + Element.getStyle(e, 'fontWeight') : "")
				+ " )", "#00f", "1.4em");
			}
		if (e.childNodes)
			{
			for (var i = 0; i < e.childNodes.length; ++i)
				{
				this.dumpTree(e.childNodes[i], indent + "&nbsp;&nbsp;&nbsp;&nbsp;", all);
				}
			}
		},
	bounds: function(e, msg)
		{
		if (typeof e == 'String')
			{
			// No worky
			var t = e;
			e = msg;
			msg = t;
			}
		if (!msg)
			{
			msg = "";
			}

		var id = e.id ? e.id : "noid";
		var name = e.nodeName;
		var classname = e.className ? "(" + e.className + ")" : "";
		var value = "";
		if (name == "P")
			{
			value = this._calcTextOfPara(e, "");
			}

		if (value.length > 0)
			{
			value = "=" + value;
			}
		this.log(msg + " " + id + classname + "[" + name + value +"] ( " + this.ss(e.offsetLeft) + "," + this.ss(e.offsetTop) + " )( " + this.ss(e.offsetWidth) + "  x  " + this.ss(e.offsetHeight) 
				+ (e.style ? ", fontsize:" + Element.getStyle(e, 'fontSize') + ",family:" + Element.getStyle(e, 'fontFamily') + ",weight:" + Element.getStyle(e, 'fontWeight') : "")
				+ " )", "#00f", "1.4em");
		},

	_calcTextOfPara: function(e, value)
		{
		if (e.childNodes)
			{
			for (var i = 0; i < e.childNodes.length; ++i)
				{
				if (e.childNodes[i].nodeName == "#text")
					{
					value += e.childNodes[i].nodeValue.truncate(10);
					}
				else
					{
					value += this._calcTextOfPara(e.childNodes[i]);
					}
				}
			}
		return(value);
		},

	ss: function(val)
		{
		return(("" + val).substring(0, 4));
		},
	log: function(msg, color, size)
		{
		if (!this.enabled)
			{
			return;
			}
		// force open
		if (!this.opened)
			{
			this.open();
			}
		if (this.opened && this.doc)
			{
			var line = this.doc.createElement("div");
			line.className = "debug-line-class";
			line.style.color = color;
			if (size)
				{
				line.style.fontSize = size;
				}
			//line.appendChild(this.doc.createTextNode("line: " + this.lineNumber++ + " - " + msg));
			line.innerHTML = "line: " + this.lineNumber++ + " - " + msg;
			this.doc.body.appendChild(line);
			this.window.scroll(0, isOpera ? this.doc.body.scrollHeight : (document.all ? this.doc.body.scrollHeight : this.doc.height));
			}
		else if (this.debug)
			{
			this.msgs.push({ msg: msg, color: color, size: size });
			}
		},

	printStackTrace: function(msg)
		{
		this.log(msg + this._printStackTrace().join('<br/>'));
		},

	_printStackTrace: function()
		{
		if (!this.mode)
			{	
			try {(0)()} catch (e)
				 {
    				this.mode = e.stack ? 'Firefox' : window.opera ? 'Opera' : 'Other';
				}
			}

		switch (this.mode)
			{
    			case 'Firefox' : 
        			try {(0)()} catch (e)
					{
            				return e.stack.replace(/^.*?\n/,'').
                           			replace(/(?:\n@:0)?\s+$/m,'').
                           			replace(/^\(/gm,'{anonymous}(').
                           			split("\n");
        				}

    			case 'Opera' : 
				try {(0)()} catch (e)
					{
            				var lines = e.message.split("\n"),
                				ANON = '{anonymous}',
                				lineRE = /Line\s+(\d+).*?in\s+(http\S+)(?:.*?in\s+function\s+(\S+))?/i,
                				i,j,len;

            				for (i=4,j=0,len=lines.length; i<len; i+=2)
						{
                				if (lineRE.test(lines[i]))
							 {
                    					lines[j++] = (RegExp.$3 ? RegExp.$3 + '()@' + RegExp.$2 + RegExp.$1 : ANON + RegExp.$2 + ':' + RegExp.$1) + ' -- ' + lines[i+1].replace(/^\s+/,'');
                					}
            					}

            				lines.splice(j,lines.length-j);
            				return lines;
        				}

			default :
				{
        			var curr  = arguments.callee.caller,
            			FUNC  = 'function', ANON = "{anonymous}",
            			fnRE  = /function\s*([\w\-$]+)?\s*\(/i,
            			stack = [],j=0,
            			fn,args,i;

        			while (curr)
					{
            				fn    = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
            				args  = stack.slice.call(curr.arguments);
            				i     = args.length;

            				while (i--)
						{
                				switch (typeof args[i])
							 {
                    					case 'string'  : args[i] = '"'+args[i].replace(/"/g,'\\"')+'"'; break;
                    					case 'function': args[i] = FUNC; break;
                					}
            					}

            				stack[j++] = fn + '(' + args.join() + ')';
            				curr = curr.caller;
        				}

        			return stack;
    				}
			}
		}

	}
var log = new TheLog();
log.initialize("Debug", true);



function ALog() { return(function() { this.initialize.apply(this, arguments); }); }
var Log = new ALog();
Log.prototype = {
	initialize: function(enabled)
		{
		this.enabled = (typeof enabled == 'undefined' ? true : enabled);
		},

	enable: function(flag)
		{
		this.enabled = flag;
		},
	printStackTrace: function(msg)
		{
		if (this.enabled) { log.printStackTrace(msg); }		
		},
	big: function(msg)
		{
		if (this.enabled) { log.big(msg); }		
		},
	green: function(msg)
		{
		if (this.enabled) { log.green(msg); }	
		},
	red: function(msg)
		{
		if (this.enabled) { log.red(msg); }	
		},
	blue: function(msg)
		{
		if (this.enabled) { log.blue(msg); }	
		},
	magenta: function(msg)
		{
		if (this.enabled) { log.magenta(msg); }	
		},
	info: function(msg)
		{
		if (this.enabled) { log.info(msg); }	
		},
	date: function(msg)
		{
		if (this.enabled) { log.date(msg); }	
		},
	html: function(_html)
		{
		if (this.enabled) { log.html(_html); }	
		},
	path: function(e)
		{
		if (this.enabled) { log.html(e); }
		},
	bounds: function(e, msg)
		{
		if (this.enabled) { log.bounds(e, msg); }
		},
	dump: function(o)
		{
		if (this.enabled) { log.dump(o); }
		},
	dumpObject: function(o)
		{
		if (this.enabled) { return(log.dumpObject(o)); }
		},
	dumpTree: function(o, i)
		{
		if (this.enabled) { return(log.dumpTree(o, i)); }
		},
	log: function(msg, color, size)
		{
		if (this.enabled)
			{
			log.log(msg, color, size);
			}	
		}
	};

//var alog = new Log(false);


