$(function(){
    /*
        Enable the slideshows.

        Parameters:
        - data-interval : integer (default: 10000)
        - data-effect-in : string (default: 'bounceIn')
        - data-effect-out : string (default: 'bounceOut')
     */
    $('*[data-slide=true]').each(function() {
        var $slide = $(this);

        var interval = parseInt($slide.attr('data-interval'), 10);
        if (_.isUndefined(interval)) {
            interval = 10000;
        }

        var effectIn = $slide.attr('data-effect-in');
        if (_.isUndefined(effectIn)) {
            effectIn = 'bounceIn';
        }

        var effectOut = $slide.attr('data-effect-out');
        if (_.isUndefined(effectOut)) {
            effectOut = 'bounceOut';
        }

        var $children = $slide.children().addClass('animated');
        $children.not(':first').show().remove();

        var current = 0, max = $children.length;
        
        setInterval(function() {
            var $current = $($children[current]);

            current++;
            if (current >= max) {
                current = 0;
            }

            var $next = $($children[current]);

            $current.removeClass(effectIn).addClass(effectOut);
            $current.on('animationend', function() {
                $current.remove();
                $next.show().removeClass(effectOut).addClass(effectIn);
                $slide.append($next);
            });
        }, interval);
    });

    /*
        Enable the animations.

        Parameters:
        - data-delay : integer (default: 0)
        - data-interval : integer (default: 10000)
        - data-effect : string (default: 'flash')
     */
    $('*[data-animate=true]:not([data-animate-trigger])').each(function() {
        $(this).startAnimate();
    });
});

$.fn.extend({
    // Start the animation.
    startAnimate: function() {
        return $(this).each(function() {
            var $this = $(this).addClass('animated');
    
            var delay = parseInt($this.attr('data-delay'), 10);
            if (_.isUndefined(delay)) {
                delay = 1;
            }
    
            var interval = parseInt($this.attr('data-interval'), 10);
            if (_.isUndefined(interval)) {
                interval = 0;
            }
    
            var effect = $this.attr('data-effect');
            if (_.isUndefined(effect)) {
                effect = 'flash';
            }
    
            var speed = $this.attr('data-speed');
            if (!_.isUndefined(speed)) {
                $this.addClass(speed);
            }

            var mustBeHidden = $this.attr('data-animate-hide') == 'true'
    
            var thisID = $this.attr('id');
            var $nextAnimations = [];
            if (!_.isUndefined(thisID) && thisID.length > 0) {
                $nextAnimations = $('*[data-animate=true][data-animate-trigger=' + thisID + ']');
            }
    
            var goAnimate = function() {
                $this.show().css('opacity', '1.0').addClass(effect);
                $this.on('animationend', function() {
                    $this.removeClass(effect);

                    if (mustBeHidden) {
                        $this.css('opacity', '0.0');
                    }
    
                    if ($nextAnimations.length > 0) {
                        $nextAnimations.startAnimate();
                    }
                });
            }
            
            setTimeout(function() {
                goAnimate();
                if (interval > 0) {
                    var id = setInterval(goAnimate, interval);
                    $this.data('setIntervalID', id);
                }
            }, delay);
        });
    },

    // Stop the animation.
    stopAnimate: function() {
        return $(this).each(function() {
            var $this = $(this).removeClass('animated');
    
            var effect = $this.attr('data-effect');
            if (_.isUndefined(effect)) {
                effect = 'flash';
            }
            $this.removeClass(effect);

            $this.off('animationend');
    
            var id = $this.data('setIntervalID');
            if (!_.isUndefined(id)) {
                clearInterval(id);
            }
        });
    }
});