// important vars
var btnTopId = 'btnTop',
	btnBtmId = 'btnBtm',
	iWndWidth,
	iWndHeight,
	bScrolling = false,
    numSlidesVis = 3,
    numSlides,
    slideHeight,
    iCurSlideOffset,
    iCurSlideIndex,
    slideShowActive = true,
    slideDelayMs = 7000,
    iIntId;

// once everything is downloaded
window.onload = function()
{
    iIntId = setInterval('nextSlide();', slideDelayMs);
    
    // late load the remaining images
    for (var i = 1; i < $('#services .slide').length; i++)
        $('#services .slide').eq(i).find('img').attr('src', $('#services .slide').eq(i).find('img').attr('rel'));
}

$(function()
{
    loadGallery();
    // bind the click events on the btns
    $('#' + btnTopId).click(scrollDown);
    $('#' + btnBtmId).click(scrollUp);
    arrowHilite = $('#slideNav #hilite');
    addHiliteArrow();

    var arrToAppend = new Array(),
        arrToInsert = new Array(),
        arrSlides = $('#slideNav .navItem');

    // store a handle to the number of slides before modification
    numSlides = arrSlides.length;

    // store the first numToDup nav items and to be added to the end of the list
    for (var i = 0; i < numSlidesVis; i++)
        arrToAppend.push(arrSlides[i]);

    // store the last numToDup nav items and to be added to the beginning of the list
    for (var i = arrSlides.length - numSlidesVis; i < arrSlides.length; i++)
        arrToInsert.push(arrSlides[i]);

    // now add items to the end of the array
    for (var i = 0; i < numSlidesVis; i++)
    {
        $('#slideNav').append($(arrToAppend[i]).clone());
    }

    // prepend items in reverse order
    for (var i = numSlidesVis - 1; i >= 0; i--)
    {
        $('#slideNav').prepend($(arrToInsert[i]).clone());
    }

    // Offest the scrolling div by
    $('#slideNav').css('top', -(slideHeight * numSlidesVis) + 'px');

    // set the current slide counter to 0
    iCurSlideOffset = 0;

    $('#slideNav .navItem').each(function()
    {
        var curSlideNav = $(this);
        var slideName = curSlideNav.attr('id');
        curSlideNav.hover(function()
        {
            var activeSlide = $('#services .' + slideName);
            if (!activeSlide.hasClass("active") && activeSlide.length > 0)
            {
                $('#services .slide').hide('fast').removeClass("active");
                activeSlide.show('fast').addClass("active");

                $('#slideNav .navItem').removeClass("active");
                curSlideNav.addClass("active");
                addHiliteArrow();

                // Update the current slide index
                if (!slideShowActive)
                    iCurSlideOffset = (curSlideNav.position().top / slideHeight) % numSlidesVis + 1;
            }
        }, function() { });
        curSlideNav.click(function()
        {
            var curLink = $(this).find('a');
            if (curLink.length > 0)
                window.location.href = curLink.attr("href");
        });
    });

    // set up the automatic portion of the slideshow
    nextSlide();

    $('#middle').hover(
        function() { slideShowActive = false; },
        function() { slideShowActive = true; });
});


function addHiliteArrow() {
	var selectedNav = $('#slideNav .active');
	var position = selectedNav.position();
	var arrowY = (selectedNav.outerHeight() / 2) + position.top - (arrowHilite.outerHeight() / 2);
	arrowHilite.show().css("top", arrowY);	
}

function loadGallery() {
    //$('#slideGallery').show();

    // set slideHeight
    slideHeight = $('#slideNav .navItem').eq(0).outerHeight(true);

    // force window height to be the height numSlidesVis slides
    $('#listWindow').height(slideHeight * numSlidesVis);
    
    // get the window width for scrolling
    iWndWidth = $('#listWindow').width();
    iWndHeight = $('#listWindow').height();

    // set the width of the slideNav window based on the number of slides
    $('#slideNav').height(iWndHeight * $('#slideNav .navItem').length / numSlidesVis)
                                .width(iWndWidth);

    // return false
    return false;
}

function scrollDown(e){
    iCurSlideOffset = 0;
    vidScroll(true, true);
    iCurSlideOffset = 1;
}
function scrollUp(e){
    iCurSlideOffset = 0;
    vidScroll(false, true);
    iCurSlideOffset = 1;
}

function vidScroll(bDown, bCallNextSlide) {
    // if we are not in the middle of an animation
    if (!bScrolling)
    {
        // prevent auto scroll from hopping over slides
        clearInterval(iIntId);
        
        // set the scrolling flag to prevent multi-click madness
        bScrolling = true;

        // get the position of the inner div
        var iYPos = $('#slideNav').position().top;
        
        // get a handle to the index of the slide at the top of the window
        var iScrollPos = -iYPos / slideHeight;

        // if the list is moving down and there are not enough at least "numSlidesVis" slides above the fold
        if (iScrollPos < numSlidesVis && bDown)
        {
            // we need to jump down the list to fake the circular effect

            // Description of the Offsets Below:
            //        |    # of slides          |   # slides above fold  |
            iYPos = -((numSlides) * slideHeight + iScrollPos * slideHeight);
            $('#slideNav').css('top', iYPos + 'px');
        }
        // if the list is moving up and there are not enough at least "numSlidesVis" slides below the fold
        else if (iScrollPos >= numSlides && !bDown)
        {
            // we need to jump up the list to fake the circular effect

            // Description of the Offsets Below:
            //        take our current position and subtract the number of slides
            iYPos = -((iScrollPos - numSlides) * slideHeight);
            $('#slideNav').css('top', iYPos + 'px');
        }
        
        // calculate the new position
        if (bDown)
            iYPos += iWndHeight;
        else
            iYPos -= iWndHeight;

        // update the iCurSlideIndex to account for list jumps and animation to follow
        iCurSlideIndex = -iYPos / slideHeight;
        
        // animate the slide
        $('#slideNav').animate({ top: iYPos + 'px' }, 800, 'swing', function() { bScrolling = false });

        // if we are to call the next slide function
        if (bCallNextSlide)
            nextSlide(true);

        // reset interval
        iIntId = setInterval('nextSlide();', slideDelayMs);
    }
}

function nextSlide(bForceAnimation){
    if (slideShowActive || bForceAnimation)
    {        
        // get a handle to the index of the slide at the top of the window
        if (!bForceAnimation)
            iCurSlideIndex = -$('#slideNav').position().top / slideHeight;

        // call the scroll function (this will modify iCurSlideIndex)
        if (iCurSlideOffset > 2 && !bForceAnimation)
        {
            iCurSlideOffset = 0;
            vidScroll(false, false);
        }

        // add the current slide offset
        iCurSlideIndex += iCurSlideOffset;

        // call the hover function for iSlideIndex
        $('#slideNav .navItem').eq(iCurSlideIndex).hover();

        // increment the current slide
        iCurSlideOffset++;
    }
}