

function ListTableView(uid, table_id, folder, action_key, seed)
{
    this.uid        = uid;
    this.table_id   = table_id;
    this.folder     = folder;
    this.action_key = action_key;
    this.seed       = seed;
}



//  separate init since we need to call the constructor before the dom
//  may be built.
ListTableView.prototype.init = function()
{
    var foo = $(this.table_id).getElementsByTagName("tbody");

    this.tbody = $(foo[0]);

    $("create-list-link").onclick = this.create_list.bind(this);
};



ListTableView.prototype.create_list = function()
{
    //  in the first container, create a new ListView with 'new' flag.
    var container = this.add_slot(0);

    new ListView({ uid:       this.uid,
                   lid:       -1, 
                   list_type: 'Text',
                   folder:     this.folder,
                   action_key: this.action_key,
                   parent:     container,
                   seed:       this.seed});

    return false;
};



ListTableView.prototype.add_slot = function(index)
{
    var containers = this.tbody.getElementsBySelector(".list-container");

    //  if number of containers is even, we need a new row, else we
    //  assume the row with both cells to exist and create only a new
    //  container.
    var expand_cell;

    if (containers.length % 2 == 0)
    {
        var row = $(document.createElement("tr"));

        row.writeAttribute("class", "list_table_view");

        expand_cell = $(document.createElement("td"));


        expand_cell.writeAttribute("valign", "top");
        expand_cell.writeAttribute("width", "50%");

        row.appendChild(expand_cell);

        var c = $(document.createElement("td"));
        c.writeAttribute("valign", "top");
        c.writeAttribute("width", "50%");

        row.appendChild(c);

        this.tbody.appendChild(row);
    }
    else
    {
        var rows = this.tbody.select("tr.list_table_view");

        var cells = rows[rows.length - 1].getElementsByTagName("td");
        
        expand_cell = cells[cells.length - 1];
    }


    var container = $(document.createElement("div"));

    container.addClassName("list-container");

    expand_cell.appendChild(container);

    containers.push(container);


    //  result here should be list of containers in which the last one
    //  is empty.  starting from the end, shift the container contents
    //  to the right.
    var i = containers.length - 1;
    for (; i > index; i--)
    {
        var foo = containers[i - 1].getElementsByTagName("div");
        var d = foo[0];
        if (d)
        {
            containers[i].appendChild(d);
        }
    }

    //  return the first, now open, container for convenience
    return containers[i];
};



ListTableView.prototype.remove_slot = function(container)
{
    //  empty the container.  iterate over all containers, for ones
    //  after the container we emptied, move the contents from the
    //  next one into it.

    container.update();

    var containers = this.tbody.getElementsBySelector(".list-container");

    var found = false;
    for(var i = 0; i < containers.length; i++)
    {
        var c = containers[i];

        if (c == container)
        {
            found = true;
        }

        if (found && i < containers.length - 1)
        {
            var foo = containers[i + 1].getElementsByTagName("div");
            var d = foo[0];
            if (d)
            {
                c.appendChild(d);
            }
        }
    }

    //  delete the last container
    var last_container = containers[containers.length - 1];

    $(last_container.parentNode).update();


    //  remove last row if empty
    var rows = this.tbody.select("tr.list_table_view");

    var last_row = $(rows[rows.length - 1]);

    containers = last_row.getElementsBySelector(".list-container");

    if (containers.length == 0)
    {
        this.tbody.removeChild(last_row);
    }
};




