(function () {
    'use strict';

    angular
    .module('editorRotas')
    .directive('collapseBox', collapseBox);

    function collapseBox() {
        var directive = {
            restrict: 'A',
            link: link
        };

        function link(scope, element, attr, ctrl) {
            var trigger = element.find('.collapse-caption');

            scope.toggleCollaps = function ($event) {
                var callBack = 'scope.' + element.data('callback');

               var trigger = $event.currentTarget;
               var target = $(trigger).next();
               $(trigger).toggleClass('open');
               $(trigger).parent().toggleClass('open');
               $(target).slideToggle();

                 $(target).parent().siblings('.open').find('.collapse-content').slideUp();
                 $(trigger).parent().siblings('.open').removeClass('open');

                //element.toggleClass('open').find('.collapse-content').slideToggle();

                eval(callBack);
            }

            $(trigger).on('click', function ($event) {
                var realTrigger = $event.target;

                if ($(realTrigger).hasClass('action')) {
                    $('.toolbar .tool').find('.icon-clock').parent().addClass('active');

                    var action = 'scope.' + $(realTrigger).data('action');
                    eval(action);
                } else {
                    scope.toggleCollaps($event);
                }
            });
        }

        return directive;
    }
})();
