jQuery.fn.slideShow = function(options) {
    var options = jQuery.extend({
        speed: 100,
        interval: 1000,
        containerIdPagination: 'SlideShowPagination',
        containerClassPaginationItemDummy: 'slideShowPaginationItemDummy',
        containerClassPaginationItemActive: 'slideShowPaginationItemActive',
        containerClassPaginationItemDeactive: 'slideShowPaginationItemDeactive'
    }, options);
    
    return this.each(function() {
        var $slideShow = $(this);
        $slideShow.css({
            position: 'relative'
        });
        var $images = $slideShow.find('img');

        // if there are no images
        if (!$images.size()) {
            return;
        }

        // if there are not enough images - we clone it
        var posInImages = 0;
        while ($images.size() <= 2) {
            $slideShow.append($images.eq(posInImages).clone());
            posInImages++;
            $images = $slideShow.find('img');
        }

        $images.css({
            position: 'absolute',
            top: 0,
            left: 0,
            zIndex: 8,
            opacity: 0,
            display: 'block'
        });

        var totalImages = $images.size();
        var loadedImages = 0;
        var started = false;
        // [2011.06.23 Art] 
        var $pagCont = $('#'+options.containerIdPagination);
        var $pagItemDummy = $('.'+options.containerClassPaginationItemDummy);
        var countInc = 0;

        $('<table style="margin: 0 auto"><tr></tr></table>').appendTo($pagCont);
        $insidePagCont = $pagCont.find('tr');
        $slideShow.find('img').each(function(){
            countInc++;
            $(this).attr('count', countInc);
            $pagItemDummy.clone().css('display', 'block').attr({'id':'Pagination'+countInc, 'countPag': countInc}).
                addClass(options.containerClassPaginationItemDeactive).
                click(function(){
                    $slideShow.stopTime('slideShow', slideSwitch);
                    slideSwitch(false, $(this).attr('countPag'));
                    $slideShow.everyTime(options.interval, 'slideShow', slideSwitch);
                    
                }).appendTo($insidePagCont).wrap('<td></td>');
        })

        // slideshow
        var slideSwitch = function(tmp, toNumber) {
            var $active = $slideShow.find('img.active');
            if ($active.length == 0) {
                $active = $slideShow.find('img:last');
            }
            // use this to pull the images in the order they appear in the markup
            //var $next =  $active.next().length ? $active.next() : $slideShow.find('img:first');

            if (typeof(toNumber) != 'undefined') {
                if ($slideShow.find('img[count='+toNumber+']').length) {
                    var $next =  $slideShow.find('img[count='+toNumber+']');
                    if ($active.attr('count') == toNumber) {
                        return;
                    }
                } else {
                    if ($active.next().length) {
                        var $next =  $active.next();
                    } else {
                        var $next =  $slideShow.find('img:first');
                    }
                }
            } else {
                var $next =  $active.next().length ? $active.next() : $slideShow.find('img:first');
            }
            

            $active.addClass('last-active').css({zIndex: 9});
            $next.css({opacity: 0.0})
                .addClass('active').css({zIndex: 10})
//                .animate({opacity: 1.0}, options.speed, function() {
                .animate({opacity: 1.0}, 2000, function() {
                    $active.removeClass('active last-active').css({zIndex: 8, opacity: 0});
                });
            $('#'+options.containerIdPagination).find('[id^=Pagination]').removeClass(options.containerClassPaginationItemActive).
                addClass(options.containerClassPaginationItemDeactive);

            $('#Pagination'+$next.attr('count')).removeClass(options.containerClassPaginationItemDeactive).
                addClass(options.containerClassPaginationItemActive);
        }

        // preloader
        $images.each(function () {
            $(this).attr('src', $(this).attr('preload_src')).load(function() {
                loadedImages++;
                if (loadedImages == totalImages && !started) {
                    started = true;
                    slideSwitch();
//                    $slideShow.everyTime(options.interval, 'slideShow', slideSwitch);
                    $slideShow.everyTime(5000, 'slideShow', slideSwitch);
                }
            });
        });

    });
}



