Divvy.VERSION = "0.1";

function Divvy(divId, workspaceWidth, workspaceHeight)
{
    this.currTopZIndex = 0;
    this.layers = new Array();
    this.init(divId, workspaceWidth, workspaceHeight);
}

Divvy.prototype.init = function(divId, workspaceWidth, workspaceHeight)
{
    this.workspaceDivId = divId;
    this.workspaceDiv = $(divId);
    this.workspaceDiv.innerHTML = "";
    this.workspaceDiv.className = "divvy";
    this.workspaceDiv.style.width = workspaceWidth + "px";
    this.workspaceDiv.style.height = workspaceHeight + "px";
}

/**
 * Add an element to the divvy on the specified layer.
 *
 * @param elem the element to add
 * @param layer the layer to add the element to
 */
Divvy.prototype.addItem = function(elem, layer)
{
    if(!layer || layer == undefined)
        return;

    elem.className = elem.className + " divvyNode";
    elem.style.zIndex = this.currTopZIndex++;
    
    var divvy = this;
    elem.onmouseover = function() { divvy.currTopZIndex = layer.moveToTop(divvy.currTopZIndex++); elem.style.zIndex = divvy.currTopZIndex++;};

    this.workspaceDiv.appendChild(elem);
    layer.addElement(elem);

    var layerId = layer.getUID();
    if(this.layers[layerId] == null || this.layers[layerId] == undefined)
        this.layers[layerId] = layer;
}

Divvy.prototype.render = function()
{
    for(layerId in this.layers)
        this.layers[layerId].render();
}
