/**
  * JSDC - JavaScript Development Console
  *
  * Description:    Easy "console-like" output window for developing JavaScript
  * Version:        0.52
  * Date:           2007-08-20
  *
  * Author:         Ronald Vilbrandt
  * E-Mail:         info@rvi-media.de
  * Website:        http://www.rvi-media.de/
  * License:        GNU General Public License (GNU GPL)
  *
  */

// Version information
var jsdcVersion = 0.52;



// Configuration
var jsdcWidth = "400px";
var jsdcHeight = "300px";
var jsdcBgColor = "#FFFFFF";
var jsdcColor = "#000000";
var jsdcLinkColor = "#0000FF";
var jsdcFontSize = "0.8em";
var jsdcFont = "Verdana, Arial, sans-serif";
var jsdcBorder = "1px solid #000000";
var jsdcInitScreen = "JSDC v" + jsdcVersion + "<br />Rendering mode: " + document.compatMode + "<br /><br />";


// Anything below is program code which doesn't need to be changed for common

window.onresize = alignAll;
window.onload = tryMiniConsole;


// Storage of the reported messages
var strError = "";

/*
// Adding report function to all objects
Object.prototype.toConsole = function() {

	for(var i in this) {

		if(i != "toConsole") {
			report(i + ": " + this[i]);
		}
	}
}*/

// Workaround for DOM-objects in IE
function reportDOM(obj) {

	for(var i in obj) {

		if(i != "reportDOM") {
			report(i + ": " + obj[i]);
		}
	}

}

// Console Alert method
function alert2(strErrorMessage) {

	var con = createConsole();

	// Old variant
	//strErrorMessage = null == strErrorMessage ? "No string given" : strErrorMessage;

	if("string" != typeof strErrorMessage && "number" != typeof strErrorMessage) {
		strErrorMessage = typeof strErrorMessage;
	}

	if(null != document.getElementById("con_text") && "" == document.getElementById("con_text").innerHTML && "display" == con.style.display) {
		strError += strErrorMessage;
	} else {
		strError += "<br />" + strErrorMessage;
	}


	if(null != document.getElementById("jsdc") && "block" == con.style.display) {
		showConsole();
	}
	else if(null != document.getElementById("jsdc") && "false" != getVisibilityCookie()) {
		showConsole();
	}


}

// Alternative Console Alert method
function report(strErrorMessage) {
	alert2(htmlspecialchars(strErrorMessage, 1));
}

// Report as HTML
function reportHTML(strErrorMessage) {
	alert2(strErrorMessage);
}

// Show Console
function showConsole() {

	var con = createConsole();
	var con_text = document.getElementById("con_text")
	//document.getElementById("con_text").innerHTML = strError + document.getElementById("con_text").innerHTML;
	con_text.innerHTML += strError;
	strError = "";
	con.style.display = "block";

	con_text.scrollTop = 100000000;
}

// Create Console
function createConsole() {

	// If there is already console existing
	if(document.getElementById("jsdc") != null) {
		return document.getElementById("jsdc");
	}

	// Check if already loaded
	if(!document.getElementsByTagName("body")[0]) {
		return false;
	}

	var c_con = document.createElement("div");
	c_con.id = "jsdc";

	// Style settings
	c_con.style.zIndex          = "999";
	c_con.style.position        = "fixed";
	c_con.style.display         = "none";
	c_con.style.fontSize        = jsdcFontSize;
	c_con.style.fontFamily      = jsdcFont;
	c_con.style.width           = jsdcWidth;
	c_con.style.height          = jsdcHeight;
	c_con.style.backgroundColor = jsdcBgColor;
	c_con.style.color           = jsdcColor;
	c_con.style.border          = jsdcBorder;
	c_con.style.padding         = "5px";
	c_con.style.opacity         = "0.95";
	c_con.style.clear           = "both";
	c_con.style.padding         = "5px";
	c_con.style.mozOpacity      = "0.95";
	c_con.style.khtmlOpacity    = "0.95";

	// This is really awful! I beg anyone to send me a better solution!!
	c_con.innerHTML = "<style type=\"text/css\">#con_text a { color: " + jsdcLinkColor + "; text-decoration: underline; }</style>";


	// Problems with scrolling in IE when using properitary IE style - WTF?!
	//c_con.style.filter			= "alpha(opacity=90)";

	c_text = document.createElement("div");
	c_text.style.backgroundColor = jsdcBgColor;
	c_text.style.fontSize        = jsdcFontSize;
	c_text.style.fontFamily      = jsdcFont;
	c_text.style.color           = jsdcColor;
	c_text.style.width          = "100%";
	c_text.style.display        = "block";
	c_text.style.marginTop      = "10px";
	c_text.style.height         = "280px";
	c_text.style.overflow       = "scroll";
	c_text.id                   = "con_text";

	// Init Text (a little bit awful too, using innerHTML :-S
	c_text.innerHTML = jsdcInitScreen;



	c_head = document.createElement("div");
	c_head.style.backgroundColor = jsdcBgColor;
	c_head.style.fontSize        = jsdcFontSize;
	c_head.style.fontFamily      = jsdcFont;
	c_head.style.color           = jsdcColor;
	c_head.style.width          = "100%";
	c_head.style.display        = "block";
	c_head.style.textAlign      = "right";
	// ToDo: Change into node creation
	c_head.innerHTML            = "<a href=\"JavaScript: clearConsole();\" style=\"color: " + jsdcLinkColor + ";\">Clear</a> - <a href=\"JavaScript: closeConsole();\" style=\"color: " + jsdcLinkColor + ";\">Close</a>";
	c_head.id                   = "con_head";


	c_con.appendChild(c_head);
	c_con.appendChild(c_text);



	document.getElementsByTagName("body")[0].appendChild(c_con);

	// Align console
	alignConsole("jsdc");

	return c_con;
}


function alignAll() {
	alignConsole();
	alignMiniConsole();
}

// Align Console
function alignConsole() {

	var con = document.getElementById("jsdc");

	if(document.getElementById("jsdc") == null) {
		return false;
	}

	var h = new browserSize();

	con.style.top = (h.y - 325) + "px";
	con.style.left = (h.x - 435) + "px";

}


// Clear Console
function clearConsole() {

	var con = createConsole();

	document.getElementById("con_text").innerHTML = "";
}

// Close/Hide Console
function closeConsole() {

	var con = createConsole();

	con.style.display = "none";

	showMiniConsole();

}

function createMiniConsole() {

	// If there is already console existing
	if(document.getElementById("mini_console") != null) {
		return document.getElementById("mini_console");
	}

	var m_con = document.createElement("div");
	m_con.id = "mini_console";

	// Style settings
	m_con.style.zIndex          = "999";
	m_con.style.position        = "fixed";
	m_con.style.textAlign       = "center";
	m_con.style.display         = "none";
	m_con.style.fontSize        = "10px";
	m_con.style.fontFamily      = jsdcFont;
	m_con.style.width           = "70px";
	m_con.style.height          = "15px";
	m_con.style.backgroundColor = jsdcBgColor;
	m_con.style.color           = jsdcColor;
	m_con.style.border          = jsdcBorder;
	m_con.style.padding         = "5px";
	m_con.style.opacity         = "0.95";
	m_con.style.clear           = "both";
	m_con.style.padding         = "5px";
	m_con.style.mozOpacity      = "0.95";
	m_con.style.khtmlOpacity    = "0.95";

	//m_con.appendChild(document.createTextNode("<a href=\"JavaScript: closeMiniConsole(); showConsole();\">Show</a>"));

	// Create "Cookie" checkbox
	var c_box = document.createElement("input");
	c_box.id = "console_cookie_box";
	c_box.type = "checkbox";
	c_box.onclick = setVisibilityCookie;
	m_con.appendChild(c_box);


	// Create "Show" link
	var a_show = document.createElement("a");
	a_show.href = "JavaScript: closeMiniConsole(); showConsole();";
	a_show.style.color = jsdcLinkColor;
	a_show.appendChild(document.createTextNode("Show"));
	m_con.appendChild(a_show);

	document.getElementsByTagName("body")[0].appendChild(m_con);

	alignMiniConsole();

	return m_con;
}

// Show Console
function showMiniConsole() {

	var m_con = createMiniConsole();

	m_con.style.display = "block";

	var c_box = document.getElementById("console_cookie_box");
	if(c_box != null) {
		c_box.checked = "true" == getVisibilityCookie() ? true : false;
	}
}

// Close/Hide Console
function closeMiniConsole() {

	document.getElementById("mini_console").style.display = "none";
}

// Align Mini Console
function alignMiniConsole() {

	var con = document.getElementById("mini_console");

	if(document.getElementById("mini_console") == null) {
		return false;
	}

	var h = new browserSize();

	con.style.top = (h.y - 50) + "px";
	con.style.left = (h.x - 120) + "px";
}

function tryMiniConsole() {
	window.setTimeout(timerMiniConsole, 1000);
}

function timerMiniConsole() {

	if("false" == getVisibilityCookie()) {

		var m_con = createMiniConsole();

		if(null != document.getElementById("mini_console")) {
			showMiniConsole();
		}
	}
}


// Set cookie value
function setVisibilityCookie() {

	boolVis = "true" == getVisibilityCookie() ? "false" : "true";

	var expire = new Date();
	var intStNimmerlein = expire.getTime() + (99999 * 24 * 60 * 60 * 1000);
	expire.setTime(intStNimmerlein);

	document.cookie = "jsdc=" + boolVis + "; expires=" + expire.toGMTString() + "; path=/;";
}

// Get cookie value
function getVisibilityCookie() {

	if(document.cookie) {
		var dc = document.cookie.split("jsdc=")[1];

		if(null != dc) {
			var dci = dc.indexOf(";");

			if(dci != -1) {
				dc = dc.substring(1, dci);
			}
			return dc;
		}
	}

	return "true";
}


////////////////////////////////////////////////////////////////////////
// Helper functions


// Determine the browser size
function browserSize() {

	var x, y;

	if (self.innerHeight) {

		this.x = self.innerWidth;
		this.y = self.innerHeight;

	} else if (document.documentElement && document.documentElement.clientHeight) {

		this.x = document.documentElement.clientWidth;
		this.y = document.documentElement.clientHeight;

	} else if (document.body) {

		this.x = document.body.clientWidth;
		this.y = document.body.clientHeight;

	}

}


// Origin: http://www.infocamp.de/javascript_htmlspecialchars.php
function htmlspecialchars(strInput, intType) {

	if("undefined" == typeof strInput) {
		strInput = "";
	}

	if("number" != typeof intType) {
		intType = 2;
	}

	intType = Math.max(0, Math.min(3, parseInt(intType)));

	var arrHtml = new Array();

	arrHtml[38] = "&amp;";
	arrHtml[60] = "&lt;";
	arrHtml[62] = "&gt;";

	if(1 == intType || 3 == intType) {
		arrHtml[39] = "&#039;";
	}
	if(2 == intType || 3 == intType) {
		arrHtml[34] = "&quot;";
	}

	if("string" == typeof strInput) {

		// Strange problem found when using in wordpress
		// Returns indices like "each", "copy", "remove", "test", "extend", ... ?!
		/*for(var i in arrHtml) {

			eval("strInput = strInput.replace(/" + String.fromCharCode(i) + "/g, \"" + arrHtml[i] + "\");");
		}*/

		for(var i = 0; i < arrHtml.length; i++) {
			if("string" != typeof arrHtml[i]) {
				continue;
			}

			eval("strInput = strInput.replace(/" + String.fromCharCode(i) + "/g, \"" + arrHtml[i] + "\");");
		}
	}

	return strInput;
}