( function( $ ) {

    "use strict";
    let selectedPart = '';
    let selectedId = -1;

    function isTouchEnabled() {
    return (('ontouchstart' in window)
        || (navigator.MaxTouchPoints > 0)
        || (navigator.msMaxTouchPoints > 0));
    }

    $(document).ready(function () {
        $("path[id^=\"unisexsbs_\"]").each(function (i, e) {
            addEvent($(e).attr('id'));
        });
    });

    function addEvent(id, relationId) {
        var _obj = $('#' + id);
        $('#unisexsbs-wrapper').css({'opacity': '1'});

        _obj.attr({'fill': 'rgba(255, 0, 0, 0)', 'stroke': 'rgba(255, 102, 102, 1)'});
        _obj.attr({'cursor': 'default'});

        if (unisexsbs_config[id]['active'] === true) {
            if (isTouchEnabled()) {
                var touchmoved;
                _obj.on('touchend', function (e) {
                    if (touchmoved !== true) {
                        _obj.on('touchstart', function (e) {
                            let touch = e.originalEvent.touches[0];
                            let x = touch.pageX - 10, y = touch.pageY + (-15);

                            let $unisexsbsatip = $('#tip-unisexsbs');
                            let unisexsbsanatomytipw = $unisexsbsatip.outerWidth(),
                                unisexsbsanatomytiph = $unisexsbsatip.outerHeight();

                            x = (x + unisexsbsanatomytipw > $(document).scrollLeft() + $(window).width()) ? x - unisexsbsanatomytipw - (20 * 2) : x
                            y = (y + unisexsbsanatomytiph > $(document).scrollTop() + $(window).height()) ? $(document).scrollTop() + $(window).height() - unisexsbsanatomytiph - 10 : y

                            if (unisexsbs_config[id]['target'] !== 'none') {
                                _obj.css({'fill': 'rgba(255, 0, 0, 0.7)'});
                            }
                            $unisexsbsatip.show().html(unisexsbs_config[id]['hover']);
                            $unisexsbsatip.css({left: x, top: y})
                        })
                        _obj.on('touchend', function () {
                            _obj.css({'fill': 'rgba(255, 0, 0, 0)'});
                            if (unisexsbs_config[id]['target'] === '_blank') {
                                window.open(unisexsbs_config[id]['url']);
                            } else if (unisexsbs_config[id]['target'] === '_self') {
                                window.parent.location.href = unisexsbs_config[id]['url'];
                            }
                            $('#tip-unisexsbs').hide();
                        })
                    }
                }).on('touchmove', function (e) {
                    touchmoved = true;
                }).on('touchstart', function () {
                    touchmoved = false;
                });
            }
            _obj.attr({'cursor': 'pointer'});

            _obj.on('mouseenter', function () {
                $('#tip-unisexsbs').show().html(unisexsbs_config[id]['hover']);
                _obj.css({'fill': 'rgba(255, 0, 0, 0.3)'})
            }).on('mouseleave', function () {
                $('#tip-unisexsbs').hide();
                if(selectedId != id){
                    _obj.css({'fill': 'rgba(255, 0, 0, 0)'});
                }
            })
            if (unisexsbs_config[id]['target'] !== 'none') {
                _obj.on('mousedown', function () {
                    _obj.css({'fill': 'rgba(255, 0, 0, 0.7)'});
                })
            }
            _obj.on('mouseup', function () {
                _obj.css({'fill': 'rgba(255, 0, 0, 0.3)'});
                if (unisexsbs_config[id]['target'] === '_blank') {
                    window.open(unisexsbs_config[id]['url']);
                } else if (unisexsbs_config[id]['target'] === '_self') {
                    //window.parent.location.href = unisexsbs_config[id]['url'];

                    selectedPart = unisexsbs_config[id]['hover'];
                  
                    var lastSelectedObject = $('#' + selectedId);
                    selectedId = id;

                     // Asignar la variable al objeto global (window)
                    window.idParteCuerpoSeleccionada = selectedId;
                    
                    lastSelectedObject.css({'fill': 'rgba(255, 0, 0, 0.0)'});
                    // Color aplicado cuando se da clic en una parte del cuerpo
                    _obj.css({'fill': 'rgba(255, 0, 0, 0.4)'});
                }
            })
            _obj.on('mousemove', function (e) {
                let x = e.pageX + 10, y = e.pageY + 15;

                let $aunisexsbs = $('#tip-unisexsbs');
                let unisexsbsanatomytipw = $aunisexsbs.outerWidth(), unisexsbsanatomytiph = $aunisexsbs.outerHeight();

                x = (x + unisexsbsanatomytipw > $(document).scrollLeft() +
                    $(window).width()) ? x - unisexsbsanatomytipw - (20 * 2) : x
                y = (y + unisexsbsanatomytiph > $(document).scrollTop() + $(window).height()) ?
                    $(document).scrollTop() + $(window).height() - unisexsbsanatomytiph - 10 : y

                $aunisexsbs.css({left: x, top: y})
            })
        } else {
            _obj.hide();
        }
    }
})(jQuery);