/*
	Source: classbehaviours.fader.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 FADER FUNCTIONS
		// create the utilities child object if it doesn't already exist
		if(typeof(classBehaviours.fader)=='undefined') classBehaviours.fader = {
			getFade : function(node){
				var fadeValue = null;
				if(node!=null){
					// get the fade value using the proper method
					if(typeof(node.style.MozOpacity)!='undefined') fadeValue = Math.round(parseFloat(node.style.MozOpacity)*100);
					if(typeof(node.filters)!='undefined') if(typeof(node.filters.alpha)!='undefined') fadeValue = parseInt(node.filters.alpha.opacity);
					if(typeof(node.style.opacity)!='undefined')	fadeValue = Math.round(parseFloat(node.style.opacity)*100);
				}
				// return the value
				return (isNaN(fadeValue)) ? null : fadeValue;
			},
			setFade : function(node, amount){
				if(node!=null){
					// set the fade value using the proper method
					if(typeof(node.style.MozOpacity)!='undefined') node.style.MozOpacity = amount/100;
					if(typeof(node.filters)!='undefined') if(typeof(node.filters.alpha)!='undefined') node.filters.alpha.opacity = amount;
//					if(typeof(node.style.filter)!='undefined') if(typeof(node.style.filter.alpha)!='undefined') node.style.filter = "alpha(opacity=" + amount + ")";
					if(typeof(node.style.opacity)!='undefined') node.style.opacity = amount/100;
				}
				//	filter:alpha(opacity=50);	imageobject.filters.alpha.opacity=opacity
				//	-moz-opacity: 0.5;			imageobject.style.MozOpacity=opacity/100
				//	opacity: 0.5;
				//	-khtml-opacity: 0.5;
			},
			getSize : function(node){
				// measure the height of the container
				var nodeWidth = node.offsetWidth;
				var nodeHeight = node.offsetHeight;
				// measure the height of all the childnodes of the container
				var totalWidth = 0;
				var totalHeight = 0;
				var contents = node.childNodes;
				for(var a=0; a<contents.length; a++){
					totalWidth += (contents[a].offsetWidth) ? contents[a].offsetWidth : 0 ;
					totalHeight += (contents[a].offsetHeight) ? contents[a].offsetHeight : 0 ;
				}
				// pass back the largest number
				return new Array(nodeWidth, nodeHeight, totalWidth, totalHeight);
			},
			setSize : function(node, xAmount, yAmount){
				if(xAmount!=null) node.style.width = xAmount + 'px';
				if(yAmount!=null) node.style.height = yAmount + 'px';
			},
			fade : function(id, start, end, step, delay, acceleration, evalOnEnd){
				var cf = classBehaviours.fader;
				// get the target node
				var target = (typeof(id)=='string') ? document.getElementById(id) : id ;
				// get the start value if missing
				if(start==null) start = cf.getFade(target);
				if(end==null) end = 100;
				if(step==null) step = 1;
				if(delay==null) delay = 10;
				if(acceleration==null) acceleration = 1;
				if(evalOnEnd==null) evalOnEnd = '';
				// calculate the new value
				if(start<end) {value = (start+step>end) ? end : start+step ;}
				else if(start>end) {value = (start-step<end) ? end : start-step ;}
				// set the fade
				cf.setFade(target, value);
				// order the next step
				var timeOut;
				if(value!=end){
					timeOut = setTimeout(function(){
						classBehaviours.fader.fade(id, value, end, step+acceleration, delay, acceleration, evalOnEnd);
					}, delay);
				}else {
					if(typeof(evalOnEnd)=='string'){
						eval(evalOnEnd);
					}else{
						evalOnEnd();
					}
				}
				// return a way of cancelling the animation
				return timeOut;
			},
			size : function(id, start, end, step, delay, acceleration, evalOnEnd){
				var cf = classBehaviours.fader;
				// get the target node
				var target = (typeof(id)=='string') ? document.getElementById(id) : id ;
				// get the start value if missing
				if(start==null) start = cf.getSize(target)[1];
				if(end==null) end = cf.getSize(target)[3];
				if(step==null) step = 10;
				if(delay==null) delay = 10;
				if(acceleration==null) acceleration = 10;
				if(evalOnEnd==null) evalOnEnd = '';
				// calculate the new value
				if(start<end) {value = (start+step>end) ? end : start+step ;}
				else if(start>end) {value = (start-step<end) ? end : start-step ;}
				// set the fade
				cf.setSize(target, null, value);
				// order the next step
				var timeOut;
				if(value!=end) {
					var nextTarget = target;
					var nextValue = value;
					var nextEnd = end;
					var nextStep = step+acceleration;
					var nextDelay = delay;
					var nextAcceleration = acceleration;
					var nextEvalOnEnd = evalOnEnd;
					timeOut = setTimeout(function(){
						classBehaviours.fader.size(nextTarget, nextValue, nextEnd, nextStep, nextDelay, nextAcceleration, nextEvalOnEnd);
					}, delay);
				}else {
					if(typeof(evalOnEnd)=='string'){
						eval(evalOnEnd);
					}else{
						evalOnEnd();
					}
				}
				// return a way of cancelling the animation
				return timeOut;
			},
			// START: legacy wrappers
			fadeIn : function(id, step, delay, evalOnEnd, acceleration){
				fade(id, 0, 100, step, delay, acceleration, evalOnEnd);
			},
			fadeOut : function(id, step, delay, evalOnEnd, acceleration){
				fade(id, 100, 0, step, delay, acceleration, evalOnEnd);
			},
			crossFade : function(idIn, idOut, amount, step, delay, evalOnEnd, acceleration){
				fade(idIn, 0, 100, step, delay, acceleration, evalOnEnd);
				fade(idOut, 100, 0, step, delay, acceleration, '');
			},
			grow : function(id, step, delay, evalOnEnd, acceleration){
				size(id, 1, null, step, delay, acceleration, evalOnEnd);
			},
			shrink : function(id, step, delay, evalOnEnd, acceleration){
				size(id, null, 1, step, delay, acceleration, evalOnEnd);
			}
			// END: legacy wrappers
		}

