﻿function SetupDivMasterFloatPosition()
{
    var l_div_master_float_heigth = window.div_master_float.clientHeight;
    var l_div_master_float_width = window.div_master_float.clientWidth;

    var l_res = window.center({ width: l_div_master_float_width, height: l_div_master_float_heigth });

    if (l_res.y > 0) {
        window.div_master_float.style.marginTop = l_res.y.toString() + "px";
    }
    else {
        window.div_master_float.style.marginTop = "0px";
    }

    if (l_res.x > 0) {
        window.div_master_float.style.marginLeft = l_res.x.toString() + "px";
    }
    else {
        window.div_master_float.style.marginLeft = "0px";
    }
}

function SetupDivMasterFloat(p_obj)
{
    if (p_obj != null) 
    {
        window.div_master_float = p_obj;
    }

    window.onresize = SetupDivMasterFloatPosition;
    SetupDivMasterFloatPosition();
}

window.size = function() {
    var w = 0;
    var h = 0;

    //IE
    if (!window.innerWidth) {
        //strict mode
        if (!(document.documentElement.clientWidth == 0)) {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    }
    //w3c
    else {
        w = window.innerWidth;
        h = window.innerHeight;
    }
    return { width: w, height: h };
}

window.center = function() {
    var hWnd = (arguments[0] != null) ? arguments[0] : { width: 0, height: 0 };

    var _x = 0;
    var _y = 0;
    var offsetX = 0;
    var offsetY = 0;

    //IE
    if (!window.pageYOffset) {
        //strict mode
        if (!(document.documentElement.scrollTop == 0)) {
            offsetY = document.documentElement.scrollTop;
            offsetX = document.documentElement.scrollLeft;
        }
        //quirks mode
        else {
            offsetY = document.body.scrollTop;
            offsetX = document.body.scrollLeft;
        }
    }
    //w3c
    else {
        offsetX = window.pageXOffset;
        offsetY = window.pageYOffset;
    }

    _x = ((this.size().width - hWnd.width) / 2) + offsetX;
    _y = ((this.size().height - hWnd.height) / 2) + offsetY;

    return { x: _x, y: _y };
}
