/**
 * Sky.Scrollbar
 * copyright by MySign AG (http://www.mysign.ch), Dominik Richner
 * depencies:	Prototype 1.6.0.2
 *				script.aculo.us 1.8.1 slider
 */

// creating namespace
var Sky;
if ( !Sky ) Sky = {};

Sky.Scrollbar = Class.create();

Sky.Scrollbar.prototype =
{
	/**
	 * @param id: scrollable elementid
	 * @param opts: map of options
	 */
	initialize: function( id, opts )
	{
		if ( Element.myIsExisting( id ) )
		{
			this.options = {
				// scroll vertical
				wrapVertical:				null,
				trackVertical:				null,
				handleVertical:				null,
				verticalRange:				1,
				
				// scroll horizontal
				wrapHorizontal:				null,
				trackHorizontal:			null,
				handleHorizontal:			null,
				
				mouseWheel:					true
			};
			
			this.id = id;
			this.element = $(id);
			Object.extend( this.options, opts || {} );
			
			this.sliderVertical = null;
			this.sliderHorizontal = null;
			
			if ( Element.myIsExisting( this.options.trackVertical )
					&& Element.myIsExisting( this.options.handleVertical ) )
			{
				if ( this.options.verticalRange <= 0 )
				{
					this.options.verticalRange = 1;
				}
				
				this.sliderVertical = new Control.Slider( this.options.handleVertical, this.options.trackVertical, {
					axis: 'vertical',
					range: $R(0,this.options.verticalRange),
					onSlide: this._scrollVertical.bind( this ),
					onChange: this._scrollVertical.bind( this )
				} );
			}
			
			if ( Element.myIsExisting( this.options.trackHorizontal )
					&& Element.myIsExisting( this.options.handleHorizontal ) )
			{
				this.sliderHorizontal = new Control.Slider( this.options.handleHorizontal, this.options.trackHorizontal, {
					onSlide: this._scrollHorizontal.bind( this ),
					onChange: this._scrollHorizontal.bind( this )
				} );
			}
			
			if ( this.options.mouseWheel )
			{
				// mozilla
				this.element.observe('DOMMouseScroll', this._wheel.bind( this ) );
				
				// IE/Opera
				this.element.observe('mousewheel', this._wheel.bind( this ) );
			}
			
			this._autoDisable();
		}
	},
	
	_wheel: function ( event )
	{
		Event.stop( event );
		
		var delta = 0;
		
		if (!event) /* For IE. */
			event = window.event;
		if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;
			if (window.opera)
				delta = delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		
		if (delta)
			this.sliderVertical.setValueBy(-delta);
		
		return false;
	},
	
	_autoDisable: function ()
	{
		if ( this.sliderVertical != null && this.element.scrollHeight <= this.element.offsetHeight )
		{
			this.sliderVertical.setDisabled();
			if ( Element.myIsExisting( this.options.wrapVertical ) )
			{
				$(this.options.wrapVertical).hide();
			}
			else
			{
				$(this.options.trackVertical).hide();
			}
		}
		
		if ( this.sliderHorizontal != null && this.element.scrollWidth <= this.element.offsetWidth )
		{
			this.sliderHorizontal.setDisabled();
			if ( Element.myIsExisting( this.options.wrapHorizontal ) )
			{
				$(this.options.wrapHorizontal).hide();
			}
			else
			{
				$(this.options.trackHorizontal).hide();
			}
		}
	},
	
	_scrollVertical: function ( value )
	{
		this.element.scrollTop = Math.round( value / this.sliderVertical.maximum * ( this.element.scrollHeight - this.element.offsetHeight ) );
	},
	
	_scrollHorizontal: function ( value )
	{
		this.element.scrollLeft = Math.round( value / this.sliderHorizontal.maximum * ( this.element.scrollWidth - this.element.offsetWidth ) );
	}
};


// For compatibility reasons
var My;
if( !My ) My = {};
My.Scrollbar = Sky.Scrollbar;
