﻿//  Title:  Home Page Javscript
// Author:  Avala Marketing


var theHomeScrollTimeOut = "";
var inAnimation = 0;

function SwapHomeSlides(divIn, divOut) {

    // Keeps the user from switching a slide during animation.
    inAnimation = 1;

    $(divIn).addClass('active');
    $(divIn).animate({ opacity: 1 }, 1000);

    $(divOut).animate({ opacity: 0 }, 1000, function () {
        $(divOut).removeClass('active');
        inAnimation = 0;
    });

    $('a.imgNavBox.active').removeClass('active');
    $("a.imgNavBox[href='" + divIn + "']").addClass('active');

}

function RotateHomeSlides() {

    var theActiveId = $('div.homeGalleryImg.active').attr('id');
    var activeParts = theActiveId.split("_");
    var totalSlides = $('div.homeGalleryImg').length;
    var nextId = parseInt(activeParts[1]) + 1;

    nextId = (nextId <= totalSlides) ? "#img_" + nextId : "#img_1";
    theActiveId = "#" + theActiveId;

    SwapHomeSlides(nextId, theActiveId);

    theHomeScrollTimeOut = setTimeout(function () { RotateHomeSlides(); }, 3000);
}

function PageLoad() {
    if (theHomeScrollTimeOut) { clearTimeout(theHomeScrollTimeOut); }
    theHomeScrollTimeOut = setTimeout(function () { RotateHomeSlides(); }, 3000);
}


$(document).ready(function () {

    PageLoad();

    $('a.imgNavBox').bind('click', function (event, info) {

        if (inAnimation == 0) {
            // Get our Values
            var linkId = $(this).attr('href');
            var activeId = $('div.homeGalleryImg.active').attr('id');
            activeId = "#" + activeId;

            // Clear the existing Timer
            if (theHomeScrollTimeOut) {
                $('div.homeGalleryImg').stop();
                clearTimeout(theHomeScrollTimeOut);
                theHomeScrollTimeOut = null;
            }

            // Swap the Slides
            SwapHomeSlides(linkId, activeId);

            // Reset the Timer
            theHomeScrollTimeOut = setTimeout(function () { RotateHomeSlides(); }, 5000);
        }

        return false;
    });

});

