/** 
 * @fileoverview dp.SyntaxHighlighter with cafen.net
 * copyright 2005 {@link http://cafen.net cafen.net} 
 * @author Kimjonggab (outmind@cafen.net)
 * @version 16.0
 */
 
 if (typeof dp == 'undefined') {
	if (cafen.srartLoad) 
		cafen.srartLoad(_cafen_service_url+'dp.SyntaxHighlighter/Uncompressed/shCore.js');
	else
		document.write('<scr'+'ipt TYPE="text/JavaScript" charset="utf-8" LANGUAGE="JavaScript1.2" SRC="'+_cafen_service_url+'dp.SyntaxHighlighter/Uncompressed/shCore.js"></scr'+'ipt>');
}

var syntaxStyleAdded = false;

function cafenSyntaxHighlighter() {
	this.url_basescript = _cafen_service_url +'dp.SyntaxHighlighter/Uncompressed/';
	this.tryoutNo = 0;
	this.tryoutMax = 50;
	
	var scriptAliases = {
		'cpp' : 'Cpp', 'c' : 'Cpp', 'c++' : 'Cpp',
		'c#' : 'CSharp', 'c-sharp' : 'CSharp', 'csharp' : 'CSharp',
		'css' : 'CSS',
		'delphi' : 'Delphi', 'pascal' : 'Delphi',
		'java' : 'Java',
		'js' :'JScript', 'jscript' :'JScript', 'javascript' :'JScript',
		'php' :'Php','php3' :'Php',
		'py' :'Python', 'python' :'Python',
		'ruby' : 'Ruby', 'rails' : 'Ruby', 'ror' : 'Ruby',
		'sql' : 'Sql',
		'vb' : 'Vb', 'vb.net' : 'Vb',
		'xml' : 'Xml', 'xhtml' : 'Xml', 'xslt' : 'Xml', 'html' : 'Xml', 'htm' : 'Xml', 'xhtml' : 'Xml'
	};
	this.scriptName = {
		'Cpp' : 'shBrushCpp.js',
		'CSharp' : 'shBrushCSharp.js',
		'CSS' : 'shBrushCss.js',
		'Delphi' : 'shBrushDelphi.js',
		'Java' : 'shBrushJava.js',
		'JScript' : 'shBrushJScript.js',
		'Php' : 'shBrushPhp.js',
		'Python' : 'shBrushPython.js',
		'Ruby' : 'shBrushRuby.js',
		'Sql' : 'shBrushSql.js',
		'Vb' : 'shBrushVb.js',
		'Xml' : 'shBrushXml.js'
	}
	this.addedScript = [];
	this.addedElements = [];
	this.registered = {};
	for(var brush in scriptAliases) {
		this.registered[brush] = scriptAliases[brush];
	}
}


cafenSyntaxHighlighter.prototype = {
	/**
	* render Element with chart
	*/
	version : 'T20',
	render : function() {
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (element != null)
				this.renderElement(element);
		}
	},
	showMsg : function (element, msg) {
		if (msg == '') {
			if (typeof element.tmp_divNode != 'undefined') 
				element.parentNode.removeChild(element.tmp_divNode);
		} else {
			if (typeof element.tmp_divNode != 'undefined') 
				element.parentNode.removeChild(element.tmp_divNode);
			element.tmp_divNode = document.createElement('div');
			element.tmp_divNode.innerHTML = msg;
			element.tmp_divNode.style.border = '1px solid #d0d0d0';
			element.tmp_divNode.style.padding = '4 4 4 4px';
			element.tmp_divNode.style.textAlign = 'center';
			element.parentNode.insertBefore(element.tmp_divNode, element);
			element.style.display = 'none';
		}
	},
	renderTxt : function(options) {
		if (typeof dp == 'undefined' || typeof dp.sh == 'undefined' || typeof dp.sh.Brushes == 'undefined') {
//			this.showMsg(element, 'Cannot load SyntaxHighlighter!! <br>load the Script ['+this.url_basescript+'<b>shCore.js</b>] on head line!');
			return false;
		}
		var language = ((options.language != null) ? options.language : 'xml').toLowerCase();
		if (this.registered[language] == null) {
//			this.showMsg(element, 'Cannot parse the language [<b>'+language+'</b>]!! CheckOut from the scriptList.');
			return true;
		}
//		this.showMsg(element, 'Wait for a second during parse ' + language);
		if (typeof dp.sh.Brushes[this.registered[language]] == 'undefined') {
			this.getScript(this.url_basescript + this.scriptName[this.registered[language]], options);
			return false;
		}
		if (typeof cafen.getMonitor != 'undefined')
			cafen.getMonitor(this.version);
		var highlighter = new dp.sh.Brushes[this.registered[language]]();
		highlighter.noGutter = (options.nogutter !=null) ? options.nogutter : false;
		highlighter.addControls = (options.nocontrols !=null) ? options.nocontrols : false;
		highlighter.collapse = (options.collapse !=null) ? options.collapse : false;
		highlighter.showColumns = (options.showcolumns !=null) ? options.showcolumns : false;
		if (highlighter.Style)
			this.addStyle(highlighter.Style);
		highlighter.firstLine = (options.firstline !=null) ? options.firstline : 1;
		highlighter.Highlight(options.text);
		if (options.callBack != null) {
			switch(options.result) {
				case 'txt' :
					var tmpObj = document.createElement('div');
					tmpObj.appendChild(highlighter.div);
					options.callBack(tmpObj.innerHTML);
					break;
				default :
					options.callBack(highlighter.div);
					break;
			}									
		}
		return true;
	},
	renderElements : function(renderObjs) {
		for(var i=0; i < renderObjs.length; i++) 
			this.renderElement(renderObjs[i]);
	},
	renderElement : function(element) {
		if (element instanceof Array) {
			for(var i = 0; i < element.length; i++) {
				this._renderElement(element[i]);
			}
			return true;
		} else
			return this._renderElement(element);
	},
	_renderElement : function(element) {
		if (typeof dp == 'undefined' || typeof dp.sh == 'undefined' || typeof dp.sh.Brushes == 'undefined') {
			this.showMsg(element, 'Cannot load SyntaxHighlighter!! <br>load the Script ['+this.url_basescript+'<b>shCore.js</b>] on head line!');
			return false;
		}
		var elementArrtib = new cafen.Attribute(element);
		var language = elementArrtib.getString('language','xml').toLowerCase();
		var position = elementArrtib.getString('position','before').toLowerCase();
		if (this.registered[language] == null) {
			this.showMsg(element, 'Cannot parse the language [<b>'+language+'</b>]!! CheckOut from the scriptList.');
			return true;
		}
		this.showMsg(element, 'Wait for a second during parse ' + language);
		if (typeof dp.sh.Brushes[this.registered[language]] == 'undefined') {
			this.getScript(this.url_basescript + this.scriptName[this.registered[language]], element);
			return false;
		}
		if (typeof cafen.getMonitor != 'undefined')
			cafen.getMonitor(this.version);
		this.addMainStyle();
		var highlighter = new dp.sh.Brushes[this.registered[language]]();
		highlighter.noGutter = elementArrtib.getBoolean('nogutter', false);
		highlighter.addControls = !elementArrtib.getBoolean('nocontrols', false);
		highlighter.collapse = elementArrtib.getBoolean('collapse', false);
		highlighter.showColumns = elementArrtib.getBoolean('showcolumns', false);
		if (highlighter.Style)
			this.addStyle(highlighter.Style);
		highlighter.firstLine = elementArrtib.getInt('firstline', 1);
		if (element.tagName == 'TEXTAREA')
			highlighter.Highlight(element.value);
		else
			highlighter.Highlight(element.innerHTML);
		
		highlighter.source = element;
		switch(position) {
			case 'after' :
				element.parentNode.insertBefore(highlighter.div, element.nextSibling);
				break;
			default :
				element.parentNode.insertBefore(highlighter.div, element);
				break;
		}
		element.linkObj = highlighter.div;
		highlighter.div.style.textAlign = 'left';
		this.showMsg(element, '');
		return true;
	},	
	/**
	* render Elements by className
	* @param {string}  className The class name of the Object to do render
	*/
	renderElementsByClassName : function(className) {
		var objs = cafen.getElementsByTagName('*');
		var result = [];
		for(var i =0; i < objs.length; i++) 
			if (objs[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) 
				result.push(objs[i]);
		this.renderElements(result);
	},
	/**
	* render Elements by id
	* @param {string}  id The id of the Object to do render
	*/
	renderElementsById : function(id) {
		if (document.getElementById(id) != null) {
			var objs = document.getElementsByTagName(document.getElementById(id).tagName);
			var result = [];
			for(var i=0;i<objs.length;i++)
				if(objs[i].id==id) 
					result.push(objs[i]);
			this.renderElements(result);
		}
	},
	renderElementsByName : function(name, tagName) {
		if (tagName == null)
			tagName = '*';
		var tags = cafen.getElementsByTagName(tagName);
		var result = [];
		for(var i = 0; i < tags.length; i++)
			if(tags[i].getAttribute('name') == name)
				result.push(tags[i]);
		this.renderElements(result);
	},
	addStyle : function(stylecss) {
		var headNode = document.getElementsByTagName('head')[0];
		if(stylecss && headNode) {
			var styleNode = document.createElement('style');
			styleNode.setAttribute('type', 'text/css');
			if(styleNode.styleSheet) {
				try {
					styleNode.styleSheet.cssText = stylecss;
				} catch(ex) {
					styleNode.nodeText = stylecss;
				}
			}	else {
				var textNode = document.createTextNode(stylecss);
				styleNode.appendChild(textNode);
			}
			headNode.appendChild(styleNode);
		}
	},
	
	addMainStyle : function() {
		if (!syntaxStyleAdded) {
			var style = '.dp-highlighter'
			+ '{'
			+ '	font-family: Consolas, Courier New, Courier, mono, serif;'
			+ '	font-size: 12px;'
			+ '	background-color: #E7E5DC;'
			+ '	width: 99%;'
			+ '	overflow: hidden;'
			+ '	text-overflow:ellipsis !important;'
			+ '	margin: 3px 0 3px 0 !important;'
			+ '	padding-top: 1px; /* adds a little border on top when controls are hidden */'
			+ '	white-space: nowrap;'
			+ '}'
			+ '.dp-highlighter ol,'
			+ '.dp-highlighter ol li,'
			+ '.dp-highlighter ol li span '
			+ '{'
			+ '	margin: 0;'
			+ '	padding: 0;'
			+ '	border: none;'
			+ '}'
			+ '.dp-highlighter a,'
			+ '.dp-highlighter a:hover'
			+ '{'
			+ '	background: none;'
			+ '	border: none;'
			+ '	padding: 0;'
			+ '	margin: 0;'
			+ '}'
			+ '.dp-highlighter .bar'
			+ '{'
			+ '	padding-left: 45px;'
			+ '}'
			+ '.dp-highlighter.collapsed .bar,'
			+ '.dp-highlighter.nogutter .bar'
			+ '{'
			+ '	padding-left: 0px;'
			+ '}'
			+ '.dp-highlighter ol'
			+ '{'
			+ '	list-style: decimal; /* for ie */'
			+ '	background-color: #fff;'
			+ '	margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */'
			+ '	padding: 0px;'
			+ '	color: #5C5C5C;'
			+ '}'
			+ '.dp-highlighter.nogutter ol,'
			+ '.dp-highlighter.nogutter ol li'
			+ '{'
			+ '	list-style: none !important;'
			+ '	margin-left: 0px !important;'
			+ '}'
			+ '.dp-highlighter ol li,'
			+ '.dp-highlighter .columns div'
			+ '{'
			+ '	list-style: decimal-leading-zero; /* better look for others, override cascade from OL */'
			+ '	list-style-position: outside !important;'
			+ '	border-left: 3px solid #6CE26C;'
			+ '	background-color: #F8F8F8;'
			+ '	color: #5C5C5C;'
			+ '	padding: 0 3px 0 10px !important;'
			+ '	margin: 0 !important;'
			+ '	line-height: 15px;'
			+ '}'
			+ '.dp-highlighter.nogutter ol li,'
			+ '.dp-highlighter.nogutter .columns div'
			+ '{'
			+ '	border: 0;'
			+ '}'
			
			+ '.dp-highlighter .columns'
			+ '{'
			+ '	background-color: #F8F8F8;'
			+ '	color: gray;'
			+ '	overflow: hidden;'
			+ '	width: 100%;'
			+ '}'
			+ '.dp-highlighter .columns div'
			+ '{'
			+ '	padding-bottom: 5px;'
			+ '}'
			+ '.dp-highlighter ol li.alt'
			+ '{'
			+ '	background-color: #FFF;'
			+ '	color: inherit;'
			+ '}'
			+ '.dp-highlighter ol li span'
			+ '{'
			+ '	color: black;'
			+ '	background-color: inherit;'
			+ '}'
			+ '.dp-highlighter.collapsed ol'
			+ '{'
			+ '	margin: 0px;'
			+ '}'
			+ '.dp-highlighter.collapsed ol li'
			+ '{'
			+ '	display: none;'
			+ '}'
			+ '.dp-highlighter.printing'
			+ '{'
			+ '	border: none;'
			+ '}'
			+ '.dp-highlighter.printing .tools'
			+ '{'
			+ '	display: none !important;'
			+ '}'
			+ '.dp-highlighter.printing li'
			+ '{'
			+ '	display: list-item !important;'
			+ '}'
			+ '.dp-highlighter .tools'
			+ '{'
			+ '	padding: 3px 8px 3px 10px;'
			+ '	font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif;'
			+ '	color: silver;'
			+ '	background-color: #f8f8f8;'
			+ '	padding-bottom: 10px;'
			+ '	border-left: 3px solid #6CE26C;'
			+ '}'
			+ '.dp-highlighter.nogutter .tools'
			+ '{'
			+ '	border-left: 0;'
			+ '}'
			+ '.dp-highlighter.collapsed .tools'
			+ '{'
			+ '	border-bottom: 0;'
			+ '}'
			+ '.dp-highlighter .tools a'
			+ '{'
			+ '	font-size: 9px;'
			+ '	color: #a0a0a0;'
			+ '	background-color: inherit;'
			+ '	text-decoration: none;'
			+ '	margin-right: 10px;'
			+ '}'
			+ '.dp-highlighter .tools a:hover'
			+ '{'
			+ '	color: red;'
			+ '	background-color: inherit;'
			+ '	text-decoration: underline;'
			+ '}'
			+ '.dp-about { background-color: #fff; color: #333; margin: 0px; padding: 0px; }'
			+ '.dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; }'
			+ '.dp-about td { padding: 10px; vertical-align: top; }'
			+ '.dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; }'
			+ '.dp-about .title { color: red; background-color: inherit; font-weight: bold; }'
			+ '.dp-about .para { margin: 0 0 4px 0; }'
			+ '.dp-about .footer { background-color: #ECEADB; color: #333; border-top: 1px solid #fff; text-align: right; }'
			+ '.dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; color: #333; width: 60px; height: 22px; }'
			+ '.dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; }'
			+ '.dp-highlighter .string { color: blue; background-color: inherit; }'
			+ '.dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; }'
			+ '.dp-highlighter .preprocessor { color: gray; background-color: inherit; }';
			this.addStyle(style);
			syntaxStyleAdded = true;
		}			
	},
	checkIsLoaded : function(src) {
		var canbe_added = true;
		for(var i = 0; i < this.addedScript.length ; i++) 
			if (this.addedScript[i].src == src) 
				canbe_added = false;
		return canbe_added;
	},
	getScript : function(src, element) {
		if (this.checkIsLoaded(src)) {
			var script = document.createElement('script');
			script.setAttribute('type','text/javascript');
			script.setAttribute('src',src);
			this.addedScript.push({obj : script, src : src});
			document.getElementsByTagName('head')[0].appendChild(script);
		}
		var donotadd = false;
		for (var i = 0; i < this.addedElements.length; i++) 
			if (this.addedElements[i] == element)
				donotadd = true;
		if (!donotadd) {
			this.addedElements.push(element);
			this.tryoutNo = 0;
			this.doTimeOut();
		}
	},
	doTimeOut : function() {
		if (this.addedElements.length > 0) {
			this.tryoutNo++;
			if (this.tryoutNo > this.tryoutMax || ((this.addedElements[0].text != null) ? this.renderTxt(this.addedElements[0]) : this.renderElement(this.addedElements[0]))) {
				if (this.tryoutNo > this.tryoutMax) {
					this.showMsg(this.addedElements[0], 'Timeout to Collect Data. Sorry!!');
					this.tryoutNo = 0;
				}
				var lastElement = [];
				for (var i = 1; i < this.addedElements.length; i++) 
					lastElement.push(this.addedElements[i]);
				this.addedElements = lastElement;
			}
			if (this.addedElements.length > 0) {
				var self = this;
				window.setTimeout(function () { self.doTimeOut(); },500);
			}
		}
	}
}


var cafenCode_HTC = new cafenSyntaxHighlighter();

if (cafen.checkLoad != null)
	cafen.checkLoad();

