﻿$(function () {
    BTFC.splash.init();
});

BTFC.splash = {
    $bannersContainer: null,
    bannerTimerId: null,
    currentBanner: 1,
    numberOfBanners: 1,
    animating: true,
    init: function () {
        this.$bannersContainer = $("#splash-banner-rotations");
        this.currentBanner = $(".active", this.$bannersContainer).index() + 1;
        this.numberOfBanners = $(">div", this.$bannersContainer).length;
        this.startBannerAnimation();
        // listeners
        $("#splash-banner-arrows a").click(this.onArrowClick);
        $("#splash-content-container").mouseover(this.onContentContainerMouseover);
    },
    startBannerAnimation: function () {
        this.animating = true;
        this.bannerTimerId = setInterval(function () {
            BTFC.splash.currentBanner++;
            if (BTFC.splash.currentBanner > BTFC.splash.numberOfBanners) {
                BTFC.splash.currentBanner = 1;
            }
            BTFC.splash.loadRandomBanner();
        }, 8000);
    },
    loadRandomBanner: function () {
        // fade current banner out, fade supplied banner in
        $(".active", this.$bannersContainer).removeClass("active").fadeTo(700, 0.0, function () {
            $(this).addClass("hide");
            $(">div:eq(" + (BTFC.splash.currentBanner - 1) + ")", BTFC.splash.$bannersContainer).addClass("active").css("opacity", "0.0").removeClass("hide").fadeTo(700, 1.0);
            // change dot
            $("#splash-banner-dots a:eq(" + (BTFC.splash.currentBanner - 1) + ")").addClass("active").siblings().removeClass("active");
        });
    },
    onArrowClick: function (e) {
        e.preventDefault();
        if ($(this).hasClass('next')) {
            clearInterval(BTFC.splash.bannerTimerId);
            if (BTFC.splash.currentBanner == BTFC.splash.numberOfBanners) {
                BTFC.splash.currentBanner = 1;
            }
            else {
                BTFC.splash.currentBanner++;
            }
        }
        else if ($(this).hasClass('prev')) {
            clearInterval(BTFC.splash.bannerTimerId);
            if (BTFC.splash.currentBanner == 1) {
                BTFC.splash.currentBanner = BTFC.splash.numberOfBanners;
            }
            else {
                BTFC.splash.currentBanner--;
            }
        }
        BTFC.splash.loadRandomBanner();
    },
    onContentContainerMouseover: function () {
        if (!BTFC.splash.animating) {
            $("#splash-banner-tabs div").hide(); // hide tab image
            $("#splash-tabs li").removeClass("selected"); // remove selected tab
            $("#splash-banner-rotations, #splash-banner-dots").show(); // show rotating banners and dots
            BTFC.splash.startBannerAnimation();
        }

    }
};

