var query = 'PREFIX geo: <http://www.opengis.net/ont/geosparql#>\
PREFIX foaf: <http://xmlns.com/foaf/0.1/>\
PREFIX ref: <http://course.geoinfo2016.org/G1/vocabulary/ref#>\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\
SELECT DISTINCT\
?name ?total ?yes ?no ?wkt \
WHERE {\
GRAPH <http://course.geoinfo2016.org/G1>{\
?district rdf:type ref:District;\
foaf:name ?name;\
ref:hasTotalVoters ?total;\
ref:hasYesVotes ?yes;\
ref:hasNoVotes ?no;\
geo:hasGeometry ?geo.\
?geo geo:hasSerialization ?wkt.\
}}';

var url = 'http://giv-lodumdata.uni-muenster.de:8282/parliament/sparql'

$(document).ready(function() {
    $.ajax({
        url: url,
        data: {
            'query': query,
            'format': 'json'
        },
        dataType: 'json',
        success: function(data) {
            L.geoJson(sparql2GeoJSON(data), {style : getStyle, onEachFeature: onEachFeature}).addTo(map);
        }
    })
});

function sparql2GeoJSON(input) {
    var output = [];
    for (i in input.results.bindings) {
        var entry = input.results.bindings[i];
        var new_entry = {
            type: "Feature",
            geometry: {},
            properties: {}
        };
        new_entry.geometry = $.geo.WKT.parse(entry.wkt.value);
        new_entry.properties.name = entry.name.value;
        new_entry.properties.totalVoters = entry.total.value;
        new_entry.properties.yes = entry.yes.value;
        new_entry.properties.no = entry.no.value;
        output.push(new_entry);
    }
    return output;
}



function getStyle(feature) {
   return {
       fillColor: feature.properties.no > feature.properties.yes ? 'red' : 'green',
       weight: 2,
       opacity: 1,
       color: 'white',
       fillOpacity: getOpacity(feature.properties.totalVoters)
   };
}

function getOpacity(voters) {
    return voters<6.000 ? 0.3:
           voters>6.000 && voters<7.000 ? 0.5:
           voters>7.000 && voters<8.000 ? 0.7:
           voters>8.000 && voters<9.000 ? 0.85:
                                        1.0;
	}

function zoomToFeature(feature){
				map.fitBounds(feature.target.getBounds());
			}



function onEachFeature(feature, layer){
    if (feature.properties) {
        var popupContent = [];
        popupContent.push("<b>District: </b>" + feature.properties.name)
        popupContent.push("<b><br/>Number of voters: </b>" + feature.properties.totalVoters)
        popupContent.push("<b><br/>Yes votes: </b>" + feature.properties.yes)
        popupContent.push("<b><br/>No votes: </b>" + feature.properties.no)
        layer.bindPopup("<p>" + popupContent.join("") + "</p>");
    }

    layer.on(
        {
            click : zoomToFeature
        }
    );
}

@@ -1069,25 +1069,23 @@
     var 
-new_entry
+feature
  = %7B%0A   
@@ -1182,25 +1182,23 @@
         
-new_entry
+feature
 .geometr
@@ -1239,33 +1239,31 @@
 e);%0A        
-new_entry
+feature
 .properties.
@@ -1291,33 +1291,31 @@
 ue;%0A        
-new_entry
+feature
 .properties.
@@ -1324,24 +1324,33 @@
 talVoters = 
+parseInt(
 entry.total.
@@ -1350,24 +1350,25 @@
 .total.value
+)
 ;%0A        ne
@@ -1361,33 +1361,31 @@
 e);%0A        
-new_entry
+feature
 .properties.
@@ -1390,16 +1390,25 @@
 s.yes = 
+parseInt(
 entry.ye
@@ -1410,24 +1410,25 @@
 ry.yes.value
+)
 ;%0A        ne
@@ -1425,25 +1425,23 @@
         
-new_entry
+feature
 .propert
@@ -1449,16 +1449,25 @@
 es.no = 
+parseInt(
 entry.no
@@ -1468,24 +1468,25 @@
 try.no.value
+)
 ;%0A        ou
@@ -1499,17 +1499,15 @@
 ush(
-new_entry
+feature
 );%0A 
@@ -1598,72 +1598,29 @@
 or: 
-feature.properties.no %3E feature.properties.yes ? 'red' : 'green'
+getFillColor(feature)
 ,%0A  
@@ -1719,38 +1719,126 @@
 ture
-.properties.totalVoters)%0A   %7D;
+)%0A   %7D;%0A%7D%0A%0Afunction getFillColor(feature)%7B%0A  return feature.properties.no %3E feature.properties.yes ? 'red' : 'green';%0A
 %0A%7D%0A%0A
@@ -1857,22 +1857,23 @@
 Opacity(
-voters
+feature
 ) %7B%0A    
@@ -1883,216 +1883,91 @@
 urn 
-voters%3C6.000 ? 0.3:%0A           voters%3E6.000 && voters%3C7.000 ? 0.5:%0A           voters%3E7.000 && voters%3C8.000 ? 0.7:%0A           voters%3E8.000 && voters%3C9.000 ? 0.85:%0A                                        1.0
+(feature.properties.no + feature.properties.yes)*3/feature.properties.totalVoters
 ;%0A
-%09
 %7D%0A%0Af
@@ -2002,12 +2002,10 @@
 e)%7B%0A
-%09%09%09%09
+  
 map.
@@ -2047,11 +2047,8 @@
 ));%0A
-%09%09%09
 %7D%0A%0A%0A

fix opacity calculations