(function($) {
  // make a div scrollable up or down
  // 2008.03.12 by Jon Buda with assistance from Dave Giunta
  $.fn.makeScrollable = function(options) {
    var opts = $.extend({}, $.fn.makeScrollable.defaults, options);
    
    return this.each(function() {
      var $this = $(this);
      if($this.height()>opts.height){
        $this.parent().append("<div class='scrollers'><span class='scrolldown'>down</span> <span class='scrollup disabled'>up</span></div>");
        $this.wrap("<div></div").parent().css({'height' : opts.height+'px', 'overflow' : 'hidden', 'position' : 'relative'});
        $this.css({'top' : '0px', 'left' : '0px', 'position' : 'absolute'});

        var container_height = $this.parent().height();
        
        $this.parent().parent().children('.scrollers').children('.scrolldown').click(function(){
          var position = parseInt($this.css('top'));
          var prevent_scroll = (Math.abs(position)+opts.scrollby)>$this.height();
          if(!prevent_scroll){           
            $this.animate({ 
                  top: (position-opts.scrollby)+"px"
                }, 600, '', canScroll(position-opts.scrollby) );
          }
        });
      
        $this.parent().parent().children('.scrollers').children('.scrollup').click(function(){
          var position = parseInt($this.css('top'));
          var prevent_scroll = (Math.abs(position)-opts.scrollby)<0;
          if(!prevent_scroll){ 
           $this.animate({ 
                  top: (position+opts.scrollby)+"px"
                }, 600, '',canScroll(position+opts.scrollby) );
          }
        });
      }

    function canScroll(value){
      //scroll down
      //alert(Math.abs(value)+opts.scrollby*2);
      if((Math.abs(value)+opts.scrollby)>$this.height()){
        $this.parent().parent().children('.scrollers').children('.scrolldown').addClass('disabled');
      }else{
        $this.parent().parent().children('.scrollers').children('.scrolldown').removeClass('disabled');
      }
      //scroll up
      if((Math.abs(value)-opts.scrollby)<0){
        $this.parent().parent().children('.scrollers').children('.scrollup').addClass('disabled');
      }else{
        $this.parent().parent().children('.scrollers').children('.scrollup').removeClass('disabled');
      }
    }
      
    });

    // private function for debugging
    function debug($obj) {
      if (window.console && window.console.log)
        window.console.log($obj);
    };

  };
  
  $.fn.makeScrollable.defaults = {
    height:395,
    scrollby:200 
  };
  
})(jQuery);

// This plugin design pattern taken from: http://www.learningjquery.com/2007/10/a-plugin-development-pattern