/*
 Original Plugin by Osvaldas Valutis, www.osvaldas.info
 http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly
 Available for use under the MIT License
 */
/**
 * jquery-doubleTapToGo plugin
 * Copyright 2017 DACHCOM.DIGITAL AG
 * @author Marco Rieser
 * @author Volker Andres
 * @author Stefan Hagspiel
 * @version 3.0.2
 * @see https://github.com/dachcom-digital/jquery-doubletaptogo
 */
(function ($, window, document, undefined) {
    'use strict';
    var pluginName = 'doubleTapToGo',
        defaults = {
            automatic: true,
            selectorClass: 'doubletap',
            selectorChain: 'li:has(ul)'
        };

    function DoubleTapToGo (element, options) {
        this.element = element;
        this.settings = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }

    $.extend(DoubleTapToGo.prototype, {
        preventClick: false,
        currentTap: $(),
        init: function () {
            $(this.element)
                .on('touchstart', '.' + this.settings.selectorClass, this._tap.bind(this))
                .on('click', '.' + this.settings.selectorClass, this._click.bind(this))
                .on('remove', this._destroy.bind(this));

            this._addSelectors();
        },

        _addSelectors: function () {
            if (this.settings.automatic !== true) {
                return;
            }
            $(this.element)
                .find(this.settings.selectorChain)
                .addClass(this.settings.selectorClass);
        },

        _click: function (event) {
            if (this.preventClick) {
                event.preventDefault();
            } else {
                this.currentTap = $();
            }
        },

        _tap: function (event) {
            var $target = $(event.target).closest('li');
            if (!$target.hasClass(this.settings.selectorClass)) {
                this.preventClick = false;
                return;
            }
            if ($target.get(0) === this.currentTap.get(0)) {
                this.preventClick = false;
                return;
            }
            this.preventClick = true;
            this.currentTap = $target;
            event.stopPropagation();
        },

        _destroy: function () {
            $(this.element).off();
        },

        reset: function () {
            this.currentTap = $();
        }
    });

    $.fn[pluginName] = function (options) {
        var args = arguments,
            returns;
        if (options === undefined || typeof options === 'object') {
            return this.each(function () {
                if (!$.data(this, pluginName)) {
                    $.data(this, pluginName, new DoubleTapToGo(this, options));
                }
            });
        } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
            this.each(function () {
                var instance = $.data(this, pluginName),
                    methodName = (options === 'destroy' ? '_destroy' : options);
                if (instance instanceof DoubleTapToGo && typeof instance[methodName] === 'function') {
                    returns = instance[methodName].apply(instance, Array.prototype.slice.call(args, 1));
                }
                if (options === 'destroy') {
                    $.data(this, pluginName, null);
                }
            });
            return returns !== undefined ? returns : this;
        }
    };
})(jQuery, window, document);
