var scrollpane = {

  autoimage:function(xpath){
    var xpath = xpath || false;
    if(!xpath) return false;
    var settings = {step:1};
    $(xpath).each(function(){
      var o = this;
      if(!$(o).is('.scrollable-ready')){
        $('.scrollable',o).serialScroll({
          items:$('.item',o),
          step:settings['step'],
          cycle:true,
          lazy:true,
          force:true,
          easing:'easeOutQuart',
          interval:8000,
          constant:false
        });
        $(o).addClass('scrollable-ready');
      }
    });
  },

  gallery:function(xpath){
    var defaults = {step:7};
    var xpath = xpath || false;
    var options = options || {};
    var settings = $.merge(defaults,options);
    var getIndex = function(o){
      var o = (typeof o == 'object') ? o : false;
      if(!o) return false;
      var i=0,index=0;
      $('.scrollable .item',o).each(function(){
        if($(this).is('.selected')) index=i;
        i++;
      });
      return index;
    };
    if(!xpath) return false;
    $(xpath).each(function(){
      var o = this;
      if(!$(o).is('.scrollable-ready')){
        if($('.item',o).length>settings['step']) {
          $('.next a',o).css('display','block');
          $('#photo_content a.btn_next',o).css('display','block');
        }
        else {
          $('.btn_navigate',o).hide();
          $('#photo_content a.btn_next',o).css('display','block');
        }

        $images = new Array();
        $('.item a img',o).each(function(){
          var img = new Image();
          img.src = ($(this).attr('src')).replace('85x85','614xX');
          $images.push(img);
        });

        $('.scrollable',o).serialScroll({
          items:$('.item',o),
          prev:$('.prev a',o),
          next:$('.next a',o),
          step:settings['step'],
          cycle:false,
          lazy:true,
          force:true,
          exclude:6,
          easing:'easeOutQuart',
          onBefore:function(event,item,obj,collection,index){
            if(index+settings['step']>=collection.length) $('.next a',o).hide();
            else $('.next a:hidden',o).css('display','block');

            if(index<=0) $('.prev a',o).hide();
            else $('.prev a:hidden',o).css('display','block');
          }
        });
        $('.item a',o).click(function(){
          $('#photo_content').show();
          $('.g_list',o).each(function(){
            $('.item', this).removeClass('selected');
          });
          $(this).parent().addClass('selected');

          var index = getIndex(o);
          var num = index+1;
          $('#photo_content img',o).attr('src', ($('img', this).attr('src')).replace('85x85','614xX'));
          $('#photo_content img',o).attr('alt', $('img', this).attr('alt'));
          $('#photo_content img',o).attr('title', $('img', this).attr('title'));
          $('#photo_content .photo_descr',o).html('<span class="index">Фото '+num+'.</span> '+$('img', this).attr('alt'));
          if(index) $('.scrollable',o).trigger('goto',[index]);

          var prev = $('.selected',o).prev();
          if($(prev).html() != null) {
            $('#photo_content a.btn_prev',o).show();
          }
          else $('#photo_content a.btn_prev',o).hide();

          var next = $('.selected',o).next();
          if($(next).html() != null) {
            $('#photo_content a.btn_next',o).show();
          }
          else $('#photo_content a.btn_next',o).hide();
        });

        $('#photo_content a.btn_next',o).click(function(){
          var next = $('.selected',o).next();
          if($(next).html() != null) {
            $('a', next).click();
            $('#photo_content a.btn_prev:hidden',o).show();
            if($(next,o).next().html() == null) {
              $('#photo_content a.btn_next',o).hide();
            }
          }
          else $('#photo_content a.btn_next',o).hide();
        });

        $('#photo_content a.btn_prev',o).click(function(){
          var prev = $('.selected',o).prev();
          if($(prev).html() != null) {
            $('a', prev).click();
            $('#photo_content a.btn_next:hidden',o).show();
            if($(prev,o).prev().html() == null) {
              $('#photo_content a.btn_prev',o).hide();
            }
          }
          else $('#photo_content a.btn_prev',o).hide();
        });

        $(o).addClass('scrollable-ready');
      }
    });
  }
};

