/*
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,
			
			transitions:			true
		};
		Object.extend( this.options, opts || {} );
		
		this.element = $(id);
		this.element.absolutize();
		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 = this.elementPos.left;
		var newY = this.elementPos.top;
		var dOffset = document.viewport.getScrollOffsets();
		
		if ( this.options.scrollHorizontal )
		{
			newX += dOffset.left;
		}
		
		if ( this.options.scrollVertical )
		{
			newY += 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;