/*
---
name: bbAnimate
version: beta1
description: Classes for creating easy transitions between elements in a container.
license: MIT license
authors: [Tim Wienk]
requires: [Core/Fx.Tween, Core/Fx.Transitions, Core/Element.Event, Core/Element.Dimensions Core/DomReady, Core/Selectors]
provides: [bbAnimate, bbAnimate.Fade, bbAnimate.Scroll]
usage:
  - new bbAnimate.Fade(container, itemSelector, options);
  - new bbAnimate.Scroll(container, itemSelector, options);
...
*/
var bbAnimate=new Class({Implements:[Options,Events],options:{/*
	onStartAutoplay: function(){},
	onStopAutoplay: function(){},
	onStart: function(element){},
	onComplete: function(element){},
	direction: 'left',
	perPage: 1,
	perStep: null,*/
	transition: 'sine:in:out',
	interval: 5000,
	duration: 1000,
	autoplay: true,
	loop: true
},initialize:function(c,a,b){this.setOptions(b);this.container=document.id(c);this.elements=this.container.getElements(a);if(this.container.getStyle("position")=="static"){this.container.setStyle("position","relative")}this.fx=[];this.current=0;this.setup();if(this.options.autoplay){this.startAutoplay()}},setup:function(){this.bound={previous:this.previous.bind(this),next:this.next.bind(this)};this.build();var a=this.container.getProperty("id");if(a){this.attach(a)}},build:function(){this.elements.each(function(b,a){b.setStyle("position","absolute");this.fx[a]=new Fx.Tween(b,{property:"opacity",duration:this.options.duration,transition:this.options.transition}).set(a?0:1)},this);return this},attach:function(b){var e=document.id(b+"_prev")||$$("."+b+"_prev"),d=document.id(b+"_next")||$$("."+b+"_next"),a=document.id(b+"_paging"),c=a?a.getElements("a"):$$("."+b+"_paging");if(e){e.addEvent("click",this.bound.previous)}if(d){d.addEvent("click",this.bound.next)}if(c.length){c.each(function(f){f.store(b+"_animate",function(g){if(g){g.stop()}var h=f.getProperty("href").replace(/[^0-9]/gi,"").toInt();this.start(h)}.bind(this));f.addEvent("click",f.retrieve(b+"_animate"))},this)}return this},detach:function(b){var e=document.id(b+"_prev")||$$("."+b+"_prev"),d=document.id(b+"_next")||$$("."+b+"_next"),a=document.id(b+"_paging"),c=a?a.getElements("a"):$$("."+b+"_paging");if(e){e.removeEvent("click",this.bound.previous)}if(d){d.removeEvent("click",this.bound.next)}if(c.length){c.each(function(f){f.removeEvent("click",f.retrieve(b+"_animate"))},this)}return this},getElements:function(e,d){if(typeof e=="undefined"){return this.elements}if(typeof d=="undefined"){return $$(this.elements[e])}var b=[],a=this.fx.length;for(;e<=d;++e){var c=this.elements[e];if(!c&&this.options.loop){c=this.elements[e-a]}if(c){b.push(c)}}return $$(b)},toElement:function(){return this.container},autoplay:function(){$clear(this.timer||null);this.timer=this.start.periodical(this.options.interval,this)},startAutoplay:function(a){if(a){a.stop()}this.fireEvent("startAutoplay");this.autoplay();return this},stopAutoplay:function(a){if(a){a.stop()}if(this.timer){this.timer=$clear(this.timer);this.fireEvent("stopAutoplay")}return this},doStart:function(b){var a=this.elements[b];this.onStart(a);this.fx[this.current].start(0);this.fx[b].start(1).chain(function(){this.onComplete(a)}.bind(this))},doSet:function(a){this.fx[this.current].set(0);this.fx[a].set(1);this.onComplete(this.elements[a])},onStart:function(){this.running=true;this.fireEvent.apply(this,["start",arguments])},onComplete:function(){this.fireEvent.apply(this,["complete",arguments]);this.running=false},start:function(c){if(this.running){return this}if(typeof c==="undefined"){c=this.current+1}var a=this.fx.length,b=c>this.current?1:-1;if(c>=a){c=this.options.loop?c-a:a-1}else{if(c<0){c=this.options.loop?c+a:0}}if(this.current===c){return this}this.doStart(c,b);this.current=c;if(this.timer){this.autoplay()}return this},set:function(b){if(this.running){this.fx.each(function(c){c.cancel()})}var a=this.fx.length;if(b>=a){b=0}else{if(b<0){b=a-1}}this.running=true;this.doSet(b);this.current=b;if(this.timer){this.autoplay()}return this},previous:function(a){if(a){a.stop()}if(this.running){return}this.start(this.current-1);if(this.timer){this.autoplay()}return this},next:function(a){if(a){a.stop()}if(this.running){return}this.start(this.current+1);if(this.timer){this.autoplay()}return this}});bbAnimate.Fade=bbAnimate;bbAnimate.Scroll=new Class({Extends:bbAnimate,options:{perPage:1,direction:"left"},build:function(){var a=this.container.getSize();if(!this.options.perStep){this.options.perStep=this.options.perPage}if(this.options.direction=="top"||this.options.direction=="bottom"){this.containerSize=a.y}else{this.containerSize=a.x}this.scrollSize=this.containerSize/this.options.perPage;this.container.setStyles({height:a.y,width:a.x,overflow:"hidden"});this.elements.each(function(c,b){c.setStyle("position","absolute");this.fx[b]=new Fx.Tween(c,{property:this.options.direction,duration:this.options.duration,transition:this.options.transition}).set(this.options.perPage>b?b*this.scrollSize:this.containerSize)},this);return this},getTweens:function(e,d){var c=[],a=this.fx.length,b=this.options.perPage+d-e+(e>d?a:0);while(b--){c[b]=this.fx[e+b];if(!c[b]&&this.options.loop){c[b]=this.fx[e+b-a]}}return c},doStart:function(e,b){var a=this.getElements(e,e+this.options.perPage-1),d=b>0?this.getTweens(this.current,e):this.getTweens(e,this.current),c=d.length-this.options.perPage;this.onStart(a);d.each(function(h,g){var f=this.scrollSize*g-(b>0?0:c*this.scrollSize);if(h){h.setOptions({link:b>0?"cancel":"ignore"}).set(f).start(f-this.scrollSize*c*b)}},this);this.fx[e].chain(function(){this.onComplete(a)}.bind(this))},doSet:function(b){var a=this.fx.length;this.fx.each(function(d,c){var e=c-b;e=Math.min(this.options.perPage,(e<0?e+a:e));d.set(this.scrollSize*e)},this);this.onComplete(this.getElements(b,b+this.options.perPage-1))},start:function(a){if(typeof a==="undefined"){a=this.current+this.options.perStep}if(!this.options.loop&&a>=this.fx.length){return this}return this.parent(a)},previous:function(a){if(a){a.stop()}if(this.running){return this}this.start(this.current-this.options.perStep);if(this.timer){this.autoplay()}return this},next:function(a){if(a){a.stop()}if(this.running){return this}this.start(this.current+this.options.perStep);if(this.timer){this.autoplay()}return this}});

