var sparkles = {};

var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() {
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }
        anHttpRequest.open( "GET", aUrl, true );
        anHttpRequest.send( null );
    }
}

// Get sparkles from the server
function getSparkles() {
  sClient = new HttpClient();
  sClient.get('/sparkles', function(response) {
    sparkles = JSON.parse(response);
    buildSparkleGraphEdges();
  });
}

// Given sparkles "s", build a graph. This is called from getSparkles.
var edges = [];

// Build a directed graph of who sparkled whom. Only edges are required.
function buildSparkleGraphEdges() {
  _.forEach(sparkles, function(sparkle) {
    var idx = _.findIndex(edges, function(e) { return e.a == sparkle.sparklee && e.b == sparkle.sparkler });
    if (idx > 0) {
      edges[idx].score++;
    } else {
      edges = edges.concat({"a": sparkle.sparklee, "b": sparkle.sparkler, "score": 1});
    }
  })
}

function graphSparklesOverTime(s) {
  // Graph sparkles <s> over time
}

function getStatsForUser(user) {
  // Get stats for a user 
}

// Display for everyone:
// Sparkles over time (line graph)
// Groups (cluster graph)
// Interesting stats?
// * Likelihood someone sparkles another user after being sparkled
// * Sparkle word cloud (minus sparkle party)
// * Party prevalence

// Display for a user:
// * Sparkles over time (line graph)
// * First sparkle received
// * First sparkle given
// * Top sparkled (given and received)
// * Trait: more sparkles given or received, partier, popular words?
// * Similar users



function init() {
  getSparkles();
}

init();

@@ -607,453 +607,133 @@
 ;%0A  
-  buildSparkleGraphEdges();%0A  %7D);%0A%7D%0A%0A// Given sparkles %22s%22, build a graph. This is called from getSparkles.%0Avar edges = %5B%5D;%0A%0A// Build a directed graph of who sparkled whom. Only edges are required.%0Afunction buildSparkleGraphEdges() %7B%0A  _.forEach(sparkles, function(sparkle) %7B%0A    var idx = _.findIndex(edges, function(e) %7B return e.a == sparkle.sparklee && e.b == sparkle.sparkler %7D);%0A    if (idx %3E 0) %7B%0A      edges%5Bidx%5D.score++;%0A    %7D el
+%7D);%0A%7D%0A%0Avar edges = %5B%5D;%0Afunction getGraphEdges() %7B%0A  sClient = new HttpClient();%0A  sClient.get('/graph', function(respon
 se
+)
  %7B%0A
-  
     
@@ -744,87 +744,29 @@
 s = 
-edges.concat(%7B%22a%22: sparkle.sparklee, %22b%22: sparkle.sparkler, %22score%22: 1%7D);%0A    %7D
+JSON.parse(response);
 %0A  %7D
@@ -766,24 +766,25 @@
 e);%0A  %7D)%0A%7D%0A%0A
+%0A
 function gra
@@ -902,17 +902,16 @@
 r a user
- 
 %0A%7D%0A%0A// D
@@ -1412,30 +1412,32 @@
 it() %7B%0A  get
-Sparkl
+GraphEdg
 es();%0A%7D%0A%0Aini

put the edges in the browsah