/*
	Author: Gab Nucum
	Filename: filter.js
	Description: Makes the dropdown text heading switch with the selected item.
	Dependencies:
		- jQuery
	How to use:
		1. The element must have a 'js-dropdown-filter' class.
		2. The label heading must have a 'js-dropdown-heading' class.
		3. The dropdown menu/list must have a 'js-dropdown-menu' class.
	Page Used: 
		1. Download Center

		
*/

;(function($) {
    'use strict'

    B2C.filter = {
        init: function(options) {
            this._default = {
                target: '.js-dropdown-filter',
                menu: '.js-dropdown-menu',
                heading: '.js-dropdown-heading'
            }

            this._options = $.extend(true, this._default, options)

            if (!$(this._options.target).length) return

            this.eventHandler()
        },

        eventHandler: function() {
            const self = this

            //active switcher
            $(`${self._options.menu} li`).on('click', function() {
                self.activeSwitcher(this)
            })

            //change dropdown text on select
            $(`${self._options.menu} li`).on('click', function() {
                self.setActiveFilter(this)
            })
        },

        activeSwitcher: function(element) {
            $(element).addClass('active')
            $(element)
                .siblings()
                .removeClass('active')
        },

        setActiveFilter: function(element) {
            const self = this
            $(element)
                .parents(self._options.target)
                .find(self._options.heading)
                .text($(element).text())
        }
    }

    const defaultFilter = Object.create(B2C.filter)

    defaultFilter.init()
})(jQuery)
