﻿function PositionElements()
{
    var x = parseInt(document.documentElement.clientWidth);
    var y = parseInt(document.documentElement.clientHeight);
    
    // Minumum accepted "area" is 900x550 (a little less than 1024x768 resolution)
    var minX = 900;
    var minY = 550;
    if (x < minX)
        x = minX;
    if (y < minY)
        y = minY;
    
    var menuWidth = parseInt(document.getElementById('menu').style.width);
    var width = parseInt(x)-421;
    
    var flashWidth = width;
    if(flashMaxX != undefined && flashWidth > flashMaxX)
        flashWidth = flashMaxX;
        
    var flashHeight = flashWidth*710/780;
    if(!keepContraint)
    {
        flashHeight = y;
        if(flashMaxY != undefined && flashHeight > flashMaxY)
            flashHeight = flashMaxY;
    }

    var zIndex = 1;
    try
    {
        zIndex = contentZIndex;
    }
    catch (e) { }

    // Positions flash
    document.getElementById('flash').style.zIndex = 0;
    document.getElementById('flash').style.width = parseInt(flashWidth) + 'px';
    document.getElementById('flash').style.height = parseInt(flashHeight) + 'px';
    document.getElementById('flash').style.top = 0 + 'px';
    document.getElementById('flash').style.left = 0 + 'px';
    
    // Positions content
    document.getElementById('content').style.zIndex = zIndex;
    document.getElementById('content').style.width = width + 'px';
    document.getElementById('content').style.height = y + 'px';
    document.getElementById('content').style.top = 0 + 'px';
    document.getElementById('content').style.left = 0 + 'px';
    
    // Position menu
    document.getElementById('menu').style.height = y + 'px';
    document.getElementById('menu').style.top = 0 + 'px';
    document.getElementById('menu').style.left = x-menuWidth + 'px';
    document.getElementById('menu').style.height = y + 'px';

    // Positioning done - make elements visible
    document.getElementById('pageloader').style.visibility = '';
    document.getElementById('pageloader').style.display = 'none';
    document.getElementById('flash').style.visibility = '';
    document.getElementById('content').style.visibility = '';
    document.getElementById('menu').style.visibility = '';
    
    // Also set the proper content children
    SetElementWidth('productCategory', -140);
}

function SetElementWidth(name, adjustment)
{
    var oElement = document.getElementById(name);
    if(oElement == null || oElement == undefined) return;
    
    oElement.style.width = parseInt(document.getElementById('content').style.width) + adjustment + 'px';
}

function AdjustMenuHeight()
{
    // Client height is the minimum
    var y = parseInt(document.documentElement.clientHeight);
    var scrollTop = parseInt(window.pageYOffset || document.documentElement.scrollTop || 0);
    document.getElementById('menu').style.height = y + scrollTop + 'px';
}

function getAbsoluteLeft(objectId)
{
    o = document.getElementById(objectId)
    return findPosition(o)[0];
}

function getAbsoluteTop(objectId)
{
    return findPosition(o)[1];
}

function requestQueryString(name)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(window.location.href);
    if(results == null)
        return "";
    else
        return results[1];
}

function findPosition(oElement)
{
    if(typeof(oElement.offsetParent ) != 'undefined')
    {
        for(var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent)
        {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [ posX, posY ];
    }
    else
    {
        return [ oElement.x, oElement.y ];
    }
}
