/**
 * SLIDESHOW CLASS
 *
 * @author Florian Plank
 * @version 0.6
 *
 */
;function SlideShow(list, settings){
    // List element actually existing
    if (!list || $(list).length != 1) {
        //_e('Invalid list element ["' + list + '"]');
        return false;
    }
    
    // Logging for debugging
    _i('Slideshow instanciated');
    
    this.list = $(list);
    this.currentPage = 0;
    
    // Set and extend default settings
    this.options = jQuery.extend({
        duration: 3000, // how long is one page visible
        transitionDuration: 1500, // from fade to fade
        paginationLabel: '#', // # will be replaced with page number
        autoPlay: true, // start playing after init()
        cssClass: 'slideshow-js', // classname for list
        cssClassPagination: 'slideshow-pagination', // classname for slideshow pagination
        cssClassCurrent: 'current', // classname for current page list element
        easingTypeHide: 'easeOutCirc', // type of easing for transitions for fadeOut
        easingTypeShow: 'easeInCirc', // type of easing for transitions for fadeIn
        playButtonLabel: 'Play slideshow', // Label for link: play slideshow
        pauseButtonLabel: 'Pause slideshow', // Label for link: pause slideshow
        fadeThroughBG: true, // if false, images will be blended into each other
        onInit: function(){
        }, // callback onInit
        afterShow: function(){
        }, // callback afterShow
        onPause: function(){
        }, // callback onInit
        onStop: function(){
        } // callback onInit
    }, settings);
    
    // Init
    this.init();
    
    // Start slideshow if autoplay == true
    if (this.options.autoPlay) 
        this.play();
}

// Methods
SlideShow.prototype = {
    init: function(){
        this.pages = this.list.addClass(this.options.cssClass).find('li').each(function(i){
            if (i > 0) 
                $(this).css('opacity', 0);
        });
        $(this.pages[this.currentPage]).css({
            'z-index': '1000'
        });
        this.amtPages = this.pages.length;
        this.options.onInit.call();
    },
    play: function(){
    
        if (!this.list) 
            return false;
        var self = this;
        if (!this.options.autoPlay) 
            self.show((self.currentPage + 1 < self.amtPages) ? self.currentPage + 1 : 0);
        if (!this.interval) 
            this.interval = setInterval(function(){
                self.show((self.currentPage + 1 < self.amtPages) ? self.currentPage + 1 : 0);
            }, self.options.duration);
        
    },
    show: function(pageID){
        var self = this;
        if (pageID == this.currentPage) 
            return false;
        
        $(this.pages[this.currentPage]).stop().animate({
            'opacity': 0
        }, this.options.transitionDuration / parseInt(self.options.fadeThroughBG ? 2 : 1), self.options.easingTypeHide, function(){
            if (jQuery.browser.msie) 
                this.style.removeAttribute('filter');
            self.currentPage = pageID;
            self.updatePagination();
            $(self.pages[self.currentPage]).css({
                'z-index': '1'
            });
            if (self.options.fadeThroughBG) 
                $(self.pages[pageID]).stop().animate({
                    'opacity': 1
                }, self.options.transitionDuration / 2, self.options.easingTypeShow, function(){
                    if (jQuery.browser.msie) 
                        this.style.removeAttribute('filter');
                    self.options.afterShow.call();
                    $(self.pages[pageID]).css({
                        'z-index': '1000'
                    });
                })
        });
        
        if (!self.options.fadeThroughBG) 
            $(self.pages[pageID]).stop().animate({
                'opacity': 1
            }, self.options.transitionDuration, self.options.easingTypeShow, function(){
                if (jQuery.browser.msie) 
                    this.style.removeAttribute('filter');
                self.options.afterShow.call();
                $(self.pages[pageID]).css({
                    'z-index': '1000'
                });
            })
    },
    pause: function(){
        if (!this.list) 
            return false;
        
        clearInterval(this.interval);
        delete this.interval;
        this.updatePlayButton('play');
        this.options.onPause.call();
    },
    stop: function(){
        if (!this.list) 
            return false;
        
        clearInterval(this.interval);
        delete this.interval;
        this.currentPage = 0;
        this.updatePlayButton('play');
        this.options.onStop.call();
    },
    next: function(){
        var nextPage = (this.currentPage + 1 < this.amtPages) ? this.currentPage + 1 : 0;
        this.show(nextPage);
    },
    prev: function(){
        var prevPage = (this.currentPage > 0) ? this.currentPage - 1 : this.amtPages - 1;
        this.show(prevPage);
    },
    // ??? COULD BE MORE ELEGANT ???
    getPlayButton: function(){
        var self = this;
        var playButton = document.createElement('a');
        var playButtonLabel = (this.options.autoPlay) ? this.options.pauseButtonLabel : this.options.playButtonLabel;
        var playButtonClass = (this.options.autoPlay) ? "slideshow-pause" : "slideshow-play";
        var playButtonTitle = (this.options.autoPlay) ? "Pause the slideshow" : "Start the slideshow";
        $(playButton).text(playButtonLabel).addClass(playButtonClass).attr('title', playButtonTitle);
        
        this.playButton = $(playButton);
        this.updatePlayButton(this.options.autoPlay ? "pause" : "play");
        
        // keep chain intact
        return this.playButton;
    },
    // ??? COULD BE MORE ELEGANT ???
    updatePlayButton: function(state){
        if (!this.playButton) 
            return false;
        
        state = (typeof(state) == 'undefined') ? (this.playButton.hasClass('slideshow-play') ? 'pause' : 'play') : state;
        
        var self = this;
        this.playButton.unbind('click');
        
        if (state == 'pause') {
            this.playButton.text(self.options.pauseButtonLabel).removeClass('slideshow-play').addClass('slideshow-pause').bind('click', function(){
                self.pause();
                self.updatePlayButton('play');
            });
        }
        else {
            this.playButton.text(self.options.playButtonLabel).removeClass('slideshow-pause').addClass('slideshow-play').bind('click', function(){
                self.play();
                self.updatePlayButton('pause');
            });
        }
    },
    getPagination: function(){
        if (!this.list) 
            return false;
        
        var insert = [], i = 1;
        insert[0] = '<ul class="' + this.options.cssClassPagination + '">';
        for (var j = 0; j < this.amtPages; j++) {
            var current = (j == 0) ? ' class="' + this.options.cssClassCurrent + '"' : '';
            insert[i++] = '<li' + current + '><a href="#">' + this.options.paginationLabel.replace(/#/g, j + 1) + '</a></li>';
        }
        insert[i++] = '</ul>';
        this.pagination = $(insert.join(''));
        var self = this;
        this.pagination.find('li').each(function(i){
            $(this).find('a').bind('click', function(){
                self.pause();
                self.show(i);
                return false;
            });
        })
        
        return this.pagination;
    },
    updatePagination: function(){
        if (!this.pagination) 
            return false;
        this.pagination.find('li.' + this.options.cssClassCurrent).removeClass(this.options.cssClassCurrent);
        this.pagination.find('li:eq(' + this.currentPage + ')').addClass(this.options.cssClassCurrent);
        return this.pagination;
    }
}
