
var ImageSlider = Class.create({

	currentIndex: 0,
	isMoving: false,

	initialize: function ( element, options ) {

		if ( !$(element) ) {
			console.log( 'Element not found' );
			return false;
		}

		this.wrapper = $(element);

		this.options 				= options || {};
		this.options.duration	 	= this.options.duration || 0.75;
		this.options.slideClass 	= this.options.slideClass || false;
		this.options.slideMargin 	= this.options.slideMargin || 0;
		this.options.slider		 	= $( this.options.slider ) || false;

		if ( this.options.slideClass === false ) {
			return false;
		} else {
			this.slides = this.wrapper.select( '.' + this.options.slideClass );
		}


		/*****  Set wrapper styles  *****/

		this.wrapper.setStyle({
			left: '0px',
			overflow: 'hidden',
			position: 'relative',
			top: '0px'
		});


		/*****  Add slider wrapper  *****/

		if ( this.options.slider === false ) {
			this.slider 	= new Element( 'div', { style: 'left:0px; position:absolute; top:0px; width:5000px;' } );
			var container	= new Element( 'div' );

			this.wrapper.insert({top: this.slider});
			this.slider.insert( container );
		} else {

			var container	= new Element( 'div' );
			this.slider 	= this.options.slider;
			this.slider.insert( container );

		}


		/*****  Set slide styles  *****/

		this.slides.each( function ( slide ) {

			container.insert( slide );

			slide.setStyle({
				float:'left'
			});

		}.bind( this ));


		/*****  Buttons  *****/

		if ( this.options.buttons ) {

			if ( this.options.buttons.next && $(this.options.buttons.next) ) {

				$(this.options.buttons.next).observe( 'click', function ( ev ) {
					Event.stop( ev );
					this.goNext();
				}.bind( this ));

			}

			if ( this.options.buttons.previous && $(this.options.buttons.previous) ) {
				$(this.options.buttons.previous).observe( 'click', function ( ev ) {
					Event.stop( ev );
					this.goPrevious();
				}.bind( this ));

			}

		}

	},


	goNext: function () {

		if ( ( this.isMoving ) || ( this.currentIndex >= this.slides.length - 1 ) ) {
			return;
		}

		this.setCurrentIndex();

		this.move({ x: -(this.slides[this.currentIndex].getWidth() + this.options.slideMargin) });

		this.currentIndex++;

	},


	goPrevious: function () {

		if ( ( this.isMoving) || ( this.currentIndex == 0 ) ) {
			return;
		}

		this.setCurrentIndex();

		this.move({ x: this.slides[this.currentIndex-1].getWidth() + this.options.slideMargin });

		this.currentIndex--;

	},


	move: function ( offset ) {

		this.isMoving = true;

		offset.x = offset.x || 0;
		offset.y = offset.y || 0;

		new Effect.Move( this.slider, {
			x: offset.x,
			y: offset.y,
			mode: 'relative',
			duration: this.options.duration,
			afterFinish: function () {
				this.isMoving = false;
				if ( typeof this.options.afterFinish == 'function' ) {
					this.options.afterFinish( this.slides[this.currentIndex] );
				}
			}.bind( this )
		});

	},


	setCurrentIndex: function () {
		this.currentIndex = this.slides.find( function ( slide ) {
			return slide.hasClassName( 'active' );
		}).previousSiblings().length;
	}

});
