/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// Show the source-code of a linked file
	jQuery.classBehaviours.handlers.showSourceCode = {
		// properties
		name: 'showSourceCode',
		// methods
		start: function(node){
			// link event handler
			node.onclick = this.loadFile;
		},
		waitForFile: function(waitStatus, waitNode, waitError){
			// find the target node
			targetId = jQuery.classBehaviours.utilities.getClassParameter(waitNode, 'id', 'tbxExampleCode');
			targetNode = document.getElementById(targetId);
			// show the code object
			targetNode.style.display = 'block';
			// set its status
			if(waitStatus>0){
				jQuery.classBehaviours.fader.setFade(targetNode, waitStatus*100);
				targetNode.innerHTML = 'loading: ' + Math.round(waitStatus * 100) + '%';
			}else{
				jQuery.classBehaviours.fader.setFade(targetNode, 100);
				targetNode.innerHTML = 'error: ' + waitError;
			}
		},
		processFile: function(processXml, processNode, processText){
			// find the target node
			targetId = jQuery.classBehaviours.utilities.getClassParameter(processNode, 'id', 'tbxExampleCode');
			targetNode = document.getElementById(targetId);
			// if the content came from the exception process it first
			if(processText.indexOf('<!-- exampleCode -->')>-1 && processNode.href.indexOf('/xhtml/')>-1){
				processText = processText.split('<!-- exampleCode -->')[1].split('<!-- /exampleCode -->')[0];
			}
			// encode the content
			processText = processText.replace(/>/gi, '&gt;').replace(/</gi, '&lt;').replace(/\t/gi, '&nbsp;&nbsp;&nbsp;');
			// publish the content
			targetNode.innerHTML = '<pre class="codeExample">' + processText + '</pre>';
		},
		// events
		loadFile: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ssc = jQuery.classBehaviours.handlers.showSourceCode;
			// process the link for an exception
			targetLink = (objNode.href.indexOf('/xhtml/')>-1) ? document.location.href : objNode.href;
			// load the targeted file
			jQuery.classBehaviours.ajax.addRequest(targetLink, ssc.processFile, ssc.waitForFile, null, objNode);
			// cancel the click
			return false;
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.showSourceCode = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.showSourceCode.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".showSourceCode").showSourceCode();
			}
		);
	}
