/*
CER Simple Scroller
by Carlos Escribano Rey (carlosescri at gmail dot com)

This work is licensed under the "Creative Commons Attribution-Share Alike 3.0 Unported License".
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a
letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Usage: Two nested divs; the parent one will not show the excess of content. Up and down triggers
must be named as the parent div but ending with 'Up' and 'Down', and be outside the parent div.
The content must be the first child of the container.

Example:

<div id="wrapper" style="width:400px;height:400px;overflow:hidden;">
  <div id="content">
    <!-- The content -->
  </div>
</div>

<a id="wrapperUp" ...>...</a>
<a id="wrapperDown" ...>...</a>

$('#wrapper').scroller();

If you want to trigger the scrolling action on mouse over:

$('#wrapper').scroller({ mouseover: true });
*/

(function($) {
  
  $.fn.scroller = function(settings) {
    settings = $.extend({}, $.fn.scroller.defaults, settings);
    return this.each(function() {
      var up = $('#' + this.id + 'Up');
      var dn = $('#' + this.id + 'Down');
      
      $.extend(this, { settings: settings, timeout: null, scroll: $.fn.scroller.scroll });
      
      if (settings.mouseover)
      {
        this.settings.events.on  = 'mouseover';
        this.settings.events.off = 'mouseout';
      }
      
      if (!up.size() || !dn.size())
      {
        throw "Triggers does not exist.";
      }
      
      $(up)
        .bind(this.settings.events.on,  { id: '#' + this.id, up: 1 }, $.fn.scroller.initScroll)
        .bind(this.settings.events.off, { id: '#' + this.id, up: 1 }, $.fn.scroller.stopScroll);

      $(dn)
        .bind(this.settings.events.on,  { id: '#' + this.id, up: 0 }, $.fn.scroller.initScroll)
        .bind(this.settings.events.off, { id: '#' + this.id, up: 0 }, $.fn.scroller.stopScroll);
    });
  };
  
  $.fn.scroller.defaults = {
    speed     : 50,
    step      : 20,
    margin    : 50,
    mouseover : false,
    events    : {
      on  : 'mousedown',
      off : 'mouseup'
    }
  };
  
  $.fn.scroller.scroll = function(e) {
    var c = $(this).children(':first-child');
    var h = $(this).height();
    var H = c.height() - this.settings.margin;
    var m = parseInt(c.css('marginTop'));

    m = (e.data.up ? Math.max(-1 * H, m - this.settings.step) : Math.min(0, m + this.settings.step));
    c.css({ marginTop: m + 'px' });

    if (Math.abs(m) > 0 && Math.abs(m) < H)
      this.timeout = window.setTimeout('$(\'#' + this.id + '\').get(0).scroll({ data: { id: \'' + e.data.id + '\', up: ' + e.data.up + ' }});', this.settings.speed);
    else
      $.fn.scroller.stopScroll(e);
  };
  
  $.fn.scroller.initScroll = function(e) {
    var w = $(e.data.id).get(0);
    w.scroll(e);
  };
  
  $.fn.scroller.stopScroll = function(e) {
    var w = $(e.data.id).get(0);
    window.clearTimeout(w.timeout);
    w.timeout = null;
  };
  
})(jQuery);
