
$(document).ready(function()
{
	$("script[type=code]").each(function()
	{
		//Grab the text from the script tag.
		//Use .html instead of .text for browser compatiblity.
		var RawCode = $(this).html();

		//Replace tabs with non breaking spaces to indent properly in html.
		RawCode = RawCode.replace(new RegExp("\t", "gi"), "&nbsp;&nbsp;&nbsp;");

		//Some browsers delimit lines in script tags by using \r\n instead of \n
		//so strip out the \r's.
		RawCode = RawCode.replace(new RegExp("\r", "gi"), "");

		//When using the .split() method to return an array some browsers
		//strip out empty cells of the returned array. So here i'm adding
		//a space inbetween double \n's to stop cells being empty.
		RawCode = RawCode.replace(new RegExp("\n\n", "gi"), "\n \n");

		//Split the text into array cells.
		var Lines = RawCode.split(new RegExp("\n"));

		//Remove empty cells at the beginning.
		while(true)
		{
			if(Lines[0] == "" || Lines[0] == " ")
			{	
				Lines.shift();
			}
			else
			{
				break;
			}
		}

		//Remove empty cells at the end.
		while(true)
		{
			if(Lines[Lines.length - 1] == "" || Lines[Lines.length - 1] == " ")
			{	
				Lines.pop();
			}
			else
			{
				break;
			}
		}

		var NewHTML = "<table class=\"code\">"
		NewHTML += "<tr><td class=\"gutter\">1</td><td class=\"line1\"></td></tr>";

		for (var x = 0; x < Lines.length; x++)
		{
			Line = Lines[x];
			NewHTML += "<tr>";
				NewHTML += "<td class=\"gutter\">";
					NewHTML += (x + 2).toString();
				NewHTML += "</td>";

				if (x % 2 == 0)
				{
					NewHTML += "<td class=\"line2\">";
				}
				else
				{
					NewHTML += "<td class=\"line1\">";
				}
				NewHTML += Line;
				NewHTML += "</td>";
			NewHTML += "</tr>";
		}

		if (Lines.length % 2 != 0)
		{
			NewHTML += "<tr><td class=\"gutter\">" + (Lines.length + 2) + "</td><td class=\"line1\"></td></tr>";
		}
		else
		{
			NewHTML += "<tr><td class=\"gutter\">" + (Lines.length + 2) + "</td><td class=\"line2\"></td></tr>";
		}
		
		NewHTML += "</table>";

		$(this).replaceWith(NewHTML);

	});
});

















