    var lastpic=0;
    var current='pic0';
$(document).ready(function(){

    // выстраиваем первые 10 элементов (по идее уже должен появится скролл)
    for(i in thumbs)
    {
        if(i<10)
            {
                $("#gallery_list_img").append("<a href='/admin/file/gallery/"+thumbs[i]['large']+"' target='_blank' id='pic"+i+"'><img src='/admin/file/gallery/"+thumbs[i]['small']+"' /></a>");
                lastpic=i;
            }
    }
    // показываем самую первую картинку
    showimg(current);

    $("#gallery_list_img").scroll(function(){
        // как только скрывается первый элемент слева - подгружаем ещё один направо
       if($("#pic"+(lastpic-9)).position().left<0)
           {
               lastpic++;
               if(thumbs[lastpic])
                   {  
                        $("#gallery_list_img").append("<a href='/admin/file/gallery/"+thumbs[lastpic]['large']+"' target='_blank' id='pic"+lastpic+"'><img src='/admin/file/gallery/"+thumbs[lastpic]['small']+"' /></a>");
                   }
           }
    });

    $("#gallery_list_img a").live("click", function(){
        showimg($(this).attr("id"));
        return false;
    });

    $("#gallery_strelka_2").click(function(){
        var l_id=parseInt(current.replace("pic", ''))+1;
        if(thumbs[l_id])
            {
             showimg('pic'+l_id);
             $("#gallery_list_img").scrollLeft(l_id*40);
            }
    });

    $("#gallery_strelka_1").click(function(){
        var l_id=parseInt(current.replace("pic", ''))-1;
        if(thumbs[l_id])
            {
             showimg('pic'+l_id);
             $("#gallery_list_img").scrollLeft(l_id*40);
            }
    });

});

function showimg(id)
{
        $(".activeSlide").removeClass("activeSlide");
        $("#gallery_list_img a[id='"+id+"']").addClass("activeSlide");
        current=id;
        $(".loader").show();
          var img = new Image();
          $(img)
            .load(function () {
              $(this).hide();
              $("#gallery_foto img").remove();
              $('.loader').hide();
              $("#gallery_foto").append(this);
              $(this).fadeIn();
            })


            // *finally*, set the src attribute of the new image to our image
            .attr('src', $("#gallery_list_img a[id='"+id+"']").attr("href"));
}
