/************************************************************\
 *        (c) 2009, Bluebear Internet Data Solutions        *
 *    http://www.bluebear.nl/        http://www.tic4.nl/    *
 *                                                          *
 *                    Licensed under the                    *
 * Creative Commons Attribution-Share Alike 3.0 Netherlands *
 *      http://creativecommons.org/licenses/by-sa/3.0/      *
\************************************************************/
var bbAnimate = new Class({
	Implements: [Events, Options],
	options: {
		con: 'animation',           // ID of container
		obj: '.animate',            // Selector for animating objects
		ani: 'fade',                // Type of animation [fade, scroll]
		dir: 'left',                // Direction (for scroll animation only) [left, right, top, bottom]
		tra: Fx.Transitions.linear, // Transition to use for the animation
		btn: 0,                     // Use buttons? [0: no, 1: yes, 2: both]
		int: 4000,                  // Interval between animations (milliseconds)
		dur: 1000                   // Animation duration (milliseconds)
	},
	initialize: function(options) {
		this.setOptions(options);
		this.con = $(this.options.con);
		this.obj = $(this.options.con).getElements(this.options.obj);
		this.cur = 0;
		this.fx = new Array();
		if (this.con.getStyle('position') == 'static') this.con.setStyle('position', 'relative');

		switch(this.options.ani) {
			case 'scroll':
				this.scroll();
				break;

			default:
				this.fade();
		}

		if (this.options.btn == 0 || this.options.btn == 2) {
			this.play();
		}
		if (this.options.btn == 1 || this.options.btn == 2) {
			if ($(this.options.con+'_prev')) {
				$(this.options.con+'_prev').addEvent('click', this.prev.bind(this));
			}
			if ($(this.options.con+'_next')) {
				$(this.options.con+'_next').addEvent('click', this.next.bind(this));
			}
		}
	},
	fade: function() {
		this.obj.each(function(o,i) {
			o.setStyle('position', 'absolute');
			this.fx[i] = new Fx.Tween(o, {property: 'opacity', duration: this.options.dur, transition: this.options.tra});
			if (i == 0) {
				this.fx[i].set(1);
			} else {
				this.fx[i].set(0);
			}
		}, this);

		this.doAnimate = function(toObj) {
			this.fx[this.cur].start(0);
			this.fx[toObj].start(1);
			this.cur = toObj;
		};
	},
	scroll: function() {
		mod = 1;
		if (this.options.dir == 'right' || this.options.dir == 'bottom') mod = -1;
		var scrollTo = this.con.getSize().x;
		if (this.options.dir == 'top' || this.options.dir == 'bottom') scrollTo = this.con.getSize().y;

		this.con.setStyles({
			'height': this.con.getSize().y,
			'width': this.con.getSize().x,
			'overflow': 'hidden'
		});
		this.obj.each(function(o,i) {
			o.setStyle('position', 'absolute');
			this.fx[i] = new Fx.Tween(o, {property: this.options.dir, duration: this.options.dur, transition: this.options.tra});
			if (i == 0) {
				this.fx[i].set(0);
			} else {
				this.fx[i].set(scrollTo);
			}
		}, this);

		this.doAnimate = function(toObj) {
			this.fx[this.cur].start(-scrollTo*backwards);
			this.fx[toObj].set(scrollTo*backwards);
			this.fx[toObj].start(0);
			this.cur = toObj;
		};
	},
	animate: function(to) {
		if (this.options.btn == 1 || this.options.btn == 2) {
			if ($(this.options.con+'_prev')) {
				$(this.options.con+'_prev').removeEvents('click');
				$(this.options.con+'_prev').addEvent('click', function(e){if(e)e.stop()});
			}
			if ($(this.options.con+'_next')) {
				$(this.options.con+'_next').removeEvents('click');
				$(this.options.con+'_next').addEvent('click', function(e){if(e)e.stop()});
			}
		}

		toObj = this.cur+1;
		backwards = 1;
		if (typeof(to) == 'number') {
			toObj = to;
			if (toObj < this.cur) {
				backwards = -1;
			}
		}
		if (toObj > this.fx.length-1) {
			toObj = 0;
		} else if (toObj < 0) {
			toObj = this.fx.length-1;
		}

		if (this.fx[toObj]) {
			this.doAnimate(toObj);
		}

		if (this.options.btn == 1 || this.options.btn == 2) {
			(function() {
				if ($(this.options.con+'_prev')) {
					$(this.options.con+'_prev').removeEvents('click');
					$(this.options.con+'_prev').addEvent('click', this.prev.bind(this));
				}
				if ($(this.options.con+'_next')) {
					$(this.options.con+'_next').removeEvents('click');
					$(this.options.con+'_next').addEvent('click', this.next.bind(this));
				}
			}).bind(this).delay(this.options.dur+100);
		}
	},
	play: function(e) {
		if (e) e.stop();
		if (this.animation) {
			$clear(this.animation);
		}
		this.animation = this.animate.bind(this).periodical(this.options.int);
	},
	pause: function(e) {
		if (e) e.stop();
		if (this.animation) {
			$clear(this.animation);
		}
	},
	prev: function(e) {
		if (e) e.stop();
		if (this.animation) {
			$clear(this.animation);
			this.animate(this.cur-1);
			this.animation = this.animate.bind(this).periodical(this.options.int);
		} else {
			this.animate(this.cur-1);
		}
	},
	next: function(e) {
		if (e) e.stop();
		if (this.animation) {
			$clear(this.animation);
			this.animate();
			this.animation = this.animate.bind(this).periodical(this.options.int);
		} else {
			this.animate(this.cur+1);
		}
	}
});
