
/*
	Source: classbehaviours.handlers.spriteAnimation.js
	ClassBehaviours is a javascript framework based on class-name parsing.
	Copyright 2011 by Maurice van Creij and published on http://www.woollymittens.nl/
	This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/

	// CLASSBEHAVIOURS CLASS
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(classBehaviours)=='undefined') classBehaviours = {};

		// CLASSNAME BEHAVIOUR FUNCTIONS
		// create the handlers child object if it doesn't already exist
		if(typeof(classBehaviours.handlers)=='undefined') classBehaviours.handlers = {}

			// we don't want this running twice
			if(typeof(classBehaviours.handlers.spriteAnimation)=='undefined'){

				// runs through a sequence of class names
				classBehaviours.handlers.spriteAnimation = {
					// properties
					name: 'spriteAnimation',
					index: 0,
					timeout: null,
					msie6: (navigator.userAgent.indexOf('MSIE 6')>-1),
					// methods
					start: function(node){
						// give an id if the item has none
						node.id = (node.id) ? node.id : this.name + this.index++ ;
						// add a default classname
						classBehaviours.utilities.setClassParameter(node, 'busy', 'no');
						// see if mouse interaction is required
						playOnLoad = classBehaviours.utilities.getClassParameter(node, 'load', null);
						playOnFocus = classBehaviours.utilities.getClassParameter(node, 'focus', null);
						playOnBlur = classBehaviours.utilities.getClassParameter(node, 'blur', null);
						playOnClick = classBehaviours.utilities.getClassParameter(node, 'click', null);
						// set the default parameters
						classBehaviours.utilities.setClassParameter(node, 'start', 0);
						classBehaviours.utilities.setClassParameter(node, 'step', 0);
						classBehaviours.utilities.setClassParameter(node, 'end', 0);
						classBehaviours.utilities.setClassParameter(node, 'loop', 0);
						// set the event handlers
						if(playOnClick) node.onclick = this.clickToPlay;
						if(playOnFocus){
							node.onmouseover = this.focusToPlay;
							node.onfocus = this.focusToPlay;
						}
						if(playOnBlur){
							node.onmouseout = this.blurToPlay;
							node.onblur = this.blurToPlay;
						}
						if(playOnLoad) this.loadToPlay(node);
					},
					loop: function(animId){
						var acn = classBehaviours.handlers.spriteAnimation;
						// get the target node
						animNode = document.getElementById(animId);
						// get the animation properties
						animationStart = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'start', '0'));
						animationStep = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'step', '0'));
						animationEnd = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'end', '3'));
						animationLoop = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'loop', animationEnd));
						animationRate = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'rate', '50'));
						animationTimeout = parseInt(classBehaviours.utilities.getClassParameter(animNode, 'timeout', '0'));
						animationId = classBehaviours.utilities.getClassParameter(animNode, 'id', null);
						animationName = classBehaviours.utilities.getClassParameter(animNode, 'name', null);
						// cancel any remaining timeouts
						clearTimeout(animationTimeout);
						// get an alternate target of the animation if required
						animationTarget = (animationId!=null) ? (animationId!='next') ? document.getElementById(animationId) : classBehaviours.utilities.nextNode(animNode) : animNode;
						// get the next animation step
						nextAnimationStep = (animationStep<animationEnd) ? animationStep+1 : animationLoop ;
						// particularly slow browers get to skip the whole animation
						if(acn.msie6) nextAnimationStep = animationEnd;
						// set the animation name in the target
						if(animationName!=null){
							classBehaviours.utilities.setClassParameter(animationTarget, 'name', animationName);
						}
						// set the next animation step
						if(animationStep!=nextAnimationStep){
							// update the step count
							classBehaviours.utilities.setClassParameter(animationTarget, 'step', nextAnimationStep);
							// store the current step
							if(animationTarget!=animNode) classBehaviours.utilities.setClassParameter(animNode, 'step', nextAnimationStep);
							// order the next update
							animationTimeout = (nextAnimationStep!=animationStep) ? setTimeout('classBehaviours.handlers.spriteAnimation.loop("'+animId+'")', animationRate) : null ;
							// store the timeout
							classBehaviours.utilities.setClassParameter(animNode, 'timeout', animationTimeout);
						}
						// if there is no next step do some end housekeeping
						else{
							// swap around the click and reclick animations
							if(animNode.className.indexOf(' reclick_')>-1){
								animNode.className = animNode.className.replace(' click_', ' splurn_');
								animNode.className = animNode.className.replace(' reclick_', ' click_');
								animNode.className = animNode.className.replace(' splurn_', ' reclick_');
							}
						}
					},
					reset: function(objNode, animationSettings){
						var acn = classBehaviours.handlers.spriteAnimation;
						// get the animation settings
						currentSettings = classBehaviours.utilities.getClassParameter(objNode, 'start', '0') + ',';
						currentSettings += classBehaviours.utilities.getClassParameter(objNode, 'end', '0') + ',';
						currentSettings += classBehaviours.utilities.getClassParameter(objNode, 'loop', '0');
						// if this isn't a double
						if(currentSettings != animationSettings.join() || objNode.className.indexOf('click_')>-1){
							// set the animation settings
							classBehaviours.utilities.setClassParameter(objNode, 'start', animationSettings[0]);
							classBehaviours.utilities.setClassParameter(objNode, 'step', animationSettings[0]);
							classBehaviours.utilities.setClassParameter(objNode, 'end', animationSettings[1]);
							classBehaviours.utilities.setClassParameter(objNode, 'loop', animationSettings[2]);
							// start the animation
							classBehaviours.handlers.spriteAnimation.loop(objNode.id);
						}
					},
					// events
					loadToPlay: function(that){
						var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
						// get the animation settings
						animationSettings = classBehaviours.utilities.getClassParameter(objNode, 'load', '0_0_0').split('_');
						// if there isn't already interaction, reset the animation
						classBehaviours.handlers.spriteAnimation.reset(objNode, animationSettings);
					},
					clickToPlay: function(that){
						var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
						// get the animation settings
						animationSettings = classBehaviours.utilities.getClassParameter(objNode, 'click', '0_0_0').split('_');
						// if there isn't already interaction, reset the animation
						classBehaviours.handlers.spriteAnimation.reset(objNode, animationSettings);
						// if the click needs to be canceled
						return !(classBehaviours.utilities.getClassParameter(objNode, 'cancel', 'no')=='yes');
					},
					focusToPlay: function(that){
						var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
						// get the animation settings
						animationSettings = classBehaviours.utilities.getClassParameter(objNode, 'focus', '0_0_0').split('_');
						animationTimeout = classBehaviours.utilities.getClassParameter(objNode, 'mousefilter', '0');
						animationRate = parseInt(classBehaviours.utilities.getClassParameter(objNode, 'rate', '50'));
						// reset the animation
						clearTimeout(parseInt(animationTimeout));
						// order a new animation
						animationTimeout = setTimeout('classBehaviours.handlers.spriteAnimation.reset(document.getElementById("' + objNode.id + '"), new Array(' + animationSettings.join() + '));', animationRate);
						// store its timeout
						classBehaviours.utilities.setClassParameter(objNode, 'mousefilter', animationTimeout);
					},
					blurToPlay: function(that){
						var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
						// get the animation settings
						animationSettings = classBehaviours.utilities.getClassParameter(objNode, 'blur', '0_0_0').split('_');
						animationTimeout = classBehaviours.utilities.getClassParameter(objNode, 'mousefilter', '0');
						animationRate = classBehaviours.utilities.getClassParameter(objNode, 'rate', '50');
						// reset the animation
						clearTimeout(parseInt(animationTimeout));
						// order a new animation
						animationTimeout = setTimeout('classBehaviours.handlers.spriteAnimation.reset(document.getElementById("' + objNode.id + '"), new Array(' + animationSettings.join() + '));', animationRate);
						// store its timeout
						classBehaviours.utilities.setClassParameter(objNode, 'mousefilter', animationTimeout);
					}
				}

				// sets an alternative target for the click event
				classBehaviours.handlers.deferClick = {
					// properties
					name: 'deferClick',
					// methods
					start: function(node){
						node.onclick = this.clicked;
					},
					// events
					clicked: function(that){
						var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
						var acn = classBehaviours.handlers.spriteAnimation;
						// where do we want this click defered to
						targetId = classBehaviours.utilities.getClassParameter(objNode, 'id', null);
						targetNode = document.getElementById(targetId);
						// triger the click event
						acn.clickToPlay(targetNode);
						// cancel the clicked link
						return false;
					}
				}

			}

	// JQUERY WRAPPER
	if(typeof(jQuery)!='undefined'){
		(function($){
			var methods = {
				init : function(options) {
					return this.each(function(){
						classBehaviours.handlers.spriteAnimation.start($(this).context);
					});
				},
				deferClick : function(){
					return this.each(function(){
						classBehaviours.handlers.deferClick.start($(this).context);
					});
				}
			};
			$.fn.spriteAnimation = function(method){
				if(methods[method]) {
					return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
				}else if(typeof method === 'object' || !method){
					return methods.init.apply(this, arguments);
				}else{
					$.error('Method ' +  method + ' does not exist on jQuery.spriteAnimation');
				}
			};
		})(jQuery);

		// JQUERY EVENTS
		$(document).ready(function() {
			$(".spriteAnimation").spriteAnimation();
			$(".deferClick").spriteAnimation('deferClick');
		});

		/* DIRECT APPLICATION
		$(".linkButton").addClass('spriteAnimation focus_0_5_5 blur_6_9_9');
		$(".linkButton").spriteAnimation();
		*/
	}

/*
	Source: classbehaviours.utilities.js
	ClassBehaviours is a javascript framework based on class-name parsing.
	Copyright 2011 by Maurice van Creij and published on http://www.woollymittens.nl/
	This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/

	// CLASSBEHAVIOURS CLASS
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(classBehaviours)=='undefined') classBehaviours = {};

		// COMMON UTILITY FUNCTIONS
		// create the utilities child object if it doesn't already exist
		if(typeof(classBehaviours.utilities)=='undefined') classBehaviours.utilities = {
			// returns all nodes of the same class
			getElementsByClassName : function(className, node){
				// use the whole body if no target was provided
				var target = (node!=null) ? node : document ;
				// make an empty array for the results
				var foundNodes = new Array();
				// make a regular expression to recognise the class name
				var wantedClass = new RegExp('\\b'+className+'\\b');
				// for all elements in the parent node
				var allNodes = (target.all) ? target.all : target.getElementsByTagName("*");
				for(var a=0; a<allNodes.length; a++){
					// if the classname was found add it to the results
					if(wantedClass.test(allNodes[a].className)) foundNodes[foundNodes.length] = allNodes[a];
				}
				// return the list
				return foundNodes;
			},
			// gets the value of a parameter from a className
			getClassParameter : function(targetNode, paramName, defaultValue){
				if(targetNode!=null){
					var parameterValue = (targetNode.className.indexOf(' ' + paramName + '_')>-1) ?
						targetNode.className.split(' ' + paramName + '_')[1].split(' ')[0] :
						defaultValue ;
					return (isNaN(defaultValue) || defaultValue==null || defaultValue=='' || typeof(defaultValue)!='string') ?
						parameterValue :
						parseFloat(parameterValue.replace('D','.'));
				}else{
					return defaultValue;
				}
			},
			// sets the value of a parameter from a className
			setClassParameter : function(targetNode, paramName, newValue){
				if(targetNode!=null){
					// create a default value, if there isn't one
					if(targetNode.className==null) targetNode.className = '';
					if(targetNode.className.indexOf(' ' + paramName + '_')<0) targetNode.className += ' ' + paramName + '_0';
					// get the old value
					oldValue = this.getClassParameter(targetNode, paramName, null);
					// replace the old value
					if(oldValue!=null) targetNode.className = targetNode.className.replace(paramName+'_'+oldValue, paramName+'_'+newValue);
				}
			},
			// get the next node without worrying about text nodes
			nextNode : function(targetNode, count){
				var testNode = targetNode;
				if(count==null) count = 1;
				for(var a=0; a<count; a++){
					do {
						testNode = (testNode.nextSibling!=null) ? testNode.nextSibling : targetNode ;
					}while(testNode.nodeName.indexOf('#text')>-1);
				}
				return testNode;
			},
			// get the previous node without worrying about text nodes
			previousNode : function(node, count){
				testNode = node;
				if(count==null) count = 1;
				// look for the previous html node
				for(var a=0; a<count; a++){
					do {
						testNode = testNode.previousSibling;
						if(testNode==null) testNode = node;
					}while(testNode.nodeName.indexOf('#text')>-1);
				}
				// return it
				return testNode;
			},
			// find the parent node with the given classname
			findParentNode : function(node, rootTag, rootId, rootClass){
				// try parent nodes until you find the one which meets the conditions
				rootFound = false;
				while(!rootFound && node.nodeName!='BODY'){
					rootFound = (rootTag && node.nodeName) ? (node.nodeName.indexOf(rootTag)>-1) : rootFound ;
					rootFound = (rootId && node.id) ? (node.id.indexOf(rootId)>-1) : rootFound ;
					rootFound = (rootClass && node.className) ? (node.className.indexOf(rootClass)>-1) : rootFound ;
					node = (!rootFound) ? node.parentNode : node;
				}
				// pass it back
				return node;
			},
			// gracefully add an event handler
			addEvent : function(node, eventName, eventHandler){
				if('addEventListener' in node){
					node.addEventListener(eventName, eventHandler, false);
				}else if('attachEvent' in node){
					node.attachEvent('on'+eventName, function(event){eventHandler(event)});
				}else{
					node['on'+eventName] = eventHandler;
				}
				return true;
			},
			// trigger an event handler manually
			triggerEvent : function(node, eventName){
				if('fireEvent' in node){
					node.fireEvent('on' + eventName);
				}else if('dispatchEvent' in node){
					var evt = document.createEvent('HTMLEvents');
					evt.initEvent(eventName, false, true);
					node.dispatchEvent(evt);
				}else{
					eval('node.on' + eventName + '()');
				}
				return true;
			},
			// trim whitespace from around a string
			trim : function(string){
				var cu = classBehaviours.utilities;
				return cu.rtrim(cu.ltrim(string));
			},
			ltrim : function(string){
				var left = 0;
				while(left < string.length && string[left] == ' '){
					left++;
				}
				return string.substring(left, string.length);
			},
			rtrim : function(string){
				var right = string.length - 1;
				while(right > 0 && string[right] == ' '){
					right -= 1;
				}
				return string.substring(0, right + 1);
			}
		}

/*
	Source: classbehaviours.parser.js
	ClassBehaviours is a javascript framework based on class-name parsing.
	Copyright 2011 by Maurice van Creij and published on http://www.woollymittens.nl/
	This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/

	// CLASSBEHAVIOURS CLASS
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(classBehaviours)=='undefined') classBehaviours = {};

 		// COMMON PARSER FUNCTIONS
		// create the utilities child object if it doesn't already exist
		if(typeof(classBehaviours.parser)=='undefined') classBehaviours.parser = {
			// timeout constant
			interval : null,
			// verify the state of the object
			start : function(){
				var cp = classBehaviours.parser;
				// check if enough of the DOM was loaded
				if(/interactive|loaded|complete/i.test(document.readyState)){
					// cancel the interval
					clearInterval(cp.interval);
					// start the parser
					setTimeout(function(){cp.parseDocument()}, 100);
				// else if we're not already waiting for another test
				}else if(cp.interval==null){
					// test again
					cp.interval = setInterval(cp.start, 100);
				}
			},
			// scan the whole document
			parseDocument : function(){
				// pass the document object to the parser
				classBehaviours.parser.parseNode(document);
				// return the status
				return false;
			},
			// recursive version of the same function
			parseNode : function(node){
				// process the node
				parseChildNodes = (node.className!=null) ? this.processNode(node) : true ;
				// parse any childnodes
				if(parseChildNodes) for(var a=0; a<node.childNodes.length; a++) this.parseNode(node.childNodes[a]);
			},
			// process the classnames of the node
			processNode : function(node){
				// for all class behaviours
				var wantedClass;
				for(b in classBehaviours.handlers){
					// define the search based on the handler's expected classn ame
					wantedClass = new RegExp('\\b'+classBehaviours.handlers[b].name+'\\b');
					// if the behaviour name is in the className tested node
					if(wantedClass.test(node.className)){
						// apply its respective behaviour
						classBehaviours.handlers[b].start(node);
					}
				}
				// decide if to continue parsing deeper into this branch
				return (node.className.indexOf('doNotParse')<0);
			}
		}

		// initialise the parser manually if jQuery isn't used
		if(typeof(jQuery)=='undefined') classBehaviours.parser.start();

