<!DOCTYPE html>
<meta charset="utf-8">
<style>

.link {
  stroke: #3182bd;
  stroke-width: 1.5px;
}

.node {
  cursor: move;
  fill: #3182bd;
  stroke: #3182bd;
  stroke-width: 1.5px;
}

.node.fixed {
  fill: #3182bd;
}

</style>
<body>

<svg width="1400" height="600" class="worldmap">
<image xlink:href="BlankMap-World_gray.svg.png" height="600" width="1400" x="0" y="0"/>
</svg>

<script src="../d3.v3.min.js"></script>
<script>

var width = 1400,
    height = 600;

var force = d3.layout.force()
    .size([width, height])
    .gravity(0)
    .charge(-20)
    .friction(0.5) 	
    .on("tick", tick);

var drag = force.drag()
    .on("dragstart", dragstart);

var svg = d3.select(".worldmap");

var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

d3.json("graph.json", function(error, graph) {
  force
      .nodes(graph.nodes)
      .links(graph.edges)
      .start();

  link = link.data(graph.edges)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke", "#3182bd")
     .style("stroke-width", function(d){return d.weight/15});;

  node = node.data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d){ return (d.type=="fixedPoint")?1:1})
      .style("fill", "#3182bd");

  var mobileNodes = svg.selectAll(".node")
				.filter(function(d){return d.type=="mobilePoint"})
				.style("fill","#3182bd")
                                .on("dblclick", dblclick)
				.call(force.drag);
      
});

function tick() {
  link.attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });

  node.attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; });
}

function dblclick(d) {
  d3.select(this).classed("fixed", d.fixed = false);
}

function dragstart(d) {
  d3.select(this).classed("fixed", d.fixed = true);
}



</script>