<commit_before>/* From http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ */

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

/*end from*/


function initAutocomplete(map) {
//	function log( message ) {
//		$( "<div>" ).text( message ).prependTo( "#log" );
//		$( "#log" ).scrollTop( 0 );
//	}
	var sql = cartodb.SQL({ user: 'codeforkansascity' });
	$( ".cartodb-searchbox .text" ).autocomplete({
		source: function( request, response ) {
			var s;
			sql.execute("SELECT cartodb_id, address, the_geom FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd WHERE address LIKE '" + request.term + "%' ORDER BY address")
			.done(function(data) {
				response(data.rows.map(function(r) {
            		//console.log(r);
					return {
						label: r.address,
						value: r.address
					}
				}))
			})
		},
		minLength: 2,
		select: function( event, ui ) {
			console.log("Selected: " + ui.item.value);
            sql.execute("SELECT ST_X(ST_Centroid(the_geom)) as X, ST_Y(ST_Centroid(the_geom)) as Y \
                FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd \
                WHERE address LIKE '" + ui.item.value + "%'").done(function(data){
                map.panTo({lon: data.rows[0].x, lat: data.rows[0].y});

                var geomQuery = "WITH query_geom \
                        AS (SELECT the_geom AS geom \
                            FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd \
                            WHERE address LIKE '" + ui.item.value + "%') \
                        SELECT parcels.cartodb_id, parcels.kivapin, parcels.the_geom, parcels.the_geom_webmercator \
                        FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd AS parcels, query_geom \
                        WHERE ST_DWithin(query_geom.geom::geography, parcels.the_geom::geography, 5)"

                geomLayer.getSubLayer(0).setSQL(geomQuery);

            });           
			



            //var bounds = new google.maps.LatLngBounds();
			//for(i=0;i<r.length;i++) {
			//	bounds.extend(r[i].getPosition());
			//}
			//map.fitBounds(bounds);
			// "Nothing selected, input was " + this.value );
		}
	});

};
//console.log("SELECT cartodb_id, address FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd WHERE address LIKE '" + request.term + "%' ORDER BY address");


function createGoogleMap(){
	var map;

	// create google maps map
	var mapOptions = {
		zoom: 15,
		center: new google.maps.LatLng(39.082981, -94.557747),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
    map = new google.maps.Map(document.getElementById('map'),  mapOptions);

    return map;
}

var geomLayer;

function attachMapLayers(map){

    var datalayer = 'https://code4kc.cartodb.com/api/v2/viz/8167c2b8-0cf3-11e5-8080-0e9d821ea90d/viz.json';
    var geomlayer = 'https://codeforamerica.cartodb.com/u/codeforkansascity/api/v2/viz/4e032b12-1dfe-11e5-8ca7-0e49835281d6/viz.json'

    cartodb.createLayer(map, geomlayer).addTo(map, 0).on('done', function(layer){
		var v = cdb.vis.Overlay.create('search', map.viz, {})
		v.show();
		$('#map').append(v.render().el);
		initAutocomplete(map);
        geomLayer = layer;
    })
    .on('error', function(){
    	cartodb.log.log("Error");
    });

    cartodb.createLayer(map, datalayer).addTo(map, 1).on('done', function (layer) {
        var sublayer = layer.getSubLayer(1); //sublayer generated from the data.json file
        sublayer.infowindow.set('template', $('#infowindow_template').html());
        sublayer.setInteraction(true);
        //add more data as needed:
        sublayer.setInteractivity('cartodb_id, address, apn, kivapin, land_ban30, land_ban_6, land_ban_4, land_ban_6, land_ban_7, land_ban10, land_ban36, land_ban56, land_ban60, land_bank_, own_name, landusecod, land_ban32, land_ban_3');
        sublayer.on('featureClick', function (e, latlng, pos, data, layer) {

            var request_jd_wp = createCORSRequest("get", "http://address-api.codeforkc.org/jd_wp/" + data.apn);
            var api_data = null;
            if (request_jd_wp) {
                request_jd_wp.onload = function () {
                    api_data = JSON.parse(request_jd_wp.responseText);

                    if (api_data) {
                        data.blvd_front_footage = api_data.blvd_front_footage;
                        data.assessed_land = api_data.assessed_land;
                        data.assessed_improve = api_data.assessed_improve;
                        data.exempt_improve = api_data.exempt_improve;
                        data.acres = api_data.acres;
                        data.perimeter = api_data.perimeter;
                        data.assessed_value = api_data.assessed_value;
                        data.api_id = api_data.id;
                    } else {

                        data.blvd_front_footage = '';
                        data.assessed_land = '';
                        data.assessed_improve = '';
                        data.exempt_improve = '';
                        data.acres = '';
                        data.perimeter = '';
                        data.assessed_value = '';
                        data.api_id = '';
                    }

                    $('.cartodb-infowindow').off('click', '#addtofolder'); 
                    $('.cartodb-infowindow').on('click', '#addtofolder', function () {
                    	addParcel(data);
                        $('.cd-panel').addClass('is-visible');
                    });


                };
                request_jd_wp.send();
            }

        });
    }).on('error', function () {
        console.log("Error");
    });
}


function initMap(useGMaps){
	$('#mainclass').html("<div id='map'></div>");
		map = createGoogleMap();
    attachMapLayers(map)
}

var ParcelArea;

function buildEnvelope(zone) {

    var template = $('#envelope_template').html();
    //store some variables:
    if (ZoneTable[zone]) {
        var maxfloors = Math.floor((ZoneTable[zone]["maxheight"]/10));
        var BSFMax = calculateBSF(ParcelArea, ZoneTable[zone]["LC"], 1.00, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"], ZoneTable[zone]["PF"]);
        var BuildingComponent = calculateBuildingComponent(BSFMax, ZoneTable[zone]["LC"], 1.00, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"], ZoneTable[zone]["PF"], ZoneTable[zone]["far"]);
        var ParkingComponent = calculateParkingComponent(BuildingComponent, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"]);<commit_msg>Switched to the wendell phillips only dataset (since our trial focus is on this set)<commit_after>/* From http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ */

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

/*end from*/
function initAutocomplete(map) {
//	function log( message ) {
//		$( "<div>" ).text( message ).prependTo( "#log" );
//		$( "#log" ).scrollTop( 0 );
//	}
	var sql = cartodb.SQL({ user: 'codeforkansascity' });
	$( ".cartodb-searchbox .text" ).autocomplete({
		source: function( request, response ) {
			var s;
			sql.execute("SELECT cartodb_id, address, the_geom \
                FROM kcmo_parcels_6_18_2015_wendell_phillips \
                WHERE address LIKE '" + request.term + "%' ORDER BY address")
			.done(function(data) {
				response(data.rows.map(function(r) {
            		//console.log(r);
					return {
						label: r.address,
						value: r.address
					}
				}))
			})
		},
		minLength: 2,
		select: function( event, ui ) {
            /* This code is currently unusable for PAT in it's current form, I am only leaving it here for future reference
			console.log("Selected: " + ui.item.value);
            sql.execute("SELECT ST_X(ST_Centroid(the_geom)) as X, ST_Y(ST_Centroid(the_geom)) as Y \
                FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd \
                WHERE address LIKE '" + ui.item.value + "%'").done(function(data){
                map.panTo({lon: data.rows[0].x, lat: data.rows[0].y});

                var geomQuery = "WITH query_geom \
                        AS (SELECT the_geom AS geom \
                            FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd \
                            WHERE address LIKE '" + ui.item.value + "%') \
                        SELECT parcels.cartodb_id, parcels.kivapin, parcels.the_geom, parcels.the_geom_webmercator \
                        FROM codeforkansascity.kcmo_parcels_6_18_2015_kiva_nbrhd AS parcels, query_geom \
                        WHERE ST_DWithin(query_geom.geom::geography, parcels.the_geom::geography, 5)"

                geomLayer.getSubLayer(0).setSQL(geomQuery);

            });           
			*/
            //var bounds = new google.maps.LatLngBounds();
			//for(i=0;i<r.length;i++) {
			//	bounds.extend(r[i].getPosition());
			//}
			//map.fitBounds(bounds);
			// "Nothing selected, input was " + this.value );
		}
	});

};

function createGoogleMap(){
	var map;

	// create google maps map
	var mapOptions = {
		zoom: 15,
		center: new google.maps.LatLng(39.082981, -94.557747),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
    map = new google.maps.Map(document.getElementById('map'),  mapOptions);

    return map;
}

var geomLayer;

function attachMapLayers(map){

    //var datalayer = 'https://code4kc.cartodb.com/api/v2/viz/8167c2b8-0cf3-11e5-8080-0e9d821ea90d/viz.json';
    //var geomlayer = 'https://codeforamerica.cartodb.com/u/codeforkansascity/api/v2/viz/4e032b12-1dfe-11e5-8ca7-0e49835281d6/viz.json';
    var geomlayer = 'https://codeforamerica.cartodb.com/u/codeforkansascity/api/v2/viz/2e96078a-4b90-11e5-bb2b-0e9d821ea90d/viz.json';


    cartodb.createLayer(map, geomlayer).addTo(map, 0).on('done', function(layer){
		var v = cdb.vis.Overlay.create('search', map.viz, {});
		v.show();
		$('#map').append(v.render().el);
		initAutocomplete(map);
        geomLayer = layer;

        //enable interactivity with the sublayer
        var subLayer = layer.getSubLayer(0); //sublayer generated from this data
        subLayer.infowindow.set('template', $('#infowindow_template').html());
        subLayer.setInteraction(true);
        subLayer.setInteractivity('apn');

        //enable pop up on parcel click
        subLayer.on('featureClick', function(e, latlng, pos, data, layer){
            var request = createCORSRequest(
                "get", 
                "http://address-api.codeforkc.org/jd_wp/" + data.apn
            );
            var api_data = null;

            if (request) {
                request.onload = function () {
                    api_data = JSON.parse(request.responseText);

                    if (api_data) {
                        data.blvd_front_footage = api_data.blvd_front_footage;
                        data.assessed_land = api_data.assessed_land;
                        data.assessed_improve = api_data.assessed_improve;
                        data.exempt_improve = api_data.exempt_improve;
                        data.acres = api_data.acres;
                        data.perimeter = api_data.perimeter;
                        data.assessed_value = api_data.assessed_value;
                        data.api_id = api_data.id;
                    } else {

                        data.blvd_front_footage = '';
                        data.assessed_land = '';
                        data.assessed_improve = '';
                        data.exempt_improve = '';
                        data.acres = '';
                        data.perimeter = '';
                        data.assessed_value = '';
                        data.api_id = '';
                    }

                    $('.cartodb-infowindow').off('click', '#addtofolder'); 
                    $('.cartodb-infowindow').on('click', '#addtofolder', function () {
                        addParcel(data);
                        $('.cd-panel').addClass('is-visible');
                    });


                };
                request.send();
            }
        });
    })
    .on('error', function(){
    	cartodb.log.log("Error");
    });

    /*
    cartodb.createLayer(map, datalayer).addTo(map, 1).on('done', function (layer) {
        var sublayer = layer.getSubLayer(1); //sublayer generated from the data.json file
        sublayer.infowindow.set('template', $('#infowindow_template').html());
        sublayer.setInteraction(true);
        //add more data as needed:
        sublayer.setInteractivity('cartodb_id, address, apn, kivapin, land_ban30, land_ban_6, land_ban_4, land_ban_6, land_ban_7, land_ban10, land_ban36, land_ban56, land_ban60, land_bank_, own_name, landusecod, land_ban32, land_ban_3');
        sublayer.on('featureClick', function (e, latlng, pos, data, layer) {

            var request_jd_wp = createCORSRequest("get", "http://address-api.codeforkc.org/jd_wp/" + data.apn);
            var api_data = null;
            if (request_jd_wp) {
                request_jd_wp.onload = function () {
                    api_data = JSON.parse(request_jd_wp.responseText);

                    if (api_data) {
                        data.blvd_front_footage = api_data.blvd_front_footage;
                        data.assessed_land = api_data.assessed_land;
                        data.assessed_improve = api_data.assessed_improve;
                        data.exempt_improve = api_data.exempt_improve;
                        data.acres = api_data.acres;
                        data.perimeter = api_data.perimeter;
                        data.assessed_value = api_data.assessed_value;
                        data.api_id = api_data.id;
                    } else {

                        data.blvd_front_footage = '';
                        data.assessed_land = '';
                        data.assessed_improve = '';
                        data.exempt_improve = '';
                        data.acres = '';
                        data.perimeter = '';
                        data.assessed_value = '';
                        data.api_id = '';
                    }

                    $('.cartodb-infowindow').off('click', '#addtofolder'); 
                    $('.cartodb-infowindow').on('click', '#addtofolder', function () {
                    	addParcel(data);
                        $('.cd-panel').addClass('is-visible');
                    });


                };
                request_jd_wp.send();
            }

        });
    }).on('error', function () {
        console.log("Error");
    });
    */
}

function initMap(useGMaps){
	$('#mainclass').html("<div id='map'></div>");
		map = createGoogleMap();
    attachMapLayers(map)
}

var ParcelArea;

function buildEnvelope(zone) {

    var template = $('#envelope_template').html();
    //store some variables:
    if (ZoneTable[zone]) {
        var maxfloors = Math.floor((ZoneTable[zone]["maxheight"]/10));
        var BSFMax = calculateBSF(ParcelArea, ZoneTable[zone]["LC"], 1.00, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"], ZoneTable[zone]["PF"]);
        var BuildingComponent = calculateBuildingComponent(BSFMax, ZoneTable[zone]["LC"], 1.00, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"], ZoneTable[zone]["PF"], ZoneTable[zone]["far"]);
        var ParkingComponent = calculateParkingComponent(BuildingComponent, ZoneTable[zone]["PI"], ZoneTable[zone]["SA"]);
{'fraction_non_alphanumeric': 0.1387206661619985, 'fraction_numerical': 0.018988140297754228, 'mean_word_length': 2.062789799072643, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, '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': '5119132', 'n_tokens_mistral': 5462, 'n_tokens_neox': 4949, 'n_words': 813}