function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function print_r(obj,hideValue){ 
     var objString = '<ul>';
     
     for( var varName in obj ) {
     	objString += '<li>'+ varName +'('+ typeof obj[varName] +')';
     	
     	if ( typeof obj[varName] == 'object' ) {
     		if ( hideValue ) continue;
     		objString += print_r(obj[varName],true);
     	} else {
     		objString += ':'+ escape(obj[varName]);
     	}
     }
     objString += '</ul>';
     return objString;
}

/*has a negative effect on input-field's focus...in IE (whenever deselect is called, the currentinput field, loses its focus)*/
function deselect(){
	if ( document.selection ) document.selection.empty();
	else if ( window.getSelection ) window.getSelection().removeAllRanges();
}

function rdebug(obj){
	debug(print_r(obj));
}

function debug(value){
	document.getElementById('debugOut').innerHTML += '<br>'+value;
}

function stopEvent(event){
	if ( event.stopPropagation ) { event.stopPropagation(); }
	else { event.cancelBubble = true; }
}

function execScripts(node){
	if ( node.tagName == 'SCRIPT' ){
		try{
			eval(node.text);
		} catch( e ){
			if ( window.console ) window.console.log(e);
		}
	} else {
		var child = node.firstChild;
		while ( child  ) {
			execScripts(child);
			child = child.nextSibling;
		}
	}
};

function remoteRequest(method,url,successHandler){
	var request;
	if ( window.XMLHttpRequest) request = new XMLHttpRequest();
	else request = new ActiveXObject("Microsoft.XMLHTTP");
	
	request.open(method,url,true);
	
	if ( successHandler ) {
		request.onreadystatechange = function() {
			if ( request.readyState == 4 && request.status == 200 ) {
				successHandler(request);
			}
		};
	}
	
	request.send();
};

function lightbox(link,event){
	var background = document.createElement('div');
	var lightbox = document.createElement('div');
	var closeButton = document.createElement('div');
	
	var closeHandler = function(){
		background.parentNode.removeChild(background);
		lightbox.parentNode.removeChild(lightbox);
		
		document.body.onkeydown = null;
	};
	
	document.body.onkeydown = function(event){
		if ( !event ) event = window.event;
		
		switch( event.keyCode ) {
			case 27:
				closeHandler();
				break;
		}
	};
	
	background.className = 'lightbox-background';
	closeButton.className = 'close';
	lightbox.className = 'lightbox loading';
	
	closeButton.onclick = closeHandler;
	background.onclick = closeHandler;
	document.body.appendChild(background);
	lightbox.appendChild(closeButton);
	document.getElementById('Content').appendChild(lightbox);

	remoteRequest('GET',link,function() {
		if ( request.readyState == 4 && request.status == 200 ) {
			lightbox.className = 'lightbox';
			lightbox.innerHTML = request.responseText;
			
			execScripts(lightbox);
			Calculator.initInstances();
			
			lightbox.appendChild(closeButton);
		}
	});
	
	return false;
};
