/**
    sv-slideShow 
    Cross fade a series of content blocks
    
    Requires:
    jQuery 1.3.2+
 */     
/*
#Settings:
 #slideDelay: number (time unit) - default is 3500 ms,
 #    fadeSpeed: number (time unit) - default is 1500 ms
 #    showPagingation:  boolean default is true
 #    paginationAttributes: use object notation to add attribures and their values if you need to comma separated string pairs (which are split by a colon)
*/
(function($) {
    jQuery.fn.slideshow = function(settings) {
        var slideshow = jQuery.fn.slideshow,
            settings = $.extend({}, slideshow.defaultSettings, settings);
                    
        return this.each(function() {
            if ($(this).data('slideshow')) {
                return; // avoid slideshow being setup more than once on same block
            };
            
            var slideshowContainer = this,
                slides = $(this).children(),
                pagination = $('<ol>').attr(settings.paginationAttributes),
                interval;
            
            $(this).data('slideshow', {currentSlide: 0}); // create a value store for the current slide
            
            if (settings.showPagination === true) {
                $(slides).each(function(index) { // create buttons for switching between slides
                    var title = $(this).attr('title') || index;
                   $('<li>').text(title).appendTo(pagination);
                });

                $(pagination).click(function(e) { // handler to switch to a slide and stop playback when the user clicks
                    if (e.target == this) {
                        return; // ignore clicks which are on the button container
                    };
                    
                    var slideIndex = $(this).children().index(e.target);
                    jQuery.fn.slideshow.showSlideAndStop.apply(slideshowContainer, [settings, slideIndex, interval]);
                });
                
                $(slideshowContainer).bind('slideHidden.sv', function(e, data) { // capture event when slide changes
                    $(pagination).children().removeClass('selected')
                                            .eq(data.nextSlide).addClass('selected');               
                });
                

                $(slideshowContainer).after(pagination); // add the pagination controls to the page
                $(pagination).find('li:first').addClass('selected');
            };
            
            $(slides).not(':first').hide(); // show only the first slide for startup
            $(slideshowContainer).trigger('slideShown.sv', [0]); // trigger event for first slide
            
            interval = setInterval(function() {  // setup the timer to change slides
                        jQuery.fn.slideshow.nextSlide.apply(slideshowContainer, [settings]);
                    }, settings.slideDelay);
        });
    };
    
    jQuery.fn.slideshow.nextSlide = function(settings, slideIndex) { // switch to a slide, the next in sequence if no slideIndex is specified
        var slides = $(this).children(), // stop all animation on slides
            slideshowContainer = this,
            currentSlide = $(this).data('slideshow').currentSlide,
            nextSlide = slideIndex === undefined ? currentSlide + 1 : slideIndex,
            eventData = { 'currentSlide': currentSlide, 'nextSlide': nextSlide }; 
            
            if (nextSlide > (slides.length - 1)) { // wrap around to the beginning if the last slide is shown
                nextSlide = 0;
            };
            
            $(slides).eq(currentSlide).fadeOut(settings.fadeSpeed, function() {
                $(this).trigger('slideHidden.sv', [eventData]);
                $(slides).eq(nextSlide).fadeIn(settings.fadeSpeed, function() { // only show the next slide when the fade out is complete
                    $(slideshowContainer).data('slideshow', { currentSlide: nextSlide});
                    $(this).trigger('slideShown.sv', [eventData]); // trigger an event, passing slideIndex argument
                });
            });
    };
    
    jQuery.fn.slideshow.showSlideAndStop = function(settings, slideIndex, interval) {
        clearInterval(interval);
        $(this).children().stop();
        jQuery.fn.slideshow.nextSlide.apply(this, [settings, slideIndex]); // switch to the specified slide
    };
    
    jQuery.fn.slideshow.defaultSettings = {
        slideDelay: 3500,
        fadeSpeed: 1500,
        showPagination: true,
        paginationAttributes: {
            'id': 'sv-slideshow-pagination'
        }
    };
     

})(jQuery);
