var jQuery = jQuery || {};
var bm = bm || {};


bm.filterTags = (function ($) {

    var FilterTags = function (element, options) {
        this.$element = $(element);

        this.options = $.extend({}, FilterTags.DEFAULTS, options);
        this.$items = null;

        this.current = {
            filter: {},
            showitems: 0
        }
        this.init();
    };

    FilterTags.VERSION = '1.0.2';


    FilterTags.DEFAULTS = {
        filter: {
        },
        show: 'all',
        forFilter: [],
        classes: {
            hide: 'hideMe',
            show: 'showMe'
        }
    };

    FilterTags.prototype.init = function () {
        this.$element.data('bm.filtertags', this);
        if (this.$element.data('filter-for')){
            this.options.forFilter = this.$element.data('filter-for').split(',');
        }
        var elementId = this.$element.attr('id');
        this.addTags();
        addEvents.call(this);
    };

    FilterTags.prototype.update = function (options) {
        if (options){
            options.filter = $.extend({}, this.options.filter, options.filter);
            this.options = $.extend({}, this.options, options);
        };

        this.addTags();
    };


    FilterTags.prototype.addTags = function(){
        var filterGroups = this.options.filter;

        var self = this;
        var counter = 0;
        var $html = getButtonGroup.call(this);
            $html.html('');
        $.each(filterGroups, function (key, value) {
            var values = value.split(',');
            if (self.options.forFilter.length <= 0 || $.inArray(key, self.options.forFilter) >=0){
                $.each(values, function(subKey, subValue){
                    if (subValue == 'all') {
                        // mache nichts wenn cat == all
                    } else {
                        counter++;
                        var newValue = $('.filtercontent-detail-filters  [data-filter-type="'+key+'"]').filter('[data-filter-show="'+subValue+'"]').data('filter-value');
                        $html.append('<button class="btn btn-xs btn-nordsee--tag btn-nordsee--addCloseIcon" data-filter-type="'+key+'" data-filter-show="'+subValue+'">' + newValue + '<i class="icon-i_btn_mehr asCloseicon"></i></button>');
                    }
                })
            }
        })
        if (counter >0){
            this.$element.trigger('hasItems.bm.filtertags');
        } else {
            this.$element.trigger('isEmpty.bm.filtertags');
        }

        //this.$element.html($html);
    }

    var getButtonGroup = function(){
        var $btnGroup = this.$element.find('.filtertag-listing')
        if ($btnGroup.length <= 0){
            $btnGroup = $('<div class="filtertag-listing btn-group btn-group-left btn-group--spacing-default " />');
            this.$element.append($btnGroup);
        }
        return $btnGroup;
    }

    var addEvents = function() {
        this.$element.on('click', 'button', clickHandler);
    }

    var clickHandler = function(e){
        var $this = $(this);
        var $parent = $(e.delegateTarget);
        var $target = $($parent.data('observer'));
        if (!$target.hasClass('js-filterObserver')) {
            return;
        }

        var filterType = $this.data('filter-type') || 'categories';
        var filter = {};
        filter[filterType] = $this.data('filter-show');
        var options = {filter: filter};
        $target.filterobserver(options);
        e.preventDefault();
    }

    // Contentfilter PLUGIN DEFINITION
    // ==========================

    function Plugin(option) {
        return this.each(function () {
            var $this = $(this);
            var pluginData = $this.data('bm.filtertags');

            if (!pluginData) {
                var options = $.extend({}, FilterTags.DEFAULTS, $this.data(), typeof option === 'object' && option);
                pluginData = new FilterTags(this, options);
                $this.data('bm.filtertags', pluginData);
            } else {
                if (typeof option === 'string') {
                    pluginData[option]();
                } else {
                    pluginData.update(option);
                }
            }
        });

    }

    var old = $.fn.filtertags;

    $.fn.filtertags = Plugin;
    $.fn.filtertags.Constructor = FilterTags;


    // Contentfilter NO CONFLICT
    // ====================

    $.fn.filtertags.noConflict = function () {
        $.fn.filtertags = old;
        return this;
    };


    // Contentfilter DATA-API
    // =================





}(jQuery));
