/*
	Debug Watch Box clas
	--------------------------

Simple in-page debug window.

To do:
	Add "evaluate" command.

Written by:
 Mikhail Diatchenko
 Sep 2006


Usage:
	<script type='text/javascript' src='/js.shared/debug.js'></script>

	<script language="javascript">
		var debug = new DebugBox();
		//debug.enabled = false;
		debug.init();
		debug.writeLine("debug loaded.");
	</script>

*/

document.write("<script type='text/javascript' src='/js/md.drag.js'></script>");
document.write("<script type='text/javascript' src='/js/md.utils.js'></script>");


//this is required to callback functions. read http://www.snook.ca/archives/javascript/javascript_pass/ for more info.
if ( ! Function.prototype.bind ) {
    Function.prototype.bind = function( object ) {
        var __method = this;
        return function() {
            __method.apply( object, arguments );
        };
    };
}

function DebugBox() {
	this.enabled = true;

	this.isIE = true;
	if (navigator.userAgent.toLowerCase().indexOf('ie')==-1)
		this.isIE = false;
	this.box = null;

	//this.init = 
	DebugBox.prototype.init = function () {
		if (!this.enabled)
			return false;

		this.box = document.body.debugBox;
		if (this.box==null) {
			var oThis = this;

			this.box = document.createElement( 'div' );
			this.box.id = "debugBox";
			document.body.appendChild( this.box );
		
			document.body.debugBox = this.box;
			
			this.box.style.position = "absolute";
			this.box.style.padding = 5;
			this.box.style.border = "1px solid gray";
			this.box.style.borderRightWidth = "2px";
			this.box.style.borderBottomWidth = "2px";
			this.box.style.backgroundColor = "white";
			this.box.style.textAlign = "left";
			//box.style.left = DL_GetElementLeft(parent.parentNode)+2;
			//box.style.top = DL_GetElementTop(parent.parentNode)+2;
			//box.style.top = "100px";
			this.box.style.zIndex = 1000;
			if (typeof this.box.style.filter != 'undefined')
				this.box.style.filter += "progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
				//this.box.style.filter += "progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color='gray', Positive='true')";
			else if (typeof this.box.style.opacity != 'undefined')
				this.box.style.opacity = 0.9

			this.box.drag = new DragAny(this.box);
			this.box.drag.cookieName = "debug";
			this.box.drag.savePosition = true;
			if (this.box.drag.getPosition()) {
				this.box.style.left = this.box.drag.left;
				this.box.style.top = this.box.drag.top;
			}

			this.header = document.createElement( 'div' );
			this.header.style.padding = 0;
			this.header.style.marginBottom = 5;
			this.header.style.textAlign = "center";
			this.box.appendChild( this.header );
			this.header.innerHTML = "<b>Debug Window<b>";

			this.toggle = document.createElement( 'a' );
			this.toggle.style.padding = 0;
			this.toggle.style.marginBottom = 5;
			//this.toggle.style.styleFloat = "right";
			//this.toggle.style.width = "auto";
			this.toggle.style.position = "absolute";
			this.toggle.style.top = "5px";
			this.toggle.style.right = "5px";
			this.header.appendChild( this.toggle );
			this.toggle.innerText = "hide";
			this.toggle.href = "#";
			Utils.cancelBubble(this.toggle, 'mousedown');
			Utils.attachEvent(this.toggle, "click", this.hideShow.bind(this));
			if (Utils.getCookie(this.box.drag.cookieName+"H")=="1") {
				this.hideShow();
			}

			//--setup evaluator--
			// setup text box
			this.expr = document.createElement( 'input' );
			//this.expr.style.width="70%";
			this.expr.style.border="1px solid gray";
			this.expr.style.fontFamily = "Tahoma";
			this.expr.style.fontSize = "11px";
			this.box.appendChild( this.expr );
			this.expr.debug = this;
			if(window.addEventListener){ // Mozilla, Netscape, Firefox
				this.expr.addEventListener('keyup', function(e) { 
					if (e.keyCode==13)
						oThis._eval();
				}, false);
			} else { // IE
				this.expr.attachEvent('onkeyup', function() {
					if (event.keyCode==13)
						oThis._eval();
				});
			}
			Utils.cancelBubble(this.expr, 'mousedown');
			
			// setup run button
			this.runBtn = document.createElement( 'input' );
			this.runBtn.value = "Eval";
			this.runBtn.type = "button";
			this.runBtn.style.border="1px solid gray";
			this.runBtn.style.fontFamily = "Tahoma";
			this.runBtn.style.fontSize = "11px";
			this.box.appendChild( this.runBtn );
			this.runBtn.debug = this;
			Utils.cancelBubble(this.runBtn, 'mousedown');
			Utils.attachEvent(this.runBtn, "click", this._eval.bind(this));

			// setup clear button
			this.clearBtn = document.createElement( 'input' );
			this.clearBtn.value = "Clear";
			this.clearBtn.type = "button";
			this.clearBtn.style.border="1px solid gray";
			this.clearBtn.style.fontFamily = "Tahoma";
			this.clearBtn.style.fontSize = "11px";
			this.box.appendChild( this.clearBtn );
			this.clearBtn.debug = this;
			Utils.cancelBubble(this.clearBtn, 'mousedown');
			Utils.attachEvent(this.clearBtn, "click", this.clear.bind(this));
			
			//-- setup log text container -----
			this.log = document.createElement( 'div' );
			this.log.style.padding = 5;
			Utils.cancelBubble(this.log, 'mousedown');
			this.log.style.cursor = "default"; 
			this.box.appendChild( this.log );

		} else {
		}
	}

	DebugBox.prototype.hideShow = function () {
		if (this.box.style.overflow == "hidden") {
			this.box.style.overflow = "";
			this.box.style.height = "auto";
			this.box.style.width = "auto";
			this.toggle.innerText = "hide";
			Utils.setCookiePerm(this.box.drag.cookieName+"H", "0");
		} else {
			this.box.style.overflow = "hidden";
			if (document.all)
				this.box.style.height = "25px";
			else
				this.box.style.height = "16px";
			this.box.style.width = "200px";
			this.toggle.innerText = "show";
			Utils.setCookiePerm(this.box.drag.cookieName+"H", "1");
		}
	}

	DebugBox.prototype._eval = function () {
				var res;
				if (this.expr.value=='')
					return;
				try {
					res = eval(this.expr.value);
				} catch (e) {
					res = e.message;
					this.writeLine("<font color=red>eval: "+res+"</font>");
					return;
				}
				if (typeof res != 'undefined') {
					this.writeLine("eval: "+res);
				}
	}

	DebugBox.prototype.clear = function () {
		this.log.innerHTML = "";
	}
	
	this.write = function(s) {
		if (this.enabled)
		{
		}this.log.innerHTML += s;
	}
	
	this.writeLine = function(s) {
		if (this.enabled)
		{
			this.log.innerHTML += s+"<br />\n";
		}
	}
	this.mark = function(s) {
		if (this.enabled)
		{
			this.log.innerHTML += "<li>";
			if (s)
			{
				this.log.innerHTML += s + "<br />";
			}
		}
	}
	this.writeHeader = function(s) {
		if (this.enabled)
		{
			this.log.innerHTML += "<b>"+s+"</b><br />";
		}
	}
}

function onEvalClick(e) {
	alert(e);
}
