/*______________
|       ______  |   U I Z E     J A V A S C R I P T     A P I
|     /      /  |   -----------------------------------------
|    /    O /   |    MODULE : Uize.Widget.Swap.Image.Cycle Class (version 1.0.0)
|   /    / /    |    AUTHOR : Chris van Rensburg (http://www.tomkidding.com)
|  /    / /  /| |    ONLINE : http://www.tomkidding.com/uize/uize-js-api
| /____/ /__/_| | COPYRIGHT : (c)2005-2008 UIZE
|          /___ |   LICENSE : Distributed under the terms of the GNU General Public License
|_______________|             http://www.gnu.org/licenses/gpl.txt
*/

/*
	DESCRIPTION
		Implements a JavaScript class for automatically cycling through images with dissolve support

	REQUIRES
		- Uize.Widget.Swap.Image.js (superclass)
*/

/*ScruncherSettings Mappings="=e" LineCompacting="TRUE"*/

Uize.module ({
	name:'Uize.Widget.Swap.Image.Cycle',
	builder:function  (_superclass) {
		/*** Variables for Scruncher Optimization ***/
			var
				_true = true,
				_false = false
			;

		/*** Object Constructor ***/
			var
				_class = _superclass.subclass (
					function () {
						var _this = this;
						/*** Private Instance Properties ***/
							_this._imageNo = -1;
							_this._running = _false;
							_this._advanceTimeout = null;

						/*** Initialization ***/
							_this.fade.addEventHandler (
								'Done',
								function () {
									if (_this._running && (_this._imageNo < _this._images.length - 1 || _this._loop))
										_this._advanceTimeout = setTimeout (function () {_this._advance ()},_this._interval)
									;
								}
							);
					}
				),
				_classPrototype = _class.prototype
			;

		/*** Private Instance Methods ***/
			_classPrototype._clearAdvanceTimeout = function () {
				if (this._advanceTimeout) {
					clearTimeout (this._advanceTimeout);
					this._advanceTimeout = null;
				}
			};

			_classPrototype._advance = function () {
				var _this = this;
				_this._clearAdvanceTimeout ();
				_this._imageNo = (_this._imageNo + 1) % _this._images.length;
				_this.set (_this._cycleSettings [_this._imageNo % _this._cycleSettings.length]);
				_this.set ({src:_this._images [_this._imageNo]});
			};

		/*** Public Instance Methods ***/
			_classPrototype.start = function () {
				this._running = _true;
				this._advance ();
			};

			_classPrototype.stop = function () {
				this._clearAdvanceTimeout ();
				this._running = _false;
			};

		/*** Setup Properties ***/
			_class.registerProperties ({
				_images:{
					name:'images',
					value:[]
				},
				_interval:{
					name:'interval',
					value:2000
				},
				_loop:{
					name:'loop',
					value:_true
				},
				_cycleSettings:{
					name:'cycleSettings',
					value:[]
				}
			});

		return _class;
	}
});

