$.fn.slider = function() {
    return this.each(function() {
        var $this = $(this);
        var $slideList = $this.find('div[class*="slide"]');
        var cntSlide = $slideList.length;
        var curSlide = 0;
        var timeoutId = 0;
        var timeout = 7000;
        var fadeSpeed = 1000;
        var $pagination = $this.find('ul.pagination li > a');

        $slideList.each(function(index) {
            var $slide = $(this);
            $slide.css({
                width   : $slide.width(),
                position: 'absolute',
                left    : '50%',
                top     : '0',
                margin  : '0 0 0 -' + Math.floor($slide.innerWidth() / 2) + 'px'
            });

            if (index == 0) {
                $slide.css({display: 'block'});
            } else {
                $slide.css({display: 'none'});
            }
        });

        $pagination.each(function(index) {
            $(this).click(function(e) {
                if (!$(this).parent('li.active').length) {
                    stop();
                    gotoSlide(index);
                    start();
                }
                e.preventDefault();
            });
        });

        gotoSlide = function(newSlide) {
            if (newSlide >= cntSlide) {
                return;
            }

            $($pagination.get(curSlide)).parent().removeClass('active');
            $($pagination.get(newSlide)).parent().addClass('active');

            if (newSlide > curSlide) {
                $($slideList.get(newSlide)).fadeIn(fadeSpeed, function() {
                    $($slideList.get(curSlide)).css({display: 'none'});
                    curSlide = newSlide;
                });
            } else if (newSlide < curSlide) {
                $($slideList.get(newSlide)).css({display: 'block'});
                $($slideList.get(curSlide)).fadeOut(fadeSpeed);
                curSlide = newSlide;
            }
        };

        nextSlide = function() {
            if (curSlide == cntSlide - 1) {
                gotoSlide(0);
            } else {
                gotoSlide(curSlide + 1);
            }
        };

        start = function() {
            timeoutId = setTimeout('temp()', timeout);

            temp = function() {
                nextSlide()
                timeoutId = setTimeout('temp()', timeout);
            };
        };

        stop = function() {
            if (timeoutId) {
                clearTimeout(timeoutId);
            }
        };

        start();
    });
}
