var Quote = new Class({
  Implements: [Options],
  options: {
    quotes: [],
    controlHeight: 20
  },


  initialize: function(options)
  {
    this.setOptions( options );

    if ( this.options.quotes.length < 2 )
    {
      return;
    }

    this.prepareSlider();

    this.fx = new Fx.Morph( this.$slideContent, { duration: 'long', link: 'cancel' } );
    this.scrollFx = new Fx.Scroll(window, { duration: 'short', link: 'cancel' });

    this.$leftButton.addEvent( 'click', this.previous.bind( this ) );
    this.$rightButton.addEvent( 'click', this.next.bind( this ) );

    this.index = 0;
    this.interval = setInterval( function(){ this.next(); }.bind( this ), 7000 );
  },


  prepareSlider: function()
  {
    this.$element = new Element( 'div', {
     'class': 'quote-slider'
    });

    this.$slideContent = new Element( 'div', {
      'class': 'quoteContent'
    });

    this.$slideControl = new Element( 'div', {
      'class': 'quoteControl',
      'styles': {
        'height': this.options.controlHeight
      }
    });

    this.$leftButton = new Element( 'a', {
      'class': 'quote-leftButton quote-button',
      'href': '#',
      'text': '<'
    });

    var $buttonSeparator = new Element( 'span', {
      'text': ' '
    });

    this.$rightButton = new Element( 'a', {
      'class': 'quote-rightButton quote-button',
      'href': '#',
      'text': '>'
    });


    var size = this.options.quotes[0].getSize();
    this.$element.setStyles({ 
      width: size.x
    });

    var totalWidth = size.x * this.options.quotes.length;

    this.$slideContent.setStyles({ 
      width: totalWidth,
      height: size.y
    });

    this.options.quotes.each( function( $quote, i ){
      $quote.setStyles({
      'width': size.x,
      'float': 'left'
      });
    }.bind( this ));

    this.$element.inject( this.options.quotes[0], 'before' );
    this.$slideContent.inject( this.$element );
    this.$slideControl.inject( this.$element );
    this.$leftButton.inject( this.$slideControl );
    $buttonSeparator.inject( this.$slideControl );
    this.$rightButton.inject( this.$slideControl );
    this.$slideContent.adopt( this.options.quotes );
    this.width = size.x;
  },


  previous: function( event )
  {
    event.stop();
    $clear( this.interval );

    this.index -= 1;
    if ( this.index < 0 )
    {
      this.index = this.options.quotes.length - 1;
    }

    this.change();
  },


  next: function( event )
  {
    if ( event )
    { 
      event.stop();
      $clear( this.interval );
    }

    this.index += 1;
    if ( this.index > this.options.quotes.length - 1 )
    {
      this.index = 0;
    }

    this.change();
  },


  change: function()
  {
    this.fx.start({ 'margin-left': this.width * -this.index, 'height': this.options.quotes[ this.index ].getSize().y });
  }

  
});



$( document ).addEvent( 'domready', function(){
  if ( ! Browser.Engine.trident4 )
  {
    if ( $( 'minHeightProtector' ) )
    {
      $( 'minHeightProtector' ).setStyle( 'height', 700 );
    }

    var quotes    = $$( '.quote' );
    var processed = [];
    var sliders   = [];

    quotes.each( function( $quote, i ){
      if ( processed.contains( $quote ) )
      {
        return;
      }

      var quote_group = [ $quote ];
      processed.include( $quote );
      var $next;

      while ( ( $next = $quote.getNext() ) && $next.hasClass( 'quote' ) )
      {
        quote_group.include( $next );
        processed.include( $next );
        $quote = $next;
      }

      sliders.include( new Quote({ quotes: quote_group }) );
    });
  }
});
