
// Dynamic Layout functions

function scheduleDynamicLayout() {
    // Delay needed to let Telerik Splitters sort out their Panes.
    setTimeout( dynamicLayout, 0 );
}

function dynamicLayout() {
    fitMap();
    fitOverview();

    posZoomBar();
    posBufferSelector();
    posLoadingImage();
    posCoords();
    posMeasure();

    requestMap();
}

function fitMap() {
    var mapPane           = mapSplitter.GetPaneById( "MapControl_MapPane" );
    m_imgMapCanvas.height = mapPane.GetInnerHeight();
    m_imgMapCanvas.width  = mapPane.GetInnerWidth();

    // Create a map coordinate manager.
    agiMapViewer = new map(
        m_imgMapCanvas.offsetLeft,
        m_imgMapCanvas.offsetTop,
        m_imgMapCanvas.width,
        m_imgMapCanvas.height,
        m_ExtentWidths,
        new rect( m_hvMinX.value, m_hvMinY.value, m_hvMaxX.value, m_hvMaxY.value ),
        "true"
    );

    agiPan = new Pan( agiMapViewer, m_imgMapCanvas );
}

function fitOverview() {
    m_imgOverviewCanvas.height     = m_imgMapCanvas.height / 8;
    m_imgOverviewCanvas.width      = m_imgMapCanvas.width  / 8;
    m_imgOverviewCanvas.style.top  = m_imgMapCanvas.offsetTop  + m_imgMapCanvas.height - m_imgOverviewCanvas.height;
    m_imgOverviewCanvas.style.left = m_imgMapCanvas.offsetLeft + m_imgMapCanvas.width  - m_imgOverviewCanvas.width;

    m_divOverviewBox.style.height = m_imgOverviewCanvas.height / OVERVIEW_ZOOM;
    m_divOverviewBox.style.width  = m_imgOverviewCanvas.width  / OVERVIEW_ZOOM;
    m_divOverviewBox.style.top    = m_imgOverviewCanvas.offsetTop  + m_imgOverviewCanvas.height / 2 - pxToInt(m_divOverviewBox.style.height) / 2;
    m_divOverviewBox.style.left   = m_imgOverviewCanvas.offsetLeft + m_imgOverviewCanvas.width  / 2 - pxToInt(m_divOverviewBox.style.width)  / 2;

    // Create an overview coordinate manager.
    m_overviewViewer = new map(
        m_imgOverviewCanvas.offsetLeft,
        m_imgOverviewCanvas.offsetTop,
        m_imgOverviewCanvas.width,
        m_imgOverviewCanvas.height,
        m_ExtentWidths,
        new rect( m_hvMinX.value, m_hvMinY.value, m_hvMaxX.value, m_hvMaxY.value ),
        "false"
    );
}

function posZoomBar() {
    m_zoomBar.style.top  = m_imgMapCanvas.offsetTop  + 10;
    m_zoomBar.style.left = m_imgMapCanvas.offsetLeft + 10;
}

function posBufferSelector() {
    m_bufferSelector.style.top  = m_imgMapCanvas.offsetTop  + 10;
    m_bufferSelector.style.left = m_imgMapCanvas.offsetLeft + 50;
}

function posLoadingImage() {
    m_imgLoading.style.top  = m_imgMapCanvas.offsetTop  + (m_imgMapCanvas.height / 2) - (m_imgLoading.height / 2);
    m_imgLoading.style.left = m_imgMapCanvas.offsetLeft + (m_imgMapCanvas.width  / 2) - (m_imgLoading.width  / 2);
}

function posCoords() {
    if (m_sClientBrowserType == 'IE')  leftOffset = -210
    else                               leftOffset = -180
    m_divCoords.style.top  = m_imgMapCanvas.offsetTop + 6;
    m_divCoords.style.left = m_imgMapCanvas.offsetLeft + m_imgMapCanvas.width + leftOffset;
}

function posMeasure() {
    m_divMeasure.style.top        = m_divCoords.offsetTop   + 32;
    m_divMeasure.style.left       = m_divCoords.offsetLeft  - 24;
    m_divMeasureButton.style.top  = m_divMeasure.offsetTop;
    m_divMeasureButton.style.left = m_divMeasure.offsetLeft + 184;
}

function showWaitImage() {
    m_imgLoading.style.visibility = 'visible';
}

function afterImageLoad() {
    m_imgLoading.style.visibility = 'hidden';
    agiPan.moveMapTo( 0, 0 );
}

function reportCoords( ptReport ) {
    if (agiMapUnits.value.toUpperCase() == "DEGREES") {
        if (m_sClientBrowserType == "IE" && m_sClientPlatform == "Mac") {
            m_txtXCoord.value = "long: " + (Math.round(ptReport.x*100000.0)/100000.0).toString();
            m_txtYCoord.value = "lat: "  + (Math.round(ptReport.y*100000.0)/100000.0).toString();
        }
        else { // Use nicer windows only format method
            m_txtXCoord.value = "long: " + ptReport.x.toFixed( 5 ).toString();
            m_txtYCoord.value = "lat: "  + ptReport.y.toFixed( 5 ).toString();
        }
    }
    else {
        if (m_sClientBrowserType == "IE" && m_sClientPlatform == "Mac") {
            m_txtXCoord.value = "X: " + Math.round(ptReport.x).toString() + " utm";
            m_txtYCoord.value = "Y: " + Math.round(ptReport.y).toString() + " utm";
        }
        else { // Use nicer windows only format method
            m_txtXCoord.value = "X: " + ptReport.x.toFixed( 0 ).toString() + " utm";
            m_txtYCoord.value = "Y: " + ptReport.y.toFixed( 0 ).toString() + " utm";
        }
    }
}

function resetMouseCursor() {
    m_imgMapCanvas.style.cursor = "crosshair";
}

function pxToInt( pxString ) {
    return parseInt( pxString.replace('px','') );
}

