var HMP_DEBUG = true;
var HMP_DEBUG_CONTAINER = '';
var PAGE_LOADED = false;

function addEvent(objElement, strEventType, ptrEventFunc) {
	if (objElement.addEventListener) objElement.addEventListener(strEventType, ptrEventFunc, false);
	else if (objElement.attachEvent) objElement.attachEvent('on' + strEventType, ptrEventFunc);
	else objElement['on' + strEventType] = ptrEventFunc;
}

addEvent(window, 'load', function(){
  	PAGE_LOADED = true;
})

// Debuging Layer
function debug(something, recursive, recursion_level){
	var recursive = recursive !== true ? false : true,
		recursion_level = recursion_level || 4;

	if (HMP_DEBUG){
		if(typeof something == "object"){
			HMP_DEBUG_CONTAINER += dumpObjectRecursive(something);
		} else {
			HMP_DEBUG_CONTAINER += escapeDebugStr(something) + '<br/>';
		}
		if (!$("HMP_DEBUG_ERRORS")){
			var DIV = document.createElement('DIV');
			DIV.innerHTML = '<div id="HMP_DEBUG_WINDOW" style="position:absolute;z-index:9999;top:0px;left:0px;width:450px;overflow:auto; border:1px solid black; background:#ffffff; color:#000000; padding:5px 5px 10px 5px">'
			  + '&nbsp;<a onmouseover="this.style.borderColor=\'#C34802\';this.style.background=\'#FDEEE6\'" onmouseout="this.style.borderColor=\'#ffffff\';this.style.background=\'#ffffff\'" style="padding:3px 5px;border:1px solid #ffffff; font:normal 11px/15px Tahoma; color:#C34802; text-decoration:none" href="#" onclick="$(\'HMP_DEBUG_ERRORS\').style.display = ($(\'HMP_DEBUG_ERRORS\').offsetWidth) ? \'none\':\'\';" >Hide/Show me!</a>'
			  + '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onmouseover="this.style.borderColor=\'#C34802\';this.style.background=\'#FDEEE6\'" onmouseout="this.style.borderColor=\'#ffffff\';this.style.background=\'#ffffff\'" style="padding:3px 5px;border:1px solid #ffffff; font:normal 11px/15px Tahoma;color:#C34802; text-decoration:none" onclick="$(\'HMP_DEBUG_ERRORS\').innerHTML = \'\'; HMP_DEBUG_CONTAINER=\'\'; return false;">Clear</a>'
			  + '<div id="HMP_DEBUG_ERRORS" style="margin:10px 5px 0px 5px;padding:10px;border:1px dashed #aaaaaa; background:#fcfcfc; font:normal 11px/15px Tahoma"></div>'
			  + '</div>';
			  
			 if (PAGE_LOADED){
			 	insertDebugContainer(DIV);
			 } else {
				addEvent(window, 'load', function(){
				  	insertDebugContainer(DIV);
				})
			 }
		} else {
			$("HMP_DEBUG_ERRORS").innerHTML = HMP_DEBUG_CONTAINER ;
		}
	}
	function insertDebugContainer(container){
		if (document.body) {document.body.appendChild(container)}
		else {document.lastChild.appendChild(container)}
		$("HMP_DEBUG_ERRORS").innerHTML = HMP_DEBUG_CONTAINER;
	}
	function dumpObjectRecursive(obj, recurs){
		var dump = "", recurs = recurs || 1;
		for(key in obj){
			dump += "[<b>" + key + "</b>] => ";
			if (typeof(obj[key]) == "object" && obj[key].constructor == Array){
				dump += "<div style='padding:5px 10px'>" + dumpObjectRecursive(obj[key], recurs+1) + "</div>";
			} else if (typeof(obj[key]) == "object" && recursive === true && recurs < recursion_level){
				dump += "<div style='padding:5px 10px'>" + dumpObjectRecursive(obj[key], recurs+1) + "</div>";
			} else {
				dump += escapeDebugStr(obj[key]);
			}
			dump += "<br/>";
		}
		return dump;
	}
	function escapeDebugStr(str){
		str += " ";
		return str.replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/[\n|\r]+/g, "<br/>").replace(/(function\s?\(.*?\)\s?{)(.+)}/g, "<span style='color:blue'>$1</span><div style='padding-left:15px'>$2</div><span style='color:blue'>}</span>");
	}
}





