/*********************************************************************************************
* 描述: 调试函数，方法，变量的定义
* 作者: weide
* 日期: 2010-6-30 09:17:08
* 邮箱: weide001@gmail.com
* all copyright reserved by weide
*********************************************************************************************/

/**
 * 获取当前执行的函数名称
 * @param caller_arguments: 调用此函数的函数arguments对象
 * @return 函数名称
 */
function _FUNCTION_(caller_arguments)
{
	if (typeof caller_arguments=='undefined' || caller_arguments==null || !caller_arguments) return 'TOP-CALL()';
	var _callee = caller_arguments.callee;
	var _text = _callee.toString();
	var _scriptArr = document.scripts;
	var result = "";
	for (var i=0; i<_scriptArr.length; i++) {
		var _start = _scriptArr[ i].text.indexOf(_text);
		if (_start != -1) {
				if (/^\s*function\s*\(.*\).*\s*\{/.test(_text)) {
					result = "*anonymous-function*()";
					break;
				} else {
					var matches = _text.match(/^\s*function\s*([^\(]+).*\s*\{/);
					if(matches) result = matches[1]+"()"; else result = "*temporary-function*()";
					break;
				}
		}
	}
	if (result==''){
		if (/^\s*function\s*\(.*\).*\s*\{/.test(_text)) {
			result = "*anonymous-function*()";
		} else {
			var matches = _text.match(/^\s*function\s*([^\(]+).*\s*\{/);
			if(matches) result = matches[1]+"()"; else result = "*temporary-function*()";
		}
	}
	return result;
}

/**
 * 获取调用当前函数的呼叫者
 * @param fun 当前函数对象
 * @return 函数名称
 */
function _CALLER_(fun)
{
	var result = '';
	if (typeof fun!='function' || typeof fun.caller=='undefined' || fun.caller==null || !fun.caller) return 'TOP-CALL()';
	var caller = fun.caller.toString();
	if (typeof caller=='undefined' || caller==null || !caller) return 'TOP-CALL()';
	if (/^\s*function\s*\(.*\).*\s*\{/.test(caller)) {
		result = "*anonymous-function*()";
	} else {
		var matches = caller.match(/^\s*function\s*([^\(]+).*\s\{*/);
		if(matches) result = matches[1]+"()"; else result = "*temporary-function*()";
	}
	return result;
}

/**
 * 调试提示
 * @param message_string	可变输出参数
 * @return void
 */
function _DEBUG_()
{
	if( typeof this.DEBUG=='undefined' || this.DEBUG!==true ) return;

	var message_string = "";
	for(var i=0; i<arguments.length; i++) message_string += arguments[i];
	alert( _CALLER_(_DEBUG_) + message_string );
}

/**
 * 定义__FUNCTION__函数
 * @return 当前函数名称
 */
function __FUNCTION__()
{
 	return _CALLER_( __FUNCTION__ );
}

/**
 * 定义__FILE__常量
 * 备注：FireFox, Opara下生效，IE系列无效
 */
(function(){
	try{
		this.__defineGetter__("__FILE__", function() {
			var filepath = (new Error).stack.split("\n")[2].split("@")[1].split(":").slice(0,-1).join(":");
			var matches = filepath.match(/^(http[s]?:\/\/)(.*)$/i);
			if(matches) filepath = matches[2].replace(/\/+|\\+/g,'/'); else filepath = '';
			return matches[1] + filepath;
		});
	}catch(e){
		this.__FILE__ = ''; //__FILE__ unavailable under the IE core browser
	}
})();

/**
 * 定义常量
 */
(
function (){
	this.TRUE	= true;
	this.FALSE	= false;
	this.NULL	= null;
	this.nil	= null;
	this.NIL	= null;
	
	//调试开关
	this.DEBUG	= true;
}
)();

/**
 * 调试信息输出API
 * @param ...	string	调试信息
 * @return void
 */
function debug(){
	if( this.DEBUG!==true ) return false;
	
	var message = "";
	for(var i=0; i<arguments.length; i++){
		arguments[i] += "";
		var array = arguments[i].split('\n');
		for(var j=0; j<array.length; j++) message += array[j]+"<br />";
	}
	var o = this.document.getElementById('debug');
	if( !o ){
		if( typeof this.window_debug=='undefined' || !this.window_debug ){
			var name = "";
			if ( self.name == '' ) name = 'console_debug_window';  else name = 'console_debug_' + self.name;
			var title = "调试: 【标题:" + this.document.title + " 页面地址:" + this.document.location.href + "】";
			this.window_debug = window.open("",name,"width=800,height=600,resizable,scrollbars=yes");
			this.window_debug.document.write( '' +
					'<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n' +
					'<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n' +
					'<head>\n' +
					'	<title>'+title+'</title>' +
					'</head>' +
					'<body style=\"font-size:9pt;\">' +
					'	<div id=\"debug\">' +
					'	</div>' +
					'</body>' +
					'</html>' );
			this.window_debug.document.close();
			var curr_on_closed = new Function("return false;");
			try{
				if(this.document.body.onunload) curr_on_closed = this.document.body.onunload;
				this.document.body.onunload = function(){ this.window_debug.close(); return curr_on_closed(); }
			}catch(ex){
				_DEBUG_(ex.message);
			}
		}
		try{
			o = this.window_debug.document.getElementById('debug');
		}catch(ex){
			this.window_debug = null;
		}
	}

	if(o) o.innerHTML += message;
	return true;
}

