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

	// hide the alternative beforehand
	document.writeln("<style>.flashTitle .flashAlternative, .flashTitle .alternative{visibility : hidden;}</style>");
	
	// Use SwfObject to replace a title with a flash version
	jQuery.classBehaviours.handlers.flashTitle = {
		// properties
		name: 'flashTitle',
		index: 0,
		cachedXml: '',
		// methods
		start: function(node){
			// give the node an id of it doesn't have one
			if(!node.id){
				node.id = this.name + this.index;
				this.index += 1;
			}
			// check if there is flash
			requiredVersion = parseInt(jQuery.classBehaviours.utilities.getClassParameter(node, 'version', '7'));
			if(this.checkForFlash(requiredVersion)){
				// load the source file
				if(this.cachedXml=='') this.loadXml(node)
				else if(this.cachedXml=='loading...') this.retryLater(node)
				else this.processXml(null, node, this.cachedXml);
			}else{
				// make the alternative content visible, just in case it didn't work
				jQuery.classBehaviours.handlers.flashTitle.showAlt(node);
			}
		},
		checkForFlash: function(requiredVersion){
			// check if Adobe's flash checking script is available
			return (typeof(DetectFlashVer)!='undefined') ? DetectFlashVer(requiredVersion, 0, 0) : true ;
		},
		loadXml: function(node){
			jQuery.classBehaviours.handlers.flashTitle.cachedXml = 'loading...';
			// load the xml file
			loadUrl = node.getElementsByTagName('input')[0].value;
			jQuery.classBehaviours.ajax.addRequest(loadUrl, this.processXml, this.loadProgress, null, node);
		},
		retryLater: function(node){
			// wait while loading
			setTimeout('jQuery.classBehaviours.handlers.flashTitle.start(document.getElementById("' + node.id + '"))', 100);
		},
		loadProgress: function(progress, referer){
			// if loading fails show the alternative
			if(progress<0) jQuery.classBehaviours.handlers.flashTitle.showAlt(referer);
		},
		processXml: function(sourceDoc, node, sourceText){
			// store the xml for re-use
			jQuery.classBehaviours.handlers.flashTitle.cachedXml = sourceText;
			// set the default values for all parameters
			flashMovie		= "../flash/flashTitle.swf";
			flashWidth		= node.offsetWidth;
			flashHeight		= node.offsetHeight;
			flashWmode		= "Transparent";
			flashScale		= "noScale";
			flashSalign		= "l";
			flashName		= "_" + node.id;
			flashVariables	= "inputText=" + node.innerHTML.replace(/<(.|\n)*?>/gi,"").replace(/^\s+/gi,"").replace(/&/gi,"&");
			// for all childnodes of node which may contain configuration variables
			allInputs = node.getElementsByTagName('input');
			for(var a=0; a<allInputs.length; a++){
				switch(allInputs[a].name){
					case 'movie' 			: flashMovie = allInputs[a].value; break;
					case 'width' 			: flashWidth = allInputs[a].value; break;
					case 'height' 			: flashHeight = allInputs[a].value; break;
					case 'wmode' 			: flashWmode = allInputs[a].value; break;
					case 'scale' 			: flashScale = allInputs[a].value; break;
					case 'salign' 			: flashSalign = allInputs[a].value; break;
					case 'name' 			: flashName = allInputs[a].value; break;
					case 'flashvars' 		: flashVariables += '&' + allInputs[a].value; break;
					default 				: break;
				}
			}
			// if the dimensions are in %, be sure to tell the container
			if(flashWidth.toString().indexOf('%')>-1) node.style.width = flashWidth;
			if(flashHeight.toString().indexOf('%')>-1) node.style.height = flashHeight;
			// load the flash plugin
			sourceText = sourceText.replace('<?xml version="1.0"?>', '');
			sourceText = sourceText.replace(/{movie}/gi, flashMovie);
			sourceText = sourceText.replace(/{width}/gi, flashWidth);
			sourceText = sourceText.replace(/{height}/gi, flashHeight);
			sourceText = sourceText.replace(/{wmode}/gi, flashWmode);
			sourceText = sourceText.replace(/{scale}/gi, flashScale);
			sourceText = sourceText.replace(/{salign}/gi, flashSalign);
			sourceText = sourceText.replace(/{name}/gi, flashName);
			sourceText = sourceText.replace(/{flashvars}/gi, flashVariables);
			node.innerHTML = sourceText;
		},
		setFlashVar: function(nodeId, varName, varValue){
			// find the proper buried ID
			node = 	(document.getElementById('__'+nodeId)) ? 
						document.getElementById('__'+nodeId) : 
							(document.getElementById('_'+nodeId)) ? 
								document.getElementById('_'+nodeId) : 
									document.getElementById(nodeId) ;
			// send the parameter to the flash object
			if(typeof(node.SetVariable)!='undefined') node.SetVariable(varName, varValue);
		},
		// events
		showAlt: function(node){
			allContents = node.getElementsByTagName('*');
			if(allContents.length>0) 
				for(var a=0; a<allContents.length; a++) 
					if(allContents[a].className.indexOf('flashAlternative')>-1) 
						allContents[a].style.visibility = 'visible';
		}
	}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.flashTitle = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.flashTitle.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".flashTitle").flashTitle();
			}
		);
	}

