/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.3.17	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(){}

	// Replace a link to external content with the actual content
	jQuery.classBehaviours.handlers.insertFromFile = {
		// properties
		name: 'insertFromFile',
		index: 0,
		// methods
		start: function(node){
			// if the node is marked to load automatically start loading else set an onclick event
			automatic = jQuery.classBehaviours.utilities.getClassParameter(node, 'auto', 'no');
			// apply the event handler or the event directly
			if(automatic=='yes' || node.nodeName=='IFRAME') this.clicked(node)
			else node.onclick = this.clicked;
		},
		wait: function(importProgress, referedNode, importError){
			// get the target for the indicator
			targetId = jQuery.classBehaviours.utilities.getClassParameter(referedNode, 'id', null);
			if(targetId!=null) targetNode = document.getElementById(targetId)
			else targetNode = referedNode;
			// display the progress indicator
			targetNode.innerHTML = 'loading: ' + Math.round(importProgress*100) + '%';
		},
		insert: function(importedObj, referedNode, importedText){
			var iff = jQuery.classBehaviours.handlers.insertFromFile;
			// clean the imported text and replace optional strings
			replaceText = referedNode.href.split('src=')[1];
			importedText = importedText.replace('{slideName}', replaceText);
/*
			cutCommentedSection = jQuery.classBehaviours.utilities.getClassParameter(referedNode, 'cut', null);
			replaceInputsId = jQuery.classBehaviours.utilities.getClassParameter(referedNode, 'replace', null);
			importedText = iff.clean(importedText, replaceInputsId, cutCommentedSection);
*/
			// if an ID was given
			targetId = jQuery.classBehaviours.utilities.getClassParameter(referedNode, 'id', null);
			if(targetId!=null){
				// use that as target
				targetNode = document.getElementById(targetId);
			}else{
				// or replace the current node
				newDiv = document.createElement('div');
				newDiv.id = (newDiv.id) ? newDiv.id : iff.name + iff.index++ ;
				referedNode.parentNode.replaceChild(newDiv, referedNode);
				targetNode = document.getElementById(newDiv.id);
			}
			// insert the content in the given container
			targetNode.innerHTML = importedText;
			// activate any classbehaviours in there
			jQuery.classBehaviours.parser.parseNode(targetNode);
		},
		clean: function(importedText, replaceInputsId, cutCommentedSection){
			// strip the content from the body
			if(importedText.indexOf('<body')>-1){
				importedText = importedText.split('<body')[1].split('</body>')[0];
				importedText = importedText.substr(importedText.indexOf('>') + 1, importedText.length);
			}
			// cut the content at the specified place
			if(cutCommentedSection!=null){
				importedText = importedText.split('<!-- ' + cutCommentedSection + ' -->')[1];
			}
			// process optional replaces
			if(replaceInputsId!=null){
				replaceInputs = document.getElementById(replaceInputsId).getElementsByTagName('INPUT');
				for(var a=0; a<replaceInputs.length; a++) importedText = importedText.replace(replaceInputs[a].name, replaceInputs[a].value);
			}
			return importedText;
		},
		// events
		clicked: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var iff = jQuery.classBehaviours.handlers.insertFromFile;
			// get the url to load
			targetUrl = (objNode.nodeName=='IFRAME') ? objNode.getAttribute('src') : objNode.getAttribute('href');
			targetPost = (objNode.nodeName=='IFRAME') ? document.cookie.replace(/; /gi, "&") : null ;
			// place the AJAX request
			jQuery.classBehaviours.ajax.addRequest(targetUrl, iff.insert, iff.wait, targetPost, 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.insertFromFile = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.insertFromFile.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".insertFromFile").insertFromFile();
			}
		);
	}

