/*
 * Moving blocks - Dynamic block game
 *
 * Copyright (c) 2010 Boris Dinkevich (dinkevich.com)
 * Licensed under the GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2010-01-01 10:10:01 -0200 (Fri, 01 Jan 2010) $
 * $Rev: 0001 $
 */

var border = 5;

function Floater(elem, master) 
{
  this.x = parseInt(elem.css("left"));
  this.y = parseInt(elem.css("top"));
  this.w = parseInt(elem.css("width"));
  this.h = parseInt(elem.css("height"));

  /* Since the all other divs are relative to the master, put the master at 0,0 */
  if (typeof(master) != 'undefined')
  {
    this.h -= 2 * border;
    this.w -= 2 * border;
    this.x = 0;
    this.y = 0;
  }
};

var Moving = function() {
  return {
    mouseX: 0,
    mouseY: 0,

    run_away: 0,
    hover: 0,

    masterFloater: null,

    floaters: new Array(),

    init: function() 
    {
      var that = Moving;

      $(document).mousemove(function(e) {
        that.mouseX = e.clientX - 100;
        that.mouseY = e.clientY - 100;
      });

      $("div.floater").hover(
        function() {
          that.run_away = 1;
          that.hover = $(this).attr("id");
        },
        function() {
          that.run_away = 0;
      });

      that.start();
      setInterval(that.update_floaters, 20);
    },

    set_cords: function(item, x, y)
    {
      item.css("left", x+"px").css("top", y+"px");
    },

    updateCords: function()
    {
      var that = Moving;

      for (var i=that.floaters.length-1; i>=0; --i) 
      {
        var x = that.floaters[i].x;
        var y = that.floaters[i].y;
        that.set_cords($('#'+i), x, y);
      }
    },

    start: function() 
    {
      var that = Moving;
      var count = 0;

      $(".floater").map(function() {
        $(this).attr("id", count);
        that.floaters[count] = new Floater($(this));
        count++;
      });

      that.masterFloater = new Floater($("div.master"), 1);

      that.updateCords();
    },

    in_box: function(floater_a, floater_b)
    {
      var Mx = floater_a.x - border;
      var My = floater_a.y - border;
      var Nx = floater_a.x + floater_a.w + border;
      var Ny = floater_a.y + floater_a.h + border;

      var Ox = floater_b.x - border;
      var Oy = floater_b.y - border;
      var Px = floater_b.x + floater_b.w + border;
      var Py = floater_b.y + floater_b.h + border;

      if (Mx<Ox && My<Oy && Px<Nx && Py<Ny)
        return 1;
      return 0;
    },

    collision: function(floater_a, floater_b)
    {
      var Mx = floater_a.x - border;
      var My = floater_a.y - border;
      var Nx = floater_a.x + floater_a.w + border;
      var Ny = floater_a.y + floater_a.h + border;

      var Ox = floater_b.x - border;
      var Oy = floater_b.y - border;
      var Px = floater_b.x + floater_b.w + border;
      var Py = floater_b.y + floater_b.h + border;

      if ((Mx>Px) || (Ox>Nx) || (My>Py) || (Oy>Ny))
        return 0;

      return 1;
    },

    collisions: function(obj)
    {
      var that = Moving;

      for (var i=that.floaters.length-1; i>=0; --i)
      {
        if (i == obj)
          continue;

        if (that.collision(that.floaters[obj], that.floaters[i]))
          return 1;
      }

      return 0;
    },   

    update_floaters: function()
    {
      var that = Moving;

      for (var i=that.floaters.length-1; i>=0; --i)
      {
        // If we hover over an object, it should stay put
        if (that.run_away && that.hover == i)
          continue;

        var floater = that.floaters[i];
        var speed = 100;
        var dir = 1;

        // When someone is being hovered, the rest 'run away'
        if (that.run_away)
          dir = -1;

        var x_dir = (floater.x - that.mouseX) / 50;
        var y_dir = (floater.y - that.mouseY) / 50;

        var new_x = floater.x - x_dir * dir;
        var new_y = floater.y - y_dir * dir;

        var temp_x = floater.x;
        var temp_y = floater.y;

        that.floaters[i].x = new_x;
        if (that.collisions(i) || !that.in_box(that.masterFloater, that.floaters[i]))
          that.floaters[i].x = temp_x;

        that.floaters[i].y = new_y;
        if (that.collisions(i) || !that.in_box(that.masterFloater, that.floaters[i]))
          that.floaters[i].y = temp_y;
      }

      that.updateCords();
    }
  }
}();




