$(document).ready(function() { 
	$('#current_acticity_spy').spy({ 
	  limit: 10, 
	  fadeLast: 2,
	  'fadeInSpeed': 'slow',
	  ajax: '/acf_cases/spy.js', 
	  push : custom_push, 
	  method: 'json',
	  timestamp : custom_timestamp,  
	  isDupe: isDupe,
	  pushTimeout: 3000
	});
	
	$('#current_acticity_spy').mouseover(pauseSpy).mouseout(playSpy);
	
  // Stop the AJAX calls after 2 minutes
	$.timer(120000, function (timer) {
    pauseSpy()
    timer.stop();
  });
  
	
});

var ids = [];

function isDupe(latest, last) 
{	
  
  if (latest.id == last.id)
    return true;
    
  if (ids.length == 10)
    ids = ids.slice(1,ids.length);
    
  if (jQuery.inArray(latest.id, ids) == -1) {
    ids.push(latest.id);
    return false;
  }
  else
    return true;
}

function custom_push(r)
{
	var json = r;
	var html = '<div class="item"><a href="/acf_cases/' + json.id + '">' + json.name + '</a></div>';
	$('#' + this.id).prepend(html);
}

function custom_timestamp() {
  var d = new Date();
  current_timestamp = d.getFullYear() + '-' +
    pad(d.getMonth()+1) + '-' + // because month starts at zero
    pad(d.getDate()) + ' ' +
    pad(d.getHours()) + ':' +
    pad(d.getMinutes()) + ':' +
    pad(d.getSeconds());
    return current_timestamp
}

function pad(n) {
  n = n.toString();
  return n.length == 1 ? '0' + n : n;
}