/**
 * @author jdreissig
 */

if (mobile == undefined) var mobile = {};

// Needs prototype & scriptaculous effects.

mobile.AbstractScroller = Class.create({
	_element: null,
	SCROLL_STEP: 100,			// how many pixels to scroll per step
	USE_EFFECTS: true,			// scroll with animation?
	ANIMATION_DURATION: 0.5,	// duration of scrolling animation
	
	_offsetProperty: 'xxx',		// override with "scrollLeft" or "scrollTop"		
	_offsetMaxProperty: 'xxx',	// override with "offsetWidth" or "offsetHeight"
	_scrollMaxProperty: 'xxx',	// override with "scrollWidth" or "scrollHeight"
	
	initialize: function(element) {
		this._element = $(element);
		this._setScroll(0);
	},
	
	_scroll: function(newScrollOffset) {
		if (this.USE_EFFECTS) {
			this._animateScroll(newScrollOffset);
		} else {
			this._setScroll(newScrollOffset);
		}
	},

	scrollForward: function() {
		var maxScroll = this._element[this._scrollMaxProperty] - this._element[this._offsetMaxProperty];
		var newScroll = (this._element[this._offsetProperty] + this.SCROLL_STEP) > maxScroll ? maxScroll : this._element[this._offsetProperty] + this.SCROLL_STEP;
		this._scroll(newScroll);
	},
	
	scrollBackward: function() {
		var newScroll = this._element[this._offsetProperty] < this.SCROLL_STEP ? 0 : this._element[this._offsetProperty] - this.SCROLL_STEP;
		this._scroll(newScroll);
	},
	
	_animateScroll: function(newScrollOffset) {
		new Effect.Tween(this._element, this._element[this._offsetProperty], newScrollOffset, {duration: this.ANIMATION_DURATION}, this._offsetProperty);
	},
	
	_setScroll: function(newScrollOffset) {
		this._element[this._offsetProperty] = newScrollOffset;
	}
});


mobile.HorizontalScroller = Class.create(mobile.AbstractScroller, {

	_offsetProperty: 'scrollLeft',
	_offsetMaxProperty: 'offsetWidth',
	_scrollMaxProperty: 'scrollWidth'	

});


mobile.VerticalScroller = Class.create(mobile.AbstractScroller, {
	
	_offsetProperty: 'scrollTop',
	_offsetMaxProperty: 'offsetHeight',
	_scrollMaxProperty: 'scrollHeight'		

});					




