var dbg_win;
function createWin () {
	dbg_win = window.open('', 'dbg_win', 'width=450,height=250,resizable,scrollbars=yes');
	dbg_win.document.write('<html><head>');
	dbg_win.document.write('<style>.singleDbg{border:1px solid green;margin-bottom:3px}</style>');
	dbg_win.document.write('<title>DEBUG CONSOLE JS</title></head><body>');
	dbg_win.document.write("<h3>DEBUG</h3>");
	dbg_win.document.write('<span onclick="document.getElementById(\'listDbg\').innerHTML=\'\'">Clear</span>');
	dbg_win.document.write("<div id='listDbg' />");
	dbg_win.document.write('</body></html>');
	dbg_win.document.close();
	return dbg_win;
}

/**
Se venisse passato il secondo parametro, il testo viene messo dentro al div con idElem passato. (in realtà il vecchio div vien cancellato e appeso questo nuovo)
*/
function addJSDebug (txt, idElem) {
	if (idElem == undefined) {
		idElem = '';
	};
	if (dbg_win == undefined) {
		dbg_win = createWin();
	}
	if (idElem != '' && idElem != undefined) {
		// viene cancellato il vecchio div con id ieElem passato come parametro
		var elemDest = dbg_win.document.getElementById(idElem);
		if (elemDest) {
			elemDest.parentNode.removeChild(elemDest);//.InnerHTML = "<xmp>"+txt+"</xmp>";
			// dbg_win.document.close();
			// return;
		};
	}

	var div = dbg_win.document.createElement('div');
	div.id=idElem;
	div.innerHTML = "<xmp>"+txt+"</xmp>";
	div.className = 'singleDbg';
	dbg_win.document.getElementById('listDbg').appendChild(div);

	dbg_win.document.close();
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function becca_la_pos()
{
	if (document.layers) { // Netscape
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = captureMousePosition;
	} else if (document.all) { // Internet Explorer
		document.onmousemove = captureMousePosition;
	} else if (document.getElementById) { // Netcsape 6
		document.onmousemove = captureMousePosition;
	}
}
addOnload(becca_la_pos);


var xMousePos; var yMousePos;
var xMousePosMax; var yMousePosMax;
var ctrlPressMouse;
var oldWidthDbg;
function captureMousePosition(e) {
	if (document.layers) { //Netscape
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	} else if (document.all) { //IE
		xMousePos = window.event.x+document.body.scrollLeft;
		yMousePos = window.event.y+document.body.scrollTop;
		xMousePosMax = document.body.clientWidth+document.body.scrollLeft ;
		yMousePosMax = document.body.clientHeight+document.body.scrollTop ;
	} else if (document.getElementById) {
		// Netscape 6 behaves the same as Netscape 4 in this regard
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}
	if (ctrlPressMouse != undefined) {
		var contenitore = document.getElementById('dbgDiv');
		// addJSDebug('x: '+xMousePos+' // y: '+yMousePos+"\n"+document.body.clientWidth, 'mousepos');
		var a = document.body.clientWidth-xMousePos + 17;
		contenitore.style.width = a+'px';
	}
}

function createDbgDiv () {
	var contenitore = document.getElementById('dbgDiv');

	if (contenitore != null) {
		return contenitore;
	}

	var styleTag = document.createElement('style');
	styleTag.type="text/css";
	styleTag.innerHTML ='.singleDbg{border:1px solid green;margin-bottom:5px;background: white;}#dbgDiv { position: fixed; right: 0px; top: 0px;width: 25%; height: 100%;overflow: auto;background:url(/images/sfondoDebug.png);} #dbgBorder {cursor: col-resize;float: left;height: 100%;border: 2px solid #999;}xmp {margin: 2px;}';
	document.getElementsByTagName('head')[0].appendChild(styleTag);


	var contenitore = document.createElement('div');
	contenitore.id = 'dbgDiv';
	document.body.appendChild(contenitore);

	var inverti = document.createElement('div');
	inverti.id = 'dbgInverti';
	contenitore.appendChild(inverti);

	var border = document.createElement('div');
	border.id = 'dbgBorder';
	contenitore.appendChild(border);

	border.onmousedown = function () {
		ctrlPressMouse = true;
	}
	document.onmouseup = function () {
		ctrlPressMouse = undefined;
	}
	border.ondblclick = function () {
		var closedWidth = '8px';
		contenitore.style.width = closedWidth;
	}
	inverti.innerHTML = '<span onclick="var d=document.getElementById(\'dbgDiv\').style;if(d.width!=\'100%\'){d.width=\'100%\';d.height=\'50%\'}else{d.width=\'25%\';d.height=\'100%\'};">Sposta Debug</span>';
	return contenitore;

}

function addJSDebugDiv (txt, color) {
	// txt = toString(txt);
	if (txt == '') {
		txt = '</xmp><i>empty</i><xmp>';
	};
	var cont = createDbgDiv ();
	var t = document.createElement('div');
	if (typeof(txt) == 'string') {
		txt = txt.replace(/\t/mg, " "); // rimpiazzo il \t con uno spazio
	};
	t.innerHTML = "<xmp>"+txt+"</xmp>";
	t.className = 'singleDbg';
	if (color != undefined) {
		t.style.color = color;
	};
	cont.appendChild(t);

	var border  = document.getElementById('dbgBorder');
	border.style.height = border.parentNode.scrollHeight;

}


