function WindowManager(){
    
    var focusedObject;
    var windowman = document.createElement("div");

    function updateViewById(id){
        if(id.toLowerCase() === "desktop"){
            System.ajaxRequest(window.location,null,"desktop",true).send();
        }else{
            var sections = id.split(".");
            if (sections.length > 1){
                System.ajaxRequest(""+sections[0]+"/"+sections[1],null,id).send();
            }
        }
    }

    function updateViewByObject(domElement){
        var sections= domElement.id.split(".");
        if(sections.length > 1){
            System.ajaxRequest(""+sections[0]+"/"+sections[1],null,domElement.id).send();
        }
    }

    function createRootContainer(){
        windowman.id = "desktop";
        //windowman.style.left = "0px";
        //windowman.style.top = "0px";
        windowman.style.margin = "0px";
        windowman.style.padding = "0px";

        //var body = document.body;
        try{
            window.addSubscriber("onresize",windowman.resize);
        }catch(e){
            try{
                document.addSubscriber("onresize",windowman.resize);
            }catch(e){
                document.body.addSubscriber("onresize",windowman.resize);
            }
        }
        var b = document.body;
        if(b.firstChild.nodeName == "NOSCRIPT"){
            for(var x = b.childNodes.length; x > 0; x--){
                b.removeChild(b.firstChild);
            }
        }
        document.body.appendChild(windowman);
    }

    windowman.resize = function(){
        var dimensions = System.display.getPageDimensions();
        windowman.style.width = dimensions[0] + "px";
        windowman.style.height = dimensions[1] + "px";
    };
    
    windowman.getFocusedObject = function(){
        return focusedObject;
    };
    
    windowman.setFocusedObject = function(obj){
        focusedObject = obj;
        return focusedObject;
    };
    
    windowman.refocus = function(){
        if (focusedObject !== undefined){
            focusedObject.focus();
        }
    };
        
    windowman.updateView = function(object){
        if(typeof(object) === 'string'){
            updateViewById(object);
        }else if(object.nodeType === 1){
            updateViewByObject(object);
        }
    };
    
    windowman.appendStyleSheet = function(url){
        var html = document.childNodes[1];
        var head = html.childNodes[0];
        var links = head.getElementsByTagName("link");
        var fullUrl;
        var exists = false;
        for (var x = 0; x < links.length; x++){
            fullUrl = "http://"+window.location.hostname+url;
            if(links[x].href == fullUrl){
                exists = true;
                break;
            }
        }
        if(!exists){
            var link = document.createElement("link");
                link.type = "text/css";
                link.href = url;
                link.rel = "stylesheet";
                head.appendChild(link);
        }
    };
    
    windowman.appendJavaScript = function(url){
        var html = document.childNodes[1];
        var head = html.childNodes[0];
        var scripts = head.getElementsByTagName("script");
        var fullUrl;
        var exists = false;
        for (var x = 0; x < scripts.length; x++){
            fullUrl = "http://"+window.location.hostname+url;
            if(scripts[x].src == fullUrl){
                exists = true;
                break;
            }
        }
        if(!exists){
            var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = url;
                head.appendChild(script);
        }
    };

    windowman.changeViewPort = function(objId,xDelta,yDelta){
        var node = document.getElementById(objId);
        //node.style.position = "absolute";

        node.style.left = (node.offsetLeft+xDelta) + "px";
        node.style.top = node.offsetTop + yDelta;
    };

    windowman.noTextOverflow = function(obj){
        var textNode;
        for(var x = 0; x < obj.childNodes.length; x++){
            if (obj.childNodes[x].nodeName == "#text"){
                textNode = obj.childNodes[x];
            }
        }
        var text = textNode.nodeValue;
        var newNode;
        while(obj.scrollWidth > System.display.getObjectWidth(obj) || obj.scrollHeight > System.display.getObjectHeight(obj)){
            text = this.abbreviateText(text);
            if(text === "..."){
                break;
            }
            newNode = document.createTextNode(text);
            obj.replaceChild(newNode,textNode);
            textNode = newNode;
        }
    };

    windowman.abbreviateText = function(text){
        var modifiedText = text.substring(0,text.length - 4);
        modifiedText += "...";
        return modifiedText;
    };

    windowman.loadingImage = function(){
        var div = document.createElement("div");
        div.className = "loadingImage";
        return div;
    };

    createRootContainer();
    
    Container(windowman);

    System.windowmanager = windowman;
}

