jQuery(document).ready(function($) {
    var imgCursor = [];
    var imagesList = [];
    $('span.imgset').each(function(set) {
        imagesList[set] = [];
        imgCursor[set] = 0;
        $('img', this).each( function(k) {
            imagesList[set][k] = this.src;
        });
        if (imagesList[set].length > 1) {
            var string = $('<div class="slideshow"> <div class="controls"> <a href="#"><img width="17" height="17" src="/_img/sh_prev.gif" alt="&larr;" /></a> \
            <span>1</span> из '+imagesList[set].length+' <a href="#"><img width="17" height="17" src="/_img/sh_next.gif" alt="&rarr;" /></a> </div>\
            <div><img class="inset" src="'+imagesList[set][0]+'"/></div> </div>');
            $('a', string).each(function (i) {
                $(this).click(function () {
                    var move = (i) ? 1 : -1;
                    if (imgCursor[set] + move < 0) {
						imgCursor[set] = imagesList[set].length - 1;
					} else if (imgCursor[set] + move >= imagesList[set].length) {
						imgCursor[set] = 0;
					} else {
						imgCursor[set] = imgCursor[set] + move;
					}
                    $('span', string).text(imgCursor[set] + 1);
                    var img = $('img.inset', string);
                    $(img).fadeOut('fast', function() {
                        $(img).attr('src', imagesList[set][ imgCursor[set] ]);
                        $(img).fadeIn('fast');
                    });
                    return false;
                });
            });
            $(this).html(string);
        }
    });
});
