﻿/********************************************************
*   Carousel Hero JS
*   This is a tone-down version of Brent's explore hero
*   
*********************************************************/
function CarouselHero() {
    this._animDur_pageChange = 1000;
    this._autoslideDuration = 15000;
    this._curPage = 0;
    this._isAnimatingPage = false;
    this._currentIndex = 0;
    this._heroEntities = jQuery("#carouselHero #fullVersionHero .heroPage");
    this._heroTiles = jQuery("#carouselHero .heroTile li");
    this._navigation = jQuery("#carouselHero #carouselNavigation");
    this._heroCount = this._heroEntities.length;
    this._impressionFired = new Array();
    this.thisObject = null;
    this._autoRotate = true;
    this.timeoutObject = null;
    this._slideWidth = jQuery("#carouselHero").width();
}

CarouselHero.prototype =
{
    /* Initialize */
    initialize: function () {
        this.thisObject = this;
        this.bindPrevNext();
        this.bindThumbnail();
        this.loadHero();
        if (this._autoRotate) {
            this.autoCycle();
            this.bindAutoSlideEvent();
        }
    },

    //Auto Slide mouse over and out binding
    bindAutoSlideEvent: function () {
        var self = this;
        //attach mousein function
        jQuery("#carouselHero").bind("mouseover", function () {
            clearInterval(self.timeoutObject);
        });

        jQuery("#carouselHero").bind("mouseout", function () {
            self.autoCycle();
        });
    },

    bindThumbnail: function () {
        var self = this;
        var count = 0;
        jQuery("#carouselHero .heroTile li a").each(function (index) {
            jQuery(this).bind("click", function (e) {
                if (self._curPage != index) {
                    self.goToSlide(index);
                }
            });
        });
    },

    //set auto cycle
    autoCycle: function () {
        var self = this;
        if (null != self.timeoutObject) {
            clearInterval(self.timeoutObject);
        }
        self.timeoutObject = setInterval(function () {
            self.goToSlide("next");
        }, self._autoslideDuration);
    },

    //hero positioning loop
    loadHero: function () {
        //set positioning of all hero
        for (var i = 0; i < this._heroEntities.length; i++) {
            jQuery(this._heroEntities[i]).css({ "left": ((i - this._curPage) * this._slideWidth) + "px", "display": "block" });
        }

        //set tile for the first time and show navigation
        this.updateHeroTile();
        this.showCurrentHero("start");
        this.showNavigation();
    },

    //after animation call: fade in content
    showCurrentHero: function (direction, prevSlide) {
        var self = this;
        //do a fade in animation on current hero
        jQuery(this._heroEntities[this._curPage]).find(".content").fadeIn("slow", function () {
            //hide previous hero content
            if (null != prevSlide) {
                jQuery(self._heroEntities[prevSlide]).find(".content").hide();
            }
            self._isAnimatingPage = false;
        });
        if (typeof (impressionTrackingArray) != "undefined" && impressionTrackingArray.length == this._heroCount && !this._impressionFired[this._curPage]) {
            eval(impressionTrackingArray[this._curPage]);
            this._impressionFired[this._curPage] = true;
        }
    },

    updateHeroTile: function () {
        this._heroTiles.find(".active").removeClass("active").addClass("inactive");
        this._heroTiles.eq(this._curPage).find(".inactive").removeClass("inactive").addClass("active");
    },

    //improved custom go to slide functionality
    goToSlide: function (slide) {
        var slideDir;
        var slideNum;
        var slideDistance;
        var prevSlide = this._curPage;

        if ("number" == typeof (slide)) {
            //go to specific slide
            if (slide < this._curPage) {
                slideDir = "prev";
                slideDistance = prevSlide - slide;
            } else {
                slideDir = "next";
                slideDistance = slide - prevSlide
            }
            slideNum = slide;
        } else {
            if (slide == "prev") {
                slideNum = this._curPage - 1;
                if (slideNum == -1) {
                    slideNum = this._heroCount - 1;
                }
            } else if (slide == "next") {
                slideNum = this._curPage + 1;
                if (slideNum >= this._heroCount) {
                    slideNum = 0;
                }
            }
            slideDir = slide;
            slideDistance = 1;
        }
        this.beginChangePage(slideDir, slideDistance, prevSlide, slideNum);
    },

    //Slide left to right functionality
    beginChangePage: function (dir, distance, prevSlide, destPage) {
        var self = this;
        this._isAnimatingPage = true;
        var animTo = "";
        this.positionSlides(dir);
        if (dir == "prev") {
            animTo = "+=" + (self._slideWidth * distance);
        } else if (dir == "next") {
            animTo = "-=" + (self._slideWidth * distance);
        }
        this._curPage = destPage;

        this.hideNavigation();
        this.updateHeroTile();

        jQuery(this._heroEntities).animate({ left: animTo }, { duration: this._animDur_pageChange + (distance * 250), easing: "easeOut",
            complete: function () {
                self.showCurrentHero(dir, prevSlide);
                self.showNavigation();
            }
        });
    },

    //bind button left and right functionality
    bindPrevNext: function () {
        var self = this;
        jQuery("#carouselHero .btnPrev").bind("click", function () { self.btnPrevClick() });
        jQuery("#carouselHero .btnNext").bind("click", function () { self.btnNextClick() });
    },

    //call in previous hero scroll
    btnPrevClick: function (e) {
        var hero = this;
        if (!this._isAnimatingPage) {
            hero.goToSlide("prev");
        }
        return false;
    },

    //call in next hero scroll
    btnNextClick: function () {
        var hero = this;
        if (!this._isAnimatingPage) {
            hero.goToSlide("next");
        }
        return false;
    },

    hideNavigation: function () {
        this._navigation.hide();
    },

    showNavigation: function () {
        this._navigation.fadeIn("slow");
    },

    //slides positioning for infinite looping of heroes
    positionSlides: function (direction) {
        var self = this;
        if (this._heroEntities.length > 1) {
            for (var i = 0; i < this._heroCount; i++) {
                if ((self._curPage == 0) && (i == self._heroCount - 1) && direction == "prev") {
                    self._heroEntities.eq(i).css("left", "-" + self._slideWidth + "px");
                }
                else if ((self._curPage == (self._heroCount - 1)) && (i == 0) && direction == "next") {
                    self._heroEntities.eq(i).css("left", self._slideWidth + "px");
                }
                else {
                    var slideShift = (i - self._curPage) * self._slideWidth;
                    self._heroEntities.eq(i).css("left", slideShift + "px")
                }
            }
        }
    },

    log: function (logText) {
        try {
            console.log(logText)
        } catch (err) {
            //bail
        }
    }
}

jQuery(function () {
    jQuery.extend(jQuery.easing, {
        easeOut: function (x, t, b, c, d) {
            return -c * ((t = t / d - 1) * t * t * t - 1) + b;
        }
    });
});
