
var SlideShow = new Class({
	start: function(elArray) {
		this.slideShow = this.next.periodical(this.options.slideShowDelay * 3000, this);
	},
	stop: function() {
		this.clearChain();
		$clear(this.slideShow);
	}
});



 var ElementSwapper = new Class({
     Implements: [Options, Events, SlideShow],

     options: {
         selectedClass: 'active',
         panelWrapClass: 'element_swap',
         activateOnLoad: 0,
         slideShowDelay: 3
     },

     initialize: function(elements, options) {

         this.setOptions(options);
         this.elements = $$(elements);

         this.wrap = new Element('div', {
             'id': 'elementswap_wrap',
             'class': this.options.panelWrapClass
         }).inject(this.elements[0], 'before');

         this.addToWrap(this.elements);

         if ($type(this.options.activateOnLoad) == 'number') this.showElement(this.options.activateOnLoad);

         this.start();

     },

     addToWrap: function(elements) {
         $$(elements).each(function(element){
             this.wrap.adopt(element);
             element.fade('hide');
         },this);
     },

     showElement: function(index) {
         if (this.now) this.now.fade('out');
         this.now = this.elements[index];
         this.elements.removeClass(this.options.selectedClass);
         this.now.addClass(this.options.selectedClass);
         this.now.fade('in');
     },

     next: function() {
         var nextElement = this.now.getNext();
         if (!nextElement) nextElement = this.elements[0];
         this.showElement(this.elements.indexOf(nextElement));
     },

     previous: function() {
         var previousElement = this.now.getPrevious();
         if (!previousElement) previousElement = this.elements[this.elements.length - 1];
         this.showElement(this.elements.indexOf(previousElement));
     }

 });
//]]>


window.addEvent('domready', function(){
    var elSwap = new ElementSwapper('.div_swap');
});

