// Class for building a 'slideshow' from array of images
// Takes:
//  - array of images
//  - array of captions (optional)
var Slideshow = new Class({
  options: {
    container:          null,
    toggle:             null,
    next_handler:       null,
    previous_handler:   null,
    toggle_labels:      [],
    images:             [],
    captions:           [],
    captions_container: null,
    path:               null,
    image_path:         null
  },
  initialize:function(options) {
    this.setOptions(options);
    if ($defined(this.options.container)) {
      this.container = $(this.options.container);
    } else {
      if (console.log) console.log("You must specify a container element.");
    }
    if ($defined(this.options.toggle)) {
      this.toggle = $(this.options.toggle);
    } else {
      if (console.log) console.log("You must specify a toggle element.");
    }
    this.captions           = this.options.captions;
    this.captions_container = $(this.options.captions_container);
    this.inject_images_into_container.bind(this);
    this.build_full_image_path.bind(this);
    this.build_full_image_paths.bind(this);
    this.build_full_image_paths();
    this.load_images.bind(this);
    this.images = this.load_images();
    this.labels = this.options.toggle_labels;
    this.inject_images_into_container();
    this.current = 0;
    this.setup_toggle_handler.bind(this);
    this.setup_toggle_handler();
    this.setup_previous_handler.bind(this);
    this.setup_next_handler.bind(this);
    this.setup_previous_handler();
    this.setup_next_handler();
    this.image_out_of_bounds.bind(this);
    this.toggle_image.bind(this);
    this.switch_labels.bind(this);
    this.switch_captions.bind(this);
    if (this.captions.length != 0) {
      this.switch_captions(this.current);
    }
  },
  load_images:function(){
    var images = new Asset.images(this.options.images);
    return images;
  },
  build_full_image_paths:function(){
    this.options.images.each(function(image, index){
      image_part = this.options.images[index];
      this.options.images[index] = this.build_full_image_path(image_part);
    }, this);
  },
  build_full_image_path:function(image_part){
    if (this.options.image_path) {
      full_path = this.options.image_path + image_part;
    } else {
      full_path = image_part;
    };
    return full_path;
  },
  inject_images_into_container:function(){
    this.images.each(function(image, index){
      image.inject(this.container);
      if (index != 0) {
        image.setStyle('display', 'none');
      } else {
        this.switch_captions(this.current);
      };
    }, this);
  },
  setup_toggle_handler:function(){
    original_this = this;
    this.toggle.addEvent('click', function(){
      original_this.current = original_this.current + 1;
      original_this.current = original_this.image_out_of_bounds(original_this.current);
      original_this.toggle_image(original_this.current);
      original_this.switch_labels(original_this.current);
      original_this.switch_captions(original_this.current);
    });
  },
  setup_next_handler:function(){
    original_this = this;
    if (this.options.next_handler) {
      this.next_handler = $(this.options.next_handler);
      this.next_handler.addEvent('click', function(){
        original_this.current = original_this.current + 1;
        original_this.current = original_this.image_out_of_bounds(original_this.current);
        original_this.toggle_image(original_this.current);
        original_this.switch_labels(original_this.current);
        original_this.switch_captions(original_this.current);
      });
    };
  },
  setup_previous_handler:function(){
    original_this = this;
    if (this.options.previous_handler) {
      this.previous_handler = $(this.options.previous_handler);
      this.previous_handler.addEvent('click', function(){
        original_this.current = original_this.current - 1;
        original_this.current = original_this.image_out_of_bounds(original_this.current);
        original_this.toggle_image(original_this.current);
        original_this.switch_labels(original_this.current);
        original_this.switch_captions(original_this.current);
      });
    };
  },
  image_out_of_bounds:function(next_image){
    last_image = this.images.length - 1;
    if (next_image > last_image) {
      return 0;
    } else if (next_image < 0) {
      return last_image;
    } else {
      return next_image;
    }
  },
  toggle_image:function(image_index){
    this.images.each(function(image, index){
      if (index == image_index) {
        image.setStyle('display', 'block');
      } else {
        image.setStyle('display', 'none');
      }
    }, this);
  },
  switch_captions:function(image_index){
    if (this.captions.length != 0) {
      this.captions_container.set('html', this.captions[image_index]);
    };
  },
  switch_labels:function(image_index){
    if (this.labels.length != 0) {
      this.toggle.set('text', this.labels[image_index]);
    };
  }
});

Slideshow.implement(new Options, new Events);

