G.ImageSwitcher = new Class({

  Implements: [ Options, Events ],
  
  bannerArray: [],
  
  initialize: function( imagesArray, options ){
    this.setOptions( options );
    this.imagesArray = imagesArray;
    this.preloadBanners();
    this.bannerArray = this.getBuiltBanners();
    this.startSwitcher( this.num );
  },
  
  preloadBanners: function() {
    this.imagesArray.each( function( images ) {
      var image = new Image();
      image.src = images[ 0 ];
      image.src = images[ 1 ];
    } );
  },
  
  switcher: function() {
    $$( '.banners_container' )[0].getLast().
      setStyles( { 'height': 0 } ).
      inject( $$( '.banners_container' )[0], 'top' ).
      set( 'tween', {duration: 2000} ).
      tween( 'height', 302 );

  },
  
  getBuiltBanners: function() {
    var bannerElements = $$( '.banners_container' )[0].getChildren();
    return bannerElements.map( function( banner, i ) {
      var bgUrlString = 'url( {image_path} )';
      var headUrlString = 'url( {image_path2} )';
      banner.setStyle( 
        'background-image', 
        bgUrlString.substitute( { image_path: this.imagesArray[ i ][ 1 ] } ) 
      );
      banner.getElement( 'a' ).setStyle( 
        'background-image', 
        headUrlString.substitute( { image_path2: this.imagesArray[ i ][ 0 ] } ) 
      );
      return banner;
    }, this );
  },
  
  startSwitcher: function( start ) {
    this.switcher.periodical(8000, this)
  }
  
});

