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


bm.filterObserver = (function ($) {
    var FilterObserver = function (element, options) {
        this.$element = $(element);
        this.identifier = this.$element.attr('id');

        var pathName = window.location.pathname.split('/').join('-');
        this.localStorageName = this.identifier+'_'+pathName+'_v2.0';

        this.options = $.extend({}, FilterObserver.DEFAULTS, options);
        this.init();
    }

    FilterObserver.VERSION = '1.0.1';

    FilterObserver.DEFAULTS = {
        multiple: true,
        replace: false,
        filter: {}
    }

    FilterObserver.prototype.init = function () {
        this.$element.data('bm.filterobserver', this);

        var testHash = window.location.hash;
        if (!testHash.length && localStorage.getItem(this.localStorageName)){
            this.options = JSON.parse(localStorage.getItem(this.localStorageName));
        }
        
        this.update();
    }

    FilterObserver.prototype.update = function (options) {
        var self = this;
        if (options) {
            if (options.replace) {
                $.each(options.filter, function(filterCat, filterValues){
                    //if (self.options && typeof self.options[filterCat])
                    //    console.log('typeof',typeof self.options.filter[filterCat]);
                    //console.log(self.options[''+filterCat+''], filterCat, filterValues);
                    self.options.filter[filterCat] = filterValues
                })
                //this.options.filter = options.filter;
            } else {
                if (this.options.multiple) {
                    options.filter = this.updateMultiple(this.options.filter, options.filter);
                }
                options.filter = $.extend({}, this.options.filter, options.filter);
                this.options = $.extend({}, this.options, options);
            }
            var store = JSON.stringify(this.options)
            localStorage.setItem(this.localStorageName, store);
        }


        var updatedEvent = $.Event('updated.bm.filterObserver', {
            relatedTarget: this
        });
        this.$element.trigger(updatedEvent);

    }

    FilterObserver.prototype.updateMultiple = function (currentFilter, newFilter) {
        $.each(currentFilter, function (key, filterValues) {
            newFilterValue = newFilter[key];
            if ('all' == newFilterValue) {

            } else {
                filterValues = filterValues.split(',');
                filterIndex = filterValues.indexOf('all');
                if (filterIndex > -1) {
                    filterValues.splice(filterIndex, 1);
                }

                filterIndex = filterValues.indexOf(newFilterValue);
                if (filterIndex > -1) {
                    filterValues.splice(filterIndex, 1);
                } else {
                    filterValues.push(newFilter[key]);
                }

                filterValues = filterValues.filter(function (n) {
                    return n != undefined
                });

                newFilter[key] = filterValues.join(',');
                if (newFilter[key] == '') {
                    newFilter[key] = 'all';
                }
            }
        })

        return newFilter;

    }


    // FilterObserver PLUGIN DEFINITION
    // ==========================

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

            if (!pluginData) {
                var options = $.extend({}, FilterObserver.DEFAULTS, $this.data(), typeof option === 'object' && option);

                pluginData = new FilterObserver(this, options);
                $this.data('bm.filterobserver', pluginData);
            } else {
                if (typeof option === 'string') {
                    pluginData[option]();
                } else {
                    pluginData.update(option);
                }
            }
        });

    }

    var old = $.fn.filterobserver;

    $.fn.filterobserver = Plugin;
    $.fn.filterobserver.Constructor = FilterObserver;


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

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


// alle filterTrigger
// subscribe observer
// doOnX ->update Observer


// alle content objec