if (typeof window.RadControlsNamespace == "undefined")
{
	window.RadControlsNamespace = {};
}

if (
	typeof(window.RadControlsNamespace.Log) == "undefined" ||
	typeof(window.RadControlsNamespace.Log.Version) == null ||
	window.RadControlsNamespace.Log.Version < 1
	)
{
	
	
	RadControlsNamespace.LL = 
	{
		DEBUG : 4,
		INFO  : 3,
		WARN  : 2,
		ERROR : 1,
		NONE  : 0
	}
	
	RadControlsNamespace.Log = function (ll, handleJSErrors) 
	{
		
		this.Version = 1; // Change the version when make changes. Change the value in the IF also
			
		this.LogLevel = ll;
		
		var self = this;
		this.__debug_console_buffer = '';
		if (this.LogLevel != RadControlsNamespace.LL.NONE && handleJSErrors)
		{
			window.onerror = function (sMessage, sUrl, iLine) {self.Error(['JS error. Msg[%s], url[%s], line[%d]',sMessage, sUrl, iLine]); return false;};		
		}
		
	 	this.Write = function (args, ll) {
	
			try {
				
				this.__debug_console_element_id = '__debug_console__';
		
				if (ll > this.LogLevel) return;
				
				// log the date
				var d = new Date();
				var dateStr = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "." + d.getMilliseconds();
			
				if (typeof(console) != 'undefined') { // firebug
			
					for (var i=0;i<args.length;i++) {
						args[i] = '"' + args[i] + '"';
					}
			
					args[0] = '"[' + dateStr + '] ' + args[0].substr(1);
			
					var str = args.join(',');
					var consoleMethod = 'info';
					switch (ll) {
						case RadControlsNamespace.LL.DEBUG :
							consoleMethod = 'debug';
							break;
						
						case RadControlsNamespace.LL.WARN :
							consoleMethod = 'warn';
							break;
							
						case RadControlsNamespace.LL.ERROR :
							consoleMethod = 'error';
							break;
							
						case RadControlsNamespace.LL.INFO :
						default :
							consoleMethod = 'info';
					}
					
					eval('console.' + consoleMethod + '(' + str + ')')
				
				} else {
					
					var consoleElement = document.getElementById(this.__debug_console_element_id);
					if (consoleElement && consoleElement.style.display == 'none') return;
					
					args[0] = '[' + dateStr + '] ' + args[0];
					
					// parse the string
					var str = args[0];
		
					for( var i=1; i < args.length; i ++)
					{
						str = str.replace(/\%\w/, '<b>' + args[i] + '</b>');
					}
					
					var debug_str = str;
					debug_str = debug_str.replace(/\\n/, "<br>");
					
					
					switch (ll)
					{
						case RadControlsNamespace.LL.ERROR :
							debug_str = '<font style="background-color:#ff0000">' + debug_str + '</font>';
						case RadControlsNamespace.LL.WARN :
							debug_str = '<font style="background-color:#ff9999">' + debug_str + '</font>';
					}
					
					// buffer the str until the page is loaded
					if (document.readyState != 'complete') {
						this.__debug_console_buffer = debug_str + '<br>' + this.__debug_console_buffer;
						return;
					}
					
					// build the UI
						
					if (consoleElement == null) {
						var _tmp = document.createElement('DIV');
						var left = 0;
						var top = document.body.clientHeight - 215;
						_tmp.innerHTML = '<a href="javascript:void(null)" onclick = "javascript: var left = document.getElementById(\'' + this.__debug_console_element_id + '_left\').value;var t = document.getElementById(\'' + this.__debug_console_element_id + '\').parentElement;t.style.left=left;var r=5;">left</a>:<input id="' +this.__debug_console_element_id + '_left" style="width:30;height:10;font-size:9px;" value="' + left + '"> <a href="javascript:void(null)" onclick = "javascript: var top = document.getElementById(\'' + this.__debug_console_element_id + '_top\').value;var t = document.getElementById(\'' + this.__debug_console_element_id + '\').parentElement;t.style.top=top;var r=5;">top</a>: <input id="' + this.__debug_console_element_id + '_top" style="width:30;height:10;font-size:9px;" value="' + top + '"> <a href = "javascript:var t = document.getElementById(\'' + this.__debug_console_element_id + '\').innerHTML = \'\'">clear</a> <a href = "javascript:var t = document.getElementById(\'' + this.__debug_console_element_id + '\').style.display = \'none\'">hide</a> <a href = "javascript:var t = document.getElementById(\'' + this.__debug_console_element_id + '\').style.display = \'\'">show</a> <a href = "javascript:var t = document.getElementById(\'' + this.__debug_console_element_id + '\').parentElement.style.display=\'none\'">close</a> ';
			
						_tmp.style.width = '1000';
						_tmp.style.height = 200;
						_tmp.style.position = 'absolute';
						_tmp.style.font = '10px Verdana';
						_tmp.style.lineHeight = '1.5';
						_tmp.style.top = top;
						_tmp.style.left = left;
						_tmp.style.textAlign = "left";
						_tmp.style.border = '2px solid #aaaaaa';
						_tmp.style.backgroundColor = '#dddddd';
			
						consoleElement = document.createElement('DIV');
						consoleElement.id = this.__debug_console_element_id;
						consoleElement.style.width = '100%';
						consoleElement.style.height = 200;
						consoleElement.style.overflow = 'auto';
						consoleElement.style.border = '1px solid #aaaaaa';
						consoleElement.style.textAlign = 'left';
						
						consoleElement.style.backgroundColor = '#eeeeee';
						_tmp.appendChild(consoleElement);
						document.body.appendChild(_tmp);
					}
					
					if (this.__debug_console_buffer) {
						this.__debug_console_buffer = '<br><b>------------- FROM BUFFER (before onload) -------------</b><br>' + this.__debug_console_buffer;
					}
					
					// display the string
					consoleElement.innerHTML = debug_str + '<br>' + consoleElement.innerHTML.substr(0, 10000) + this.__debug_console_buffer;
					this.__debug_console_buffer = '';
				}
			} catch (e) {
				if (currentLL == RadControlsNamespace.LL.DEBUG) alert("this.Debug.js error ->" + e)
				// we dont need js errors from this lib
			}
		}
	
		this.Debug = function (args) 
		{
			this.Write(args, RadControlsNamespace.LL.DEBUG);
		}
	
		this.Error = function (args) 
		{
			this.Write(args, RadControlsNamespace.LL.ERROR);
		}
	
		this.Warning = function (args) 
		{
			this.Write(args, RadControlsNamespace.LL.WARN);
		}
	}
}



