/**
 * Geshibot JS
 */

if (typeof(Geshibot) == 'undefined') {
	Geshibot = {};
}

/**
* Replaces the geshi default classes with mambo/joomla classes
*/
Geshibot.replaceGeshiWithMamboClasses = function() { 
	if (document && document.getElementsByTagName) { 
		var pre_items = document.getElementsByTagName('pre');
		var n = pre_items.length; 
		for(var i = 0; i < n; i++) {
			// divs
			var divs = pre_items[i].getElementsByTagName('div');
			var dlen = divs.length; 
			for (var j = 0; j < dlen; j++) {
				if (divs[j].className == 'head') { // header
					var className = 'sectiontableheader';
				} else if (divs[j].className == 'foot') { // footer
					var className = 'small';
				} else if (divs[j].className == ' ') { // code lines
					var className = 'sectiontableentry1';
				} else {
					var className = '';
				}
				divs[j].className += ' '+className;
			}
			
			// anchors
			var a_items = pre_items[i].getElementsByTagName('a');
			var alen = a_items.length; 
			for (var l = 0; l < alen; l++) {
				a_items[l].target = '_blank';
				a_items[l].setAttribute('target', '_blank');
			}
		
			// list items
			var li_items = pre_items[i].getElementsByTagName('li');
			var ilen = li_items.length; 
			for (var k = 0; k < ilen; k++) {
				li_items[k].className += ' sectiontableentry1';
			}
		}
	}
};

// add copy link to code highlights
Geshibot.addCodeView = function() { 
	if (document && document.getElementsByTagName) { 
		var divs = document.getElementsByTagName('div');
		var dlen = divs.length; 
		for (var j = 0; j < dlen; j++) {
			var pattern = new RegExp("(^|\\s)foot(\\s|$)");
			if ( pattern.test(divs[j].className) ) { // footer class
				if (divs[j]._button == 1) continue;
				if (Geshibot.copy_type == 0) {
					divs[j].innerHTML += ' <input type="button" onclick="Geshibot.copy(this); return false;" value="'+Geshibot.show_copy_label+'" class="button" />';
				} else {
					divs[j].innerHTML += ' <a href="#" onclick="Geshibot.copy(this); return false;">'+Geshibot.show_copy_label+'</a>';
				}
				divs[j]._button = 1;
			}
		}
	}
};

// copy the code text to new window
Geshibot.copy = function(_this) {
	if (_this && _this.parentNode) {
		var source = _this.parentNode.parentNode.nextSibling;
		var name = source.className;
		var _source = Geshibot.decode(source.innerHTML);
		_source = '<textarea style="width: 100%; height: 100%;">'+Geshibot.html2entities(_source)+'</textarea>';
		Geshibot.writeSourceWindow(_source, name, '[source view]');
	}
};

// writes content to a new window
Geshibot.writeSourceWindow = function(txt, name, title) {
	top.winRef = window.open('', name,
	'width=550,height=450'
	+',menubar=0'
	+',toolbar=0'
	+',status=0'
	+',scrollbars=0'
	+',resizable=1');
	top.winRef.document.writeln(
	'<html><head><title>'+title+'</title></head>'
	+'<body bgcolor=white onLoad="self.focus()">'
	+txt
	+'</body></html>'
	);
	top.winRef.document.body.style.overflow = 'hidden'; // hide scroll in FF
	top.winRef.document.close();
	top.winRef.focus();
};

// decode uriencoded text
Geshibot.decode = function(txt) {
	txt = txt.replace(/\+/g, ' ');
	if (decodeURIComponent) {
		return decodeURIComponent(txt);
	}
	if (unescape) {
		return unescape(txt);
	}
	return txt;
};

Geshibot.html2entities = function(str){
	var re = /[<>"'&]/g
	str = str.replace(re, function(match){
		if (match=="<")
			return "&lt;";
		else if (match==">")
		 	return "&gt;";
		else if (match=="\"")
		 	return "&quot;";
		else if (match=="'")
		 	return "&#039;";
		else if (match=="&")
		 	return "&amp;";
	});
	return str;
};


