﻿//--begin scroll animation routines
//functions required to encapulate getting a window's scroll properties -- required by aniScrollTop
function winScrollTop(win) {
    return FilterResults(
                  win.pageYOffset ? win.pageYOffset : 0,
                    win.document.documentElement ? win.document.documentElement.scrollTop : 0,
                    win.document.body ? win.document.body.scrollTop : 0
              );
}

function winHeight(win) {

    return this.FilterResults (
		    win.innerHeight ? win.innerHeight : 0,
		    win.document.documentElement ? win.document.documentElement.clientHeight : 0,
		    win.document.body ? win.document.body.clientHeight : 0
    )
}

function FilterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function winBodyHeight(win) {
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        return win.document.body.scrollHeight;
    } else {
        return win.document.body.offsetHeight;
    }
}

//aniScrollTop -- smoothly scroll the viewport to a given location
var aniEl;
var aniScrollTopSaved;
var aniLastScrollTop;
var stepSize = 5
function aniScrollTop(el, scrollTop, step) {
    Trace("aniScrollTop - scrollTop: " + scrollTop + ", step: " + step);
    aniEl = el;
    aniScrollTopSaved = scrollTop;
    aniLastScrollTop = -1;
    stepSize = step;
    setTimeout("doAniScrollTop(" + step + ")", 22);
}

var scrollSlack = 40 // a static number to allow the user some slack -- the page needs to be within the slack limit to initiate a Navigate event
var scrollMargin = 30 // a static number to leave visible -- about 30 pixels from the previous viewport will be visible after the scroll.
function doAniScrollTop(step) {
    continueScrolling = false;
    if (step > 0) {
        if (winScrollTop(aniEl) < aniScrollTopSaved - stepSize - scrollMargin) continueScrolling = true;
    } else {
        if (winScrollTop(aniEl) > aniScrollTopSaved + stepSize + scrollMargin) continueScrolling = true;
    }
    Trace("doAniScrollTop: continueScrolling = " + continueScrolling);
    if (continueScrolling) {
        aniEl.scrollBy(0, stepSize);

        if (stepSize != step * 10) stepSize += step;

        //if we didn't move then stop animating, otherwise continue
        if (winScrollTop(aniEl) != aniLastScrollTop) {
            aniLastScrollTop = winScrollTop(aniEl);
            setTimeout("doAniScrollTop(" + step + ")", 22);
        }
    }
}
//--end scroll animation routines  

