// On document load
$(function() {
  var n = 6; // Number of boxes/entries
  var timerInterval = 20; // ms
  
  var $body = $("body");
  var boxes = [];
  for(i = 0; i < n; i++) {
    $box = $("<div class='entry'></div>");
    boxes[i] = new FloatingEntry($box, i, timerInterval / 1000);
    $body.append($box);
  }
  
  var cb = function() {
    for(var i = 0; i < boxes.length; i++)
      boxes[i].timerCallback();
  }
  
  // Set timer interval
  setInterval(cb, timerInterval);  
});


function FloatingEntry($box, id, timerInterval) {
  var me = this;
  this.a = 2; // Acceleration
  this.friction = 0.001; // Friction - to limit speed
  this.sqrNextRadius = 100; // Square distance to PoG before swithing PoG.

  this.PoG = {x: 0, y: 0}; // Point of Gravity
  this.v = {x: 0, y: 0}; // speed
  this.cpos = {x: 0, y: 0}; // centre position
  this.cwidth = 0;
  this.cheight = 0;
  this.dataloaded = false;
  
  this.initialise = function(val) {
    // Load data
    $box.html("");
    $.ajax({
      url: '/blogitem.php?item='+val,
      success: me.data
    });
    
    // Initialize position and calc new PoG
    me.calculateNewPoG();
    me.cpos = {x: me.PoG.x, y: me.PoG.y};
    me.calculateNewPoG();
  };
  
  this.data = function(data) {
    data = data.replace(/<br( \/)?>/gi, "\n");
    $box.html("<pre>" + data + "</pre>");
    me.cwidth = $box.width() / 2;
    me.cheight = $box.height() / 2;
    me.updatePosition();
    setInterval(me.show, id*1000);  
    // $box.css({width: me.calcWidth(data) + 'px'});
    me.dataloaded = true;
  };
  
  this.show = function() {
    $box.fadeIn(2000);  
  };
  
  this.click = function() {
    $box.fadeOut(1000, function() {me.initialise(-1);});
  };
  
  this.calcWidth = function(data) {
    var c = 0;
    var lines = data.split(/<br \/>/);
    for(line in lines) {
      c = Math.max(c, lines[line].length);
    }
    return c * 9;
  };
  
  this.timerCallback = function() {
    if(!me.dataloaded)
        return;

    if(me.isCloseToPoG())
      me.calculateNewPoG();

    me.updateVelocity();
    me.updatePosition();
  };
  
  this.calculateNewPoG = function() {
    me.PoG.x = Math.floor(Math.random() * $(window).width());
    me.PoG.y = Math.floor(Math.random() * $(window).height());
  };
  
  this.updateVelocity = function() {
    var dir = me.normalize({x: me.PoG.x - me.cpos.x, y: me.PoG.y - me.cpos.y});
    var fa = (me.v.x * me.v.x + me.v.y * me.v.y) * me.friction;
    var vn = me.normalize(me.v);
    me.v.x = me.v.x + (dir.x * me.a - vn.x * fa) * timerInterval;
    me.v.y = me.v.y + (dir.y * me.a - vn.y * fa) * timerInterval;
  };
  
  this.updatePosition = function() {
    me.cpos.x = me.cpos.x + me.v.x * timerInterval;
    me.cpos.y = me.cpos.y + me.v.y * timerInterval;
    $box.css({left: me.cpos.x-me.cwidth, top: me.cpos.y-me.cheight});
  };
  
  this.isCloseToPoG = function() {
    var x = me.cpos.x-me.PoG.x;
    var y = me.cpos.y-me.PoG.y;
    var sqrDistToPoG = x*x + y*y;
    return sqrDistToPoG < me.sqrNextRadius;
  };
  
  this.normalize = function(v) {
    var l = Math.sqrt(v.x*v.x + v.y*v.y);
    if(l == 0)
        return v;

    l = 1 / l;
    return {x: v.x*l, y: v.y*l};
  };
  
  this.initialise(id);  
  $box.click(me.click);
}


