﻿var BH = BH || {}

BH.homeCarousel = function () {
    var carousels = [];
    var defaultConfig = {
        intervalDuration: 7000,
        startIndex: 0
    }

    function carousel(el, options) {
        this.el = $(el);
        this.carouselItems = [];
        this.config = {};

        for (var option in defaultConfig) {
            this.config[option] = defaultConfig[option];
        }

        for (var option in options) {
            this.config[option] = options[option];
        }

        this.findElements();
        this.bindEvents();

        this.showItem(this.config.startIndex, false);
        this.play();
    }

    carousel.prototype.findElements = function () {
        var _this = this;

        this.el.find(".category").each(function (i) {

            var item = new carouselItem($(this), i, _this);

            _this.carouselItems.push(item);

        });

        this.controlEl = $("<a class='carousel-control' href='javascript:void(0)' />").prependTo(this.el);
    }

    carousel.prototype.bindEvents = function () {
        var _this = this;

        this.controlEl.bind("click", function (e) { _this.controlEl_click(e); });
    }

    carousel.prototype.controlEl_click = function (e) {
        e.preventDefault();

        if (this.isPlaying == true) {
            this.stop();
        } else {
            this.isStopped = false;
            this.play(true);
        }
    }

    carousel.prototype.showItem = function (item, animate) {
        if (item instanceof carouselItem) item = item.index;

        for (var i = 0; i < this.carouselItems.length; i++) {
            if (i == item) {
                this.carouselItems[i].show(animate);
                this.activeItem = this.carouselItems[i];
            } else {
                this.carouselItems[i].hide();
            }
        }
    }

    carousel.prototype.play = function (instant) {
        var _this = this;

        this.controlEl.text('Pause').removeClass('play').addClass('pause');
        this.isPlaying = true;

        var showNext = function () {
            if (!_this.activeItem || _this.activeItem.index == _this.carouselItems.length - 1) {
                _this.showItem(0);
            } else {
                _this.showItem(_this.activeItem.index + 1);
            }
        };

        if (instant) showNext();

        window.clearInterval(this.interval);
        this.interval = setInterval(showNext, this.config.intervalDuration);
    }

    carousel.prototype.pause = function () {
        this.controlEl.text('Play').removeClass('pause').addClass('play');
        this.isPlaying = false;

        window.clearInterval(this.interval);
    }

    carousel.prototype.stop = function () {
        this.isStopped = true;
        this.pause();
    }


    function carouselItem(el, index, manager) {
        this.el = el;
        this.index = index;
        this.manager = manager;

        this.findElements();
        this.bindEvents();
    }

    carouselItem.prototype.findElements = function () {
        this.infoEl = this.el.find(".category-info").show();
        var infoEl_height = this.infoEl.height();
        this.infoEl.hide().css("height", infoEl_height);

        this.imageEl = this.el.find("img").eq(0);
        this.manager.el.append(this.imageEl);
    }

    carouselItem.prototype.bindEvents = function () {
        var _this = this;

        this.el.click(function () {
            _this.manager.showItem(_this);
            _this.manager.pause();
        });

        this.el.hover(function () {
            if (_this.manager.activeItem == _this) {
                _this.manager.pause();
            }
        }, function () {
            if (!_this.manager.isStopped && _this.manager.activeItem == _this) {
                _this.manager.play();
            }
        });
    }

    carouselItem.prototype.hide = function (animate) {
        if (animate || typeof animate == "undefined") {
            this.infoEl.slideUp();

            if (this.imageEl.length > 0) {
                this.imageEl.fadeOut(1000);
            }
        } else {
            this.infoEl.show();

            if (this.imageEl.length > 0) {
                this.imageEl.hide();
            }
        }

        return this;
    }

    carouselItem.prototype.show = function (animate) {
        if (animate || typeof animate == "undefined") {
            this.infoEl.slideDown();

            if (this.imageEl.length > 0) {
                this.imageEl.fadeIn(1000);
            }
        } else {
            this.infoEl.show();

            if (this.imageEl.length > 0) {
                this.imageEl.show();
            }
        }

        return this;
    }

    function init(el, options) {
        carousels.push(new carousel(el, options));
    }

    return {
        init: init,
        carousels: carousels
    }

} ();
