Desktop.prototype = new Box();
Desktop.prototype.constructor=Desktop;
Desktop.prototype.parent = Box.prototype;

function Desktop (tag) {
  this.columns = [];
  this.mouse = new Mouse();
  if (tag)
    this.attachTagObject(tag);
}

Desktop.prototype.attachTagObject = function(tag) {
  this.parent.attachTagObject.apply(this, arguments);

  var p = 10;

  this.setDemensions(this.object.offsetWidth-(p*2), this.object.offsetHeight-(p*2));
  this.top = this.object.offsetTop+p;
  this.left = this.object.offsetLeft+p;
}

Desktop.prototype.attachColumn = function(col, pos) {
  var colTag = col.getTagObject();
  if (pos===0) {
    this.moveColumnBefore(col,this.columns[0]);
  } else if (parseInt(pos)) {
    this.moveColumnAfter(col,this.columns[pos-1]);
  } else {
    this.columns.push(col);
  }
  this.recalculateColumns();
  this.getTagObject().appendChild(colTag);
}

Desktop.prototype.removeColumn = function (col) {
  var pos = this.getColumnPosition(col);

  var columns2 = [];
  for (var i in this.columns) {
    if (pos !== i)
      columns2.push(this.columns[i]);
  }
  this.columns = columns2;
  this.getTagObject().appendChild(col);
  this.recalculateColumns();
}

Desktop.prototype.replaceColumn = function (col1, col2, inHtml) {
  var pos1 = this.getColumnPosition(col1);

  this.columns[pos1] = col2;
  if (inHtml)
    this.getTagObject().replaceChild(col2.getTagObject(), col1.getTagObject());
  else
    this.getTagObject().appendChild(col2.getTagObject());
    this.recalculateColumns();
}

Desktop.prototype.moveColumnAfter = function (col1, col2) {
  var pos1 = this.getColumnPosition(col1);
  var pos2 = this.getColumnPosition(col2);

  var columns2 = [];
  for (var i in this.columns) {
    if (pos1 !== i)
      columns2.push(this.columns[i]);
    if (i == pos2)
      columns2.push(col1);
  }
  this.columns = columns2;
}

Desktop.prototype.moveColumnBefore = function (col1, col2) {
  var pos1 = this.getColumnPosition(col1);
  var pos2 = this.getColumnPosition(col2);

  var columns2 = [];
  for (var i in this.columns) {
    if (i == pos2)
      columns2.push(col1);
    if (pos1 !== i)
      columns2.push(this.columns[i]);
  }
  this.columns = columns2;
}

Desktop.prototype.recalculateColumns = function () {
  var units = 0;
  var padding = 10;
  for (var a in this.columns) {
    units += this.columns[a].getFlex();
  }

  var unit = (this.getDemensions().width-(padding*this.columns.length)) / units;
  var p = 0;
  for (var a in this.columns) {
    this.columns[a].setPosition(this.top,this.left+p+(padding*a));
    this.columns[a].setDemensions(unit*this.columns[a].getFlex(),this.getDemensions().height);
    p+=unit*this.columns[a].getFlex();
  }
}

Desktop.prototype.setActiveColumn = function (col) {
  for (var a in this.columns) {
    if (col == this.columns[a])
      this.columns[a].addClass('active');
    else
      this.columns[a].delClass('active');
  }
  return true;
}

Desktop.prototype.findNearestColumn = function (top,left,hv) {
  var nearest = [null,null,null]; // distance in px, object, pre|post
  for (var a in this.columns) {

    var candidate = 0;
    candidate = Math.max(left, this.columns[a].getPosition().left+(this.columns[a].getDemensions().width/2)) - Math.min(left, this.columns[a].getPosition().left+(this.columns[a].getDemensions().width/2));
    candidate += Math.max(top, this.columns[a].getPosition().top+(this.columns[a].getDemensions().height/2)) - Math.min(top, this.columns[a].getPosition().top+(this.columns[a].getDemensions().height/2));
    if (nearest[0] === null || candidate < nearest[0]) {
      nearest = [candidate,this.columns[a]];
    }
  }

  if (hv==0) {
    nearest[2] = ((nearest[1].getPosition().left+(nearest[1].getDemensions().width/2))-left)<0?1:0;
  } else {
    nearest[2] = ((nearest[1].getPosition().top+(nearest[1].getDemensions().height/2))-top)<0?1:0;
  }
  return nearest;
}

Desktop.prototype.getColumnPosition = function (col) {
  for (var a in this.columns) {
    if(col==this.columns[a])
      return a;
  }
}

Desktop.prototype.getMouse = function () {
  return this.mouse;
}
