(function() {
  let group = []
  d3.xml("/img/mapa_ps.svg").then(function(svg) {
    Array.prototype.forEach.call(svg.getElementsByTagName("path"), function(path) {
      let data = {'1':{region:'Hlavni mesto Praha',party:'TOP 09',percent:'27.27 %',votes:' (173 840 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=1',color:'#9966CC',state:'finished'},'2':{region:'Stredocesky kraj',party:'ODS',percent:'23.87 %',votes:' (150 465 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=2',color:'#3366FF',state:'finished'},'3':{region:'Jihocesky kraj',party:'ODS',percent:'21.36 %',votes:' (71 173 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=3',color:'#3366FF',state:'finished'},'4':{region:'Plzensky kraj',party:'CSSD',percent:'22.01 %',votes:' (61 688 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=4',color:'#FF9933',state:'finished'},'5':{region:'Karlovarsky kraj',party:'CSSD',percent:'23.29 %',votes:' (30 324 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=5',color:'#FF9933',state:'finished'},'6':{region:'Ustecky kraj',party:'CSSD',percent:'24.93 %',votes:' (91 214 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=6',color:'#FF9933',state:'finished'},'7':{region:'Liberecky kraj',party:'ODS',percent:'20.47 %',votes:' (43 789 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=7',color:'#3366FF',state:'finished'},'8':{region:'Kralovehradecky kraj',party:'ODS',percent:'20.12 %',votes:' (58 165 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=8',color:'#3366FF',state:'finished'},'9':{region:'Pardubicky kraj',party:'CSSD',percent:'21.95 %',votes:' (59 040 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=9',color:'#FF9933',state:'finished'},'10':{region:'Vysocina',party:'CSSD',percent:'23.43 %',votes:' (63 486 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=10',color:'#FF9933',state:'finished'},'11':{region:'Jihomoravsky kraj',party:'CSSD',percent:'23.35 %',votes:' (139 476 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=11',color:'#FF9933',state:'finished'},'12':{region:'Olomoucky kraj',party:'CSSD',percent:'24.47 %',votes:' (78 786 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=12',color:'#FF9933',state:'finished'},'13':{region:'Zlinsky kraj',party:'CSSD',percent:'21.93 %',votes:' (67 691 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=13',color:'#FF9933',state:'finished'},'14':{region:'Moravskoslezsky kraj',party:'CSSD',percent:'29.13 %',votes:' (170 081 votes)',processed:'100.00 % processed wards',link:'ps311?xjazyk=EN&xkraj=14',color:'#FF9933',state:'finished'}},
          id = path.id
      if (data[path.id]) {
        group.push({
          region: data[id].region,
          party: data[id].party,
          percent: data[id].percent,
          votes: data[id].votes,
          processed: data[id].processed,
          showTooltip: !!data[id].region,
          link: data[id].link,
          color: data[id].color,
          state: data[id].state,
          id: id,
          d: path.attributes.d.value
        })
      } else {
        group.push({
          showTooltip: false,
          id: id,
          d: path.attributes.d.value
        })
      }
    })
    draw(group)
  })
  function tooltipHtml(d) {
    return (
      "<p class=\"region\"> " + d.region + " </p>" +
      "<p class=\"party\"> " + d.party + " </p>" +
      "<p class=\"votes\"> " + d.percent + d.votes + "</p>" +
      "<p class=\"processed\"> " + d.processed + "</p>" +
      "<p class=\"state\"> " + d.state + " </p>"
    )
  }
  function draw(group) {
    d3.select("#region-svg")
      .selectAll(".state")
      .data(group)
      .enter()
      .append("g")
      .attr("class", "state-g")
      .attr("stroke", "#666666")
      .attr("stroke-width", "1")
      .append("a")
      .attr("class", "state-a")
      .attr("target", "_self")
      .attr("xlink:href", function(d) {return (d.name === "") ? "#" : d.link})
      .append("path")
      .attr("class", "state-path")
      .attr("d", function(d) {return d.d})
      .attr("id", function(d) {return d.id})
      .attr("transform", "scale(0.8)")
      .style("fill", function(d) {
        if (!!d.party) {
          return d.color
        } else {
          return "#686868"
        }
      })
      .on("mouseover", mouseOver).on("mouseout", mouseOut)
    function mouseOver(d) {
      if (d.showTooltip) {
        d3.select("#tooltip").transition().duration(200).style("opacity", 1)
        d3.select("#tooltip").html(tooltipHtml(d))
        .style("left", (d3.event.clientX) + "px")
        .style("top", (d3.event.clientY) + "px")
      }
    }
    function mouseOut(d) {
      if (d.showTooltip) {
        d3.select("#tooltip").transition().duration(500).style("opacity", 0)
      }
    }
  }
})()
