﻿/****************************************************
* jquery slideshow plugin
* author: drousseau
* date: 5/27/2010
*
* Use:  $('div').slideShow([{options}]);
* Call on any element containing a UL.  This plugin 
* will create a slideshow and rotate through the 
* LIs.  
*
* Options:
* displayDuration: time each LI is displayed (ms)
* transitionDuration: time to transition between slides (ms)
* prevText: text to display in the Prev page link
* nextText: text to display in the Next page link
*****************************************************/

$.fn.slideshow = function(options) {

    var defaults = {
        displayDuration: 8000,
        transitionDuration: 1000,
        prevText: '&lt;&lt;',
        nextText: '&gt;&gt'
    };

    // Overwrite default options 
    // with user provided ones 
    // and merge them into "options".
    var options = $.extend({}, defaults, options);

    transitionToNextLI = function(obj1) {
        //if no LIs have the show class, grab the first LI
        var current = ($('ul li.show', obj1) ? $('ul li.show', obj1) : $('ul li:last', obj1));

        //Get next image, if it reached the end of the slideshow, rotate it back to the first image
        var next = ((current.next().length) ? (current.next()) : $('ul li:first', obj1));

        //Set the fade in effect for the next image, show class has higher z-index
        next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, options.transitionDuration);

        //Hide the current image
        current.animate({ opacity: 0.0 }, options.transitionDuration).removeClass('show');

        //Set the class on the current page in the pager
        $('.sliderPager span', obj1).removeClass('selected');
        $($('.sliderPager span', obj1)[parseInt($('ul li', obj1).index(next) + 1)]).addClass('selected');
    }

    return this.each(function() {
        var obj = $(this);
        $(obj).css("position", "relative");
        // set opacity to 0, so the first slide will fade in
        $('ul li', obj).css({ opacity: 0.0, filter: 'alpha(opacity=0)' }).show();
        transitionToNextLI(obj);

        // set the display timer
        var timer = setInterval(function() { transitionToNextLI(obj); }, options.displayDuration);

        //pause the slideshow on mouse over
        $('ul', obj).hover(
		function() {
		    clearInterval(timer);
		},
		function() {
		    timer = setInterval(function() { transitionToNextLI(obj); }, options.displayDuration);
		}
        );
        // add the paging
        var pagerHtml = '<div class="sliderPager"><span class="prev">' + options.prevText + '</span>';
        $('ul li', obj).each(function(i) {
            pagerHtml += '<span class="page">' + parseInt(i + 1) + '</span>';
        });
        pagerHtml += '<span class="next">' + options.nextText + '</span></div>';
        obj.append(pagerHtml);

        // get the pagers, and set the first one to selected
        var pagers = $('div.sliderPager span', obj);
        $(pagers[1]).addClass("selected");

        // bind the click event to each pager
        pagers.click(function() {
            // if the current page is clicked, just reset the timer
            if (!($($('ul li', obj)[parseInt($(this).text()) - 1]).hasClass('show'))) {
                var startSlide;
                var current = ($('ul li.show', obj) ? $('ul li.show', obj) : $('ul li:last', obj));
                current.removeClass('show');
                if ($(this).hasClass('prev')) {
                    startSlide = current.prev().length == 0 ? $('ul li:last', obj).prev() : (current.prev().prev().length == 0 ? $('ul li:last', obj) : current.prev().prev());
                } else if ($(this).hasClass('next')) {
                    startSlide = current;
                } else {
                    startSlide = $($('ul li', obj)[parseInt($(this).text()) - 1]).prev();
                }

                startSlide.addClass('show');

                clearInterval(timer);
                //Call the gallery function to run the slideshow
                transitionToNextLI(obj);
            } else {
                clearInterval(timer);
            }
        });
    });
};

