( function( $ ) {

    "use strict";

    function isTouchEnabled() {
    return (('ontouchstart' in window)
        || (navigator.MaxTouchPoints > 0)
        || (navigator.msMaxTouchPoints > 0));
    }

    $(document).ready(function () {
        $("path[id^=\"afmap_\"]").each(function (i, e) {
            addEvent($(e).attr('id'));
        });
    });

    function addEvent(id, relationId) {
        var _obj = $('#' + id);
        var _Textobj = $('#' + id + ',' + '#' + af_map_config[id]['title']);
        $('#afmapwrapper').css({'opacity': '1'});

        if(af_map_config['default']['afshowvisns'] === true) {
            $('#afvisns').css({'fill': af_map_config['default']['afvisns']});
            $('#afvisns').css({'opacity': '1'});
        } else {
            $('#afvisns').css({'opacity': '0'});
        }

        _obj.attr({'fill': af_map_config[id]['upclr'], 'stroke': af_map_config['default']['afbrdrclr']});

        _Textobj.attr({'cursor': 'default'});

        if (af_map_config[id]['enbl'] === true) {
            if (isTouchEnabled()) {
                var touchmoved;
                _Textobj.on('touchend', function (e) {
                    if (touchmoved !== true) {
                        _Textobj.on('touchstart', function (e) {
                            let touch = e.originalEvent.touches[0];
                            let x = touch.pageX - 10, y = touch.pageY + (-15);

                            let $afmtip = $('#tipafmap');
                            let afmaptipw = $afmtip.outerWidth(),
                                afmaptiph = $afmtip.outerHeight();

                            x = (x + afmaptipw > $(document).scrollLeft() + $(window).width()) ? x - afmaptipw - (20 * 2) : x
                            y = (y + afmaptiph > $(document).scrollTop() + $(window).height()) ? $(document).scrollTop() + $(window).height() - afmaptiph - 10 : y

                            if (af_map_config[id]['targt'] !== 'none') {
                                _obj.css({'fill': af_map_config[id]['dwnclr']});
                            }
                            $afmtip.show().html(af_map_config[id]['hover']);
                            $afmtip.css({left: x, top: y})
                        })
                        _Textobj.on('touchend', function () {
                            _obj.css({'fill': af_map_config[id]['upclr']});
                            if (af_map_config[id]['targt'] === '_blank') {
                                window.open(af_map_config[id]['url']);
                            } else if (af_map_config[id]['targt'] === '_self') {
                                window.parent.location.href = af_map_config[id]['url'];
                            }
                            $('#tipafmap').hide();
                        })
                    }
                }).on('touchmove', function (e) {
                    touchmoved = true;
                }).on('touchstart', function () {
                    touchmoved = false;
                });
            }
            _Textobj.attr({'cursor': 'pointer'});

            _Textobj.on('mouseenter', function () {
                $('#tipafmap').show().html(af_map_config[id]['hover']);
                _obj.css({'fill': af_map_config[id]['ovrclr']})
            }).on('mouseleave', function () {
                $('#tipafmap').hide();
                _obj.css({'fill': af_map_config[id]['upclr']});
            })
            if (af_map_config[id]['targt'] !== 'none') {
                _Textobj.on('mousedown', function () {
                    _obj.css({'fill': af_map_config[id]['dwnclr']});
                })
            }
            _Textobj.on('mouseup', function () {
                _obj.css({'fill': af_map_config[id]['ovrclr']});
                if (af_map_config[id]['targt'] === '_blank') {
                    window.open(af_map_config[id]['url']);
                } else if (af_map_config[id]['targt'] === '_self') {
                    window.parent.location.href = af_map_config[id]['url'];
                }
            })
            _Textobj.on('mousemove', function (e) {
                let x = e.pageX + 10, y = e.pageY + 15;

                let $maf = $('#tipafmap');
                let afmaptipw = $maf.outerWidth(), afmaptiph = $maf.outerHeight();

                x = (x + afmaptipw > $(document).scrollLeft() +
                    $(window).width()) ? x - afmaptipw - (20 * 2) : x
                y = (y + afmaptiph > $(document).scrollTop() + $(window).height()) ?
                    $(document).scrollTop() + $(window).height() - afmaptiph - 10 : y

                $maf.css({left: x, top: y})
            })
        }
    }
})(jQuery);
