/*______________
|       ______  |   U I Z E     J A V A S C R I P T     A P I
|     /      /  |   -----------------------------------------
|    /    O /   |    MODULE : Uize.Widget.AutoScroll Class (version 1.0)
|   /    / /    |    AUTHOR : Jan Martin Borgersen
|  /    / /  /| |    ONLINE : http://www.tomkidding.com/uize/uize-js-api
| /____/ /__/_| | COPYRIGHT : (c)2007 Jan Martin Borgersen
|           |__ |   LICENSE : Distributed under the terms of the GNU General Public License
|_______________|             http://www.gnu.org/licenses/gpl.txt
*/

/*
	DESCRIPTION
		Implements a widget that automatically scrolls contents.

	REQUIRES
		- Uize.Widget.js (base class)
		- Uize.Node.js
*/

/*ScruncherSettings Mappings="=c" LineCompacting="TRUE"*/

(function () {
	/*** Variables for Scruncher Optimization ***/
		var
			_Uize_Node = Uize.Node,
			_Uize_Widget = Uize.Widget
		;

	/*** Object Constructor ***/
		var
			_superclass = _Uize_Widget,
			_class = _superclass.AutoScroll = _superclass.subclass (
				null,
				function () {
					var _this = this;
					_this._scrollTimeout = null;
					_this._contents1top = 0;
					_this._contents2top = 0;
					_this._contentsHeight = 0;
				}
			),
			_classPrototype = _class.prototype
		;

	/*** Public Instance Methods ***/

		_classPrototype.setContents = function (_newContents) {
			var 
				_this = this,
				_contents1 = _this.getNode('contents1'),
				_contents2 = _this.getNode('contents2')
			;

			// reset contents regions and top coordinates
			_this.set({scrolling:false});
			_this._contents1top = 0;
			_contents1.style.top = '0px';
			_contents2.style.top = '10000px';
			_this.setNodeInnerHtml([_contents1,_contents2],_newContents);
			_this._contentsHeight = _this._contents2top = _Uize_Node.getCoords(_contents1).height;
			_contents2.style.top = _this._contents2top+'px';
		};

		_classPrototype.startScroll = function () {
			var
				_this = this
			;
			_this._scrollTimeout = null;
			if(_this.wired ()) {
			}
		};

		_classPrototype.wireUi = function () {
			var _this = this;
			if (!_this.wired ()) {
				_this.wireNodeEvents('',{
					'onmouseover':function () {
						if(_this._pauseOnMouseover)
							_this.set({scrolling:false})
						;
					},
					'onmouseout':function () {
						if(_this._pauseOnMouseover)
							_this.set({scrolling:true})
						;
					}
				});
				_superclass.prototype.wireUi.call (_this);
			}
		};
		
/*** Setup Properties ***/
		_class.registerProperties ({
			_pauseOnMouseover:{
				name:'pauseOnMouseover',
				value:true
			},
			_timeoutDuration:{
				name:'timeoutDuration',
				value:50
			},
			_stepSize:{
				name:'stepSize',
				value:1
			},
			_direction:{
				name:'direction',
				value:'up' // 'up' or 'down'
			},
			_scrolling:{
				name:'scrolling',
				value:false,
				onChange:function () {
					var _this = this;
					if (_this._scrollTimeout) {
						clearTimeout (_this._scrollTimeout);
						_this._scrollTimeout = null;
					}
					if( _this.get('scrolling') && _this.get('enabled') ) {
						var
							_contents1 = _this.getNode('contents1'),
							_contents2 = _this.getNode('contents2')
						;
						// start up the scrolling
						function _updateScroll() {
							if( _this._direction == 'up' ) {
								_this._contents1top -= _this._stepSize;
								if( _this._contents1top < -_this._contentsHeight )
									_this._contents1top += _this._contentsHeight
								;
							} else {
								_this._contents1top += _this._stepSize;
								if( _this._contents1top > 0 )
									_this._contents1top -= _this._contentsHeight
							}
							_this._contents2top = _this._contents1top + _this._contentsHeight;
							_contents1.style.top = _this._contents1top+'px';
							_contents2.style.top = _this._contents2top+'px';
							_this._scrollTimeout = setTimeout( _updateScroll, _this._timeoutDuration );
						}
						_this._scrollTimeout = setTimeout( _updateScroll, 0 );
					}
				}
			}
		});

}) ();


