function debugToolbarBigger() {
	
	$("debugToolbarBig").hide();
	$("debugToolbarSmall").show();
	
	$("debugToolbar").setStyle({ 'height': '150px' });
	
	return false;
	
}

function debugToolbarSmaller() {
	
	$("debugToolbarBig").show();
	$("debugToolbarSmall").hide();
	
	$("debugToolbar").setStyle({ 'height': '24px' });
	
	return false;
	
}

var debugToolbarPosition = 0;
var debugToolbarContents = new Array();

function debugToolbarAdd(content) {
	
	var html = '';
	
	html += '<div id="toolbar-position-'+ debugToolbarPosition +'">';
	
	if(debugToolbarPosition++) {
		html += '<div style="border-bottom: 1px dashed #555; padding-bottom: 2px; margin-bottom: 2px">';
	} else {
		html += '<div>';
	}
	
	if(typeof(content) == 'undefined') {
		content = $("debugPage").innerHTML;
	}
	
	html += content;
	html += '</div>';
	html += '</div>';
	
	html += $("debugToolbarPages").innerHTML;
	
	if(debugToolbarPosition > 6) {
		
		var regex = '\<div id\=\"toolbar\-position\-'+ (debugToolbarPosition - 7) +'\"\>.*';
		var htmlOld = html.match(regex);
		
		if(htmlOld) {
			html = html.substitute(htmlOld[0], '');
		}
			
	}
	
	$("debugToolbarPages").innerHTML = html;
	
}

function debugToolbarStart() {
	
	debugToolbarAdd();
	
}

function debugIncludedFilesBox(id) {
	
	$("debugToolbarContent").innerHTML = $("debugPageIncludedFiles"+ id).innerHTML;
	
	showBox('debugToolbarBox');
	
	return false;
	
}

function debugSqlQueriesBox(id) {
	
	$("debugToolbarContent").innerHTML = $("debugPageSqlQueries"+ id).innerHTML;
	
	showBox('debugToolbarBox');
	
	return false;
	
}

function restoreDdebugSqlQueriesBox(id) {
	
	$("debugToolbarContent").innerHTML = $("debugPageSqlQueries"+ id).innerHTML;
	return false;
	
}

function debugErrorsBox(id) {
	
	$("debugToolbarContent").innerHTML = $("debugPageErrors"+ id).innerHTML;
	
	showBox('debugToolbarBox');
	
	return false;
	
}

function debugBacktraceBox(id) {
	
	$("debugToolbarContent").innerHTML = $("debugPageBacktrace"+ id).innerHTML;
	return false;
	
}

function debugResultBox(id) {
	
	var content = debugToolbarContents[id];
	
	if(typeof(content) != 'undefined') {
		
		var html = '';
		
		html += '<h3>Content</h3>';
		html += debugBuildContent(content);
	
		$("debugToolbarContent").innerHTML = html;
		showBox('debugToolbarBox');
		
	} else {
		alert('Content is only available for AJAX requests.')
	}
	
	return false;
	
}

var debugBuildColors = new Array("505", "550", "500", "055", "050", "005");
var debugBuildPosition = 0;

function debugBuildContent(content) {
	
	var html = '';
	
	for(i in content) {
		
		var type = typeof(content[i]);
		
		if(type === 'function') {
			continue;
		}
		
		var color;
		
		if(typeof(debugBuildColors[debugBuildPosition]) != 'undefined') {
			color = debugBuildColors[debugBuildPosition];
		} else {
			color = '555';
		}
		
		html += '<div style="border: 1px solid #888; margin: 2px 2px 2px 6px; padding: 2px">';
		html += '<strong  style="color: #'+ color +'">'+ i +'</strong>';
		html += ' =&gt; ';
		
		if(content[i] == null) {
			html += '<em>null</em>';
		} else if(type === 'object') {
			if(content[i].length === 0) {
				html += '<em>empty</em>';
			} else {
				debugBuildPosition++;
				html += debugBuildContent(content[i]);
				debugBuildPosition--;
			}
		} else if(type === 'string') {
			if(content[i] === '') {
				html += '<em>empty</em>';
			} else {
				html += '"'+ content[i] +'"';
			}
		} else if(type === 'number') {
			html += content[i];
		} else if(type === 'boolean') {
			if(content[i]) {
				html += '<em>true</em>';
			} else {
				html += '<em>false</em>';
			}
		} else if(type === 'undefined') {
			html += '<em>undefined</em>';
		} else {
			console.log(type);
		}
		
		html += '</div>';
		
	}
	
	return html;
	
}

var debugAjaxSave = null;

function debugAjaxParseJs(content) {

	var regex = '\<div id\=\"debugPage\" name\=\"([0-9]+)\" style\=\"display\: none\"\>(.*)\<\/div\>$';
	var debug = content.match(regex);

	if(debug) {
		debugToolbarAdd(debug[2]);
		content = content.substitute(debug[0], '');
	}
	
	debugAjaxSave = debug;
	
	return content;
		
}

function debugAjaxStoreObject(object) {
	
	if(debugAjaxSave) {
		debugToolbarContents[debugAjaxSave[1]] = object;
	}
	
	debugAjaxSave = null;
		
}

function translationMonitor() {
	
	if($('translationId')) {

		text = $('translationId').value;
		
		if(text != '') {
			
			var textMatch = text.match('\{?([0-9]+)[ \|]*?');

			if(textMatch) {
				$('translationId').value = textMatch[1];
			}
			
		}
		
	}
	
}

function translationToolbarStart() {
	
	setInterval(translationMonitor, 200);
	
}

function translationSearch(url) {
	
	var text = $('translationId').value;
	
	if(
		typeof(translationVersions[text]) == 'string'
	) {
		
		var version = translationVersions[text];
		var file = translationFiles[text];
		var id = translationIds[text];
	
		$('translationIframe').src = 'http://'+ url +'/translation/search?frame&text='+ id.urlEncode() +'&version=' + version.urlEncode() +'&file=' + file.urlEncode();
		
	} else {
		
		$('translationMessage').innerHTML = 'Translation can not be found.<br/>Please check again.';
		setTimeout(translationClean, 3000);
		
	}
	
	return false;
	
}

function translationClean() {
	
	$('translationMessage').innerHTML = '';
	
}

function translationAjax(content) {

	var regex = '\<script id\=\"translationPage\" language\=\"JavaScript\"\>(.*)\<\/script\>$';
	var translation = content.match(regex);

	if(translation) {
			content = content.substitute(translation[0], '');
			eval(translation[1]);
	}
	
	return content;
		
}