﻿Event.observe(window, 'resize', Window_OnResize);
document.observe('dom:loaded', Window_OnResize);

var ChildPage_CalculateLayoutDimensions = null;

function Window_OnResize()
{
    try { Window_RecalculateLayoutAfterResize() } catch(e) {}
    // kewl trick from Mathieu to make sure that the scrollbars are not shown as whitespace in page
    // rendering for the second time, the browser will know the new sizes and realizes it does not 
    // need scrollbars, mathieu will fix this in the next release
    try { Window_RecalculateLayoutAfterResize() } catch(e) {}
}

function Window_RecalculateLayoutAfterResize()
{
    var SideBarLeft = $('SideBarLeft');
    var SideBarLeft_Top = $('SideBarLeft_Top');
    var SideBarLeft_Backdrop = $('SideBarLeft_Backdrop');
    var SideBarLeft_Mid = $('SideBarLeft_Mid');
    var SideBarLeft_Low = $('SideBarLeft_Low');
    var TopBar = $('TopBar');
    var TopBar_Left = $('TopBar_Left');
    //var TopBar_Right = $('TopBar_Right');
    var TopBarItems = $('TopBarItems');
    var MainContent = $('MainContent');
    
    var windowHeight = WindowHeight();
    var windowWidth = WindowWidth();

    var offsetSideBarHeight = -100;

    var pixelExtract = function(strPx) { return strPx == null ? 0 : parseInt(strPx.replace("px", ""), 10);}

    SideBarLeft.style.height = (document.documentElement.clientHeight + offsetSideBarHeight) + "px";
    SideBarLeft_Mid.style.height = (windowHeight - SideBarLeft_Top.offsetHeight - SideBarLeft_Low.offsetHeight + offsetSideBarHeight) + "px";
    SideBarLeft_Backdrop.style.height = (windowHeight - SideBarLeft_Top.offsetHeight - SideBarLeft_Low.offsetHeight + offsetSideBarHeight) + "px";

    var marginLeft = 0;
    marginLeft += pixelExtract(MainContent.getStyle('margin-left'));
    marginLeft += pixelExtract(MainContent.getStyle('padding-left'));

    var marginTop = 0;
    marginTop += pixelExtract(MainContent.getStyle('margin-top'));
    marginTop += pixelExtract(MainContent.getStyle('padding-top'));
    
    MainContent.setStyle({
        left : (SideBarLeft.offsetLeft + SideBarLeft.offsetWidth) + "px",
        top : TopBar.offsetHeight + "px",
        width : (windowWidth - (SideBarLeft.offsetLeft + SideBarLeft.offsetWidth) - marginLeft) + "px",
        height : (windowHeight - TopBar.offsetHeight - marginTop) + "px"
    });


    if (ChildPage_CalculateLayoutDimensions != null)
    {
        ChildPage_CalculateLayoutDimensions(windowWidth, windowHeight);
    }
}



function WindowHeight()
{
	if (self.innerHeight) // all except Explorer
	{
		return self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientHeight;
	}
}

function WindowWidth()
{
	if (self.innerWidth) // all except Explorer
	{
		return self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
		// Explorer 6 Strict Mode
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientWidth;
	}
}


function ExpandHeightToBottom(divExpander, divContainer, fudgePx)
{
    $(divExpander).setStyle({ height: ($(divContainer).getHeight() - $(divExpander).cumulativeOffset().top + $(divContainer).cumulativeOffset().top + fudgePx) + "px" });
}
