$(function() {
 if ($(".gal-list").length) {

    $(".gal-list").width(function() {
      var kids = $(this).find("li"),
      sum_width = 0;
      kids.each(function(i, el) {
        sum_width += $(el).width();
        });
      return sum_width;
      });   
    $(".zoom-list .b-pos").each(function() {$(this).css({"margin-left": (664-$(this).width())/2})});
    $(".zoom-list").width(function(){
          var kids = $(this).find("li");     
          return kids.length * kids.width();    
      });    
    $(".scroll-pane").jScrollHorizontalPane({scrollbarHeight:18, scrollbarMargin:0, animateInterval : 30});
    $(".jumper").click(function(){
      var num = $(this).parents("li").prevAll().length;
      scroll("-" + $(".zoom-list li:eq(" + num +")").position().left);
      var c = $(".scroll-pane");
      c[0].scrollTo($(this).closest("li").position().left);
      return false;
      });    

    var $galItems = $(".zoom-list li"),
    oneItemW = $galItems.width(),
    step = oneItemW,
    maxLeft = 0,
    minLeft = ($(".zoom-list").width() - step) * -1,
    sPane = $(".zoom-wrap2"),
    $next = $(".next"),
    $prev = $(".prev");

    function checkState(){
    var curpos = sPane.position().left;        

    if (curpos == minLeft){
    $next.addClass("disabled");
    } else {
    $next.removeClass("disabled");
    }

    if (curpos == maxLeft){
    $prev.addClass("disabled");           
    } else {
      $prev.removeClass("disabled");
    }

    };

    function forward(){
      scroll(Math.max(sPane.position().left - step, minLeft));
    }

    function revers(){
      scroll(Math.min(sPane.position().left + step, maxLeft));
    }

    function scroll(targetPos){
      sPane.animate({"left":targetPos}, 300, checkState);        
    }

    $next.click(forward);
    $prev.click(revers);
    checkState();

    $(".zoom-wrap2").bind("scroll", checkState);

    window.checkState = checkState;
 }
});

