// Name: Highlight 
// Source: http://marijn.haverbeke.nl/codemirror/
function _highlight(id) {
var csss='<style type="text/css" id="HighlightEd">'
+'.CodeMirror-line-numbers {background-color:#EEEEEE;color:#AAAAAA;font-family:monospace;font-size:10pt;padding-right:0.3em;padding-top:0.4em;text-align:right;width:2.2em}'
+'.codepad {}'
+'.codepad > div {height:100%}'
+'.codepad-buttons {background-color:#f0f0f0;color:#000000;border-bottom:1px solid #aaaaaa;padding:3px;overflow:hidden;zoom:1;-moz-user-select:none}'
+'.codepad-separator {height:24px;margin:1px;width:0px;border-left:3px double #C0C0C0;float:left}'
+'.codepad-button {background-color:#e0e0e0;float:left;cursor:pointer;border:1px solid #c0c0c0;padding:1px;margin:1px;font-size:11px}'
+'.codepad-button span {padding:0 5px 0 3px}'
+'.codepad-button img {margin:2px;height:16px;width:16px;background:url(Scripts/CodeMirror/menuicon.png) no-repeat 0 0;vertical-align:middle}'
+'.codepad-button-active {background-color:#b0b0b0}'
+'.codepad-button-disabled {cursor:default;filter:alpha(opacity=100);background-color:#f0f0f0}'
+'.codepad-button-disabled img {opacity:0.3;filter:alpha(opacity=30)}'
+'.codepad-button-Undo img {background-position:0 -48px}'
+'.codepad-button-Redo img {background-position:0 -64px}'
+'.codepad-button-LineNumbers img {background-position:0 -112px}'
+'.codepad-button-Wraping img {background-position:0 -128px}'
+'.codepad-button-Reindent img {background-position:0 -144px}'
+'.codepad-button-FileManager img {background-position:0 -160px}'
+'</style>';
if (!document.getElementById('HighlightEd'))
$jq("head").append(csss);
return new Highlight(id,{code:'html'});
}

function Highlight(id,params) {

	this.constructor = Highlight;

	this.idPrefix = 'codepad_';

	this.textarea_id = id;

	this.editor = {};
	this.button = {};

	this.opts = jQuery.extend({
		code:'html'
	}, params || {});
	
	this.translate = params!=undefined
				?jQuery.extend(Highlight.translate, params.translate || {})
				:Highlight.translate;
	
	if (!CodeMirror)
		throw new Error("CodeMirror must be loaded for code sintax highlight.");

	if (!document.getElementById(this.textarea_id))
		throw new Error("No textarea with this ID.");

	this.init();

};
Highlight.translate = {
	'Undo':'Undo',
	'Redo':'Redo',
	'LineNumbers':'Line Numbers',
	'WordWrap':'Word Wrap',
	'Adjust':'Adjust Code',
	'HighlightOff':'Code Highlight Off'
};
Highlight.prototype = {

	onChangeText: function() {
		var h = this.editor.historySize();

		if (h.undo==0) this.button.$Undo.addClass('codepad-button-disabled');
		else this.button.$Undo.removeClass('codepad-button-disabled');

		if (h.redo==0) this.button.$Redo.addClass('codepad-button-disabled');
		else this.button.$Redo.removeClass('codepad-button-disabled');
	},

	init: function() {

		this.path1 = 'Scripts/CodeMirror/';

		var files = this.getFilesParser(this.opts.code);
		var self = this;
		
		var textarea = document.getElementById(this.textarea_id);
		
		var height = "300px";
		if(textarea.style.height != undefined)
			height = textarea.style.height;
		
		this.editor = CodeMirror.fromTextArea(this.textarea_id, {
					parserfile: files.parserfile,
					stylesheet: files.stylesheet,
					app: this,
					indentUnit: 4,
					path: 'Scripts/CodeMirror/js/',
					lineNumbers: true,
					textWrapping: false,
					tabMode: "shift",
					height: height,
					onChange: function(){self.onChangeText()},
					initCallback: function(){
								self.editor.focus();
								self.editor.active=true;}
					});

		this.buttonPanel = document.createElement('div');
		this.buttonPanel.className = 'codepad-buttons';

		this.makeButton("Undo", this._txt('Undo'), {disabled:1});
		this.makeButton("Redo", this._txt('Redo'), {disabled:1});
		this.makeSeparator();
		this.makeButton("LineNumbers", this._txt('LineNumbers'), {active:1});
		this.makeButton("Wraping", this._txt('WordWrap'), {disabled:1});

		if (this.opts.code!='css') {
			this.makeSeparator();
			this.makeButton("Reindent", this._txt('Adjust'));
		}

		this.makeButton("Off", this._txt('HighlightOff'), {right:1});

		$jq('#'+this.textarea_id).before(this.buttonPanel);

		return this.buttonPanel;
	},

   	_txt: function (sign) {
		var s = this.translate[sign];
		return s||sign;
	},

	makeSeparator: function (width) {

		width = 0 || width;

		var sep = document.createElement("span");
		sep.className = 'codepad-separator';
		if (width) sep.style.width = parseInt(width)+'px';
		this.buttonPanel.appendChild(sep);
	},

	makeButton: function (action, name, opts) {

			opts = opts || {};

			var button = document.createElement("div");

			button.id = this.idPrefix+'button'+action;
			button.title = name;

			var cn = 'codepad-button codepad-button-'+action;
			if (opts.disabled) cn +=' codepad-button-disabled';
			if (opts.active) cn +=' codepad-button-active';
			button.className = cn;

			if (opts.right) {
				button.style.styleFloat='right';
				button.style.cssFloat='right';
			}

			button.onmouseout = function(){this.style.backgroundColor=''};
			button.onmouseover = function(){if(!$jq(this).hasClass('codepad-button-disabled')) this.style.backgroundColor='silver'};
			button.onmousedown = function(){if(!$jq(this).hasClass('codepad-button-disabled')) this.style.backgroundColor='gray'};
			button.onmouseup = function(){this.style.backgroundColor=''};

			var img = new Image();
			img.src='Scripts/CodeMirror/1px.gif';
			img.alt=name;
			button.appendChild(img);
			if (opts.hint) {
				var sp = document.createElement("span");
				sp.innerHTML = action;
				button.appendChild(sp);
			}

			this.buttonPanel.appendChild(button);
			var $button = $jq(button);
			this.button[action] = button;
			this.button['$'+action] = $button;
			$button.bind('click',this,
					function(e) {
						if (e.data.editor!=null) {
							if (!$jq(this).hasClass('codepad-button-disabled'))
								e.data['button'+action+'Click'].call(e.data);
						}
					});
 	},
	
	buttonOffClick: function() {
		debugger;
		var t_area = document.getElementById(this.textarea_id);
		if(t_area){
			if(t_area.style.display=='none') {
				$jq('#'+this.textarea_id).show();
				$jq('iframe').hide();
				this.getTxtCode();
			}else{
				$jq('#'+this.textarea_id).hide();
				$jq('iframe').show();
				this.editor.setCode(t_area.value);
			}
		}
	},

	buttonReindentClick: function() {
		if (this.editor.selection()) {
			this.editor.reindentSelection();
			this.editor.focus();
		}
		else
			_uWnd.alert('<div style="padding-top:15px">Please, select a text.</div>','',{w:230,h:90,tm:3000});
	},
	
	buttonUndoClick: function() {
		this.editor.undo();
	},
	
	buttonRedoClick: function() {
		this.editor.redo();
	},
	
	buttonMacroClick: function() {
		var name = prompt("Name your function:", "");
		if (name)
			this.editor.replaceSelection("function " + name + "() {\n  \n}\n");
	},
	
	buttonLineNumbersClick: function() {
		this.editor.options.lineNumbers = !this.editor.options.lineNumbers;
		if (this.editor.options.lineNumbers) {
			this.editor.frame.nextSibling.style.width = '';
			this.button.$LineNumbers.addClass('codepad-button-active');
		}
		else {
			this.editor.frame.nextSibling.style.width = '0px';
			this.button.$LineNumbers.removeClass('codepad-button-active');
		}

		this.buttonWrapingClick(this.editor.options.lineNumbers);
	},
	
	buttonWrapingClick: function(linesOn) {
		this.editor.options.textWrapig = !this.editor.options.textWrapig;

		if (linesOn!=undefined) {
			this.editor.options.textWrapig = false;
			if (linesOn)
				this.button.$Wraping.addClass('codepad-button-disabled');
			else
				this.button.$Wraping.removeClass('codepad-button-disabled');
		}

		if (this.editor.options.textWrapig) {
			this.editor.editor.container.style.whiteSpace='normal';
			this.button.$Wraping.addClass('codepad-button-active');
		}
		else {
			this.editor.editor.container.style.whiteSpace='nowrap';
			this.button.$Wraping.removeClass('codepad-button-active');
		}
	},

	getFilesParser: function(types){
		var files = {};

		switch (types) {
			case 'html':
			case 'htm':
			case 'xml':
				files =	{
						parserfile: ["parsexml.js","parsecss.js","tokenizejavascript.js","parsejavascript.js","parsehtmlmixed.js"],
						stylesheet: [this.path1 + "css/xmlcolors.css",this.path1 + "css/jscolors.css",this.path1 + "css/csscolors.css"]
				};
				break;
			case 'css':
				files =	{
						parserfile: "parsecss.js",
						stylesheet: this.path1 + "css/csscolors.css"
				};
				break;
            default:
				files = {
					parserfile: 'parsedummy.js',
					stylesheet: ''
				}
		}
		return files;
	},

	getTxtCode: function(){
		$jq('#'+this.textarea_id).val(this.editor.getCode());
	},

	setTxtCode: function(str) {
		$jq('#'+this.textarea_id).val(str);
		this.editor.setCode(str);
	}
};
