/*************************************************************************
Name:	log.js
Desc:	debug logger

TODO:
	need delay after creating window - cannot be used 'instantly'
************************************************************************/
	

var myLogWindow;
var myLogTimer;
var myLogWindowOdd = false;
var myLogTimeLast = new Date();


//create objects needed for log
function createLogWindow() {
	myLogWindow = createWindow({	
		name:'debugwindow',
		left:50,
		width:500,
		height:800,
		menubar:false,
		location:false,
		status:false,
		resizable:true,
		scrollbars:true,
		toolbar:false
	});
	
	var output = '';
	output += '<html>';
	output += '<link rel="stylesheet" type="text/css" href="' + DOM_SCRIPT_PATH + '/log/logstyle.php" >';
	output += '<scr' + 'ipt>function log_showhide(id) {document.getElementById(id).style.display = (document.getElementById(id).style.display == "none") ? "block" : "none";}</scr' + 'ipt>';
	output += '<body>';
	output += '<table>';
	//output += '<div id="content"></div>';
	//output += '</body>';
	//output += '</html>';
	
	//var style = myLogWindow.document.createElement('HTML');
	myLogWindow.document.write(output);
	
	
	myLogTimer = new TimerJS();
	myLogTimeLast = myLogTimer.getElapsed();
}

function createLogRow(time, inc, owner, value, title) {
	myLogWindowOdd = !myLogWindowOdd;
	
	var style = (title == true) ? 'title' : ((myLogWindowOdd) ? 'odd' : 'even');
	var incstyle = '';
	if (is_numeric(inc)) {
		if (inc <= 50) {
			incstyle = 'incgood';
		} else if (inc <= 100) {
			incstyle = 'incmed';
		} else {
			incstyle = 'incbad';
		}
	}
	
	output = '<tr class="'+style+'">';//<div class="item">';
	output += '<td class="time">' + time + '</td>';
	output += '<td class="inc"><div class="' + incstyle + '">' + inc + '</div></td>';
	output += '<td class="owner">' + (owner || '---') + '</td>';
	output += '<td class="content">' + value + '</td>';
	output += '</tr>';	//</div>
	if (myLogWindow.document == null) {
		createLogWindow();
	}
	
	var content = myLogWindow.document.getElementById('content');
	myLogWindow.document.write(output);
}

function addLog(value, owner) {
	
	if (myLogWindow == null) {
		createLogWindow();
		
		createLogRow('Elapsed', 'Inc', 'Owner', 'Value', true);
	}
	var	elapsed = myLogTimer.getElapsed();
	var diff = new Date();
	diff.setTime(elapsed.valueOf() - myLogTimeLast.valueOf());
	myLogTimeLast = elapsed;
	
	var timevalue = elapsed.getMinutes() + ':' + str_pad(elapsed.getSeconds(), 2, '0', true) + ':' + str_pad(elapsed.getMilliseconds(), 3, '0', true);
	var incvalue = '+' + diff.valueOf();
	createLogRow(timevalue, incvalue, owner, createLogObject(value));
	/**/
}





//flattens values/arrays etc for log output
function createLogObject(obj, opened, depth) {
	if (depth == null) {depth = 0;}
	var output = '';
	var is_func = is_function(obj);
	var is_arr = is_array(obj);
	var is_obj = is_object(obj);
	
	//alert(typeof(obj));
	
	if (depth < 20) {
		if (is_func) {
			output += '<div class="">FUNCTION</div>';
		} else if (is_arr || is_obj) {
		
			output += '<div class="group">';
			var count = 0;
			for (var i in obj) {
				
				var unique = Math.random() * 1000000;
				var id = 'log_'+unique+'_'+depth+'_'+count;
				var odd = (depth % 2 == 0) ? 'item_even' : 'item_odd';
				var style = (depth == 0) ? 'margin-left:0px;' : '';
				output += '<div class="' + odd + '" style="'+style+'">';
				output += '<span onclick="log_showhide(\''+id+'\');" class="bold">['+i+']</span>';
				var style = '';//((depth == 0) && (opened != true)) ? 'style="display:none;"' : '';
				output += '<div id="'+id+'" '+style+'>';
				if (i != 'target') {
					output += createLogObject(obj[i], opened, depth+1);
				} else {
					output += '<div>-- VALUE NOT SHOWN --</div>';
				}
				output += '</div>';
				output += '</div>';
				count++;
			}
			output += '</div>';
		} else {
			var rows = 1;
			if (is_boolean(obj)) {
				var style = 'boolean';
				obj = (obj) ? 'TRUE' : 'FALSE';
			} else if (obj === null) {
				var style = 'null';
				obj = 'NULL';
			} else {
				var style = 'string';
				if (is_number(obj)) {
					style = 'number';
				}
				if (is_query(obj)) {
					obj = queryFormat(obj);
					rows = 10;
				}
				if (String(obj).length > 256) {rows = 10};
			}
			output += '<textarea class="'+style+'" style="height:'+(rows*16)+'px">' + obj + '</textarea>';
		}
	}
	
	return output;
}








function queryFormat(value) {
	value = value.replace(/ LEFT JOIN/g,	'\nLEFT JOIN');
	value = value.replace(/ FROM/g,			'\n   FROM');
	value = value.replace(/ WHERE/g,		'\nWHERE');
	value = value.replace(/ LIMIT/g,		'\nLIMIT');
	value = value.replace(/ GROUP/g,		'\nGROUP');
	value = value.replace(/ ORDER/g,		'\nORDER');
	value = value.replace(/ ON/g,			'\n   ON');
	value = value.replace(/ ASC,/g,			' ASC,\n');
	value = value.replace(/ DESC,/g,		' DESC,\n');
	return value;
}