$(document).ready(function(){
  scrollmargin=0;
  scrollval=0;
  scrollInterval=0;
  maxscroll=($('#scrolling').height()*-1)+360;    
  manualScrolling=false;
  manStartY=0;
  startscrollmargin=0;
  speed=0;
  function debug(el){
    $("#debug").html(el.toString());
  }
  function startSlowdownScrolling(val){
    clearInterval(scrollInterval);
    scrollval=val;
    scrollInterval=window.setInterval(autoSlowdownScroll, 6);
  }
  function autoSlowdownScroll(){
    maxscroll=($('#scrolling').height()*-1)+360;    
    scrollval/=1.05;
    scrollmargin+=scrollval;
    if(scrollmargin>0){
      scrollmargin=0;
      stopScrolling();
    }
    else if(scrollmargin<maxscroll){
      scrollmargin=maxscroll;
      stopScrolling();
    }
    else if(scrollval<0.01 && scrollval>-0.01){
      stopScrolling();
    }
    $('#scrolling').css('margin-top', scrollmargin+"px");  
  }  
  function stopScrolling(){
    scrollval=0;
    clearInterval(scrollInterval);
  }
  $(".scrollbutton-top").click(function(){
        $(".scrollbutton-top").blur();
        startSlowdownScrolling(15);
      });
  $(".scrollbutton-bottom").click(function(){
        $(".scrollbutton-bottom").blur();
        startSlowdownScrolling(-15);
      });
  $(".scrollimga").click(function(e){
    e.preventDefault();
    $(this).blur();
    showInfo($(this));
  });
  $(".morebutton").click(function(e){
    e.preventDefault();
    $(this).blur();
    showInfo($(this));
  });  
  function showInfo(id){
    $("#loading").fadeIn(500,function(){
    $("#references").load("references.php", "id="+$(id).attr("id"), function(){
      $("#loading").fadeOut(500);
        $(".morebutton").click(function(e){
        e.preventDefault();
        $(this).blur();
        showInfo($(this));
      });
    });
    });
  }
  $('#scrolling').mousewheel(function(event, delta) {
      maxscroll=($('#scrolling').height()*-1)+360;    
      stopScrolling();
      scrollmargin+=(delta*10);
      if(scrollmargin>0){
        scrollmargin=0;
      }
      else if(scrollmargin<maxscroll){
        scrollmargin=maxscroll;
      }
      $('#scrolling').css('margin-top', scrollmargin+"px");
      return false; //prevent default
    });  
});