(function () {
    "use strict";

    YUI().use('node', 'event', function (Y) {
        $(document).ready(function () {
            var homPos = Y.one('#home').getY() - 80,
                serPos = Y.one('#services').getY() - 80,
                aboPos = Y.one('#about').getY() - 80,
                evePos = Y.one('#events').getY() - 80,
                bloPos = Y.one('#blog').getY() - 80,
                conPos = Y.one('#contact').getY() - 80,
                stepy,      // id for scroll animation
                timer;

            function smoothScroll(button, end) {
                $(button).click(function () {
                    // clear previously set intervals, in case any are currently running.
                    clearInterval(stepy);
                    clearTimeout(timer);

                    // set new interval and clear condition
                    stepy = setInterval(function () {
                        var currentPos = $('#superdiv').scrollTop(),
                            finishPos = end,
                            distance = finishPos - currentPos,
                            step = 0.15 * distance;

                        // clear interval when you start traveling less than 1 pixel
                        if (Math.abs(step) < 1) {
                            clearInterval(stepy);
                            //console.log("Cleared!");
                        }
                        else {
                            $('#superdiv').scrollTop(currentPos + step);
                            //console.log(currentPos);
                        }
                    }, 20);
                    
                    timer = setTimeout(function(){
                        clearInterval(stepy);
                    }, 2800);
                });
            }

            smoothScroll('#hom', homPos);
            smoothScroll('#ser', serPos);
            smoothScroll('#abo', aboPos);
            smoothScroll('#eve', evePos);
            smoothScroll('#blo', bloPos);
            smoothScroll('#con', conPos);
        });
    });
}());
