<commit_before>$(function () {
    var AppRouter = Backbone.Router.extend({
        routes: {
            "": "navigateEmpty",
            "/?marker=:id&start_date=:start&end_date=:end&show_fatal=:showFatal&show_severe=:showSevere&show_light=:showLight&show_inaccurate=:showInaccurate&zoom=:zoom&lat=:lat&lon=:lon": "navigate"
        },
        navigate: function (id, start, end, showFatal, showSevere, showLight, showInaccurate, zoom, lat, lon) {
            app.model.set("currentMarker", parseInt(id));
            app.model.set("dateRange", [new Date(start), new Date(end)]);
            app.model.set("showFatal", showFatal);
            app.model.set("showSevere", showSevere);
            app.model.set("showLight", showLight);
            app.model.set("showInaccurateMarkers", showInaccurate);
            app.map.setZoom(parseInt(zoom));
            app.map.setCenter(new google.maps.LatLng(lat, lon));
        },
        navigateEmpty: function () {
            app.model.set("currentMarker", null);
        }
    });

    var Discussion = Backbone.Model.extend({});

    window.MarkerCollection = Backbone.Collection.extend({
        url: "/markers",

        parse: function (response, options) {
            return response.markers;
        }
    });

    window.ClusterCollection = Backbone.Collection.extend({
        url: "/clusters",

        parse: function (response, options) {
            return response.clusters;
        }
    });

    window.AppView = Backbone.View.extend({
        el : $("#app"),
        events : {
        },
        initialize : function() {
            this.markers = new MarkerCollection();
            this.clusters = new ClusterCollection();
            this.model = new Backbone.Model();
            this.markerIconType = true;
            this.markerList = [];
            this.clusterList = [];
            this.firstLoadDelay = true;
            this.show_markers = '1';
            this.show_discussions = '1';
            this.accurate = '1';
            this.approx = '1';
            this.show_fatal = '1';
            this.show_severe = '1';
            this.show_light = '1';
            this.show_urban = 3;
            this.show_intersection = 3;
            this.show_lane = 3;
            this.show_day = 7;
            this.show_holiday = 0;
            this.show_time = 24;
            this.start_time = 25;
            this.end_time = 25;
            this.weather = 0;
            this.road = 0;
            this.separation = 0;
            this.surface = 0;
            this.acctype = 0;
            this.controlmeasure = 0;
            this.district = 0;

            this.dateRanges = [new Date($("#sdate").val()), new Date($("#edate").val())];

            setTimeout(function(){
                this.firstLoadDelay = false;
            }.bind(this), 2200);

            this.markers
                .bind("reset", this.loadMarkers, this)
                .bind("destroy", this.loadMarkers, this)
                .bind("add", this.loadMarker, this)
                .bind("change:currentModel", this.chooseMarker, this);

            this.clusters
                .bind("reset", this.loadClusters, this)
                .bind("add", this.loadCluster, this);

            this.initLayers();

            this.model
                .bind("change:user", this.updateUser, this)
                .bind("change:showFatal",
                _.bind(this.reloadMarkersIfNeeded, this, "showFatal"))
                .bind("change:showSevere",
                _.bind(this.reloadMarkersIfNeeded, this, "showSevere"))
                .bind("change:showLight",
                _.bind(this.reloadMarkersIfNeeded, this, "showLight"))
                .bind("change:showInaccurateMarkers",
                _.bind(this.reloadMarkersIfNeeded, this, "showInaccurateMarkers"))
                .bind("change:dateRange", this.reloadMarkers, this);
        },
        reloadMarkersIfNeeded: function(attr) {
            if (this.clusterMode() || this.model.get(attr)) {
                this.reloadMarkers();
            } else {
                this.updateUrl();
                this.loadMarkers();
            }
        },
        updateUrl: function (url) {
            if (typeof url == 'undefined') {
                if (app.infoWindow || app.discussionShown) return;
                url = "/?" + this.getCurrentUrlParams();
            }
            Backbone.history.navigate(url, true);
        },
        clusterMode: function () {
            return this.map.zoom < MINIMAL_ZOOM;
        },
        zoomChanged: function () {
            this.resetOnMouseUp = true;
            var reset = this.previousZoom < MINIMAL_ZOOM;
            this.fetchData(reset);
            this.previousZoom = this.map.zoom;
        },
        reloadMarkers: function () {
            if (!this.firstLoadDelay){
                this.oms.unspiderfy();
                this.clearMarkersFromMap();
                this.fetchMarkers();
            }
        },
        fetchMarkers: function () {
            if (!this.isReady) return;

            var params = this.buildMarkersParams();
            if (!params) return;

            var reset = this.clusterMode() || this.previousZoom < MINIMAL_ZOOM;
            this.previousZoom = this.map.zoom;
            if (reset) {
                this.resetMarkers();
            }

            if (this.clusterMode()) {
                this.closeInfoWindow();
                this.clusters.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            } else {
                this.clearClustersFromMap();
                $("#view-filter").prop('disabled', false);
                if (!this.markerList.length) {
                    this.loadMarkers();
                }

                this.clearClustersFromMap();

                this.markers.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            }
        },
        fetchData: function (reset) {
            if (!this.isReady) return;
            this.updateUrl();
            var params = this.buildMarkersParams();

            reset = this.clusterMode() || (typeof reset !== 'undefined' && reset);
            reset &= this.resetOnMouseUp;
            google.maps.event.clearListeners(this.map, "mousemove");
            this.resetOnMouseUp = false;
            if (reset) {
                this.resetMarkers();
            }

            if (this.clusterMode()) {

                this.clusters.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });

            } else {
                if (!this.markerList.length) {
                    this.loadMarkers();
                }

                this.clearClustersFromMap();

                this.markers.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            }
        },
        reloadSidebar: function () {
            if (this.clusterMode()) {
                this.sidebar.showClusterMessage();
            } else { // close enough
                this.setMultipleMarkersIcon();
                this.sidebar.reloadMarkerList(this.markerList);
            }
            if (jsPanelInst!=null){
                startJSPanelWithChart(jsPanelInst, $("#statPanel").width(), $("#statPanel").height(),
					$("#statPanel").width() - 30, $("#statPanel").height() - 80);
            }
            this.chooseMarker();
        },
        buildMarkersParams: function () {
            var bounds = this.map.getBounds();
            if (!bounds) return null;
            var zoom = this.map.zoom;
            var dateRange = this.model.get("dateRange");

            var params = {};
            params["ne_lat"] = bounds.getNorthEast().lat();
            params["ne_lng"] = bounds.getNorthEast().lng();
            params["sw_lat"] = bounds.getSouthWest().lat();
            params["sw_lng"] = bounds.getSouthWest().lng();
            params["zoom"] = zoom;
            params["thin_markers"] = (zoom < MINIMAL_ZOOM || !bounds);
            // Pass start and end dates as unix time (in seconds)
            params["start_date"] = this.dateRanges[0].getTime() / 1000;
            params["end_date"] = this.dateRanges[1].getTime() / 1000;
            params["show_fatal"] = this.show_fatal;
            params["show_severe"] = this.show_severe;
            params["show_light"] = this.show_light;
            params["approx"] = this.approx;
            params["accurate"] = this.accurate;
            params["show_markers"] = this.show_markers;
            params["show_discussions"] = this.show_discussions;
            params["show_urban"] = this.show_urban;
            params["show_intersection"] = this.show_intersection;
            params["show_lane"] = this.show_lane;
            params["show_day"] = this.show_day;
            params["show_holiday"] = this.show_holiday;
            params["show_time"] = this.show_time;
            params["start_time"] = this.start_time;
            params["end_time"] = this.end_time;
            params["weather"] = this.weather;
            params["road"] = this.road;
            params["separation"] = this.separation;
            params["surface"] = this.surface;
            params["acctype"] = this.acctype;
            params["controlmeasure"] = this.controlmeasure;
            params["district"] = this.district;
            return params;
        },
        setMultipleMarkersIcon: function () {
            var groupID = 1;
            var groupsData = [];

            _.each(this.oms.markersNearAnyOtherMarker(), function (marker) {
                marker.view.model.unset("groupID");
            });

            _.each(this.oms.markersNearAnyOtherMarker(), function (marker) {
                marker.title = marker.view.getTitle('multiple');
                var groupHead = marker.view.model;
                if (!groupHead.get("groupID")) {
                    groupHead.set("groupID", groupID);
                    var groupHeadSeverity = groupHead.get('severity');
                    var groupsHeadOpacity = groupHead.get("locationAccuracy") == 1 ? 'opaque' : 1;
                    groupsData.push({severity: groupHeadSeverity, opacity: groupsHeadOpacity});

                    _.each(this.oms.markersNearMarker(marker), function (markerNear) {
                        var markerNearModel = markerNear.view.model;
                        markerNearModel.set("groupID", groupID);
                        if ((groupHeadSeverity != markerNearModel.get('severity'))) {
                            groupsData[groupsData.length - 1].severity = SEVERITY_VARIOUS;
                        }
                        if (groupsData[groupsData.length - 1].opacity != 'opaque') {
                            if (markerNearModel.get("locationAccuracy") == 1) {
                                groupsData[groupsData.length - 1].opacity = 'opaque';
                            } else {
                                groupsData[groupsData.length - 1].opacity++;
                            }
                        }
                    });
                    groupID++;

                }

            },this);
            this.groupsData = groupsData;
              // agam
            if(tourLocation == 5) {
                var myLatlng = new google.maps.LatLng(32.09170,34.86435);
                var location1 = new google.maps.Marker({
                    position: myLatlng,
                    map: this.map,
                    icon: app.retinaIconsResize(MULTIPLE_ICONS[SEVERITY_VARIOUS])
                });
                tourLocation = 6 ;
                console.log("inside the group id "+tourLocation+"new2");
                contentString = '<p>בנקודה זו התרחשו מספר תאונות, לחיצה על האייקון תציג אותן בנפרד ותאפשר בחירה</br> בתאונה בודדת.</p>';
                titleString = 'אייקון של מספר תאונות באותו מקום';
                defInfoWindows();
                infowindow = new google.maps.InfoWindow({
                    content: htmlTourString,
                    maxWidth: 350
                });
                infowindow.open(this.map, location1);
                tourStyle(infowindow);
            }
        },
        downloadCsv: function () {
            if (this.markers.length > 0) {
                params = this.buildMarkersParams();<commit_msg>Fixed opacity issue for aggregated icons<commit_after>$(function () {
    var AppRouter = Backbone.Router.extend({
        routes: {
            "": "navigateEmpty",
            "/?marker=:id&start_date=:start&end_date=:end&show_fatal=:showFatal&show_severe=:showSevere&show_light=:showLight&show_inaccurate=:showInaccurate&zoom=:zoom&lat=:lat&lon=:lon": "navigate"
        },
        navigate: function (id, start, end, showFatal, showSevere, showLight, showInaccurate, zoom, lat, lon) {
            app.model.set("currentMarker", parseInt(id));
            app.model.set("dateRange", [new Date(start), new Date(end)]);
            app.model.set("showFatal", showFatal);
            app.model.set("showSevere", showSevere);
            app.model.set("showLight", showLight);
            app.model.set("showInaccurateMarkers", showInaccurate);
            app.map.setZoom(parseInt(zoom));
            app.map.setCenter(new google.maps.LatLng(lat, lon));
        },
        navigateEmpty: function () {
            app.model.set("currentMarker", null);
        }
    });

    var Discussion = Backbone.Model.extend({});

    window.MarkerCollection = Backbone.Collection.extend({
        url: "/markers",

        parse: function (response, options) {
            return response.markers;
        }
    });

    window.ClusterCollection = Backbone.Collection.extend({
        url: "/clusters",

        parse: function (response, options) {
            return response.clusters;
        }
    });

    window.AppView = Backbone.View.extend({
        el : $("#app"),
        events : {
        },
        initialize : function() {
            this.markers = new MarkerCollection();
            this.clusters = new ClusterCollection();
            this.model = new Backbone.Model();
            this.markerIconType = true;
            this.markerList = [];
            this.clusterList = [];
            this.firstLoadDelay = true;
            this.show_markers = '1';
            this.show_discussions = '1';
            this.accurate = '1';
            this.approx = '1';
            this.show_fatal = '1';
            this.show_severe = '1';
            this.show_light = '1';
            this.show_urban = 3;
            this.show_intersection = 3;
            this.show_lane = 3;
            this.show_day = 7;
            this.show_holiday = 0;
            this.show_time = 24;
            this.start_time = 25;
            this.end_time = 25;
            this.weather = 0;
            this.road = 0;
            this.separation = 0;
            this.surface = 0;
            this.acctype = 0;
            this.controlmeasure = 0;
            this.district = 0;

            this.dateRanges = [new Date($("#sdate").val()), new Date($("#edate").val())];

            setTimeout(function(){
                this.firstLoadDelay = false;
            }.bind(this), 2200);

            this.markers
                .bind("reset", this.loadMarkers, this)
                .bind("destroy", this.loadMarkers, this)
                .bind("add", this.loadMarker, this)
                .bind("change:currentModel", this.chooseMarker, this);

            this.clusters
                .bind("reset", this.loadClusters, this)
                .bind("add", this.loadCluster, this);

            this.initLayers();

            this.model
                .bind("change:user", this.updateUser, this)
                .bind("change:showFatal",
                _.bind(this.reloadMarkersIfNeeded, this, "showFatal"))
                .bind("change:showSevere",
                _.bind(this.reloadMarkersIfNeeded, this, "showSevere"))
                .bind("change:showLight",
                _.bind(this.reloadMarkersIfNeeded, this, "showLight"))
                .bind("change:showInaccurateMarkers",
                _.bind(this.reloadMarkersIfNeeded, this, "showInaccurateMarkers"))
                .bind("change:dateRange", this.reloadMarkers, this);
        },
        reloadMarkersIfNeeded: function(attr) {
            if (this.clusterMode() || this.model.get(attr)) {
                this.reloadMarkers();
            } else {
                this.updateUrl();
                this.loadMarkers();
            }
        },
        updateUrl: function (url) {
            if (typeof url == 'undefined') {
                if (app.infoWindow || app.discussionShown) return;
                url = "/?" + this.getCurrentUrlParams();
            }
            Backbone.history.navigate(url, true);
        },
        clusterMode: function () {
            return this.map.zoom < MINIMAL_ZOOM;
        },
        zoomChanged: function () {
            this.resetOnMouseUp = true;
            var reset = this.previousZoom < MINIMAL_ZOOM;
            this.fetchData(reset);
            this.previousZoom = this.map.zoom;
        },
        reloadMarkers: function () {
            if (!this.firstLoadDelay){
                this.oms.unspiderfy();
                this.clearMarkersFromMap();
                this.fetchMarkers();
            }
        },
        fetchMarkers: function () {
            if (!this.isReady) return;

            var params = this.buildMarkersParams();
            if (!params) return;

            var reset = this.clusterMode() || this.previousZoom < MINIMAL_ZOOM;
            this.previousZoom = this.map.zoom;
            if (reset) {
                this.resetMarkers();
            }

            if (this.clusterMode()) {
                this.closeInfoWindow();
                this.clusters.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            } else {
                this.clearClustersFromMap();
                $("#view-filter").prop('disabled', false);
                if (!this.markerList.length) {
                    this.loadMarkers();
                }

                this.clearClustersFromMap();

                this.markers.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            }
        },
        fetchData: function (reset) {
            if (!this.isReady) return;
            this.updateUrl();
            var params = this.buildMarkersParams();

            reset = this.clusterMode() || (typeof reset !== 'undefined' && reset);
            reset &= this.resetOnMouseUp;
            google.maps.event.clearListeners(this.map, "mousemove");
            this.resetOnMouseUp = false;
            if (reset) {
                this.resetMarkers();
            }

            if (this.clusterMode()) {

                this.clusters.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });

            } else {
                if (!this.markerList.length) {
                    this.loadMarkers();
                }

                this.clearClustersFromMap();

                this.markers.fetch({
                    data: $.param(params),
                    reset: reset,
                    success: this.reloadSidebar.bind(this)
                });
            }
        },
        reloadSidebar: function () {
            if (this.clusterMode()) {
                this.sidebar.showClusterMessage();
            } else { // close enough
                this.setMultipleMarkersIcon();
                this.sidebar.reloadMarkerList(this.markerList);
            }
            if (jsPanelInst!=null){
                startJSPanelWithChart(jsPanelInst, $("#statPanel").width(), $("#statPanel").height(),
					$("#statPanel").width() - 30, $("#statPanel").height() - 80);
            }
            this.chooseMarker();
        },
        buildMarkersParams: function () {
            var bounds = this.map.getBounds();
            if (!bounds) return null;
            var zoom = this.map.zoom;
            var dateRange = this.model.get("dateRange");

            var params = {};
            params["ne_lat"] = bounds.getNorthEast().lat();
            params["ne_lng"] = bounds.getNorthEast().lng();
            params["sw_lat"] = bounds.getSouthWest().lat();
            params["sw_lng"] = bounds.getSouthWest().lng();
            params["zoom"] = zoom;
            params["thin_markers"] = (zoom < MINIMAL_ZOOM || !bounds);
            // Pass start and end dates as unix time (in seconds)
            params["start_date"] = this.dateRanges[0].getTime() / 1000;
            params["end_date"] = this.dateRanges[1].getTime() / 1000;
            params["show_fatal"] = this.show_fatal;
            params["show_severe"] = this.show_severe;
            params["show_light"] = this.show_light;
            params["approx"] = this.approx;
            params["accurate"] = this.accurate;
            params["show_markers"] = this.show_markers;
            params["show_discussions"] = this.show_discussions;
            params["show_urban"] = this.show_urban;
            params["show_intersection"] = this.show_intersection;
            params["show_lane"] = this.show_lane;
            params["show_day"] = this.show_day;
            params["show_holiday"] = this.show_holiday;
            params["show_time"] = this.show_time;
            params["start_time"] = this.start_time;
            params["end_time"] = this.end_time;
            params["weather"] = this.weather;
            params["road"] = this.road;
            params["separation"] = this.separation;
            params["surface"] = this.surface;
            params["acctype"] = this.acctype;
            params["controlmeasure"] = this.controlmeasure;
            params["district"] = this.district;
            return params;
        },
        setMultipleMarkersIcon: function () {
            var groupID = 1;
            var groupsData = [];

            _.each(this.oms.markersNearAnyOtherMarker(), function (marker) {
                marker.view.model.unset("groupID");
            });

            _.each(this.oms.markersNearAnyOtherMarker(), function (marker) {
                marker.title = marker.view.getTitle('hidden');
                var groupHead = marker.view.model;
                if (!groupHead.get("groupID")) {
                    groupHead.set("groupID", groupID);
                    var groupHeadSeverity = groupHead.get('severity');
                    var groupsHeadOpacity = groupHead.get("locationAccuracy") == 1 ? 'opaque' : 1;
                    groupsData.push({severity: groupHeadSeverity, opacity: groupsHeadOpacity});

                    _.each(this.oms.markersNearMarker(marker), function (markerNear) {
                        var markerNearModel = markerNear.view.model;
                        markerNearModel.set("groupID", groupID);
                        if ((groupHeadSeverity != markerNearModel.get('severity'))) {
                            groupsData[groupsData.length - 1].severity = SEVERITY_VARIOUS;
                        }
                        if (groupsData[groupsData.length - 1].opacity != 'opaque') {
                            if (markerNearModel.get("locationAccuracy") == 1) {
                                groupsData[groupsData.length - 1].opacity = 'opaque';
                            } else {
                                groupsData[groupsData.length - 1].opacity++;
                            }
                        }
                    });
                    groupID++;
                }
            },this);

            // Set icon only for group heads (optional: add number of accident in title)
            var groups = _.groupBy(this.oms.markersNearAnyOtherMarker(), function(marker){ return marker.view.model.get("groupID")});
            var groupHeads = _.map(groups, _.first);
            _.each(groupHeads, function(groupHead){groupHead.title = groupHead.view.getTitle("multiple")});

            this.groupsData = groupsData;
              // agam
            if(tourLocation == 5) {
                var myLatlng = new google.maps.LatLng(32.09170,34.86435);
                var location1 = new google.maps.Marker({
                    position: myLatlng,
                    map: this.map,
                    icon: app.retinaIconsResize(MULTIPLE_ICONS[SEVERITY_VARIOUS])
                });
                tourLocation = 6 ;
                console.log("inside the group id "+tourLocation+"new2");
                contentString = '<p>בנקודה זו התרחשו מספר תאונות, לחיצה על האייקון תציג אותן בנפרד ותאפשר בחירה</br> בתאונה בודדת.</p>';
                titleString = 'אייקון של מספר תאונות באותו מקום';
                defInfoWindows();
                infowindow = new google.maps.InfoWindow({
                    content: htmlTourString,
                    maxWidth: 350
                });
                infowindow.open(this.map, location1);
                tourStyle(infowindow);
            }
        },
        downloadCsv: function () {
            if (this.markers.length > 0) {
                params = this.buildMarkersParams();
{'fraction_non_alphanumeric': 0.12494661230828966, 'fraction_numerical': 0.005746456998641041, 'mean_word_length': 1.5315510123845095, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7612551', 'n_tokens_mistral': 7334, 'n_tokens_neox': 6796, 'n_words': 1170}