function PageOrganizer() {
    this.width;
    //this.height; height is dynamic, depends on contained elements
    this.elementReference;

    this.stacks = [];

    this.createBody();
}

PageOrganizer.prototype.createBody = function () {
    var object = this;
    var container = $('<div class="stackPanel"></div>');
    this.elementReference = container;

    this.getBodyReference().bind("stackOpenStart", function (event) {
        object.closeAllStacksExcept(event.stack);
    });
};

PageOrganizer.prototype.closeAllStacks=function(){
	for (var currentStackIndex = 0; currentStackIndex < this.stacks.length; currentStackIndex++) {
        this.stacks[currentStackIndex].close();
        this.stacks[currentStackIndex].changeStackContentState('normal');
    }
}

PageOrganizer.prototype.closeAllStacksExcept = function (stack) {
    for (var currentStackIndex = 0; currentStackIndex < this.stacks.length; currentStackIndex++) {
        if (this.stacks[currentStackIndex] != stack) {
            this.stacks[currentStackIndex].close();
            this.stacks[currentStackIndex].changeStackContentState('normal');
        }
    }
};

PageOrganizer.prototype.getBodyReference = function () {
    return this.elementReference;
};

PageOrganizer.prototype.setWidth = function (newWidth) {
    this.width = newWidth;
};

PageOrganizer.prototype.refreshBody = function () {
    this.getBodyReference().width(this.width);
    this.repositionStacks();
};

PageOrganizer.prototype.repositionStacks = function () {
    for (var currentStackIndex = 0; currentStackIndex < this.stacks.length; currentStackIndex++) {
        this.repositionStack(this.stacks[currentStackIndex]);
    }
};

PageOrganizer.prototype.addStack = function (newStack) {
    this.stacks.push(newStack);
    this.getBodyReference().append(newStack.getBodyReference());
    this.repositionStack(newStack);
};

PageOrganizer.prototype.repositionStack = function (stack) {
    stack.setWidth(this.width);
    stack.refreshBody();
};

PageOrganizer.prototype.unload = function () {
    for (var currentStackIndex = 0; currentStackIndex < this.stacks.length; currentStackIndex++) {
        this.stacks[currentStackIndex].unload();
    }
};
