/*
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.1';
Sky.ImgSlider._REQUIRED_SCRIPTACULOUS_EFFECTS = '1.8.1';

Sky.ImgSlider.EVENT_IMGCHANGED = "Sky.ImgSlider:imgChanged";

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,
			effectDuration: 1,
			doStart:	true,
			actIndex:	0,
			backward:	false
		};
		Object.extend(this.options, opts || {});
		
		this.id = id;
		
		this.directionBackward = this.options.backward;
		
		this.slideItems = $A($(this.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();
		}
	},
	
	setOptions: function ( opts )
	{
		var options = 
		{
			slideTime: 	this.options.slideTime,
			effectDuration: this.options.effectDuration
		};
		Object.extend(options, opts || {});
		
		Object.extend(this.options, options || {});
	},
	
	start: function ()
	{
		this.stop();
		this.directionBackward = this.options.backward;
		this.periodicalExecuter = new PeriodicalExecuter( this._doSlide.bind( this ), this.options.slideTime );
	},
	
	_afterFade: function ( effect )
	{
		if ( this.lastFadeEffect == effect )
		{
			this.lastFadeEffect = null;
		}
	},

	_doSlide: function () 
	{
		var nextIndex = 0;
		
		if ( this.directionBackward )
		{
			if ( this.options.actIndex == 0 )
			{ 
				nextIndex = this.slideItems.length - 1;
			} 
			else
			{ 
				nextIndex = this.options.actIndex - 1;
			}
		}
		else
		{
			if ( !( this.options.actIndex == this.slideItems.length - 1 ) )
			{ 
				nextIndex = this.options.actIndex + 1;
			}
		}
		
		this.showImg( nextIndex );
	},
	
	showImg: function ( nextIndex )
	{
		var nextElement = this.slideItems[nextIndex];
		var actElement = this.slideItems[this.options.actIndex];
		var zIndex = nextElement.getStyle( "zIndex" );
		
		if ( zIndex == null )
		{
			zIndex = 1;
		}
		else
		{
			zIndex = parseInt( zIndex ) - 1;
		}
		
		nextElement.setStyle({
			zIndex: zIndex
		});
		
		actElement.setStyle({
			zIndex: zIndex + 1
		});
		
		nextElement.show();
		
		if ( this.lastFadeEffect )
		{
			this.lastFadeEffect.cancel();
			this.lastFadeEffect.element.hide();
			this.lastFadeEffect = null;
		}
		
		this.lastFadeEffect = new Effect.Fade( actElement, 
						       { afterFinish: this._afterFade.bind( this ), 
							 duration: this.options.effectDuration 
						       } );
		
		this.options.actIndex = nextIndex;
		
		$(this.id).fire( Sky.ImgSlider.EVENT_IMGCHANGED, this );
	},
	
	getActSlideItem: function ()
	{
		return this.slideItems[this.options.actIndex];
	},
	
	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;
