/**
 * Class that generates a Roller (banner rotation) based on children of a given
 * element
 *
 * Constructor arguments:
 *   - father: a DOM element that will show the banner
 *   - options: see list below
 *
 * Options available:
 *   Roller extends from Fx.Scroll and then from Fx so inherits all their
 *   options. This options are added to all of them:
 *   - step (int): number of milliseconds between transitions on continuous
 *                 rolling
 *   - height: 'auto' means class to automatically calcule height for father
 *             element (it gets one of its children height) or an int that
 *             defines this size
 *
 * Depends:
 *  - Mootools 1.2.4 (core, Fx and Fx.Scroll)
 *
 * http://www.tatai.es
 *
 * @author Fran Naranjo
 * @date 2009-11
 */
var Roller = new Class({
	Extends : Fx.Scroll,

	sons : [],
	nSons : 0,
	actual : 0,
	doForever : false,

	options : {
		'height' : 'auto',
		'transition' : 'back:in:out',
		'duration' : '500',
		'step' : 2000
	},

	initialize : function(father, options) {
		this.parent(father, options);

		this.initRoller(father);
	},

	/**
	 * Get sons that will scroll
	 *
	 * @param father DOM element to get sons from
	 * @protected
	 */
	initRoller : function(father) {
		this.sons = father.getChildren();
		this.nSons = this.sons.length;

		var s = this.options.height;
		if(s == 'auto') {
			s = this.sons.getLast().getSize().y;
		}
		father.setStyles({
			'height' : s,
			'overflow' : 'hidden'
		});
	}.protect(),

	/**
	 * Execute one rolling movement
	 *
	 * @public
	 */
	roll : function() {
		this.actual = (this.actual + 1) % this.nSons;

		this.toElement(this.sons[this.actual]);
	},

	/**
	 * Executes rolling and prepares next rolling
	 *
	 * It should be declared as protect() but it is, it cannot be call
	 * from delay function
	 *
	 * @protected
	 */
	rollForever : function() {
		if(this.doForever) {
			this.roll();
			this.rollForever.delay(this.options.step, this);
		}
	},

	/**
	 * Starts cotinuous rolling
	 *
	 * @public
	 * @sa stopRoll
	 */
	startRoll : function() {
		if(this.doForever == false) {
			this.doForever = true;

			this.rollForever.delay(this.options.step, this);
		}
	},

	/**
	 * Stops cotinuous rolling
	 *
	 * @public
	 * @sa startRoll
	 */
	stopRoll : function() {
		this.doForever = false;
	},

	/** 
	 * Converts start to protected to avoid problems
	 *
	 * @protected
	 */
	start : function(x, y) {
		this.parent(x, y);
	}.protect()
});
