/**
 *  Display Class
 *
 * @author  Devyn Cunningham
 */

function Display(){

    var screenWidth;
    var screenHeight;
    var pageWidth;
    var pageHeight;

    var mouseX = 0;
    var mouseY = 0;
    var mouseChangeX = 0;
    var mouseChangeY = 0;

    function detectScreenSize(){
        if(System.browser.isIE()){
            if(System.browser.isQuirks()){
                screenWidth = document.body.clientWidth;
                screenHeight = document.body.clientHeight;
            }else{
                screenWidth = document.documentElement.clientWidth;
                screenHeight = document.documentElement.clientHeight;
            }
        }else{
            screenWidth = window.innerWidth;
            screenHeight = window.innerHeight;
        }
        if(System.browser.isQuirks()){
            pageWidth = document.documentElement.clientWidth;
            pageHeight = document.documentElement.clientHeight;
        }else{
            pageWidth = document.body.clientWidth;
            pageHeight = document.body.clientHeight;
        }
    }

    function detectMouseLocation(obj,e){
        var tempX = mouseX;
        var tempY = mouseY;
        if(System.browser.isIE()){
            mouseX = event.clientX + document.body.scrollLeft;
            mouseY = event.clientY + document.body.scrollTop;
        }else{
            mouseX = e.pageX;
            mouseY = e.pageY;
        }
        mouseChangeX = mouseX - tempX;
        mouseChangeY = mouseY - tempY;
    }

    var display = {

        getScreenDimensions : function(){
            detectScreenSize();
            return [screenWidth, screenHeight];
        },

        getPageDimensions : function(){
            detectScreenSize();
            return [pageWidth,pageHeight];
        },

        getMouseCoordinates : function(e){
            return [mouseX, mouseY];
        },

        getMouseX : function(){
            return mouseX;
        },

        getMouseY : function(){
            return mouseY;
        },

        getMouseChange : function(){
            return [mouseChangeX, mouseChangeY];
        },

        getMouseXChange : function(){
            return mouseChangeX;
        },

        getMouseYChange : function(){
            return mouseChangeX;
        },

        getObjectBackgroundColor : function(obj){
            var color = this.getStyleValue(obj,"background-color");
            if (color == ""){
                if (obj.parentNode !== undefined && obj.parentNode !== document.body.parentNode){
                    return this.getBackgroundColor(obj.parentNode);
                }else{
                    return "rgb(255, 255, 255)";
                }
            }else{
                return color;
            }
        },

        getStyleValue : function(obj,property,numbersOnly){
            var value;
            if (System.browser.isIE()){
                value = obj.currentStyle[property];
            }else{
                var style = window.getComputedStyle(obj,null);
                value = (style === null) ? "0" : style.getPropertyValue(property);
            }
            if (numbersOnly){
                if (value !== null && value !== undefined){
                    value = value.replace("px","");
                    var parsed = parseInt(value,10);
                    return (parsed === value) ? parsed : "auto";
                }
            }
            return value;
        },

        getObjectHeight : function(obj){
            var height = obj.offsetHeight;
            height = (height === "" || height === 0) ? this.getStyleValue(obj,"height",true) : height;
            if (height === "" || height === 0){
                return (obj.firstChild !== undefined) ? this.getHeight(obj.firstChild) : 0;
            }
            return height;
        },

        getObjectWidth : function(obj){
            var width = obj.offsetWidth;
            width = (width === "" || width === 0) ? this.getStyleValue(obj,"width",true) : width;
            if (width === "" || width === 0){
                return (obj.firstChild !== undefined) ? this.getWidth(obj.firstChild) : 0;
            }
            return width;
        },
    
        getObjectLeft : function(obj){
            var left;
            left = System.display.getStyleValue(obj,"left",true);
            left = (left === "auto") ? obj.offsetLeft : left;
            if (left === "auto"){
                var right = System.display.getStyleValue(obj,"right",true);
                return (right === "auto") ? 0 : right;
            }
            return left;
        },
    
        getObjectTop : function(obj){
            var top;
            top = System.display.getStyleValue(obj,"top",true);
            top = (top === "auto") ? obj.offsetTop : top;
            if (top === "auto"){
                var bottom = System.display.getStyleValue(obj,"bottom",true);
                return (bottom === "auto") ? 0 : bottom;
            }
            return top;
        },

        getObjectRight : function(obj){
            var right;
            right = System.display.getStyleValue(obj,"right",true);
            right = (right === "auto") ? obj.offsetRight : right;
            if (right === "auto"){
                var left = System.display.getObjectLeft(obj);
                return (left === "auto") ? 0 : System.display.getDimensions()[0]-(left+Sytem.display.getObjectWidth(obj));
            }
            return right;
        },

        getObjectBottom : function(obj){
            var bottom;
            bottom = System.display.getStyleValue(obj,"bottom",true);
            bottom = (bottom === "auto") ? obj.offsetBottom : bottom;
            if (bottom === "" || bottom === 0){
                var top = System.display.getObjectTop(obj);
                return (top === "auto") ? 0 : System.display.getDimensions()[1]-(top+System.display.getObjectHeight(obj));
            }
            return bottom;
        }

    };

    document.addSubscriber("onmousemove",detectMouseLocation);
    System.display = display;
}
