// Add shufling to array
Array.implement({
	shuffle: function() {
		//destination array
		for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
		return this;
	}
});

/* Author Daniel Sokolowski danols@danols
 *
 * 1. Parent element will be made position: relative but will to be moved
 * 2. Child elements will be made position: absolute which will move them to 0,0 of parent element
 * 3. Children will be made opacity 0 before start of animation
 *
 * NOTE: If your elements seem to loose height and width than understand that at domready
 * 	that info is still not avail and hence you must explicity set it in css 
 *
 * Params = associative object with following parameters
 * 	foo.shuffle = True|False -> False default
 * 	foo.loops = 0-Infinity -> num of children elements found
 *
 * see http://www.webmonkey.com/2010/02/make_oop_classes_in_javascript/ for great javascript class tutorial
 */
function childAnim(par, selector, params) {	

    // check for parmeters
	if (selector == null) {
		selector = '';
	}
	if (params == null) {
		var params = new Object;
	}
	if (params.shuffle == null) {
		params.shuffle = false;
	}

	/** params.loop param checked a little below **/

	var timerid = null // stores the id of time for the animation

	// make parent the absolute container for childs by giving it relative positioning
	var parentEl = $(par); // private var
	parentEl.setStyle('position', 'relative');
	parentEl.setStyle('overflow', 'hidden');
	
	// create list of child elements and store with childAnim class as private variable
	var arrChildren =  parentEl.getElements(selector);
	if (params.loops == null) {
		params.loops = arrChildren.length;
	}
	
	// format these children for animation
	arrChildren.setStyle('position', 'absolute'); // this puts all children at same 0,0 relative to parent
	arrChildren.setStyle('opacity', '0');
	arrChildren.set('tween', {duration: 1000});
	// our animation starts at child num 2 assuming child 1 has been animated.
	// move last child into first position so that we start at 1
	arrChildren.unshift(arrChildren.pop());


	// shuffle children if needed
	if (params.shuffle) {
		arrChildren.shuffle();
	}

	delete parentEl // unset the no longer needed variable
	
	// moves forward one in animation
	// the this makes it publicly accassiable 
	this.next = next; // this makes the below function public avail!!!
	function next () {
		//fade out cur element
		//arrChildren.tween('opacity', 0);
		
		// animate first element out	
 		fstChild = arrChildren.shift();
 		// animate current element in
		fstChild.tween('opacity', '0');
		// move it to end
		arrChildren.push(fstChild);

		// animate now new first element in	
 		fstChild = arrChildren[0].tween('opacity', '1');
	}
	// same as next but check if there are loops left
	// and if not will not execute and stop any timers
	function nextloop () {
	
		if (params.loops > 0) {
			
			next();
			params.loops -= 1;
		}
		else {
			stop();
		}

	}

	// moves back one in animation
	// the this makes it publicly accassiable 
	this.previous = previous; // this makes the blow func public avail!!!
	function previous () {
		//fade out cur element
		//arrChildren.tween('opacity', 0);
		
		// animate first element out	
 		fstChild = arrChildren[0].tween('opacity', '0');
 		
		// grab last element animate it in	
 		lstChild = arrChildren.pop();
		// put it at fron of array
		arrChildren.unshift(lstChild);
		// animate it in
		arrChildren[0].tween('opacity', '1');
		
	}
	

	// starts an animation untill loops
	// reach 0 - default is Infinity
	this.start = function (ms) {
		if (ms == null) {
			ms = 8000;
		}
		nextloop();
		timerid = nextloop.periodical(ms);
	
	}
	

	// stop an animtaion
	
	this.stop = stop
		
	var stop = function () {
		$clear(timerid);
	}

}


