/**
* Public abstract class Presentation
* Base for different presentation types
*/
var Presentation = new Class({

    Implements: Options,
    
    // Class options
    options: {

        transitionDuration: 1000,
        transitionInterval: 5000,
        transitionType: Fx.Transitions.Quad.easeOut,
        autoSwap: true
    },

    /**
    * function initialize
    * Constructor.
    *
    * @param id String ID attribute of the element that contains a presentation
    * @param options Object Class options: transitionType, transitionDuration,
    *                       transitionInterval.
    */
    initialize: function(id, options) {
    
        this.setOptions(options);
        
        // Vet the options!
        if(this.options.transitionDuration > this.options.transitionInterval) {
        
            this.options.transitionInterval = this.options.transitionDuration;
        }
    
        this.presentation = $(id);
        
        if(this.presentation) {
        
            // Grab the slides in this presentation
            this.slides = this.presentation.getElements('.presentation-slide');
            this.slideIndex = 0;
            
            // Remove the other slides from the DOM
            for(var i = 1; i < this.slides.length; i++) {
            
                this.slides[i].dispose();
            }
        
            // Setup any presentation tabs
            this.tabs = this.presentation.getElements('.presentation-tab');
            
            this.tabs.each(function(tab, index) {

                tab.addEvent('click', this.tabClickListener.bindWithEvent(this, index));
            
            }, this);
            
            // Select the first tab
            this.selectTab(0, 0);
            
            // Setup the automatic slide swappage
            this.autoSwap.delay(this.options.transitionInterval, this);
            
            this.swapping = false;
        }
    },
    
    tabClickListener: function(evt, index) {
    
        if(!this.swapping) {
    
            this.options.autoSwap = false;
        
            var tab = $(evt.target);
            
            if(this.slideIndex != index) {
    
                this.selectTab(index, this.slideIndex);
            
                this.swap(this.slides[this.slideIndex], this.slides[index]);
                this.slideIndex = index;
            }
        }
        
        return false;
    },
    
    selectTab: function(index, selectedTabIndex) {
    	if(this.tabs[selectedTabIndex]) {
       	 	this.tabs[selectedTabIndex].removeClass('presentation-tab-on');
        	this.tabs[index].addClass('presentation-tab-on');
		}
    },
    
    /**
    * Protected abstract function swap
    * Swaps one slide with another 
    *
    * @param oldSlide Element The slide element that should disappear
    * @param newSlide Element The slide element that should appear
    */
    swap: function(oldSlide, newSlide) {
    },
    
    autoSwap: function() {

        if(!this.swapping) {
    
            if(this.options.autoSwap) {
        
                // Find next index
                var index = this.slideIndex + 1;
                
                if(index >= this.slides.length) {
                	//end auto swap as at last slide
					this.options.autoSwap = false;
                    //var index = this.slideIndex -1;
                	this.swapping = false;
				}else {				
   					this.selectTab(index, this.slideIndex);
					this.swap(this.slides[this.slideIndex], this.slides[index]);
					this.slideIndex = index;	
				}
            }
        }
        
        this.autoSwap.delay(this.options.transitionInterval, this);
    },
    
    /**
    * Protected abstract function swapCompleteListener
    * For cleanup after swap
    *
    * @param slide Element The slide element that is no longer being viewed
    */
    swapCompleteListener: function(slide) {
    
        // Remove the old slide from the DOM
        slide.dispose();
        
        this.swapping = false;
    }

});
