﻿// This script originally found at Brand Spanking New:
// http://www.brandspankingnew.net/archive/2005/09/animated_anchor.html
// Then modified for IE support by Steve Cochrane (hq at stevecochrane dot com)

// This is version 2, which is to be used for inline links that point to 
// divs with id attributes, and not empty anchors with name attributes. 
// If you would like to use named anchors please use the first version at:
// http://stevecochrane.com/js/smooth_anchors.js

var scrollInt;
var scrTime, scrSt, scrDist, scrDur, scrInt;

// Found this script at http://blog.firetree.net/2005/07/04/javascript-find-position/
// Just used here to provide offsetTop functionality for IE
function findPosY(obj) {
    var curtop = 0;

    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function replaceAnchorLinks() {
    var anchors, i, targ, targarr;

    if (!document.getElementById) {
        return;
    }

    // get all anchors
    anchors = document.getElementsByTagName("a");

    for (i = 0; i < anchors.length; i++) {
        // check if href links to an id on this page
        if (anchors[i].href.indexOf("#") != -1) {
            // get id of target
            targ = anchors[i].href.substring(anchors[i].href.indexOf("#") + 1);

            // find target
            if (document.getElementById(targ)) {
                targarr = document.getElementById(targ);
            }

            if (targarr.getAttribute("id")) {
                anchors[i].className = (targarr.offsetTop < anchors[i].offsetTop) ? "up" : "down";
                anchors[i].id = "__" + targ; // save target as id with prefix (used in onclick function below)
                anchors[i].onclick = function() {
                    try {
                        scrollToAnchor(this.id.substring(2));
                    }
                    catch (e) { }
                    return false;
                };
            }
        }
    }
}


/*
SCROLL FUNCTIONS
*/




function scrollPage() {
    scrTime += scrInt;
    if (scrTime < scrDur) {
        window.scrollTo(0, easeInOut(scrTime, scrSt, scrDist, scrDur));
    } else {
        window.scrollTo(0, scrSt + scrDist);
        clearInterval(scrollInt);
    }
}

function scrollToAnchor(aname) {
    var divs, i, ele, elePosY, heightCorrection;

    if (!document.getElementById) {
        return;
    }

    //alert("got here");
    ele = document.getElementById(aname.replace("#", ""));
    //alert(aname);

    /*
    // get divs
    divs = document.getElementsByTagName("a");
    for (i = 0; i < divs.length; i++) {
        if (divs[i].getAttribute("id") == aname) {
            ele = divs[i];
            i = divs.length;
        }
    }
    */

    // Find anchor's Y position
    elePosY = findPosY(ele);

    //	The following is just to give some vertical space above where the anchor lands, 
    //	in case you think it stops too close to the top of the window.  Set to 0 if unnecessary.
    heightCorrection = 30;

    // set scroll target
    if (typeof (window.pageYOffset) == 'number') {
        // Non-IE modern browsers
        scrSt = window.pageYOffset;
        scrDist = elePosY - heightCorrection - scrSt;
        scrDur = 500;
    } else if (document.documentElement) {
        // IE in Standards Compliance mode
        scrSt = document.documentElement.scrollTop;
        scrDist = elePosY - scrSt;
        if (window.XMLHttpRequest) {
            // IE7
            scrDur = 500;
        } else {
            // IE6
            scrDur = 1500;
        }
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM compliant method, IE Quirks Mode
        scrSt = document.body.scrollTop;
        scrDist = elePosY - scrSt;
        scrDur = 500;
    }

    scrTime = 0;
    scrInt = 10;

    // set interval
    clearInterval(scrollInt);
    scrollInt = setInterval(scrollPage, scrInt);
}




/*
EASING FUNCTIONS
*/

function easeInOut(t, b, c, d) {
    return c / 2 * (1 - Math.cos(Math.PI * t / d)) + b;
}