jQuery(document).ready(function(){	
    load_codeformat_css = 0;
	
	if(jQuery('.codeformat.css').size()>0){
		format_css();
    	load_codeformat_css = 1;
	}
	
	if(jQuery('.codeformat.html').size()>0){
		format_html();
		load_codeformat_css = 1;
	}

	//load CSS to format code
	if (load_codeformat_css==1){
		jQuery('head').append(jQuery('<link rel="stylesheet" href="/wp-content/themes/charlene/css/codeformat/codeformat.css" type="text/css" media="screen" />'));
	}


});

function format_css(){


	jQuery('.codeformat.css').each(function(n){
		
		var string= codeformat_create_list(this.innerHTML, 'css_line');		
 		
		//define regexs
			
		var regexs = new Array();
		
		//CSS
			//selectors
			//regexs.push([/([\.#]\w+[\s]*{)/g,'<span class="css_selector">$1</span>']);
			regexs.push([/([^>]*{)/g,'<span class="css_selector">$1</span>']);
		
			
			//property
			regexs.push([/(\w+[-\w+]*:)/g,'<span class="css_property">$1</span>']);
			//regexs.push([/([{\s]*)/g,'<span class="css_property">$1</span>']);
			
			
			//value ->this css class is applied to every line along with <li> tags above
			//regexs.push([/:(\d+\w+);/g,':<span class="css_value">$1</span>;']);
			
			//css selector chars
			regexs.push([/([{}:;])/g,'<span class="css_selector">$1</span>']);
		
		//Find and replace					
		this.innerHTML = codeformat_replace(regexs, string);
		
		
	
	});
}

function format_html(){

	//load CSS to format code
	//jQuery('head').append(jQuery('<link rel="stylesheet" href="/wp-content/themes/charlene/css/codeformat/codeformat.css" type="text/css" media="screen" />'));

	jQuery('.codeformat.html').each(function(n){
		
	var string = this.innerHTML;

		//encode quotations in text
		string = string.replace(/"/g, '&quot;');
		
		//define regexs
		
		var regexs = new Array();

		
		//create regexs, an array with all the regular expressions to be evaluated
		var regexs = new Array();		


		//add tags' regex rules to regexs array
			
			// Generate regexs for HTML code
			tags_list = 'html,head,body,div,p,title,ul,ol,li';
			tags_array = tags_list.split(",");
			tags_class = 'html_tag';			
			regexs = regexs.concat(codeformat_regex_tag_array(tags_array, tags_class));

			//Generate regexs for anchor tags
			tags_list = 'a';
			tags_array = tags_list.split(",");
			tags_class = 'html_anchor';			
			regexs = regexs.concat(codeformat_regex_tag_array(tags_array, tags_class));
			
		//add tag attribute regex to regexs array, so it can be evaluated first
		regexs.push([/=(&quot;.+?&quot;)/g,'=<span class="html_attr">$1</span>']);

		//add closing span regex to regexs array
		//regexs.push([/&gt;/g,'&gt;</span>']); 
			
		//Find and replace					
		var string = codeformat_replace(regexs, string);
	
		this.innerHTML= codeformat_create_list(string, 'html_line');	
	});
}



function codeformat_create_list(string, line_class){
		
		//add <li> tag to each line of text
		var lines = string.split('\n');

		var s="";
		for (i=0; i<lines.length; i++){
			s+='<li><span class="' + line_class + '">' + lines[i] + '</span></li>';
		}
		
		//add <ol>
		string = '<ol>' + s + '</ol>';
		
		return string;
}


/*
Used to format HTML code
This functions takes a list of tags to create the regex 
rules needed to identify these tags and the generate
the code that will be used to replace and format 

Parameters:
-tags_array (array, required): array containing list of tags 
-tags_class (string, required): CSS class to be applied to all tags passed in tag_array

Returns: array with regexs

*/

/*function codeformat_regex_tag_array(tags_array, tags_class){
	regexs = new Array();
	jQuery.each(tags_array, function(index,value){
		rg = new RegExp("&lt;(\/*)"+value, "g");
		regexs.push([rg, '<span class="' + tags_class + '">&lt;$1' + value]);
	});		
	
	return regexs;

}*/

function codeformat_regex_tag_array(tags_array, tags_class){
	regexs = new Array();
	jQuery.each(tags_array, function(index,value){
		//rg = new RegExp("&lt;(\/*)"+value, "g");
		//regexs.push([rg, '<span class="' + tags_class + '">&lt;$1' + value]);
		//rg = new RegExp('&lt;(\/?)'+value+'&gt;', 'g');
		
		exp1='&lt;(\/?)'+value+'&gt;';
		//attr = '[\sid=&quot;container&quot;]*';
		//exp2='&lt;' + value +'\s*(.+?&quot;.+?&quot;)*&gt;';
		exp2='&lt;' + value +'\s*([^>]*)&gt;';
		//exp2='&lt;' + value + '\s*([^(&quot;)]+)*';
		
		//<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>	
		
		rg = new RegExp(exp1, 'g');
		regexs.push([rg, '<span class="' + tags_class + '">&lt;$1' + value + '&gt;</span>']);

		rg = new RegExp(exp2, 'g');
		regexs.push([rg, '<span class="' + tags_class + '">&lt;' + value + '$1&gt;</span>']);


});		
	
	return regexs;

}


function codeformat_replace(regexs, string){
		//Find and replace
		//Now that all regexs are loaded to reps, forach reps, find and replace within code
		jQuery.each(regexs, function(index, value){
			string=string.replace(regexs[index][0], regexs[index][1]);
			if (index==3){
				//alert(string);
			}

		});
		
		
		return string;
}

/*jQuery(document).ready(function(){
	//search for html, css or php blocks
	
	
	
	
	//if(jQuery('pre.html_code').lenght!=0){
		//format_html();
	//}
	
	if(jQuery('.css_code').lenght!=0){
	
		//format_css();
	} 
});*/

function log(s){
	if(jQuery('#debug').length==0){
		jQuery('body').append(jQuery('<div id="debug"></div>'));
	}
	
	jQuery('#debug').append('<p>' + s + '</p>');
}

/*function format_html(){
	//define vars
	var htmlCode = jQuery('pre.html_code').html();
	
	//for testing
	var codeViewer = jQuery('div#code');
	var codeViewer2 = jQuery('div#code2');
	
	//load CSS to format code
	jQuery('head').append(jQuery('<link rel="stylesheet" href="/wp-content/themes/charlene/css/codeformat/html_code.css" type="text/css" media="screen" />'));

	
	
	
	//find and replace 
		// create array with tags
		var tags = ['div', 'p']; 
		
		//create reps, an array with all the regular expressions to be evaluated
		var reps = new Array();		

		//add tag attribute regex to reps array, so it can be evaluated first
		reps.push([/"(\w+)"/g,'<span class="attr">&ldquo;$1&rdquo;</span>']);

		//add tags' regex rules to reps array
		jQuery.each(tags, function(index,value){
			rg = new RegExp("&lt;(\/*)"+value, "g");
			reps.push([rg, '<span class="tag">&lt;$1' + value]);
		});		

		//add closing span regex to reps array
		reps.push([/&gt;/g,'&gt;</span>']); 
	
	
	//for testing
	
	
	//Find and replace
		//Now that all regexs are loaded to reps, forach reps, find and replace within code
		jQuery.each(reps, function(index, value){
			t=htmlCode.replace(reps[index][0], reps[index][1]);
		});
	
	//Assign new formatted html code to pre.html_code element
	jQuery('pre.html_code').html(t);
	
	
	//for testing
	codeViewer.html(t);
	codeViewer2.text(t2);

}*/



/*function format_css(){
	//define vars
	var cssCode = jQuery('pre.css_code').html();


	//load CSS to format code
	jQuery('head').append(jQuery('<link rel="stylesheet" href="http://www.vidalquevedo.com/wp-content/themes/charlene/css/codeformat/html_code.css" type="text/css" media="screen" />'));
	
	//for testing
	var codeViewer = jQuery('div#code');
	var codeViewer2 = jQuery('div#code2');
	var t = t2 = cssCode;	//find and replace 
		
		//create reps, an array with all the regular expressions to be evaluated
		var reps = new Array();
		reps.push([/(\w+)/g,'<span class="selector">$1</span>']);			
	

	//Find and replace
		//Now that all regexs are loaded to reps, forach reps, find and replace within code
		jQuery.each(reps, function(index, value){
			t=t.replace(reps[index][0], reps[index][1]);
		});
	
	//Assign new formatted html code to pre.html_code element
	//jQuery('pre.css_code').html(t);
	
	
	//for testing
	codeViewer.html(t);
	codeViewer2.text(t2);

}*/