/**
 * @author Timo 9.09.2005
 */
var f_f=String.fromCharCode;

function Log(){  
	afterCatch=function(eVent){
		if(Log.debugMessage!=null){
			//throw 
			//error=new Error(Log.debugMessage);
			error=new Error();
			Log.showExeption(error, 'Log.afterCatch, '+Log.showTrackTrace(1));
			Log.debugMessage=null;
		}
		Log.eventTool.stopEvent(eVent);
	}

	/** 
	 * a task to do in a thread
	 */    
	getTask=function(s_timerString,functionName,i_interval,i_index){
		var s_task=s_timerString
		.replace('@1',functionName)
		.replace('@2',i_interval)
		.replace('@3',i_index)
		; //'timeLoop("@1",@2,@3);'
		return s_task;
	}//getTask
	/** 
	 * to loop a thread after a determined time interval
	 */    
	timeLoop=function(functionName,i_interval,i_index,eVent,startMessage,endMessage){
		try{
			if(i_index==null||isNaN(i_index)){
				i_index=Log.arrayTool.getFirstVacancy(Log._aIntervals);
				// }else{clearInterval(Log._aIntervals[i_index]);
			}
			//set interval to repeat the task
			if(!isMemorized(functionName)){
				var s_task=getTask(timeLoop.timerString,functionName,i_interval,i_index);
				Log._aIntervals[i_index]=setInterval(s_task,i_interval);
				remember(functionName);
			}//if(!isMemorized
			else{
				//document.title=(' Log.timeLoop i_interval:'+i_interval+'| functionName:'+functionName);
			}//if(!isMemorized
			//fulfill the task
			if(busy()!=true){
				tryOnce(functionName,null);
			}//if(busy()
		}catch(error){Log.showExeption( error, 'Log.timeLoop ,'+functionName+', ');}
		afterCatch(eVent);
		return Log._aIntervals[i_index];
	}//timeLoop
	timeLoop.memory='';
	timeLoop.timerString='timeLoop("@1",@2,@3);';

	/** 
	 * to timeout threads
	 */    
	timeThread=function(functionName,i_interval,i_index,eVent,startMessage,endMessage, b_show){
		try{
			if(i_index==null||isNaN(i_index)){
				i_index=Log.arrayTool.getFirstVacancy(Log._aTimeOuts);
			}else{
				clearTimeout(Log._aTimeOuts[i_index]);
			}
			if(busy()!=true){
				//fulfill the task
				lock(functionName,startMessage,endMessage,eVent, b_show);
				//don't repeat the task
				Log._aTimeOuts[i_index]=null;
				forget(functionName);
			}else if(!isMemorized(functionName)){
				//set interval to wait with the the task
				var s_task=getTask(timeThread.timerString,functionName,i_interval,i_index);
				Log._aTimeOuts[i_index]=setTimeout(s_task,i_interval);
				remember(functionName);
			}
		}catch(error){Log.showExeption( error, 'Log.timeThread ,'+functionName+', ');}
		afterCatch(eVent);
		return Log._aTimeOuts[i_index];
	}//timeThread
	timeThread.memory='';
	timeThread.timerString='timeThread("@1",@2,@3);';

	isMemorized=function(s_input){
		return timeThread.memory.indexOf(s_input)!=-1;
	}//isMemorized

	remember=function(s_input){
		timeThread.memory+=s_input;
	}//remember

	forget=function(s_input){
		timeThread.memory.replace(s_input,'');
	}//forget
	/**
	 * for time consuming tasks use lock
	 */    
	tryOnce=function(functionName,eVent){
		var result=null;
		try{
			if(busy()!=true){
		//document.title=('tryOnce functionName:'+functionName);
				var f_unction =new Function('return '+functionName);
				result=f_unction();
			}
		}catch(error){Log.showExeption( error, 'Log.tryOnce ,'+functionName+', ');}
		afterCatch(eVent);
		return result;
	}//tryOnce

	/**
	 * For private use only
	 * Allow one thread only
	 * for time consuming tasks only otherwise use tryOnce
	 */    
	lock=function(functionName,startMessage,endMessage,eVent, b_show){
		//a lert(' Log functionName:'+functionName);
		var result=null;
		try{
			if(lock.done==true){
				setStatus(startMessage, true, b_show);
				var f_unction =new Function('return '+functionName);
				result=f_unction();
				//eval(functionName); // fails in Firefox
				setStatus(endMessage, false, b_show);
			}
		}catch(error){
			setStatus(endMessage, false, b_show);
			Log.showExeption( error, 'Log.lock ,'+functionName+', ');
		}
		afterCatch(eVent);
		//a lert(' Log.lock result:'+result);

		return result;
	}//lock
	lock.done=true;

	breakLock=function(functionName,startMessage,endMessage,eVent, b_show){
		var result=null;
		lock.done=true;
		result=lock(functionName,startMessage,endMessage,eVent, b_show);
		return result;
	}//breakLock 

	busy=function(){
		//al ert(' Log !lock.done:'+!lock.done);
		return !lock.done;
	}//busy

	setStatus=function(phraseId, b_busy, b_show){
		lock.done=!b_busy;
		var s_cursor='auto';
		var e_toolTip=document.getElementById(Log._sTooltipId);
		//showMessage the log while the browser is busy with a task
		if(phraseId!=''&&phraseId!=null&&b_show!=false/**/){
			showMessage(phraseId);
			if(b_busy==true){
				s_cursor='wait !important';
				if(e_toolTip!=null){
					Log.domTool.show(e_toolTip);
				}//if(e_toolTip
			}//if(b_busy
		}
		//hide the log when the task is completed
		if(b_busy!=true){
			if(e_toolTip!=null){
				Log.domTool.hide(e_toolTip);
			}//if(e_toolTip
		}//if(b_busy
		document.body.style.cursor=s_cursor;
	}//setStatus

	showMessage=function(phraseId){
		phraseId=Log.getPhrase(phraseId);
		if(phraseId!=''&&phraseId!=null){
			window.defaultStatus=phraseId;
			if(page!=null){
				if(Log.mouseOnBody==true){
					document.title=phraseId;
				}else{
					resetTitle();
				}//if(
				var e_toolTip=document.getElementById(Log._sTooltipId);
				if(e_toolTip!=null){
					if(Log._sToolTip==null){
						Log._sToolTip=e_toolTip.innerHTML;
					}
					e_toolTip.innerHTML=phraseId+"<br/>"+Log._sToolTip;
				}
			}//if(page
		}//if(phraseId
	}//showMessage

	/**
	 * Ensure that the title matches with the file name, when the page is saved
	 */
	resetTitle=function(){
		document.title=page._sName;
	}//resetTitle
}
/**
 * PUBLIC FUNCTIONS & VARIABLES
 */
Log.sJsBreak=String.fromCharCode(13,10);
Log._sTooltipId="H15";
Log._sToolTip=null;
Log._aTimeOuts=new Array();
Log._aIntervals=new Array();
Log._aPhrases=new Array();
Log._bPublic=true;
Log.mouseOnBody=false;
if(location.href.indexOf('/localhost/')!=-1){Log._bPublic=false;}

Log.getPhrase=function(phraseId,result){
	if(result==null){
		result='';
	}
	if(phraseId!=null){
		var index=parseInt(phraseId);
		if(!isNaN(index)){
			if(Log._aPhrases==null||index>Log._aPhrases.length){
				//result=phraseId;
			}else{
				result=Log._aPhrases[index];
			}//if(Log._aPhrases
		}//if(isNaN(index
	}//if(phraseId
	return result;
}//getPhrase

Log.showExeption=function(error, functionName){
	var errorText=functionName+Log.getErrorText(error);
	document.title=errorText;
	window.defaultStatus=errorText;
	if(Log._bPublic==false){var s_prompt=prompt(' Log.showExeption :', ' Log.debugMessage:'+Log.debugMessage+errorText);}
	//may cause an exception if(Log._bPublic==false){var s_prompt=prompt(Log._aPhrases[1], errorText);}
}//showExeption

Log.getErrorText=function(error){
	var result='| message:'+error.message+'| ,  filename:'+error.filename+'| lineNumber:'+error.lineNumber;
	return result;
}//getErrorText

Log.throwError=function(error, sMessage){
	error.message+="\n"+sMessage;
	throw (error);
}//throwError

/*
Log.prototype.showExeption=function(error, functionName){
	Log.showExeption(error, functionName);
}//showExeption

/**
 * If the main thread is still busy with a heavy task
 */
Log.prototype.busy=function(){
	return busy();
}//busy
/**
 * to timeout threads
 */    
Log.prototype.timeThread=function(v_this,functionName,i_interval){
	timeThread(v_this,functionName,i_interval);
}//timeThread
/**
 * for time consuming tasks use lock
 */    
Log.prototype.tryOnce=function(functionName){
	tryOnce(functionName);
}//tryOnce

Log.prototype.setStatus=function(phraseId, b_busy, b_show){
	setStatus(phraseId, b_busy, b_show);
}//setStatus

Log.prototype.resetTitle=function(){
	resetTitle();
}//resetTitle

Log.task=new Log();//Must be the only instance of Log class
/**
 * set focus to the default input
 */
Log.task.resetFocus=function(){
	self.focus();
}//resetFocus

/**
 * Keep the menu in focus while scrolling the page
 */
Log.setScrollTime=function(){
	return timeLoop('Log.positioner.unScroll(Log.positioner._eUnScroll);',10,null);
}//setScrollTime

/**
 * showMessage tractrace to find the way the line was called from
 */
Log.forceError=function(){
	var vNull=null;
	vNull=vNull.vNull;
	//fails throw new Error('Log.forceError');
}//forceError

/**
 * showMessage tractrace to find the way a certain line was called from
 */
Log.showTrackTrace=function(needText){
	try{
		Log.forceError();
	}catch(error){
			if(needText!=null){
				return Log.getErrorText(error);
			}else{
				Log.showExeption( error, 'Log.showTrackTrace :');}
			}//if(Log._aPhrases
}//showTrackTrace

Log.trackText=function(trackText){
	prompt("Log.trackText"
	,
	trackText+" \n trackTrace:"+Log.showTrackTrace(1)
	);
}//trackText

Log.debug=function(text,callerName){
	if(callerName==null){callerName=Log.showTrackTrace(1);}
	Log.debugMessage+=Log.sJsBreak+Log.sJsBreak+callerName+':'+text;	
}//debug
