/**
 * (c) 2005 Primary Brokers S.A. - http://www.primary.com.ar/
 */

var timestamp = 0;
var debugWindow = null;
var debug=0;
var style='odd';
var debugBuffer=null;
var debugReady=false;
var lineas=200;
var lineasMin=60;

function toggleDebug() {
	if (debug) {
		debug=0;
	} else {
		debug=1;
	}
	pstatus("debug=" + debug);
}

function showInDHTMLWindow(msg) {
try {	
	if ((debugWindow == null) || debugWindow.closed) {
		debugWindow = window.open("/ool/debugwin.html","debugWin","toolbar=no,scrollbars=yes,resizable=yes,width=600,height=400");
		debugWindow.focus();
	}
	var newText = '<div class="' + style + '">'+ msg + '</div>\n';
	if (!debugReady) {
		// La ventana no esta abierta aun. Guardo en el buffer
		debugBuffer = newText + debugBuffer;
	} else {
		// La ventana esta abierta.
		
		var statusLayer = debugWindow.document.getElementById("pstatus");
		var datosLayer = debugWindow.document.getElementById("debugDat");
		
		if (statusLayer) {
			var statusText = statusLayer.innerHTML;
			if (debugBuffer) {
				// Recien se abre la ventana. Escribo el buffer primero.
				newText +=   debugBuffer;
				debugBuffer=null;
			}	
			datos = datosLayer.innerHTML;
			var texto = new Array();
			if (isIE4 == false){
				texto = statusText.split("</div>");
			}else{
				texto = statusText.split("\n");
			}
			datos = "LineasMin=" + lineasMin + " LineasMax=" + lineas + " Lineas actuales del debug=" + texto.length;
			if (texto.length>lineas){
				statusText = "";
				 for(i=0;i<lineasMin;i++) statusText += texto[i] + "</div>" ;
			}
			newText += statusText;
			layerWrite(datosLayer,datos);
			layerWrite(statusLayer,newText);
		}
	}
} catch (e) {}	
	// Ciclo el estilo.
	if (style=='odd') {
		style='even';
	} else {
		style='odd';
	}
	
}

function pstatus(label) {
	if (!debug) {
		return false;
	}
      var funcName="none";
      
	if (pstatus.callee) {
		funcName= "ee-" +pstatus.callee;
	} else if (pstatus.caller) {
		if (pstatus.caller.id) {
			funcName="id:"+pstatus.caller.id;
		} else {
			//funcName="c:"+pstatus.caller.toString();
      			funcName = pstatus.caller.toString();
        		funcName = funcName.substring(0, funcName.indexOf(")") + 1);
		}
      }
      
      var msg = label ;
      //var msg = "[" + funcName + "] " + label ;
      
      //var now = new Date()
      //var elapsed = now - timestamp
      //if (elapsed < 10000) {
         //msg += " (" + elapsed + " msec)"
      //}
      //timestamp = now
	
      showInDHTMLWindow(msg);
   
}

