/* -------------------------------------------------------------------------- */
/* Functions to initialize and perform the scrolling anchor links. */
/* -------------------------------------------------------------------------- */

ark.widget.Scroller = function () {

    var stepIncrement = 50;// The number of pixels that each step moves the window.
    var stepDelay = 10;// The number of milliseconds between steps.
    var limit = 6 * 1000;// After 6 seconds the scroll is killed.

    var running = false;

    /* Recursive scrolling method. Steps through the complete scroll. */

    function scrollStep(to, dest, down) {

        if(!running || (down && to >= dest) || (!down && to <= dest)) {
            ark.widget.Scroller.killScroll();
            return;
        }

        if((down && to >= (dest - (2 * stepIncrement))) ||
                (!down && to <= (dest - (2 * stepIncrement)))) {
            stepIncrement = stepIncrement * .55;
        }

        window.scrollTo(0, to);

        // Assign the returned function to a public method.
        ark.widget.Scroller.nextStep = callNext(+to + stepIncrement, dest, down);
        window.setTimeout(ark.widget.Scroller.nextStep, stepDelay);
    }

    /* Create a closure so that scrollStep can be accessed by window.setTimeout(). */

    function callNext(to, dest, down) {

        return function() { scrollStep(to, dest, down); };
    }

    return {
            nextStep: null,
            killTimeout: null,
            StartScrollEvent: new YAHOO.util.CustomEvent('StartScrollEvent', ark.widget.Scroller),
            EndScrollEvent: new YAHOO.util.CustomEvent('EndScrollEvent', ark.widget.Scroller),


             /* Sets up and calls scrollStep. */
            anchorScroll: function(e) {
                  try {
                    var clickedLink = YAHOO.util.Event.getTarget(e);
                  } catch( exception ) {
                    var clickedLink = e;
                  }
                  if( typeof clickedLink == 'undefined' ) {
                    clickedLink = e;
                  }
                  var anchorId = clickedLink.href.replace(/^.*#/, '');
                  var target = YAHOO.util.Dom.get(anchorId);

                  if(target) {
                      YAHOO.util.Event.stopEvent(e);
                      running = true;

                      var yCoord = ((YAHOO.util.Dom.getY(target) - 6) < 0) ? 0 : YAHOO.util.Dom.getY(target) - 6;
                      var currentYPosition = (document.all) ? document.body.scrollTop : window.pageYOffset;
                      var down = true;

                      if(currentYPosition > yCoord) {
                          stepIncrement *= -1;// Reverse the direction if we are scrolling up.
                          down = false;
                      }

                      // Stop the scroll once the time limit is reached.
                      ark.widget.Scroller.killTimeout = window.setTimeout(ark.widget.Scroller.killScroll, limit);

                      // Start the scroll by calling scrollStep().
                      ark.widget.Scroller.StartScrollEvent.fire();
                      scrollStep(currentYPosition + stepIncrement, yCoord, down);
                  }
              },

              /* Kill the scroll after a timeout, to prevent an endless loop. */

            killScroll: function() {
                window.clearTimeout(ark.widget.Scroller.killTimeout);
                running = false;
                stepIncrement = 50;
                ark.widget.Scroller.EndScrollEvent.fire();
            }

    }

} ();

