
/*============================================================================
To go in common.js
=============================================================================*/

function hope_addEvent( obj, type, fn ) {
//  alert('hope_addEvent: ' + obj + ',' + type + ',' + fn);
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}


function hope_removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}


function noop(msg)
{
//	console.log(msg);
}

function doit(id)
{
//	console.log('doit: ' + id);
	var div = getObjectById(id);
//	console.log(div);
	if ( null != div )
	{
		div.style.opacity = "0.5";
		div.style.border = "2px solid red";
	}
}

function padInt2(num)
{
	var txt = '';
	if ( num < 10 )
	{
		txt = '0';
	}
	txt = txt + num;
	
	return txt;
}

function padInt3(num)
{
	var txt = '';
	if ( num < 100 )
	{
		txt = '0';
	}
	if ( num < 10 )
	{
		txt = txt + '0';
	}
	txt = txt + num;
	
	return txt;
}

function padStrR(str,pad)
{
	var txt = "";
	if ( str.length > pad )
	{
		txt = str.substr(0,pad);
	}
	else
	{
		txt = str;
		var l = str.length;
		while ( l < pad )
		{
			txt = txt + "&nbsp;";
			l++;
		}
	}
	
	return txt;
}

/*============================================================================
Tracer 'Static' FUNCTIONS
=============================================================================*/

var users;
var times;
var lines;
var lineCount = 0;
var userCount = 0;

var tracer_live = false;
var tracer_doShowAll = true;
var tracer_subsetSize = 30;
var tracer_doShowTimes = false;

/* register source ... */
function tracer_register(source)
{
	if ( userCount == 0 )
	{
		/* initialise storage  etc */
		users = Array();
		times = Array();
		lines = Array();
		
		/* defer display until whole page loaded */
		tracer_createViewer();	
	}
	
	/* count users of service */
	userCount++;
	
	/* return UID */
	return userCount;
}


function tracer_createViewer()
{
	// tickTimer = setTimeout("tracer_display()", 5000);
	// window.addEvent("domready", function(){
		// tracer_display();
	// });
	hope_addEvent( window, "load", function(){ tracer_display(); } );
}


function tracer_display()
{
	var viewerId = "tracer_view";
	
	var topX = 10;
	var viewerTopX = topX + 20;
	
	var viewerHTML = "\
	<div id='' style='position: absolute; right: 0px; top: " + topX + "px; width: 60px; height: 20px; background-color: #ffffff; text-align: center; ' onclick='tracer_show(); ' >\
		TRACER\
	</div>\
	<div id='' style='position: absolute; right: 60px; top: " + topX + "px; width: 40px; height: 20px; background-color: #d0d0d0; text-align: center; ' onclick='tracer_showAll(); ' >\
		50\
	</div>\
	<div id='' style='position: absolute; right: 100px; top: " + topX + "px; width: 40px; height: 20px; background-color: #d0d0d0; text-align: center; ' onclick='tracer_showTimes(); ' >\
		Time\
	</div>\
	<div id='" + viewerId + "' style='display: none; z-index: 1000; position: absolute; right: 0px; top: " + viewerTopX + "px; width: 600px; height: 500px; padding: 3px; border: 1px solid #d0d0d0; background-color: #ffffff; overflow-y: scroll; overflow-x: scroll; white-space: nowrap;' >\
	</div>\
	";
	
	/* viewer might have already been created e.g. in a Module use */
	var viewer = getObjectById(viewerId);
	if ( null == viewer )
	{
		document.body.innerHTML = document.body.innerHTML + viewerHTML;
	}
	
	/* now live */
	tracer_live = true;
	
	/* do update for any tracing already received */
	tracer_update();
}


function tracer_show()
{	
	var viewerId = "tracer_view";
		
	var viewer = getObjectById(viewerId);
	if ( null != viewer )
	{
		if ( viewer.style.display == 'none' )
		{
			viewer.style.display = '';
		}
		else
		{
			viewer.style.display = 'none';
		}
	}
}


function tracer_showAll()
{
	tracer_doShowAll = !tracer_doShowAll;
	
	tracer_update();
}


function tracer_showTimes()
{
	tracer_doShowTimes = !tracer_doShowTimes;
	
	tracer_update();
}


function tracer_trace()
{
	/* user */
	var user = arguments[0];
	
	/* time */
	var now = new Date();
	var time = padInt2(now.getHours()) + ':' + padInt2(now.getMinutes()) + ':' + padInt2(now.getSeconds()) + '.' + padInt3(now.getMilliseconds());
	
	/* body */
	var body = "";
	for( var i = 1; i < arguments.length; i++ )
	{
		body = body + arguments[i] + " ";
	}
	
	tracer_doTrace(user, time, body);
}


function tracer_traceBatch()
{
	/* user */
	var user = arguments[0];
	
	/* time */
	var time = arguments[1];
	
	/* body */
	var body = "";
	for( var i = 2; i < arguments.length; i++ )
	{
		body = body + arguments[i] + " ";
	}

	tracer_doTrace(user, time, body);
}

function tracer_doTrace(user, time, body)
{
	users[lineCount] = padStrR(user,16);
	times[lineCount] = padStrR(time,12);
	lines[lineCount] = body;
	lineCount++;
		
	/* update */
	tracer_update();
}

function tracer_update()
{
	if ( tracer_live )
	{
		var viewerId = "tracer_view";

		var viewer = getObjectById( viewerId );
		if ( null != viewer )
		{
			var showNum = tracer_doShowAll ? lineCount : tracer_subsetSize;
			
			/* display last 10 msgs */
			var txt = "";
			var startIx = 0;
			if ( lineCount > showNum )
			{
				startIx = lineCount - showNum;
			}
			
			for ( ix = startIx; ix < lineCount; ix++ )
			{	
				var body = "";
				
				/* timestamp optional */
				if ( tracer_doShowTimes )
				{
					body = times[ix] + '>&nbsp;';
				}
				
				/* user */
				body = body + users[ix] + '>&nbsp;';
				
				/* text */
				body = body + lines[ix];
				
				var line = "<p style='font-size: 10px; font-family: monospace; margin-bottom:0px; '>" + body + "</p>";
				
				txt = txt + line;
			}
			viewer.innerHTML = txt;
		}
	}
}

