/**************The logging vars en functions ***************************/

/*If set to false, debugging calls will not be logged*/
var trackDebug = false;

/**If set to true, an alert will be launched every time a log statement is made*/
var doAlert = false;

/**The generic window popUper function which imediately makes the variable opener available in the poped up window.
 */
var loggingWindow = '';
var loggingLayer = null;

function openLoggingWindow(){
	if (!loggingWindow.closed && loggingWindow.location){//if the window exists
	}else{
		loggingWindow=window.open('/scripts/loggingWindow.html', 'loggingWindow','height=720,width=900,left=50,top=50,scrollbars,resizable');
		if (!loggingWindow.opener) loggingWindow.opener = self;
	}
	if (window.focus) {
		loggingWindow.focus();
	}
}

/**Called on the unload from the loggingWindow*/
function loggingWindowClosing(){
	loggingWindow = '';
	loggingLayer = null;
}

/**On initiation of the loggingWindow, it will cal this method to return
 * the loging layer variable to write contenet to.
 */
function registerLoggingLayer(layer){
	loggingLayer = layer;
}

//Will write the entire log to the loggingLayer
function outputLog(){
	loggingWindow.logging.logField.value = getLogString('\n');
}

function clearLog(){
	logArray = new Array();
	outputLog();
}

/**Called on persistantFile loading*/
function initLogging(){
	if(trackDebug){//open the window if track debug is enabled
		openLoggingWindow();
	}
}

/*Should be called if once the persistentPage is unloaded*/
function resetLogging(){
	if(!loggingWindow.closed && loggingWindow.location)
		loggingWindow.close();
}


/*An object which represents a logging statement and the time it happened*/
function logStatement(logSort, statement){
	this.logSort = logSort;
	this.statement = statement;
	this.occurenceTime = new Date();
	this.getStatementString = getStatementString;
	if(doAlert)
		alert(this.getStatementString());
	if(!loggingWindow.closed && loggingWindow.location && loggingLayer != null){
		loggingLayer.setContent(this.getStatementString(), true);
	}
}

/*This function will return the occurencetime, logsort and logStatement all as one string.*/
function getStatementString(){
	ret = '[' + this.logSort;
	ret = ret +  ' ' + this.occurenceTime.getFullYear() + '/' + this.occurenceTime.getMonth() + '/' + this.occurenceTime.getDate() + ' ' + this.occurenceTime.getHours() + ':' +  this.occurenceTime.getMinutes() + ':' + this.occurenceTime.getSeconds() + ':' + this.occurenceTime.getMilliseconds();
	ret = ret + '] ' + this.statement;
	return ret;
}

/**This array will hold all debug objects*/
var logArray = new Array();

/*Will loop though all logstatements and return one big string containing all*/
function getLogString(lineBreak){
	ret = '';
	for(i=0;i<logArray.length;i++){
		ret = ret + logArray[i].getStatementString() + lineBreak;
	}
	return ret;
}

/*Constants representing the different logging types*/
var infoConstant = 'INFO';
var debugConstant = 'DEBUG';
var errorConstant = 'ERROR';

/*Call to log a debug message*/
function jsDebug(statement){
	if(trackDebug)
		logArray.push(new logStatement(debugConstant, statement));
}

/*Call to log an info message*/
function jsInfo(statement){
	logArray.push(new logStatement(infoConstant, statement));
}

/*Call to log an error message*/
function jsError(statement){
	logArray.push(new logStatement(errorConstant, statement));
}

/**************End of the logging vars en functions **********************/