<commit_before>"use strict"

var $numThreads = $(".num-threads");
var $numAccounts = $(".num-accounts");
var $lastRequestLabel = $(".last-request");
var $fullScanLabel = $(".full-scan");
var $scanPercentLabel = $(".current-scan-percent");

var $selectExclude = $("#exclude-pokemon");
var excludedPokemon = [];

var map;
var scanLocations = new Map();
var coverCircles = [];
var newLocationMarker;


try {
    excludedPokemon = JSON.parse(localStorage.excludedPokemon);
    console.log(excludedPokemon);
} catch (e) {}


function getFromStorage(keyName, default_value) {
    var res = localStorage.getItem(keyName);
    if(res){
        return res === "true";
    } else {
        return default_value;
    }
}

function pad(num, size) {
    var s = num+"";
    if (s.length < 2) s = "0" + s;
    return s;
}

document.getElementById('pokemon-checkbox').checked = getFromStorage("displayPokemons", "true");
document.getElementById('gyms-checkbox').checked = getFromStorage("displayGyms", "true");
document.getElementById('coverage-checkbox').checked = getFromStorage("displayCoverage", "true");

 
$.getJSON("static/locales/pokemon.en.json").done(function(data) {
    var pokeList = [];

    $.each(data, function(key, value) {
        pokeList.push( { id: key, text: value } );
    });

    $selectExclude.select2({
        placeholder: "Type to exclude Pokemon",
        data: pokeList
    });
    $selectExclude.val(excludedPokemon).trigger("change");
});

// exclude multi-select listener
$selectExclude.on("change", function (e) {
    excludedPokemon = $selectExclude.val().map(Number);
    localStorage.excludedPokemon = JSON.stringify(excludedPokemon);
    clearStaleMarkers();
});

// Stolen from http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function is_logged_in(){
    if ($(".auth-status")[0].classList.contains("label-success")) {
        return true
    } else {
        return false
    }
}


function initMap() {
    var initLat = 40.782850;  // NYC Central Park
    var initLng = -73.965288;

    if (initialScanLocations.length !== 0) {
        initLat = initialScanLocations[0].latitude;
        initLng = initialScanLocations[0].longitude;
    }

    map = new google.maps.Map(document.getElementById('map'), {
        // Change this to geolocation?
        center: {lat: initLat, lng: initLng},
        zoom: 13,
        mapTypeControl: false,
        streetViewControl: false,
        disableAutoPan: true
    });
    
    updateScanLocations(initialScanLocations);
    updateMap();

    if(is_logged_in()) {
        // on click listener for
        google.maps.event.addListener(map, 'click', function(event) {
            if (newLocationMarker) {
                newLocationMarker.setMap(null);
            }
            newLocationMarker = new google.maps.Marker({
                position: event.latLng,
                map: map
            });
            newLocationMarker.infoWindow = new google.maps.InfoWindow({
                content: "<button id=\"new-loc-btn\">Add new Location</button><input style=\"width: 75px;\" id=\"new-loc-radius\" type=\"number\" placeholder=\"Radius (meters)\">",
                disableAutoPan: true
            });
            newLocationMarker.infoWindow.open(map, newLocationMarker);
            google.maps.event.addListener(newLocationMarker.infoWindow,'closeclick',function(){
                newLocationMarker.setMap(null); //removes the marker
            });
        });

        // change-location button listener
        $('#map').on('click', '#new-loc-btn', function () {
            var radius = parseInt($('#new-loc-radius').val(), 10);
            newLocationMarker.setMap(null);
            if (isNaN(radius) || radius < 100) {
                alert("Radius not valid. Please note that the radius' unit is in meters and must be a whole number. It also should be >100m.")
                return;
            }

            $.post("location",
                {
                    'lat': newLocationMarker.getPosition().lat(),
                    'lng': newLocationMarker.getPosition().lng(),
                    'radius': radius
                },
                function(data) {
                    updateMap();
                },
            "json");
        });
    }

    initGeoLocation();
};

function initGeoLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            map.setCenter({lat: position.coords.latitude, lng: position.coords.longitude});
        });
    }
}

function pokemonLabel(name, id, disappear_time, latitude, longitude) {
    var disappear_date = new Date(disappear_time);

    var label = "<div>\
            <b>" +name+ "</b>\
            <span> - </span>\
            <small>\
                <a href='http://www.pokemon.com/us/pokedex/" +id+ "' target='_blank' title='View in Pokedex'>#"+id+"</a>\
            </small>\
        </div>\
        <div>\
            Disappears at " +pad(disappear_date.getHours())+ ":"+pad(disappear_date.getMinutes())+":"+pad(disappear_date.getSeconds())+"\
            <span class='label-countdown' disappears-at='"+disappear_time+"'>(00m00s)</span></div>\
        <div>\
            <a href='https://www.google.com/maps/dir/Current+Location/"+latitude+","+longitude+"'\
                    target='_blank' title='View in Maps'>Get Directions</a>\
        </div>";
    return label;
};

function gymLabel(team_name, team_id, gym_points) {
    var gym_color = [ "0, 0, 0, .4", "74, 138, 202, .6", "240, 68, 58, .6", "254, 217, 40, .6" ];
    var str;
    if (team_name == 0) {
        str = "<div><center>\
            <div>\<commit_msg>Hide Pokemon from pop-up label (#154)<commit_after>"use strict"

var $numThreads = $(".num-threads");
var $numAccounts = $(".num-accounts");
var $lastRequestLabel = $(".last-request");
var $fullScanLabel = $(".full-scan");
var $scanPercentLabel = $(".current-scan-percent");

var $selectExclude = $("#exclude-pokemon");
var excludedPokemon = [];

var map;
var scanLocations = new Map();
var coverCircles = [];
var newLocationMarker;


try {
    excludedPokemon = JSON.parse(localStorage.excludedPokemon);
    console.log(excludedPokemon);
} catch (e) {}


function getFromStorage(keyName, default_value) {
    var res = localStorage.getItem(keyName);
    if(res){
        return res === "true";
    } else {
        return default_value;
    }
}

function pad(num, size) {
    var s = num+"";
    if (s.length < 2) s = "0" + s;
    return s;
}

document.getElementById('pokemon-checkbox').checked = getFromStorage("displayPokemons", "true");
document.getElementById('gyms-checkbox').checked = getFromStorage("displayGyms", "true");
document.getElementById('coverage-checkbox').checked = getFromStorage("displayCoverage", "true");

 
$.getJSON("static/locales/pokemon.en.json").done(function(data) {
    var pokeList = [];

    $.each(data, function(key, value) {
        pokeList.push( { id: key, text: value } );
    });

    $selectExclude.select2({
        placeholder: "Type to exclude Pokemon",
        data: pokeList
    });
    $selectExclude.val(excludedPokemon).trigger("change");
});

// exclude multi-select listener
$selectExclude.on("change", function (e) {
    excludedPokemon = $selectExclude.val().map(Number);
    localStorage.excludedPokemon = JSON.stringify(excludedPokemon);
    clearStaleMarkers();
});

// Stolen from http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function is_logged_in(){
    if ($(".auth-status")[0].classList.contains("label-success")) {
        return true
    } else {
        return false
    }
}


function initMap() {
    var initLat = 40.782850;  // NYC Central Park
    var initLng = -73.965288;

    if (initialScanLocations.length !== 0) {
        initLat = initialScanLocations[0].latitude;
        initLng = initialScanLocations[0].longitude;
    }

    map = new google.maps.Map(document.getElementById('map'), {
        // Change this to geolocation?
        center: {lat: initLat, lng: initLng},
        zoom: 13,
        mapTypeControl: false,
        streetViewControl: false,
        disableAutoPan: true
    });
    
    updateScanLocations(initialScanLocations);
    updateMap();

    if(is_logged_in()) {
        // on click listener for
        google.maps.event.addListener(map, 'click', function(event) {
            if (newLocationMarker) {
                newLocationMarker.setMap(null);
            }
            newLocationMarker = new google.maps.Marker({
                position: event.latLng,
                map: map
            });
            newLocationMarker.infoWindow = new google.maps.InfoWindow({
                content: "<button id=\"new-loc-btn\">Add new Location</button><input style=\"width: 75px;\" id=\"new-loc-radius\" type=\"number\" placeholder=\"Radius (meters)\">",
                disableAutoPan: true
            });
            newLocationMarker.infoWindow.open(map, newLocationMarker);
            google.maps.event.addListener(newLocationMarker.infoWindow,'closeclick',function(){
                newLocationMarker.setMap(null); //removes the marker
            });
        });

        // change-location button listener
        $('#map').on('click', '#new-loc-btn', function () {
            var radius = parseInt($('#new-loc-radius').val(), 10);
            newLocationMarker.setMap(null);
            if (isNaN(radius) || radius < 100) {
                alert("Radius not valid. Please note that the radius' unit is in meters and must be a whole number. It also should be >100m.")
                return;
            }

            $.post("location",
                {
                    'lat': newLocationMarker.getPosition().lat(),
                    'lng': newLocationMarker.getPosition().lng(),
                    'radius': radius
                },
                function(data) {
                    updateMap();
                },
            "json");
        });
    }

    initGeoLocation();
};

function initGeoLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            map.setCenter({lat: position.coords.latitude, lng: position.coords.longitude});
        });
    }
}

function pokemonLabel(name, id, disappear_time, latitude, longitude) {
    var disappear_date = new Date(disappear_time);

    var label = "<div>\
            <b>" +name+ "</b>\
            <span> - </span>\
            <small>\
                <a href='http://www.pokemon.com/us/pokedex/" +id+ "' target='_blank' title='View in Pokedex'>#"+id+"</a>\
            </small>\
        </div>\
        <div>\
            Disappears at " +pad(disappear_date.getHours())+ ":"+pad(disappear_date.getMinutes())+":"+pad(disappear_date.getSeconds())+"\
            <span class='label-countdown' disappears-at='"+disappear_time+"'>(00m00s)</span></div>\
        <div>\
            <a href='https://www.google.com/maps/dir/Current+Location/"+latitude+","+longitude+"'\
                    target='_blank' title='View in Maps'>Get Directions</a>\
            <a href='#' onclick='removePokemon(\"" + id + "\")')>Hide " + name + "s</a>\
        </div>";
    return label;
};

function removePokemon(id) {
    var selected=$selectExclude.val();
    selected.push(id.toString());
    $selectExclude.val(selected);

    $selectExclude.change();
}

function gymLabel(team_name, team_id, gym_points) {
    var gym_color = [ "0, 0, 0, .4", "74, 138, 202, .6", "240, 68, 58, .6", "254, 217, 40, .6" ];
    var str;
    if (team_name == 0) {
        str = "<div><center>\
            <div>\
{'fraction_non_alphanumeric': 0.16046854521625165, 'fraction_numerical': 0.012368938401048492, 'mean_word_length': 2.453748231966054, 'pattern_counts': {'":': 4, '<': 59, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3339215', 'n_tokens_mistral': 3878, 'n_tokens_neox': 3708, 'n_words': 736}