﻿// This script animates the "Success Stories" scroller on bizcomweb.com/index.aspx
// Requires the following libraries:
//      /ControlPanel/javascript/jquery/jquery.js
//      /ControlPanel/javascript/jquery/effects.core.js
//      /ControlPanel/javascript/animation/Tween.js

// initialize global variables
var booScrolling = false;
var arrScrollItems = new Array();
var intScrollItemHeight = 186;
var intCurrentScrollItemIndex = 0;
var intNextScrollItemIndex;
$(document).ready(function () {

    // set scroll-item ID's
    $('#scroll-items .scroll-item').each(function () {
        var id = 'scroll-item-' + arrScrollItems.length;
        $(this).attr('id', id);
        arrScrollItems.push({ 'id': id });
    })

    // set position of initial scroll item
    if (arrScrollItems.length > 0) {
        $('#' + arrScrollItems[0].id).css('display', 'block');

        // set scroll button behavior and style
        if (arrScrollItems.length > 1) {
            $('#arrow-up').click(function () { Scroll('down'); }).css('cursor', 'pointer').css('display', 'inline').css('position', 'absolute').css('z-index', '2').css('top', '10px').css('left', '40px');
            $('#arrow-down').click(function () { Scroll('up'); }).css('cursor', 'pointer').css('display', 'inline').css('position', 'absolute').css('z-index', '2').css('top', '160px').css('left', '40px');
        }
    }
});
function Scroll(strDirection) {
    if (!booScrolling) {

        // set "busy" flag
        booScrolling = true;

        // set directional values
        var intNextStartPosition;
        var intCurrentEndPosition;
        switch (strDirection) {
            case 'up':
                intNextStartPosition = intScrollItemHeight;
                intCurrentEndPosition = -intScrollItemHeight;
                break;
            case 'down':
                intNextStartPosition = -intScrollItemHeight;
                intCurrentEndPosition = intScrollItemHeight;
                break;
        }

        // determine next scroll item
        if (intCurrentScrollItemIndex == 0) {
            intNextScrollItemIndex = arrScrollItems.length - 1;
        } else {
            intNextScrollItemIndex = intCurrentScrollItemIndex - 1;
        }

        // set up next scroll item for animation
        var next = document.getElementById(arrScrollItems[intNextScrollItemIndex].id);
        next.style.top = intNextStartPosition + 'px';
        next.style.display = 'block';
        tweenNext = new Tween(next.style, 'top', Tween.regularEaseOut, intNextStartPosition, 0, .5, 'px');

        // set up current scroll item for animation
        var current = document.getElementById(arrScrollItems[intCurrentScrollItemIndex].id);
        tweenCurrent = new Tween(current.style, 'top', Tween.regularEaseOut, 0, intCurrentEndPosition, .5, 'px');
        tweenCurrent.onMotionFinished = function () { ScrollCallback(current); }

        // trigger scroll animation
        tweenNext.start();
        tweenCurrent.start();
    }
}
function ScrollCallback(current) {
    current.style.display = 'none';
    intCurrentScrollItemIndex = intNextScrollItemIndex;
    booScrolling = false;
}
