/*
Sky.Sidebar
copyright by MySign AG (http://www.mysign.ch), Dominik Richner
depencies: Prototype 1.6.0.2
		   script.aculo.us 1.8.1 effects
*/

// creating namespace
var Sky;
if ( !Sky ) Sky = {};

Sky.Sidebar = Class.create();

Sky.Sidebar._requirementsChecked = false;
Sky.Sidebar._REQUIRED_PROTOTYPE = '1.6.0.2';
Sky.Sidebar._REQUIRED_SCRIPTACULOUS_EFFECTS = '1.8.1';

Sky.Sidebar.prototype =
{
	initialize: function( id, opts )
	{
		// checking requirements
		if( !Sky.Sidebar._requirementsChecked )
		{
			
			// checking Prototype
			if( !Sky.PrototypeExtensions.isPrototypeLoaded( Sky.Sidebar._REQUIRED_PROTOTYPE ) )
			{
				throw( "Sky.Sidebar requires the Prototype JavaScript framework >= " + Sky.Sidebar._REQUIRED_PROTOTYPE );
			}
			// checking script.aculo.us effects
			if( !Sky.PrototypeExtensions.isScriptaculousEffectsLoaded( Sky.Sidebar._REQUIRED_SCRIPTACULOUS_EFFECTS ) )
			{
				throw( "Sky.Sidebar requires the script.aculo.us effects JavaScript framework >= " + Sky.Sidebar._REQUIRED_SCRIPTACULOUS_EFFECTS );
			}
			
			Sky.Sidebar._requirementsChecked = true;
		}
			
		this.options = {
			scrollVertical:			true,
			scrollHorizontal:		true,
			
			stayVisible:			false,
			stayVisibleMargin:		{ left: 0, top: 0 },
			
			transitions:			true
		};
		Object.extend( this.options, opts || {} );
		
		this.element = $(id);
		this.elementPos = this.element.positionedOffset();
		this._handleScrollEventListener = this._handleScroll.bindAsEventListener( this );
		this.observingStarted = false;
		
		this.start();
	},
	
	_handleScroll: function ()
	{
		if ( this.runningEffect != null && !Object.isUndefined( this.runningEffect ) )
		{
			this.runningEffect.cancel();
			this.runningEffect = null;
		}
		
		var newX = 0;
		var newY = 0;
		var dOffset = document.viewport.getScrollOffsets();
		
		if ( this.options.stayVisible )
		{
			if ( this.options.scrollHorizontal 
				&& this.element.offsetWidth <= document.viewport.getWidth()
				&& this.elementPos.left - dOffset.left < this.options.stayVisibleMargin.left )
			{
				newX = ( dOffset.left + this.options.stayVisibleMargin.left ) - this.elementPos.left;
			}
			
			if ( this.options.scrollVertical 
				&& this.element.offsetHeight <= document.viewport.getHeight()
				&& this.elementPos.top - dOffset.top < this.options.stayVisibleMargin.top )
			{
				newY = ( dOffset.top + this.options.stayVisibleMargin.top ) - this.elementPos.top;
			}
		}
		else
		{
			if ( this.options.scrollHorizontal )
			{
				newX = this.elementPos.left + dOffset.left;
			}
			
			if ( this.options.scrollVertical )
			{
				newY = this.elementPos.top + dOffset.top;
			}
		}
		
		if ( this.options.transitions )
		{
			this.runningEffect = new Effect.Move( this.element, { x: newX, y: newY, mode: 'absolute', duration: 0.5, afterFinish: function() {
																																	this.runningEffect = null;
																																}.bind( this ) } );
		}
		else
		{
			this.element.setStyle( { 
					left: newX + 'px',
					top: newY + 'px'
				} );
		}
	},
	
	stop: function ()
	{
		if ( this.observingStarted )
		{
			Event.stopObserving( window, 'scroll', this._handleScrollEventListener );
			Event.stopObserving( window, 'resize', this._handleScrollEventListener );
			this.observingStarted = false;
		}
	},
	
	start: function ()
	{
		if ( !this.observingStarted )
		{
			Event.observe( window, 'scroll', this._handleScrollEventListener );
			Event.observe( window, 'resize', this._handleScrollEventListener );
			this.observingStarted = true;
		}
	}
}

// For compatibility reasons
var My;
if( !My ) My = {};
My.Sidebar = Sky.Sidebar;
