  /**
   * Provides slideshow functionality to a specific div
   */
  var SlideShow = new Class({

    Implements: [Options, Events],

    options: {
      visible_time: 5000,
      method: 'img'
    },
    
    slideshow_reference: null,
    active_item: null,

    initialize: function (location, items, options) {
      this.setOptions(options);

      if (items.length == 0) {
        return;
      }
      this.buildShow(location, items, options);
      this.runShow();
    },

    buildShow: function (location, items, options) {
      this.slideshow_reference = 'slideshow-' + String.uniqueID();
      
      var container = new Element('div', { // builds the container
        id: this.slideshow_reference,
        'class': 'slideshow-container'
      });
      container.setStyles({
        'position': 'relative',
        'overflow': 'hidden',
        'width': '100%',
        'height': '100%'
      });
      $(location).grab(container); // adds the slideshow container to the specified location
      // Builds each item and adds it to the container
      if(this.options.method == 'bg'){
        items.each(function (f, i) {
          var item = new Element('div', {
            'class': 'slideshow-item img-holder',
            id: this.slideshow_reference + '-item-' + (i + 1),
            'style' : 'background : url("'+ f +'") no-repeat scroll 0 0 transparent'
          });
          item.setStyles({
            'position': 'absolute'
          });
          $(this).grab(item); // Adds the item to the container
        }.bind(this));
      } else {
        items.each(function (f, i) {
          var item = new Element('div', {
            'class': 'slideshow-item',
            id: this.slideshow_reference + '-item-' + (i + 1)
          });
          item.setStyles({
            'position': 'absolute'
          });
          item.grab(new Element('img', {
            src: f
          }));
          $(this).grab(item); // Adds the item to the container
        }.bind(this));
      }
      
    },

    runShow: function () {
      // if there is only 1 item, just display it...no need to run the rotation
      if ($(this).getElements('.slideshow-item').length <= 1) {
        return;
      }

      // get all the slideshow items
      $(this).getElements('.slideshow-item').each(function (f, i) {
        if (i > 0) {
          $(f).set('opacity', 0);
        } else {
          this.active_item = $(f);
        }
      }.bind(this));

      this._periodical = this.rotate.periodical(this.options.visible_time, this);
    },

    rotate: function () {
      var active_number = this.active_item.getProperty('id').split('-');
      active_number = active_number[3];

      if (active_number == $(this).getElements('.slideshow-item').length) { //are we on the last item? if so start back at the beginning.
        var new_number = 1;
      } else { //otherwise just add 1 to the last active id
        var new_number = (parseInt(active_number) + 1);
      }

      $(this.slideshow_reference + '-item-' + new_number).set('tween', {
        duration: 3000
      });
      $(this.slideshow_reference + '-item-' + new_number).tween('opacity', 1);

      $(this.active_item).set('tween', {
        duration: 3000
      });
      $(this.active_item).tween('opacity', 0);
      this.active_item = $(this.slideshow_reference + '-item-' + new_number);
    },

    toElement: function () {
      return $(this.slideshow_reference);
    }

  });
  
window.addEvent('domready', function(){      
  if ($('slideshow')) {
    var powerit_ss = new SlideShow($('slideshow'), ['/images/slides/Eval3D_Website_slide.png','/images/slides/SchoolCMS_Website_slide.png','/images/slides/FormBuilder_Website_slide.png'], {visible_time: 12000});
  }
});
