﻿var homeHero = {
    isAutoScrolling: false,
    autoScrollTimeout: null,
    currentSlide: 0,
    slides: 1,
    slideWidth: 984,
    totalWidth: function() { return this.slideWidth * this.slides; },
    isAnimationComplete: true,
    stopAutoScroll: function() {
        window.clearTimeout(this.autoScrollTimeout);
    },
    easing: "easeInOutSine",
    duration: 750,
    slideLeft: function() {
        if (!this.isAutoScrolling) this.stopAutoScroll();
        if (this.isAnimationComplete) {
            this.isAutoScrolling = false;
            this.isAnimationComplete = false;
            var origContainer = null;
            var cloneContainer = null;
            if (this.currentSlide == this.slides - 1) {
                this.currentSlide = -1;
                origContainer = $(".slideContainerInner");
                cloneContainer = origContainer.clone();
                cloneContainer.css("left", this.slideWidth + "px");
                $(".slideContainer").append(cloneContainer);
            }
            $(".slideContainerInner").each(function() {
                $(this).animate({
                    left: "-=" + homeHero.slideWidth
                }, homeHero.duration, homeHero.easing, function() {
                    if (!homeHero.isAnimationComplete) {
                        if (cloneContainer != null) {
                            origContainer.remove();
                        }
                        homeHero.currentSlide++;
                        homeHero.isAnimationComplete = true;
                    }
                }
                );
            });
            if (this.currentSlide == -1) {
                $("div.slideInfoContainer > div.slideInfo").last().fadeOut(homeHero.duration / 2, function() {
                    $("div.slideInfoContainer > div.slideInfo").first().fadeIn(homeHero.duration / 2);
                });
                $("div.slideIndicator > div.currentIndicator").animate({
                    left: "-=" + ((this.slides - 1) * 22)
                }, homeHero.duration, homeHero.easing
                );
            } else {
                $("div.slideInfoContainer > div.slideInfo").each(function() {
                    if ($(this).css("display") == "block") {
                        $(this).fadeOut(homeHero.duration / 2, function() {
                            $(this).next().fadeIn(homeHero.duration / 2);
                        });
                        return;
                    }
                });
                $("div.slideIndicator > div.currentIndicator").animate({
                    left: "+=22"
                }, homeHero.duration, homeHero.easing);
            }

        }
    },
    viewSlide: function(index) {
        this.stopAutoScroll();
        if (this.isAnimationComplete) {
            this.isAnimationComplete = false;
            if (index > this.currentSlide) {
                $(".slideContainerInner").each(function() {
                    $(this).animate({
                        left: "-=" + (homeHero.slideWidth * (index - homeHero.currentSlide))
                    }, homeHero.duration, homeHero.easing, function() {
                        if (!homeHero.isAnimationComplete) {
                            homeHero.currentSlide = index;
                            homeHero.isAnimationComplete = true;
                        }
                    }
                    );
                });
                $("div.slideIndicator > div.currentIndicator").animate({
                    left: "+=" + (22 * (index - homeHero.currentSlide))
                }, homeHero.duration, homeHero.easing);
            } else if (index < this.currentSlide) {
                $(".slideContainerInner").each(function() {
                    $(this).animate({
                        left: "+=" + (homeHero.slideWidth * (homeHero.currentSlide - index))
                    }, homeHero.duration, homeHero.easing, function() {
                        if (!homeHero.isAnimationComplete) {
                            homeHero.currentSlide = index;
                            homeHero.isAnimationComplete = true;
                        }
                    }
                    );
                });
                $("div.slideIndicator > div.currentIndicator").animate({
                    left: "-=" + (22 * (homeHero.currentSlide - index))
                }, homeHero.duration, homeHero.easing
                );
            }
            $("div.slideInfoContainer > div.slideInfo").each(function() {
                if ($(this).css("display") == "block") {
                    $(this).fadeOut(homeHero.duration / 2, function() {
                        $(this).parent().find(".slide" + index).fadeIn(homeHero.duration / 2);
                    });
                    return;
                }
            });
        }
    },
    autoScroll: function() {
        this.autoScrollTimeout = window.setTimeout(function() {
            homeHero.isAutoScrolling = true;
            homeHero.slideLeft();
            homeHero.autoScroll();
        }, 5750);
    }
}
$(window).load(function () {
    homeHero.autoScroll();
});
