/*
 * @file
 * Javascript code for realtime callbacks
 * TODO: Connor, could you explain this code sometime?
 */
var tempForT, tempForS, tempForM;

<% @topics.each do |topic| %>
  var topic = <%= topic.selfonmap_as_json(@map.id).html_safe %>;
  tempForT = Mconsole.graph.getNode(topic.id);
  if (tempForT === undefined) {
    Mconsole.graph.addNode(topic);
    var tempForT = Mconsole.graph.getNode(topic.id);
    tempForT.setData('dim', 1, 'start');
    tempForT.setData('dim', 25, 'end');
    var newPos = new $jit.Complex(); 
    newPos.x = tempForT.data.$xloc; 
    newPos.y = tempForT.data.$yloc; 
    tempForT.setPos(newPos, 'start');
    tempForT.setPos(newPos, 'current');
    tempForT.setPos(newPos, 'end');
    Mconsole.fx.plotNode(tempForT, Mconsole.canvas);
    Mconsole.labels.plotLabel(Mconsole.canvas, tempForT, Mconsole.config);
  }
  else {
    var label = Mconsole.labels.getLabel(topic.id);
    tempForT.setData('dim', 25, 'start');
    tempForT.setData('dim', 25, 'current');
    tempForT.setData('dim', 25, 'end');    
    if (tempForT.name != topic.name) {
      tempForT.name = topic.name;
      $(label).find('.best_in_place_name').html(topic.name);
      $(label).find('.label').html(topic.name);
    }
    if (tempForT.data.$metacode != topic.data.$metacode) {
      $(label).find('.best_in_place_metacode').html(topic.data.$metacode);
      $(label).find('img.icon').attr('alt', topic.data.$metacode);
      $(label).find('img.icon').attr('src', imgArray[topic.data.$metacode].src);
    }
    if (tempForT.data.$desc != topic.data.$desc) {
      $(label).find('.best_in_place_desc').html(topic.data.$desc);
    }
    if (tempForT.data.$link != topic.data.$link) {
      $(label).find('.best_in_place_link').html(topic.data.$link);
      $(label).find('.link').attr('href',topic.data.$link);
    }
    tempForT.data = topic.data;
  }
<% end %>

<% @synapses.each do |synapse| %>
    var Node1 = Mconsole.graph.getNode(<%= synapse.topic1.id %>);
    var Node2 = Mconsole.graph.getNode(<%= synapse.topic2.id %>);
    Mconsole.graph.addAdjacence(Node1, Node2, {});
    tempForS = Mconsole.graph.getAdjacence(Node1.id, Node2.id);
    tempForS.setDataset('start', {  
      lineWidth: 0.4 
      });
    tempForS.setDataset('end', {
      lineWidth: 2
    });
    var d = new Array(<%= synapse.node1_id.to_s() %>, <%= synapse.node2_id.to_s() %>);
    tempForS.setDataset('current', {  
      desc: '<%= synapse.desc %>',  
      showDesc: false,
      category: '<%= synapse.category %>',
      id: '<%= synapse.id %>',
      userid: '<%= synapse.user.id %>',
      username: '<%= synapse.user.name %>'					
      });
    tempForS.data.$direction = d;
    Mconsole.fx.plotLine(tempForS, Mconsole.canvas); 
<% end %>

<% @mappings.each do |mapping| %>
    tempForM = Mconsole.graph.getNode(<%= mapping.topic_id %>);
    tempForM.data.$xloc = <%= mapping.xloc %>;
    tempForM.data.$yloc = <%= mapping.yloc %>;
    var newPos = new $jit.Complex(); 
    newPos.x = tempForM.data.$xloc; 
    newPos.y = tempForM.data.$yloc; 
    tempForM.setPos(newPos, 'start');
    tempForM.setPos(newPos, 'current');
    tempForM.setPos(newPos, 'end');
<% end %>
  
<% if @topics.length > 0 || @synapses.length > 0 || @mappings.length > 0 %>
  $('#map_time').val(Math.round((new Date()).getTime() / 1000));
  Mconsole.fx.animate({  
    modes: ['linear','node-property:dim','edge-property:lineWidth'], 
    transition: $jit.Trans.Quad.easeInOut,    
    duration: 500    
  });
<% end %>
