/*	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(){}

	// we don't want this running twice
	if(typeof(jQuery.classBehaviours.handlers.animatedClassName)=='undefined'){
		// runs through a sequence of class names
		jQuery.classBehaviours.handlers.animatedClassName = {
			// properties
			name: 'animatedClassName',
			index: 0,
			timeout: null,
			// methods
			start: function(node){
				// give an id if the item has none
				node.id = (node.id) ? node.id : this.name + this.index++ ;
				// get the animation properties
				animationDelay = parseInt(jQuery.classBehaviours.utilities.getClassParameter(node, 'delay', '500'));
				// see if mouse interaction is required
				onlyOnClick = (jQuery.classBehaviours.utilities.getClassParameter(node, 'click', 'no') == 'yes');
				onlyOnFocus = (jQuery.classBehaviours.utilities.getClassParameter(node, 'focus', 'no') == 'yes');
				if(onlyOnClick){
					// set the event handler
					node.onclick = this.replay;
				}else if(onlyOnFocus){
					// set a classname for keeping track of the focus
					node.className += ' active_no';
					// set the event handlers
					node.onmouseover = this.play;
					node.onmouseout = this.stop;
					node.onfocus = this.play;
					node.onblur = this.stop;
				}else{
					// start its animation sequence
					this.play(node);
				}
			},
			// events
			play: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				var acn = jQuery.classBehaviours.handlers.animatedClassName;
				// if there isn't already interaction
				if(objNode.className.indexOf('active_yes')<0){
					// add the interaction marker
					objNode.className = objNode.className.replace('active_no', 'active_yes');
					// start the animation
					jQuery.classBehaviours.handlers.animatedClassName.loop(objNode.id);
				}
			},
			stop: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				// remove the interaction marker
				objNode.className = objNode.className.replace('active_yes', 'active_no');
				// get a possible timeout to cancel
				timeout = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'timeout', '0');
				clearTimeout(parseInt(timeout));
				// reset the animation
				animationStart = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'start', '0');
				jQuery.classBehaviours.utilities.setClassParameter(objNode, 'step', animationStart);
			},
			replay: function(that){
				var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
				var acn = jQuery.classBehaviours.handlers.animatedClassName;
				// add the interaction marker
				objNode.className = objNode.className.replace('active_no', 'active_yes');
				// reset the animation
				animationStart = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'start', '0');
				jQuery.classBehaviours.utilities.setClassParameter(objNode, 'step', animationStart);
				// start the animation
				jQuery.classBehaviours.handlers.animatedClassName.loop(objNode.id);
			},
			loop: function(animId){
				var acn = jQuery.classBehaviours.handlers.animatedClassName;
				// get the target node
				animNode = document.getElementById(animId);
				// get the animation properties
				animationStart = parseInt(jQuery.classBehaviours.utilities.getClassParameter(animNode, 'start', '0'));
				animationStep = parseInt(jQuery.classBehaviours.utilities.getClassParameter(animNode, 'step', '0'));
				animationEnd = parseInt(jQuery.classBehaviours.utilities.getClassParameter(animNode, 'end', '3'));
				animationLoop = parseInt(jQuery.classBehaviours.utilities.getClassParameter(animNode, 'loop', animationEnd));
				animationDelay = parseInt(jQuery.classBehaviours.utilities.getClassParameter(animNode, 'delay', '500'));
				animationActive = jQuery.classBehaviours.utilities.getClassParameter(animNode, 'active', 'yes');
				animationId = jQuery.classBehaviours.utilities.getClassParameter(animNode, 'id', null);
				animationName = jQuery.classBehaviours.utilities.getClassParameter(animNode, 'name', null);
				// if the animation is active
				if(animationActive=='yes'){
					// get an alternate target of the animation if required
					animationTarget = (animationId!=null) ? document.getElementById(animationId) : animNode;
					// get the next animation step
					nextAnimationStep = (animationStep<animationEnd) ? animationStep+1 : animationLoop ;
					// set the animation name in the target
					if(animationName!=null){
						jQuery.classBehaviours.utilities.setClassParameter(animationTarget, 'name', animationName);
						if(navigator.userAgent.indexOf('MSIE 6')>-1 && animationName.indexOf('popUp')>-1){jQuery.classBehaviours.utilities.setClassParameter(parent.document.body, 'name', animationName); window.scrollTo(0,0);}
					}
					// set the next animation step
					if(animationStep!=nextAnimationStep){
						jQuery.classBehaviours.utilities.setClassParameter(animationTarget, 'step', nextAnimationStep);
						if(navigator.userAgent.indexOf('MSIE 6')>-1){jQuery.classBehaviours.utilities.setClassParameter(parent.document.body, 'step', nextAnimationStep);}
						if(animationTarget!=animNode) jQuery.classBehaviours.utilities.setClassParameter(animNode, 'step', nextAnimationStep);
						acn.timeout = (nextAnimationStep!=animationStep) ? setTimeout('jQuery.classBehaviours.handlers.animatedClassName.loop("'+animId+'")', animationDelay) : null ; 
						jQuery.classBehaviours.utilities.setClassParameter(animNode, 'timeout', acn.timeout);
					}
				}
			}
		}
	
		// add this addon to the jQuery object
		if(typeof(jQuery.fn)!='undefined'){
			// extend jQuery with this method
			jQuery.fn.animatedClassName = function(){
				return this.each(
					function(){
						jQuery.classBehaviours.handlers.animatedClassName.start(this);
					}
				);
			};
			// set the event handler for this jQuery method
			$(document).ready(
				function(){
					$(".animatedClassName").animatedClassName();
				}
			);
		}
	}

