/**
 * Mojebus Code Highlighter
 * http://moronicbajebus.com/blog/code-highlighter/
 *   
 * Copyright stuff first,
 *
 * Copyright (c) 2009 Seamus P. H. Leahy
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * 
 * Now that is done with, how to use this:
 *
 * This depends upon the jQuery and Chili library.
 * 
 * It converts the code samples that match "#content pre.sample code".
 * The code element has three (3) custom attributes for the settings:
 *  1. language: the language to highlight for, "css", "html", "js", etc. (ref Chili for full list)
 *  2. demo: flag if to create demo iframe below, the default is false, only should be used with full HTML samples
 *  3. line-number-start: number to start the line-numbering on, the default is 1
 *
 * <div id="content">
 *  ...
 *    <pre class="sample"><code language="html" demo="true" line-number-start="1">
 *      &lt;html>
 *        &lt;body>
 *          &lt;h1>Hello World!&lt;/h1>
 *        &lt;/body>
 *    </code></pre>
 *  ...
 * </div>
 *
 */

// My Chili settings 
ChiliBook.recipeFolder = "/media/theme/scripts/chili/";  
ChiliBook.stylesheetFolder = "/media/theme/scripts/chili/";  
ChiliBook.stylesheetLoading = false; 
ChiliBook.automatic = false;
ChiliBook.lineNumbers = true;
ChiliBook.replaceTab = "&#160 "; // non-breaking space and breaking space
ChiliBook.replaceSpace = " "
ChiliBook.codeLanguage = function( el ) { // figure out the language with custom attribute on the code element
	var recipeName = $( el ).attr( "language" );
	return recipeName ? recipeName : '';
}

// The next two are custom settings in my modified version of Chili
ChiliBook.enableSelection = false;
ChiliBook.loadInlineStyles = false;



jQuery( document ).ready( function( ){
jQuery( '#content pre.sample code' ).each(
	function( ){
		var $this = jQuery( this );
		
		// Remove the blank lines at start and bottom
		if( !jQuery.browser.msie ){
			var trimmedHtml = $this.html( );
			trimmedHtml = trimmedHtml.replace( /^\s*[\r\n]+/, '' ).replace( /[\r\n]+\s*$/, '' );
			$this.html( trimmedHtml );
		}
		
		// Line numbering
		var lineNumberStart = $this.attr( 'line-number-start' );
		if( !isNaN( lineNumberStart ) && parseInt( lineNumberStart ) == lineNumberStart ){
			$this.css( 'counter-reset', 'codeLineNumber '+(lineNumberStart-1) );
		}
		
		// Iframe with demo
		var source = $this.text();
		if( $this.attr( 'demo' ) == 'true' ){
			var iframe = jQuery( '<iframe class="demo" frameborder="0"/>' );
			$this.parent( ).after( iframe );
			
			var doc = iframe.get( 0 ).contentDocument || iframe.contentWindow.document;
			doc.open();
			doc.write( source );
			doc.close();
			
			// expand the iframe to size of the inside content
			var resizeIframe = setInterval( function( ){
				if( doc.body && doc.body.scrollHeight ){
					iframe.height( doc.body.scrollHeight );
					clearInterval( resizeIframe );
				}
			}, 500 );
			//doc.body.scrollHeight; // This needs be called before use in IE, who knows
			//
		}
		
		// Plain source
		if( $this.attr( 'plain' ) != 'false' ){
			var filtered_source = $this.text( );
			filtered_source = filtered_source.replace( /^\s*[\r\n]+/, '' ).replace( /[\r\n]+\s*$/, '' ).replace( /&/g, '&amp;' ).replace( /</g, '&lt;' ).replace( /[\r\n]+/g, '<br/>' );
			
			var $plain = jQuery( '<pre class="plain-source">'+filtered_source+'</pre>' ).insertAfter( $this.parent( ) ).hide( );
		}
		
		if( $this.attr( 'plain' ) != 'false' && $this.attr( 'highlight' ) != 'false' ){
			// Buttons to toggle between plain and highlighted
			var $showHighlighted = jQuery( '<span class="code-sample-button code-sample-button-current">Highlight Code</span>' ).insertBefore( $this.parent( ) );					
			var $showPlain = jQuery( '<span class="code-sample-button">Plain Text</span>' ).insertBefore( $this.parent( ) );
			$showPlain.click( function( ){
				$showHighlighted.removeClass( 'code-sample-button-current' );
				$this.hide( )
				$showPlain.addClass( 'code-sample-button-current' );
				$plain.show( );
			});
			$showHighlighted.click( function( ){
				$showHighlighted.addClass( 'code-sample-button-current' );
				$this.show( )
				$showPlain.removeClass( 'code-sample-button-current' );
				$plain.hide( );
			});
		}
		
		// Give it the code highlighting
		if( $this.attr( 'highlight' ) != 'false' ){
			$this.chili( ); 
		} else {
			$this.hide( );
		}
	}
);
	
} );
		