var Slideshow = new Class({
  options: {
    container: null,
    next_link: null,
    previous_link: null,
    image_click_next: true,
    link_copy: [],
    change_link_copy: false,
    images: [],
    wrap: true,
    container_link: true,
    captions: [],
    captions_container: null,
    begin_index: 0,
    onImageDisplay: $empty
  },
  initialize:function(options) {
    this.setOptions(options);
    this.index = this.options.begin_index;
    this.wrap = this.options.wrap;
    this.images = [];
    this.container = $(this.options.container);
    this.image_container = this.setup_image_container(this.options.container);
    if (this.options.captions.length != 0) {
      this.captions = this.options.captions;
      this.captions_container = this.setup_captions();
    }
    this.setup_click_events();
    this.populate_images(this.options.images);
    if (this.images.length) {
      this.display_image(this.options.begin_index);
    }
    this.display_image.bind(this);
    this.change_link_text.bind(this);
    this.display_caption.bind(this);
  },
  setup_captions:function(){
    if ($chk(this.options.captions_container)) {
      captions_container = $(this.options.captions_container);
    } else {
      captions_container = new Element('div', {
        'id':'captions'
      })
      captions_container.inject(this.image_container,'after');
    }
    return captions_container;
  },
  setup_image_container:function(container){
    if (this.container.get('tag').toLowerCase() == 'div') {
      image_container = new Element('img', {
        'src':''
      });
      image_container.inject(this.container,'top');
    } else if (this.container.get('tag').toLowerCase() == 'img') {
      image_container = this.container;
    }
    return image_container;
  },
  setup_click_events:function(){
    if (this.options.image_click_next) {
      this.image_container.addEvent('click', this.next_image.bind(this));
    }
    if ($chk(this.options.next_link)) {
      this.next_link = $(this.options.next_link);
      this.next_link.addEvent('click', this.next_image.bind(this));
    }
    if ($chk(this.options.previous_link)) {
      this.previous_link = $(this.options.previous_link)
      this.previous_link.addEvent('click', this.previous_image.bind(this));
    }
  },
  populate_images:function(images) {
    images.each(function(image) {
      this.images.include(image);
    }, this);
  },
  display_image:function(index){
    this.image_container.setProperty('src', this.images[index]);
    this.change_link_text(index);
    this.display_caption(index);
    this.fireEvent('onImageDisplay',[this.images[index--],this.images[index],this.images[index++]]);
  },
  display_caption:function(index){
    this.captions_container.set('text',this.captions[index]);
  },
  next_image:function() {
    if (this.index < this.images.length-1) {
      this.index++;
      this.display_image(this.index);
    } else if (this.index && this.wrap) {
      this.index = 0;
      this.display_image(this.index);
    }
  },
  previous_image:function(){
    if (this.index > 0) {
      this.index--;
      this.display_image(this.index);
    } else if (this.index == 0 && this.wrap) {
      this.index = this.images.length-1;
      this.display_image(this.index);
    }
  },
  change_link_text:function(index){
    if (!$defined(this.link_copy)) {
      this.link_copy = this.options.link_copy;
    }
    if (this.options.change_link_copy) {
      if (this.options.link_copy.length != 0) {
        if (index < this.images.length-1) {
          this.next_link.set('text',this.link_copy[index+1]);
        } else if (this.index && this.wrap) {
          this.next_link.set('text',this.link_copy[0]);
        }
        if ($defined(this.previous_link)) {
          if (this.wrap && index == 0) {
            index = this.images.length-1;
          } else {
            index = index-1;
          }
          this.previous_link.set('text',this.link_copy[index]);
        }
      }
    }
  }
});

Slideshow.implement(new Options, new Events);