/*
 * jQuery Colorizer Plugin
 * Copyright 2009 Evan Byrne (http://www.evanbot.com)
 */

jQuery.fn.colorize = function(lang)
{
	this.each(function()
	{
		if($.browser.msie == true){return this;}
		if(lang==null){lang='all';}
		var code = $(this).html();
		code = code+"\n";
		
		// Quotes
		code = code.replace(/'/g,'&#39;');
		code = code.replace(/"/g,'&#34;');
		code = code.replace(/\&\#39;(.*?)\&\#39;/g,"<strong class='jc_string'>&#39;$1&#39;</strong>");
		code = code.replace(/\&\#34;(.*?)\&\#34;/g,"<strong class='jc_string_double'>&#34;$1&#34;</strong>");
		
		// Language Specials
		if(lang=='php'||lang=='all')
		{
			code = code.replace(/([ \t\n]+)(function|return|include|include_once|require|require_once|new|class|extends|private|public|protected|static|self|this|array|const|define)([ \t\(]+)/ig,"$1<span class='jc_special'>$2</span>$3");
			code = code.replace(/^(function|return|include|include_once|require|require_once|new|class|extends|private|public|protected|static|self|this|array|const|define)([ \t\(]+)/ig,"<span class='jc_special'>$1</span>$2");
			
			// Variables
			code = code.replace(/\$([_a-zA-Z0-9]+)/g,"<span class='jc_variable'>&#36;$1</span>");
		}
		
		if(lang=='javascript'||lang=='all')
		{
			code = code.replace(/([ \t\n]+)(function|var|return|new|object|this|array)([ \t\(]+)/ig,"$1<span class='jc_javascript_special'>$2</span>$3");
			code = code.replace(/^(function|var|return|new|object|this|array)([ \t\(]+)/ig,"<span class='jc_javascript_special'>$1</span>$2");
		}
		
		if(lang=='css')
		{
			code = code.replace(/([-_a-z]+):/ig,"<span class='jc_css_setting'>$1</span>:");
			code = code.replace(/\.([_a-z]+)/ig,".<span class='jc_css_class'>$1</span>");
		}
		
		
		// Functions
		if(lang!='css')
		{
			code = code.replace(/([_a-zA-Z0-9]+)\(/g,"<b class='jc_function'>$1</b>(");
		}
		
		
		// Numbers
		//code = code.replace(/!([#]+)([0-9]+)/g,"<span class='jc_number'>$2</span>");
		
		
		// Comments
		if(lang=='php'||lang=='any')
		{
			code = code.replace(/\/\*(.*?)\*\//g,"<span class='jc_comment'>/*$1*/</span>");
			code = code.replace(/\/\/(.*?)\n/g,"<span class='jc_comment'>//$1</span>\n");
		}
		else if(lang=='javascript')
		{
			code = code.replace(/\/\*(.*?)\*\//g,"<span class='jc_javascript_comment'>/*$1*/</span>");
			code = code.replace(/\/\/(.*?)\n/g,"<span class='jc_javascript_comment'>//$1</span>\n");
		}
		else if(lang=='css')
		{
			code = code.replace(/\/\*(.*?)\*\//g,"<span class='jc_css_comment'>/*$1*/</span>");
		}
		
		code = code.replace(/\n$/,"");
		$(this).html(code);
		
		
		// Remove formatting from inside of already formatted elements
		var remove_string = '.jc_string,.jc_special,.jc_function,.jc_comment,'+
				'.jc_javascript_comment,.jc_javascript_special,'+
				'.jc_css_comment,.jc_css_setting,.jc_css_class,.jc_string_double';
		
		// for only double quotes
		$(this).find('>.jc_string_double').each(function(){
		
			$(this).find(remove_string).removeAttr('class');
		});
		
		remove_string = (remove_string+',.jc_variable');
		
		$(this).find(
			'>.jc_comment,>.jc_variables,>.jc_special,>.jc_function,>.jc_string,'+
			'>.jc_javascript_comment,>.jc_javascript_special,'+
			'>.jc_css_comment,>.jc_css_setting,>.jc_css_class').each(function(){
			
			$(this).find(remove_string).removeAttr('class');
			
		});
		
		// Replace <strong>,<b> tags with <span> tags
		code = $(this).html();
		
		code = code.replace(/\<b class="(.*?)"\>(.*?)\<\/b\>/g,"<span class='$1'>$2</span>");
		code = code.replace(/\<strong class="(.*?)"\>(.*?)\<\/strong\>/g,"<span class='$1'>$2</span>");
		
		code = code.replace(/\<strong\>(.*?)\<\/strong\>/g,"<span>$1</span>");
		code = code.replace(/\<b\>(.*?)\<\/b\>/g,"<span>$1</span>");
		
		$(this).html(code);
	});
	
	return this;
};