/**
 * Call the H_Init method when the document is ready.
 */
$(document).ready(H_Init);

/**
 * Removes whitespace from either side of 
 * the string. 
 */
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

/** 
 * Iterates through each <pre><code>...</code></pre>
 * block and turns it into an ordered list via the 
 * H_ToList method.
 * @see #H_ToList
 */
function H_Init()
{
	$("pre code").each(H_ToList);
}

/**
 * Turn a single <pre><code>...</code></pre> block 
 * into a list.
 */
function H_ToList()
{
	// Get the actual code
	var code = $(this).text();
	
	// Split it into lines
	var lines = code.split("\n");
	
	for(i = 0; i < lines.length; i++)
	{
		// var line = lines[i];
		
		// H_CreateLine(line);
		
		// If the line is empty, then add a space to the line because 
		// otherwise the line won't show up properly.
		// if (line.trim() == "")
		// {
		// 	line = "&nbsp;";
		// }
			
		// Create a list item out of the line	
		// line = "<li><pre><code>" + line + "</code></pre></li>";
		
		lines[i] = H_CreateLine(lines[i]);
		
		// FireBug Console
		console.log(lines[i]);
	}
	
	// Remove everything in and including the current <pre> element and replace it 
	// with the ordered list we created. 
	$(this).parent().after("<ol class='code'>" + lines.join("") + "</ol>").remove();
	
	// Zebra stripes
	$("ol.code li:nth-child(odd)").addClass("odd");
}

function H_CreateLine(line)
{
	if (line.trim() == '')
	{
		return "\t<li><code>&nbsp;</code></li>\n";
	}
	
	return "\t<li><pre><code>" + line + "</code></li>\n";
	
	var spaces = new RegExp(/^\s{4}/g);
	
	if (line.match(spaces))
	{
		var count = line.match(spaces).length;
		
		return "\t<li class=\"tabbed" + count + "\"><code>" + line + "</code></li>\n";
	}
	
	else
	{
		return "\t<li><code>" + line + "</code></li>\n";
	}
}