// Paintbrush is based on dp.SyntaxHighlighter, http://googlecode.com/p/syntaxhighlighter

var Paintbrush = new Class({

	initialize: function(brush){
		this.brush = (typeof brush == 'string') ? Paintbrush.Brushes[brush] : brush;
		this.regExps = [];
		if (this.brush) this.processBrush();
	},

	paint: function(code){
		this.code = (code || '').replace(/^\n*/, '').replace(/\n*$/, '');
		this.matches = [];
		if (this.regExps.length) this.matchRegExps();
		this.element = new Element('div');

		// if no matches found, add entire code as plain text
		if (!this.matches.length){
			this.addBit(this.code);
			return this.element;
		}

		var pos	= 0;

		// sort the matches
		this.matches = this.matches.sort(this.sortCallback);

		// The following loop checks to see if any of the matches are inside
		// of other matches. This process would get rid of highligted strings
		// inside comments, keywords inside strings and so on.
		for (var i = 0, y = this.matches.length; i < y; i++){
			if (this.isInside(this.matches[i])) this.matches[i] = null;
		}

		// Finally, go through the final list of matches and pull the all
		// together adding everything in between that isn't a match.
		for (i = 0, y = this.matches.length; i < y; i++){
			var match = this.matches[i];
			if (!match) continue;
			this.addBit(this.copy(this.code, pos, match.index));
			this.addBit(match.value, match.css);
			pos = match.index + match.length;
		}

		this.addBit(this.code.substr(pos));
		return this.element;
	},

	processBrush: function(){
		for (var css in this.brush){
			$splat(this.brush[css]).each(function(regExp){
				if (typeof regExp == 'string') regExp = new RegExp('\\b' + regExp.escapeRegExp().split(' ').join('\\b|\\b') + '\\b', 'gm');
				this.regExps.push([regExp, css]);
			}, this);
		}
	},

	matchRegExps: function(){
		this.regExps.each(function(combo){
			this.getMatches(combo[0], combo[1]);
		}, this);
	},

	getMatches: function(regex, css){
		var match;
		while ((match = regex.exec(this.code))){
			this.matches[this.matches.length] = this.matchOne(match[0], match.index, css);
		}
	},

	matchOne: function(value, index, css){
		return {value: value, index: index, length: value.length, css: css};
	},

	copy: function(string, pos1, pos2){
		return string.substr(pos1, pos2 - pos1);
	},

	sortCallback: function(m1, m2){
		// sort matches by index first
		if (m1.index < m2.index) return -1;
		else if (m1.index > m2.index) return 1;
		else {
			// if index is the same, sort by length
			if (m1.length < m2.length) return -1;
			else if(m1.length > m2.length) return 1;
		}
		return 0;
	},

	addBit: function(str, css){
		if (!str) return;
		var span = new Element('span', {'text': str});
		if (css) span.className = css;
		this.element.adopt(span);
	},

	isInside: function(match){
		if (!match) return false;
		for (var i = 0; i < this.matches.length; i++){
			var current = this.matches[i];
			if (current == null) continue;
			if ((match.index > current.index) && (match.index < current.index + current.length)) return true;
		}
		return false;
	}

});

Paintbrush.Brushes = {
  'Note': {},
  'Plain Text': {}
};

Paintbrush.RegExps = {
	multiLineComment: (/\/\*[\s\S]*?\*\//gm),
	singleLineComment: (/(?:^|[^\\])\/\/.*$/gm),
	singleLinePerlComment: (/#.*$/gm),
	doubleQuotedString: (/"(?:\.|(\\\")|[^\""\n])*"/g),
	singleQuotedString: (/'(?:\.|(\\\')|[^\''\n])*'/g),
	integer: (/\b(\d+)\b/gm),
	preprocessor: (/^\s*#.*/gm),
	regularExpression: (/\/[^\/\\\n\*]+(?:\\.[^\/\\\n]*)*\/[gimy]*/g)
};

// Element paint extension

Element.implement({

	paint: function(brush){
		return this.set('html', new Paintbrush(brush).paint(this.get('text')).get('html'));
	}

});


/*
	BRUSHES
*/

// Javascript
Paintbrush.Brushes.Javascript = {

	'comment': [Paintbrush.RegExps.singleLineComment, Paintbrush.RegExps.multiLineComment],

	'string': [Paintbrush.RegExps.singleQuotedString, Paintbrush.RegExps.doubleQuotedString],

	'integer': Paintbrush.RegExps.integer,

	'preprocessor': Paintbrush.RegExps.preprocessor,

	'regexp': Paintbrush.RegExps.regularExpression,

	'boolean': 'true false null',

	'special': 'this window',

	'keyword': 'abstract boolean break byte case catch char class const continue debugger default delete do double ' +
	'else enum export extends final finally float for function goto if implements import in instanceof int interface ' +
	'long native new package private protected public return short static super switch synchronized throw throws transient ' +
	'try typeof var void volatile while with prototype'
};

// PHP
Paintbrush.Brushes.PHP = {

	'comment': [Paintbrush.RegExps.singleLineComment, Paintbrush.RegExps.multiLineComment],

	'string': [Paintbrush.RegExps.singleQuotedString, Paintbrush.RegExps.doubleQuotedString],

	'integer': Paintbrush.RegExps.integer,

	'preprocessor': Paintbrush.RegExps.preprocessor,

	'special': (/\$\w+/g),

	'keyword': 'and or xor __FILE__ __LINE__ array as break case ' +
		'cfunction class const continue declare default die do else ' +
		'elseif empty enddeclare endfor endforeach endif endswitch endwhile ' +
		'extends for foreach function include include_once global if ' +
		'new old_function return static switch use require require_once ' +
		'var while __FUNCTION__ __CLASS__ ' +
		'__METHOD__ abstract interface public implements extends private protected throw',

	'function': 'abs acos acosh addcslashes addslashes ' +
		'array_change_key_case array_chunk array_combine array_count_values array_diff '+
		'array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_fill '+
		'array_filter array_flip array_intersect array_intersect_assoc array_intersect_key '+
		'array_intersect_uassoc array_intersect_ukey array_key_exists array_keys array_map '+
		'array_merge array_merge_recursive array_multisort array_pad array_pop array_product '+
		'array_push array_rand array_reduce array_reverse array_search array_shift '+
		'array_slice array_splice array_sum array_udiff array_udiff_assoc '+
		'array_udiff_uassoc array_uintersect array_uintersect_assoc '+
		'array_uintersect_uassoc array_unique array_unshift array_values array_walk '+
		'array_walk_recursive atan atan2 atanh base64_decode base64_encode base_convert '+
		'basename bcadd bccomp bcdiv bcmod bcmul bindec bindtextdomain bzclose bzcompress '+
		'bzdecompress bzerrno bzerror bzerrstr bzflush bzopen bzread bzwrite ceil chdir '+
		'checkdate checkdnsrr chgrp chmod chop chown chr chroot chunk_split class_exists '+
		'closedir closelog copy cos cosh count count_chars date decbin dechex decoct '+
		'deg2rad delete ebcdic2ascii echo empty end ereg ereg_replace eregi eregi_replace error_log '+
		'error_reporting escapeshellarg escapeshellcmd eval exec exit exp explode extension_loaded '+
		'feof fflush fgetc fgetcsv fgets fgetss file_exists file_get_contents file_put_contents '+
		'fileatime filectime filegroup fileinode filemtime fileowner fileperms filesize filetype '+
		'floatval flock floor flush fmod fnmatch fopen fpassthru fprintf fputcsv fputs fread fscanf '+
		'fseek fsockopen fstat ftell ftok getallheaders getcwd getdate getenv gethostbyaddr gethostbyname '+
		'gethostbynamel getimagesize getlastmod getmxrr getmygid getmyinode getmypid getmyuid getopt '+
		'getprotobyname getprotobynumber getrandmax getrusage getservbyname getservbyport gettext '+
		'gettimeofday gettype glob gmdate gmmktime ini_alter ini_get ini_get_all ini_restore ini_set '+
		'interface_exists intval ip2long is_a is_array is_bool is_callable is_dir is_double '+
		'is_executable is_file is_finite is_float is_infinite is_int is_integer is_link is_long '+
		'is_nan is_null is_numeric is_object is_readable is_real is_resource is_scalar is_soap_fault '+
		'is_string is_subclass_of is_uploaded_file is_writable is_writeable mkdir mktime nl2br '+
		'parse_ini_file parse_str parse_url passthru pathinfo readlink realpath rewind rewinddir rmdir '+
		'round str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split '+
		'str_word_count strcasecmp strchr strcmp strcoll strcspn strftime strip_tags stripcslashes '+
		'stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk '+
		'strpos strptime strrchr strrev strripos strrpos strspn strstr strtok strtolower strtotime '+
		'strtoupper strtr strval substr substr_compare'
};

// RUBY
Paintbrush.Brushes.Ruby = {

	'comment': Paintbrush.RegExps.singleLinePerlComments,

	'string': [Paintbrush.RegExps.singleQuotedString, Paintbrush.RegExps.doubleQuotedString],

	'integer': Paintbrush.RegExps.integer,

	'preprocessor': Paintbrush.RegExps.preprocessor,

	'symbol': (/:[a-z][A-Za-z0-9_]*/g),

	'variable': (/(\$|@@|@)\w+/g),

	'keyword': 'alias and BEGIN begin break case class def define_method defined do each else elsif ' +
		'END end ensure false for if in module new next nil not or raise redo rescue retry return ' +
		'self super then throw true undef unless until when while yield',

  	'special': 'Array Bignum Binding Class Continuation Dir Exception FalseClass File::Stat File Fixnum Fload ' +
		'Hash Integer IO MatchData Method Module NilClass Numeric Object Proc Range Regexp String Struct::TMS Symbol ' +
		'ThreadGroup Thread Time TrueClass'

};

window.addEvent("domready", function(){
	$$('pre').each(function(item){
		var lang = item.get('class') || "Javascript";
		item.paint(lang);
	});
});