/*
Sky.ImgSlider 
copyright by MySign AG (http://www.mysign.ch), Mike Mueller
depencies: Prototype 1.6
           script.aculo.us 1.8.1 effects
*/
// creating namespace
var Sky;
if ( !Sky ) Sky = {};

Sky.ImgSlider = Class.create();

Sky.ImgSlider._REQUIRED_PROTOTYPE = '1.6.0.2';
Sky.ImgSlider._REQUIRED_SCRIPTACULOUS_EFFECTS = '1.8.1';

Sky.ImgSlider.prototype =
{
	initialize: function( id, opts ) 
	{
		// checking Sky.PrototypeExtensions
		if( Object.isUndefined( Sky.PrototypeExtensions ) )
		{
			throw( "Sky.ImgSlider requires the Sky.PrototypeExtensions JavaScript framework" );
		}
		// checking Prototype
		if( !Sky.PrototypeExtensions.isPrototypeLoaded( Sky.ImgSlider._REQUIRED_PROTOTYPE ) )
		{
			throw( "Sky.ImgSlider requires the Prototype JavaScript framework >= " + Sky.ImgSlider._REQUIRED_PROTOTYPE );
		}
		// checking script.aculo.us effects
		if( !Sky.PrototypeExtensions.isScriptaculousEffectsLoaded( Sky.ImgSlider._REQUIRED_SCRIPTACULOUS_EFFECTS ) )
		{
			throw( "Sky.ImgSlider requires the script.aculo.us effects JavaScript framework >= " + Sky.ImgSlider._REQUIRED_SCRIPTACULOUS_EFFECTS );
		}
	
		this.options = 
		{
			tag:		'li',
			slideTime: 	1,
			firstSlideTime: null,
			effectDuration: 4,
			doStart:	true,
			actIndex:	0,
			backward:	false
		};
		Object.extend(this.options, opts || {});
		if ( this.options.firstSlideTime === null )
		{
			this.options.firstSlideTime = this.options.slideTime;
		}
		
		this.directionBackward = this.options.backward;
		
		this.slideItems = $A($(id).getElementsByTagName( this.options.tag) );

		this.slideItems.each( function( item, index ) 
		{
			if(index >= 0)
			{
				$(item).hide();
			}
		} );
		
		this.slideItems[this.options.actIndex].show();

		if ( this.options.doStart )
		{
			this.start( 0 );
		}
	},
	
	start: function ( startIndex ) 
	{
		this.directionBackward = this.options.backward;
		this.periodicalExecuter = new PeriodicalExecuter( this._doSlide.bind( this ), this.options.firstSlideTime );
	},
	
	_afterFade: function ( effect )
	{
		if ( this.lastFadeEffect == effect )
		{
			this.lastFadeEffect = null;
		}
	},

	_afterAppear: function ( effect )
	{
		if ( this.lastAppearEffect == effect )
		{
			this.lastAppearEffect = null;
		}
	},

	_doSlide: function () 
	{
		if ( this.periodicalExecuter.frequency === this.options.firstSlideTime && this.periodicalExecuter.frequency != this.options.slideTime )
		{
			this.periodicalExecuter.stop();
			this.periodicalExecuter.frequency = this.options.slideTime;
			this.periodicalExecuter.registerCallback();
		}
		
		if ( this.lastFadeEffect )
		{
			this.lastFadeEffect.cancel();
			this.lastFadeEffect.element.hide();
			this.lastFadeEffect = null;
		}
		
		this.lastFadeEffect = new Effect.Fade( this.slideItems[this.options.actIndex], 
						       { afterFinish: this._afterFade.bind( this ), 
							 duration: this.options.effectDuration 
						       } );
		if ( this.directionBackward )
		{
			if ( this.options.actIndex == 0 ) 
			{ 
				this.options.actIndex = this.slideItems.length - 1;
			} 
			else 
			{ 
				this.options.actIndex--;
			}
		}
		else
		{
			if ( this.options.actIndex == this.slideItems.length - 1 ) 
			{ 
				this.options.actIndex = 0;
			} 
			else 
			{ 
				this.options.actIndex++;
			}
		}
		if ( this.lastAppearEffect )
		{
			this.lastAppearEffect.cancel();
			this.lastAppearEffect.element.hide();
			this.lastAppearEffect = null;
		}
		this.lastAppearEffect = new Effect.Appear( this.slideItems[this.options.actIndex], 
							   { afterFinish: this._afterAppear.bind( this ),
							     duration: this.options.effectDuration 
							   } );

	},
	
	stop: function ()
	{
		if ( this.periodicalExecuter )
		{
			this.periodicalExecuter.stop();
		}
	},
	
	next: function ()
	{
		this.stop();
		this.directionBackward = false;
		this._doSlide();
	},
	
	prev: function ()
	{
		this.stop();
		this.directionBackward = true;
		this._doSlide();
	}
};

// For compatibility reasons
var My;
if( !My ) My = {};
My.ImgSlider = Sky.ImgSlider;