﻿/*!
* Filename:		    Default.js
* Project:		    www.BuckeyeCorrugated.com/js
* Description:	    Home page slidewhow animation script
* Dependencies:     jquery-1.4.4.min.js
*/

// Instantiate the interval object for the slideshow transitions
var objInterval = null;

// Instantiate the timeout object for the slideshow click events
var objTimeout = null;

// Initialize the slide index tracking variable
var slideIndex = 1;

var slideInterval = 10000;

// Initialize the maximum slide index value
var slideMax = 5;

function UpdateSlideShow()
{
    // Get the active slide
    var $activeSlide = $(".SlideTransition .ActiveSlide");

    // Determine the next slide based on where we currently are in the sequence of slides
    // We either go next or back to the beginning
    var $nextSlide = ($activeSlide.next().length) ? $activeSlide.next() : $(".SlideTransition div:first");

    // Set the visibility property for the first time through
    $nextSlide.css("visibility", "visible");

    // Keep track of where we are in the sequence
    slideIndex = $nextSlide.index() + 1;

    // Make the current slide the previously active slide
    // This preserves the slide transition effect when we go back to the beginning
    $activeSlide.addClass("LastActive");

    // Fade in/animate the next slide
    $nextSlide.css({ opacity: 0.0 }).addClass("ActiveSlide").animate({ opacity: 1.0 }, 1000, function ()
    {
        // Clear the class info of the now previously visible slide
        $activeSlide.removeClass("ActiveSlide LastActive");
    });
}

function GoToSlide(slideID)
{
    // Clear the interval of slide transitions
    clearInterval(objInterval);

    // Clear any timeout set by a click event
    clearTimeout(objTimeout);

    // Get the currently active slide
    var $activeSlide = $(".SlideTransition .ActiveSlide");

    // Get the slide that was selected
    var $selectedSlide = $(".SlideTransition #Slide" + slideID);

    // Set the visibility property for the first time through
    $selectedSlide.css("visibility", "visible");

    // Keep track of where we are in the sequence
    slideIndex = $selectedSlide.index() + 1;

    // Make the current slide the previously active slide
    // This preserves the slide transition effect when we go backwards
    $activeSlide.addClass("LastActive");

    // Fade in/animate the selected slide
    $selectedSlide.css({ opacity: 0.0 }).addClass("ActiveSlide").animate({ opacity: 1.0 }, 1000, function ()
    {
        // Clear the class info of the now previously visible slide
        $activeSlide.removeClass("ActiveSlide LastActive");
    });

    // Restart the interval of slide transitions
    //AnimateSlideShow();
    objTimeout = setTimeout("AnimateSlideShow()", slideInterval);
}

function GoToSlideNextPrevious(direction)
{
    // Determine the default based on the direction
    var defaultSlideID = (direction == 1) ? 1 : slideMax;

    // Calculate the next slide sequecne ID
    var nextSlideID = (direction == 1) ? slideIndex + 1 : slideIndex - 1;

    // Make sure the calculated slide is in bounds
    nextSlideID = (nextSlideID > slideMax || nextSlideID < 1) ? defaultSlideID : nextSlideID;

    // Go to the calculated slide
    GoToSlide(nextSlideID);
}

function AnimateSlideShow()
{
    // Clear any timeout set by a click event
    clearTimeout(objTimeout);

    // Start the interval of transitions
    objInterval = setInterval("UpdateSlideShow()", slideInterval);
}

$(".HomeSlideBox").ready(function ()
{
    AnimateSlideShow();
});
