﻿// JScript File

//alert("DefaultPageController.js");

function PageController(){
    this.size = new Size(0, 0);
    this.maxWidth = 1200;
    this.minWidth = 800;
    this.maxHeight = -1;
    this.minHeight = -1;
    this.attached = false;
    // default layout values
    this.headerHeight = 50;
    this.topNavHeight = 20;
    this.leftNavWidth = 180;
    this.footerHeight = 50;
}
PageController.prototype.attach = function(outerDiv, headerDiv, topNavDiv, leftNavDiv, contentDiv, footerDiv){
    if(outerDiv != null && headerDiv != null && topNavDiv != null && leftNavDiv != null && footerDiv != null){
        this.outerDivController = new DivController(outerDiv);
        this.headerDivController = new DivController(headerDiv);
        this.topNavDivController = new DivController(topNavDiv);
        this.leftNavDivController = new DivController(leftNavDiv);
        this.contentDivController = new DivController(contentDiv);
        this.footerDivController = new DivController(footerDiv);
        this.attached = true;
        this.updateFixedLayout();
        this.outerDivController.show();
    }else{
        this.attached = false;
    }
}
PageController.prototype.setLeftNavWidth = function(width){
    this.leftNavWidth = width;
    this.updateFixedLayout();
    this.updateVariableLayout();
}
PageController.prototype.setSize = function(width, height){
    if(width.width != null){    // they are passing a Size object
        height = width.height;
        width = width.width;
    }
    if(this.minWidth > 0 && this.minWidth > width){ width = this.minWidth; }
    if(this.maxWidth > 0 && this.maxWidth < width){ width = this.maxWidth; }
    if(this.minHeight > 0 && this.minHeight > height){ height = this.minHeight; }
    if(this.maxHeight > 0 && this.maxHeight < height){ height = this.maxHeight; }
    this.size.width = width;
    this.size.height = height;
    this.updateVariableLayout();
}
PageController.prototype.updateFixedLayout = function(){
    if(this.attached){
        this.headerDivController.setHeight(this.headerHeight);
        this.topNavDivController.setHeight(this.topNavHeight);
        this.leftNavDivController.setWidth(this.leftNavWidth);
        this.footerDivController.setHeight(this.footerHeight);
        this.headerDivController.setLocation(0, 0);
        this.topNavDivController.setLocation(0, this.headerDivController.getY2());
        this.leftNavDivController.setLocation(0, this.topNavDivController.getY2());
        this.contentDivController.setLocation(this.leftNavDivController.getX2(), this.leftNavDivController.getY());
        this.footerDivController.setX(0);
    }
}
PageController.prototype.updateVariableLayout = function(){
    if(this.attached){
        
        this.outerDivController.setSize(this.size);
        
        this.headerDivController.setWidth(this.outerDivController.getWidth());
        this.topNavDivController.setWidth(this.outerDivController.getWidth());
        this.contentDivController.setWidth(this.outerDivController.getWidth() - this.leftNavDivController.getWidth());
        this.footerDivController.setWidth(this.outerDivController.getWidth());
        
        this.leftNavDivController.setHeight(this.outerDivController.getHeight() - this.headerDivController.getHeight() - this.topNavDivController.getHeight() - this.footerDivController.getHeight());
        this.contentDivController.setHeight(this.leftNavDivController.getHeight());
        
        this.footerDivController.setY(this.leftNavDivController.getY2());
        
    }
}
