/*
    Copyright 2005 Rolando Gonzalez (rolosworld@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/*
  conf["topic"] = "RoLo";
  conf["class"] = "boxy";
  conf["drag"] = "0";
  conf["width"] = "200px";
  conf["height"] = "300px";
  conf["x"] = "100px";
  conf["y"] = "100px";
  conf["z"] = "0";
*/

// css table box object, can be dragged and closed
function cssWindow(c)
{
  var me = this;
  
  this.topic = c["topic"]?c["topic"]:"";
  this.w = c["width"]?c["width"]:"";
  this.h = c["height"]?c["height"]:"";
  this.x = c["x"]?c["x"]:0;
  this.y = c["y"]?c["y"]:0;
  this.z = c["z"]?c["z"]:0;
  this.c = c["class"]?c["class"]:null;

  this.cb = null;// close callback

  // CREATE NEEDED ELEMENTS
  this.table = document.createElement("table");
  //this.table.border = "1";
  this.table.style.width = this.w;
  this.table.style.height = this.h;
  this.table.cellSpacing = "0";
  this.table.cellPadding = "0";
  this.table.style.left = this.x;
  this.table.style.top = this.y;
  this.table.style.zIndex = this.z;

  this.thead = document.createElement("thead");
  this.tfoot = document.createElement("tfoot");
  this.tbody = document.createElement("tbody");

  this.trhead = document.createElement("tr");
  this.trfoot = document.createElement("tr");
  this.trbody = document.createElement("tr");

  this.tdhead = document.createElement("td");
  this.td1head = document.createElement("td");
  this.td2head = document.createElement("td");

  this.tdfoot = document.createElement("td");
  this.td1foot = document.createElement("td");
  this.td2foot = document.createElement("td");

  this.tdbody = document.createElement("td");
  this.td1body = document.createElement("td");
  this.td2body = document.createElement("td");


  // APPEND THE ELEMENTS
  this.thead = this.table.appendChild(this.thead);
  this.tfoot = this.table.appendChild(this.tfoot);
  this.tbody = this.table.appendChild(this.tbody);

  this.trhead = this.thead.appendChild(this.trhead);
  this.trfoot = this.tfoot.appendChild(this.trfoot);
  this.trbody = this.tbody.appendChild(this.trbody);

  this.tdhead = this.trhead.appendChild(this.tdhead);
  this.td1head = this.trhead.appendChild(this.td1head);
  this.td2head = this.trhead.appendChild(this.td2head);

  this.tdfoot = this.trfoot.appendChild(this.tdfoot);
  this.td1foot = this.trfoot.appendChild(this.td1foot);
  this.td2foot = this.trfoot.appendChild(this.td2foot);

  this.tdbody = this.trbody.appendChild(this.tdbody);
  this.td1body = this.trbody.appendChild(this.td1body);
  this.td1body.style.width = this.w;
  this.td1body.style.height = this.h;
  this.td2body = this.trbody.appendChild(this.td2body);

  // set head
  this.tdhead.appendChild(document.createTextNode(" "));
  this.td2head.appendChild(document.createTextNode(" "));

  // hide callback
  this.doHide = function()
  {
    if(me.tbody.style.display == "none")
    {
      me.table.style.height = me.h;
      me.table.style.width = me.w;
      me.tfoot.style.display = "";
      me.tbody.style.display = "";
    }
    else
    {
      me.table.style.height = "";
      me.table.style.width = me.w;
      me.tbody.style.display = "none";
      me.tfoot.style.display = "none";
    }
  };

  // close callback
  this.doClose = function()
  {
    if(me.cb)
      me.cb();
    me.table.parentNode.removeChild(me.table);
  };

  // initialice drag engine
  if(c["drag"])
  {
    this.drag = new Drag();
    this.drag.init(this.thead,this.table);
  }

  // set element classes
  this.setClass = function(cl)
  {
    me.table.className = cl;
    me.thead.className = cl;
    me.tfoot.className = cl;
    me.tbody.className = cl;
    me.trhead.className = cl+"head";
    me.trfoot.className = cl+"foot";
    me.trbody.className = cl+"body";
    me.tdhead.className = cl+"head";
    me.td1head.className = cl+"head1";
    me.td2head.className = cl+"head2";
    me.tdfoot.className = cl+"foot";
    me.td1foot.className = cl+"foot1";
    me.td2foot.className = cl+"foot2";
    me.tdbody.className = cl+"body";
    me.td1body.className = cl+"body1";
    me.td2body.className = cl+"body2";
  };

  // if class is defined, set the class
  if(this.c) this.setClass(this.c);

  // shorcut for appending valid childs
  this.setDOM = function(obj,ct)
  {
    switch(typeof ct)
    {
      case "number":
      case "string":
        return  obj.appendChild(document.createTextNode(ct));
      case "object":
        return  obj.appendChild(ct);
      default:
        alert("type not supported: "+(typeof ct));
        return null;
    }
  };

  // set a tdbody object as body
  this.setBody = function(ct)
  {
    me.clear(me.td1body);
    return me.setDOM(me.td1body,ct);
  };

  // set a tdfoot object as body
  this.setFoot = function(ct)
  {
    me.clear(me.td1foot);
    return me.setDOM(me.td1foot,ct);
  };

  // set a tdhead object as body
  this.setHead = function(ct)
  {
    me.clear(me.td1head);
    return me.setDOM(me.td1head,ct);
  };

  // clears table special DOM's
  this.clear = function(dom)
  {
    while(dom.firstChild) dom.removeChild(dom.firstChild);
  };

  // shows the box
  this.show = function()
  {
    document.body.appendChild(me.table);
  };

  // returns window
  this.get = function()
  {
    return me.table;
  };
}
