﻿(function ($) {

    $.fn.slideshow = function (option) {
        var config = {
            current: 0, 		// current selected index
            max: 0, 		// how many 
            duration: 2000, 	// speed between animation
            slideUpSpeed: "fast", //
            slideDownSpeed: "fast", //
            fadeInSpeed: "fast", //
            fadeOutSpeed: "fast", //
            easing: null, 	// not in use
            callback: null		// not in use
        }
        config = $.extend(config, option);
        var self = this,
			timer = null;

        $(self).find(".text").hide().eq(0).fadeIn(0);
        $(self).find(".img img").hide().eq(0).fadeIn(0);

        config.current = 0;
        config.max = self.find("div.text").length;

        doAnimate = function (current, next) {
            setTimeout(function () {
                $(self).find(".text").eq(current).fadeOut(config.fadeOutSpeed);
                $(self).find(".text").eq(next).fadeIn(config.fadeInSpeed);
            }, 300);

            $(self).find(".img img").eq(current).fadeOut(config.fadeOutSpeed);
            $(self).find(".img img").eq(next).fadeIn(config.fadeInSpeed);
        }

        start = function () {
            timer = setTimeout(function () {
                var next = config.current + 1;
                if (next >= config.max) {
                    next = 0;
                }
                doAnimate(config.current, next);
                config.current = next;
                start();
            }, config.duration);
        }

        start();
        return self;

    };

})(jQuery);
