/*
BOX class for Javascript

Will create a nifty little box for keeping various tools etc.

*/

var Box = function(title, body, close_button, icon) {
    // node
    this.container = document.createElement("div");
    this.node = document.createElement("table");
    this.node = this.container.appendChild(this.node);
    this.node.setAttribute("id", "b"+Math.ceil(Math.random()*3));
    this.node.setAttribute("class", "box");
    this.node.setAttribute("cellspacing", 0);
    this.node.setAttribute("cellpadding", 0);
    // vars
    if (title) this.title = title;
    if (body) this.body = body;
    if (close_button) this.close_button = close_button;
    if (icon) this.icon = icon;
    // internal build
    this.top_row();
    this.middle_row();
    this.bottom_row();
}

Box.prototype.top_row = function() {
    var row = document.createElement("tr");
    row = this.node.appendChild(row);
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "ul");
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "um");
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "ur");
}

Box.prototype.middle_row = function() {
    var row = document.createElement("tr");
    row = this.node.appendChild(row);
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "ml");
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "mm");
    // close, body and title
    if (this.title) {
        var tit = document.createElement("h3");
        tit = td.appendChild(tit);
        if (this.close_button) {
            var close = document.createElement("a");
            close = tit.appendChild(close);
            close.onclick = hide_curtain;
        }
        if (this.icon) {
            var img = document.createElement("img");
            img.setAttribute("class", "icon16");
            img.setAttribute("src", "/grafik/icons/"+this.icon);
            tit.appendChild(img);
        }
        // title
        var text = document.createTextNode(this.title);
        tit.appendChild(text);
    }
    if (typeof this.body == "string") {
        // the body was a text string
        td.innerHTML = this.body;
    } else {
        // the body was most likely a DOM node (from JS DOM!)
        //alert(typeof this.body);
        var body = td.appendChild(this.body);
    }
    // end element
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "mr");
}

Box.prototype.bottom_row = function() {
    var row = document.createElement("tr");
    row = this.node.appendChild(row);
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "ll");
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "lm");
    var td = document.createElement("td");
    td = row.appendChild(td);
    td.setAttribute("class", "lr");
}


Box.prototype.getNode = function() {
    return this.container;
}

Box.prototype.wrap = function(element) {
    var node = this.getNode();
    var element = document.getElementById(element);
	element.innerHTML = node.innerHTML;
	//return element;
}