﻿var carousel = {
    _init: function () {
        this.options.currentTab = this.options.startTab;
        this._getAllCarouselTabs().hide();
        switch (this.options.effect) {
            case 'fading':
            default:
                this._getCarouselTab(this.options.startTab).fadeIn();
                break;
        }
        if (this.options.runOnInit) {
            this.start();
        }
        this._getAllCarouselTabs().css({ position: 'absolute' });
    },
    _getCarouselContainer: function () {
        return jQuery('#' + this.options.carouselContainerId);
    },
    _getCarouselTab: function (i) {
        return jQuery('#' + this.options.carouselContainerId).find('.' + this.options.tabSelector + ':eq(' + i + ')');
    },
    _getAllCarouselTabs: function () {
        return jQuery('#' + this.options.carouselContainerId).find('.' + this.options.tabSelector);
    },
    _getCurrentCarouselTab: function () {
        return this._getCarouselTab(this.options.currentTab);
    },
    _tabCount: function () {
        return this._getAllCarouselTabs().length;
    },
    _show: function (i) {
        var oldTab = this.options.currentTab;
        this._updateTab(i);
        switch (this.options.effect) {
            case 'fading':
            default:
                this._fading(oldTab, this.options.currentTab);
                break;
        }
        if (this.options.onPageChanged != null) {
            this.options.onPageChanged(this.options.currentTab);
        }
    },
    _updateTab: function (i) {
        this.options.currentTab = i;
        if (i > this._tabCount() - 1) {
            this.options.currentTab = 0;
        }
    },
    _fading: function (fadeOutId, fadeInId) {
        this._getCarouselTab(fadeOutId).fadeOut();
        this._getCarouselTab(fadeInId).fadeIn();
    },
    next: function () {
        this._show(this.options.currentTab + 1);
    },
    previous: function () {
        this._show(this.options.currentTab--);
    },
    goto: function (i) {
        this.stop();
        this._show(i - 1);
        if (!this.options.stopOnClick) {
            this.start();
        }
    },
    start: function () {
        var $this = this;
        this.options.intervalId = window.setInterval(function () { $this.next(); }, this.options.speed);
        if (this.options.onStartEvent != null) {
            this.options.onStartEvent();
        }
    },
    stop: function () {
        window.clearInterval(this.options.intervalId);
        if (this.options.onStopEvent != null) {
            this.options.onStopEvent();
        }
    },
    off: function () {
        this.destroy(); // use the predefined function
    },
    options: {
        carouselContainerId: null,
        tabSelector: 'carouselTab',
        effect: 'fading',
        startTab: 0,
        currentTab: 0,
        speed: 3000,
        runOnInit: true,
        intervalId: null,
        stopOnClick: false,
        onStartEvent: null,
        onStopEvent: null,
        onPageChanged: null
    }
};

jQuery.widget("ui.carousel", carousel);

