(function($) {
    $.fn.slide = function(options) {
        var defaults = {
            slideWidth: 520,
            autoplay: true,
            duration: 5000,
            showNavigator: false,
            showSlideIndex: false
        };
        var options = $.extend(defaults, options);
        return this.each(function() {
            var slideshow = $(this);
            var o = options;
            var currentPosition = 0;
            var slides = $('.slide');
            var numberOfSlides = slides.length;
            var t;

            // Remove scrollbar in JS
            $('#slidesContainer').css('overflow', 'hidden');
			$('#bannercaption').css({opacity: 0.7});
			$('#bannercaption p').css({opacity: 1});

            // Wrap all .slides with #slideInner div
            slides.wrapAll('<div id="slideInner"></div>')

            // Float left to display horizontally, readjust .slides width
			.css({
			    'float': 'left',
			    'width': o.slideWidth
			});

            // Insert a clone of first slide 
            $('.slide:first').clone().appendTo('#slideInner');

            // Set #slideInner width equal to total width of all slides
            $('#slideInner').css('width', o.slideWidth * (numberOfSlides + 1));			

            // Start
            init();

            //Init function
            function init() {

                if (o.autoplay == true) setNextTimeOut(o.duration);
            }

            // Next
            function next() {
                currentPosition++;
                if (currentPosition >= numberOfSlides) currentPosition = 0;
                slideTo(currentPosition, true);
            }

            // Previous
            function prev() {
                currentPosition--;
                if (currentPosition < 0) currentPosition = numberOfSlides - 1;
                slideTo(currentPosition, false);
            }

            // Go to a slide 
            function goto(position) {
                currentPosition = position;
                slideTo(currentPosition, false);
            }

            // Set time out for next slide
            function setNextTimeOut() {
                t = setTimeout(function() { next(); }, o.duration);
            }

            function clearNextTimeOut()
            { 
                clearTimeout(t);
            }
            // Slide
            function slideTo(position, continuously) {
                $('#slideInner').stop();
                clearNextTimeOut();
                // usual cases
                if (continuously == false || o.autoplay == false || position != 0) {
                    $('#slideInner').animate({ 'marginLeft': o.slideWidth * (-position) }, '', '',
						function() {
						    if (o.autoplay == true) setNextTimeOut();
						}
					)					$('#bannercaption').css({opacity: 0.7});
                }
                // autoplay: slide from last to first one continuously
                else {
                    // slide to the 'fake' first slide (actually at the last)
                    $('#slideInner').animate({ 'marginLeft': o.slideWidth * (-numberOfSlides) }, '', '',
						function() {
						    //immediately change to the 'true' first slide
						    $('#slideInner').css('marginLeft', 0);
						    if (o.autoplay == true) setNextTimeOut();
						}
					)
                }
            }
        });
    };
})(jQuery);
