// Internet Explorer doesn't display correctly white spaces on tags PRE with innerHTML
// this simple function verify if browser is IE to use outerHTML instead of innerHTML
function ie() {
	var	browser = navigator.userAgent.toUpperCase();
	return (browser.indexOf("MSIE") >= 0 && browser.indexOf("OPERA") < 0);
};

// function to get Iframe content document
function getIframeDocument(myiframe) {
	return myiframe.contentDocument || myiframe.contentWindow.document || myiframe.document;
};

// function to change iframe source [calling a php bridge page]
function changeSource(newSource) {
	var 	myiframe = document.getElementById("myiframe"),
		baseURI = "myiframe.php?site=";
	if(!myiframe.src || myiframe.src.substr(myiframe.src.indexOf(baseURI) + baseURI.length, myiframe.src.length - baseURI.length) !== newSource)
		myiframe.src = baseURI + newSource;
	return false;
};

// function to highlight every pre tag inside the iframe
function highlightIframe() {
	var 	myiframe = getIframeDocument(document.getElementById("myiframe")),
		isIE = ie(),
		some = false;
	for(var a = 0, b = myiframe.body.getElementsByTagName("PRE"), c = b.length, d = ""; a < c; a++) {
		
		// pre tag content without html tags
		d = b[a].innerHTML.replace(/<[^>]+>/g, "");
		
		// verify that tehre's something to parse
		if(d.replace(/^\s*|\s*$/, "").length > 0) {
			
			// to be a client friend, check the lenght of the string and choose the fastest parser object
			d = (d.length < 10000) ? JSHFull.parse(d) : JSHFast.parse(d);
			if(isIE)
				b[a].outerHTML = "<pre>" + d + "</pre>";
			else
				b[a].innerHTML = d;
			some = true;
		};
	};
	if(some)
		myiframe.body.innerHTML +=
			"<style type=\"text/css\">" +
			"span.jshcomments{color:#999;}span.jshstrings{color:#800000;}span.jshnumbers{color:#F00;}" +
			"span.jshoperators{color:#808080;}span.jshglobals{color:#4040ff;}span.jshproperties{color:#070;}span.jshmethods{color:#00A;}" +
			"</style>";
};
		
// theme Object defines wich classes parser should use
var theme = {
	"comments":"jshcomments",	// single or multiline comments span class
	"strings":"jshstrings",		// strings span class
	"numbers":"jshnumbers",		// numbers span class
	"operators":"jshoperators",	// operators span class
	"globals":"jshglobals",		// OPTIONAL: global top level functions or some reserved words
	"properties":"jshproperties",	// OPTIONAL: Objects properties (i.e. str.length )
	"methods":"jshmethods"		// OPTIONAL: Objects methods (i.e. str.charAt(... )
},

// JSHighLighter with full sintax (strings, numbers, operators, comments, globals, properties, methods)
JSHFull = new JSHighLighter(theme, true),

// JSHighLighter with basic highlight (strings, numbers, operators, comments)
JSHFast = new JSHighLighter(theme);
