/*/////////////////////////////////////
//				     //
//  This is the engien for the chat  //
//  ChatEngine.addMessage();	     //
//  ChatEngine.drawAll();	     //
//  ChatEngine.deleteMess(objNr);    //
//  ChatEngine.readKey(event);	     //
//				     //
/////////////////////////////////////*/

// This is the ChatEngine object
function Box(setTop, setLeft, setHeight, setWidth, setTitle, setIcon, setContent, setSize)
{
  //Task.call(this, setTitle, setIcon, "box"+Boxs.boxs.length, "");
  
  this.top = setTop;
  this.left = setLeft;
  this.height = 100;
  this.minHeight = 120;
  this.width = 100;
  this.minWidth = 200;
  this.title = setTitle;
  this.icon = setIcon;
  this.content = setContent;
  this.size = setSize;
  this.idNumber = Boxs.boxs.length;
  
  if(setTop == null)
  {
    setTop = 60;
    if(Boxs.boxs.length > 0)
    {
      if(Boxs.boxs[Boxs.boxs.length-1].top < 300)
      {
        setTop = Boxs.boxs[Boxs.boxs.length-1].top + 50;
      }
    }
  }
//  if(setTop > document.getElementById("main").offsetHeight)
//  {
//    setTop = document.getElementById("main").offsetHeight - 30;
//  }
  this.top = setTop;
  if(setLeft == null)
  {
    setLeft = 60;
    if(Boxs.boxs.length > 0)
    {
      if(Boxs.boxs[Boxs.boxs.length-1].left < 500)
      {
        setLeft = Boxs.boxs[Boxs.boxs.length-1].left + 50;
      }
    }
  }
//  if(setLeft > document.getElementById("main").offsetWidth)
//  {
//    setLeft = document.getElementById("main").offsetWidth - 30;
//  }
  this.left = setLeft;
  
  this.setHeight(setHeight);
  this.setWidth(setWidth);
  if(setTitle == "")
  {
    this.title = "Name missing";
  }
  if(setIcon == "")
  {
    this.icon = "noicon.gif";
  }
  if(setContent == "")
  {
    this.content = "Content missing";
  }
}

// Box.prototype = new Task();


Box.prototype.setHeight=function(setHeight)
{
  setHeight = parseInt(setHeight);
  if(setHeight == "NaN" || setHeight == null)
  {
    this.setHeight(400);
  }
  else if(setHeight < this.minHeight)
  {
    this.height = this.minHeight;
  }
//  else if(setHeight > document.getElementById("main").offsetHeight)
//  {
//    this.height = document.getElementById("main").offsetHeight;
//  }
  else
  {
    this.height = setHeight;
    return true;
  }
  return false;
}

Box.prototype.setWidth=function(setWidth)
{
  setWidth = parseInt(setWidth);
  if(setWidth == "NaN" || setWidth == null)
  {
    this.setWidth(400);
  }
  else if(setWidth < this.minWidth)
  {
    this.width = this.minWidth;
  }
//  else if(setWidth > document.getElementById("main").offsetWidth)
//  {
//    this.width = document.getElementById("main").offsetWidth;
//  }
  else
  {
    this.width = setWidth;
    return true;
  }
  return false;
}

Box.prototype.drawBox=function(menu)
{
  var boxID = this.idNumber + 1;
  var printThis = "  <div class=\"box\" id=\"box"+boxID+"\" style=\"top: "+this.top+"px; left: "+this.left+"px; width: "+this.width+"px; height: "+this.height+"px; z-index: "+Boxs.zIndex+";\" onmousedown=\"Boxs.boxs["+this.idNumber+"].onTop;\">\n";
     printThis += "    <h1 id=\"box"+boxID+"h1\" onmousedown=\"Boxs.boxs["+this.idNumber+"].move;\"><img src=\""+DeskTop.toRoot+"layout/pic/icon/"+this.icon+"\" alt=\""+this.title+"\" />"+this.title+"</h1>\n";
     printThis += "    <div class=\"content\" id=\"box"+boxID+"Content\">\n";
     printThis += "      Datan har inte laddats än\n";
     printThis += "    </div>\n";
     printThis += "    <div class=\"topleft\"></div>\n";
     printThis += "    <div class=\"topmiddle\"></div>\n";
     printThis += "    <div class=\"topright\"></div>\n";
     printThis += "    <div class=\"status statusIdle\" id=\"box"+boxID+"Status\">\n";
     printThis += "      <div class=\"statusleft\"></div>\n";
     printThis += "      <div class=\"topButtons\">\n";
     printThis += "        <div class=\"innerButtons\" id=\"box"+boxID+"Buttons\">\n";
     if(menu)
     {
       printThis += "          <a href=\"#\" title=\"Meny\" onclick=\"Boxs.boxs["+this.idNumber+"].menuBox;\"><img src=\""+DeskTop.toRoot+"layout/pic/boxmenu.png\" alt=\"Meny\" /></a>\n";
     }
     printThis += "          <a href=\"#\" title=\"Minimera\" onclick=\"Boxs.boxs["+this.idNumber+"].smallBox;\"><img src=\""+DeskTop.toRoot+"layout/pic/boxmakenormal.png\" alt=\"Normal\" /></a>\n";
     printThis += "          <a href=\"#\" title=\"Stäng\" onclick=\"Boxs.boxs["+this.idNumber+"].deleteBox;\"><img src=\""+DeskTop.toRoot+"layout/pic/boxclose.png\" alt=\"Stäng\" /></a>\n";
     printThis += "        </div>\n";
     printThis += "      </div>\n";
     printThis += "      <div class=\"statusright\"></div>\n";
     printThis += "    </div>\n";
     if(menu)
     {
       printThis += "    <ul id=\"box"+boxID+"Menu\" class=\"boxMenu\"></ul>\n";
     }
     printThis += "    \n";
     printThis += "    <div class=\"middleleft\"></div>\n";
     printThis += "    <div class=\"middlemiddle\"></div>\n";
     printThis += "    <div class=\"middleright\"></div>\n";
     printThis += "    \n";
     printThis += "    <div class=\"bottomleft\"></div>\n";
     printThis += "    <div class=\"bottommiddle\"></div>\n";
     printThis += "    <div class=\"bottomright\"></div>\n";
     printThis += "  </div>\n";
  
  HtmlGen.innerHtml(document.getElementById("main"),printThis);
}

Box.prototype.menuBox=function()
{
  var theBox = this.parentNode.parentNode.parentNode.parentNode;
  var theID = theBox.getAttribute("id").replace("box","");
  if(Boxs.boxs[theID - 1].content.contentID != null)
  {
    return false;
  }
  if(document.getElementById("box"+theID+"Menu").style.display == "block")
  {
    Boxs.boxs[theID - 1].menuBoxClose("box"+theID+"Menu");
    return false;
  }
  Boxs.boxs[theID - 1].content.openMenu(theID);
  document.getElementById("box"+theID+"Menu").style.display = "block";
  return false;
}

Box.prototype.menuBoxClose=function(theId)
{
  document.getElementById(theId).innerHTML = "";
  document.getElementById(theId).style.display = "none";
}

Box.prototype.deleteBox=function()
{
  var theBox = this.parentNode.parentNode.parentNode.parentNode;
  var theID = theBox.getAttribute("id").replace("box","") - 1;
//  Boxs.boxs[theID].deleteTask();
  Boxs.boxs[theID] = "";
  document.getElementById("content").removeChild(theBox);
  return false;
}

Box.prototype.boxAnimation=function(theID, turn, changeing, endHeight, endWidth, doThis)
{
  var theBox = document.getElementById(theID);
  
  if(doThis == "tonormal")
  {
    theBox.style.top =  (this.top / 3) * turn+"px";
    theBox.style.left = (this.left / 3) * turn+"px";
    turn += changeing;
    theBox.style.height = endHeight- (((endHeight - this.height)/4)*turn)+"px";
    theBox.style.width =  endWidth - (((endWidth - this.width)/4)*turn)+"px";
  }

  if(doThis == "tomax")
  {
    theBox.style.height = ((endHeight- this.height)/turn)+this.height+"px";
    theBox.style.width =  ((endWidth- this.width)/turn) + this.width+"px";
    turn += changeing;
    theBox.style.top = (this.top / (turn + 1)) * turn+"px";
    theBox.style.left = (this.left / (turn + 1)) * turn+"px";
  }
  
  if((turn > 0 && doThis == "tomax") || (turn < 4 && doThis == "tonormal"))
  {
    setTimeout("Boxs.boxs["+this.idNumber+"].boxAnimation(\""+theID+"\", "+turn+", "+changeing+", "+endHeight+", "+endWidth+", \""+doThis+"\");",20);
  }
  else if (doThis == "tomax")
  {
    theBox.style.height = "100%";
    theBox.style.width = "100%";
    theBox.style.top = "0px";
    theBox.style.left = "0px";
  }
  else
  {
    theBox.style.height = this.height+"px";
    theBox.style.width = this.width+"px";
    theBox.style.top = this.top+"px";
    theBox.style.left = this.left+"px";
  }
}

Box.prototype.maxBox=function()
{
  if(this.tagName.toLowerCase() == "h1")
  {
    var main = this.parentNode.parentNode;
    var theBox = this.parentNode;
    var h1Tag = this;
    var aTags = theBox.getElementsByTagName("a");
    var aTag = aTags[aTags.length - 2];
  }
  else
  {
    var main = this.parentNode.parentNode.parentNode;
    var theBox = this.parentNode.parentNode;
    var h1Tag = theBox.getElementsByTagName("h1")[0];
    var aTag = this;
  }
  var theID = theBox.getAttribute("id").replace("box","");
  
  aTag.getElementsByTagName("img")[0].src = "pic/box_normal.png";
  aTag.onclick = Boxs.boxs[theID].normalBox;
  
  h1Tag.ondblclick = Boxs.boxs[theID].normalBox;
  h1Tag.onmousedown = "";
  h1Tag.className = "fullscreen";
  
  Boxs.boxs[theID].boxAnimation("box"+theID, 3, -1, main.offsetHeight, main.offsetWidth, "tomax");
  
  return false;
}

Box.prototype.normalBox=function()
{
  var theBox = this.parentNode.parentNode.parentNode.parentNode;
  var boxObject = Boxs.boxs[theBox.getAttribute("id").replace("box","") - 1];
  
  boxObject.size = "normal";
  theBox.style.height = boxObject.height+"px";
  theBox.style.width = boxObject.width+"px";
  theBox.className = "box";
  
  var img = this.getElementsByTagName("img")[0];
  img.setAttribute("src",img.getAttribute("src").replace(/boxmakenormal.png/, "boxmakesmall.png"));
  img.setAttribute("alt","Minimera");
  this.setAttribute("title","Minimera");
  this.onclick = boxObject.smallBox;
  
  return false;
}

Box.prototype.smallBox=function()
{
  var theBox = this.parentNode.parentNode.parentNode.parentNode;
  var boxObject = Boxs.boxs[theBox.getAttribute("id").replace("box","") - 1];
  
  boxObject.size = "small";
  theBox.style.height = "25px";
  theBox.style.width = "200px";
  theBox.className = "box smallbox";
  
  var img = this.getElementsByTagName("img")[0];
  img.setAttribute("src",img.getAttribute("src").replace(/boxmakesmall.png/, "boxmakenormal.png"));
  img.setAttribute("alt","Normal");
  this.setAttribute("title","Normal");
  this.onclick = boxObject.normalBox;
  
  return false;
}

Box.prototype.move=function()
{
  Move.moveID = this.parentNode.getAttribute("id");
  Move.moveObj = Boxs.boxs[Move.moveID.replace("box","")-1];
  Move.firstPass = true;
  Move.inLayer = document.getElementById("main");
  Move.doThis = "move";
}

Box.prototype.resize=function()
{
  Move.moveID = this.parentNode.getAttribute("id");
  Move.moveObj = Boxs.boxs[Move.moveID.replace("box","")];
  Move.inLayer = document.getElementById("main");
  Move.posOnLayerX = 0;
  Move.posOnLayerY = 0;
  Move.doThis = this.getAttribute("class");
  Move.firstPass = true;
}

Box.prototype.onTop=function()
{
  Boxs.zIndex++;
  this.style.zIndex = Boxs.zIndex;
  var theID = this.getAttribute("id")+"Select";
  var that = document.getElementById(theID);
  //TaskBar.select(that);
}

Box.prototype.focusThis=function()
{
  //TaskBar.select(this.parentNode);
  var theID = this.parentNode.getAttribute("id").replace(/(box)|(select)/ig,"");
  var that = document.getElementById("box"+theID);
  Boxs.zIndex++;
  that.style.zIndex = Boxs.zIndex;
  that.className = "box";
}

var Boxs = {
  boxs:new Array(),
  zIndex:20,
  
  addNewBox:function(setTop, setLeft, setHeight, setWidth, setTitle, setIcon, setContent, menu)
  {
    var newBox = new Box(setTop, setLeft, setHeight, setWidth, setTitle, setIcon, setContent, "normal");
    this.boxs.push(newBox);
    newBox.drawBox(menu);
  },
  
  addExistingBox:function(setTop, setLeft, setHeight, setWidth, setTitle, setIcon, setContent, setSize)
  {
    var newBox = new Box(setTop, setLeft, setHeight, setWidth, setTitle, setIcon, setContent, setSize);
    this.boxs.push(newBox);
    var id = this.boxs.length - 1;
    document.getElementById("box"+this.boxs.length).onmousedown = Boxs.boxs[id].onTop;
    document.getElementById("box"+this.boxs.length+"h1").onmousedown = Boxs.boxs[id].move;
    
    var buttons = document.getElementById("box"+this.boxs.length+"Buttons").getElementsByTagName("a");
    for(var i = 0; i < buttons.length; i++)
    {
      if(buttons[i].getAttribute("title") == "Minimera")
      {
        buttons[i].onclick = Boxs.boxs[id].smallBox;
      }
      else if(buttons[i].getAttribute("title") == "Normal")
      {
        buttons[i].onclick = Boxs.boxs[id].normalBox;
      }
      else if(buttons[i].getAttribute("title") == "Stäng")
      {
        buttons[i].onclick = Boxs.boxs[id].deleteBox;
      }
      else if(buttons[i].getAttribute("title") == "Meny")
      {
        buttons[i].onclick = Boxs.boxs[id].menuBox;
      }
    }
    setContent.setEvents(this.boxs.length);
  }
}
