/**
 * Created by css-dev on 10/30/2017.
 */
var lastActive = null;

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

$(function(){
    addEvent('map_1');
    addEvent('map_2');
    addEvent('map_3');
    addEvent('map_4');
    addEvent('map_5');
    addEvent('map_6');
    addEvent('map_7');
    addEvent('map_8');
    addEvent('map_9');
    addEvent('map_10');
    addEvent('map_11');
    addEvent('map_12');
    addEvent('map_13');
    addEvent('map_14');
    addEvent('map_15');
    addEvent('map_16');
    addEvent('map_17');
    addEvent('map_18');
    addEvent('map_19');
    addEvent('map_20');
    addEvent('map_21');
    addEvent('map_22');
    addEvent('map_23');
    addEvent('map_24');
    addEvent('map_25');
    addEvent('map_26');
    addEvent('map_27');
    addEvent('map_28');
    addEvent('map_29');
    addEvent('map_30');
    addEvent('map_31');
    addEvent('map_32');
    addEvent('map_33');
    addEvent('map_34');
    addEvent('map_35');
    addEvent('map_36');
    addEvent('map_37');
    addEvent('map_38');
    addEvent('map_39');
    addEvent('map_40');
    addEvent('map_41');
    addEvent('map_42');
    addEvent('map_43');
    addEvent('map_44');
    addEvent('map_45');
    addEvent('map_46');
    addEvent('map_47');
    addEvent('map_48');
    addEvent('map_49');
    addEvent('map_50');
    addEvent('map_51');
});

function addEvent(id,relationId){
    var _obj = $('#'+id);
    var _Textobj = $('#'+id+','+'#'+map_config[id]['namesId']);
    var _h = $('#map').height();

    $('#'+map_config[id]['namesId']).find('tspan').attr({'fill':map_config['default']['namescolor']});

    _obj.attr({'fill':map_config[id]['upcolor'],'stroke':map_config['default']['bordercolor']});
    _Textobj.attr({'cursor':'default'});
    if(map_config[id]['enable'] == true){
        // if (isTouchEnabled()) {
        //     //clicking effect
        //     _Textobj.on('touchstart', function(e){
        //         var touch = e.originalEvent.touches[0];
        //         $('#'+id).css({'fill':map_config[id]['downcolor']});
        //     });
        //     _Textobj.on('touchend', function(){
        //         $('#tip').hide();
        //     })
        // } else {
        _Textobj.attr({'cursor':'pointer'});
        _Textobj.hover(function(){
            //moving in/out effect
            _obj.css({'fill':map_config[id]['overcolor']});
        },function(){
            if(!map_config[id]['isActive']){
                _obj.css({'fill':map_config[id]['upcolor']});
            }
        });
        //clicking effect
        _Textobj.mousedown(function(){
            _obj.css({'fill':map_config[id]['downcolor']});

            if(lastActive != null){
                map_config[lastActive]["isActive"] = false;
                $('#'+ lastActive).css({'fill':map_config[lastActive]['upcolor']});
            }
            map_config[id]["isActive"] = true;
            lastActive = id;
        });
        _Textobj.mouseup(function(){
            $('.locations .featureditem').hide();
            $('.locations .'+ map_config[id]['featuredName']).show();

            if(map_config[id]['featuredName'] === 'linkTo'){
                location.href = map_config[id]['url'];
            }

        });
        // }
    }
}