/*
 * jQuery horizonal scroller
*/


(function($) {
  $.fn.extend({
    vertScroller: function(options) {
      
      return this.each(function(i) {
        
        var height = 30;
        var y = 0;
        var container = $(this);
        
        // Initial CSS for container
        $(this)
          .css('position', 'relative')
          .css('overflow', 'hidden')
          ;
        
        // Initial CSS for children  
        $(this).children(options.jq_item)
          .css('position', 'absolute')
          .each(function(){
            $(this).css('top', y);
            y += height;
          })
          ;
        
        var total_height = y - height;
        
        // If the total is too short, hide the buttons
        if (total_height < container.height()) {
          $(options.jq_prev).hide();
          $(options.jq_next).hide();
          return;
        }
        
        // Previous button
        $(options.jq_prev).click(function() {
          container.find(options.jq_item).stop(false, true);
          
          var last = container.find(options.jq_item + ':last');
          if (parseInt(last.css('top')) <= (container.height() - height + 30)) {
            return false;
          }
          
          container.find(options.jq_item).animate({
            top: '-=' + height + 'px'
          });
          
          return false;
        });
        $(options.jq_prev).css('cursor', 'pointer');
            
        // Next button
        $(options.jq_next).click(function() {
          container.find(options.jq_item).stop(false, true);
          
          var first = container.find(options.jq_item + ':first');
          if (first.css('top') == '0px') {
            return false;
          }
          
          container.find(options.jq_item).animate({
            top: '+=' + height + 'px'
          });
          
          return false;
        });
        $(options.jq_prev).css('cursor', 'pointer');
          
      });
    
    }
  });
})(jQuery);
