// http://gisweb.azdeq.gov/arcgis/emaps/
// ADEQ: Arizona Department of Environmental Quality

function ResizeMap()
{
    // IE fires multiple resize events
    clearTimeout(_timer);
    _timer = setTimeout("ResizeMap2();", 500);
}

function ResizeMap2()
{
    // page controls
    var panel = document.getElementById("panel");
    var map = document.getElementById("map");
    var resizer = document.getElementById("resizer");
    var info = document.getElementById("info");
    var loading = document.getElementById("loading");

    // map left
    var panelWidth = Number(panel.style.width.substring(0, panel.style.width.length - 2));
    map.style.left = String(panelWidth + _resizerWidth) + "px";

    // map width
    var mapWidth = document.documentElement.clientWidth - panelWidth - _scrollbarWidth;
    if (mapWidth < _minMap) mapWidth = _minMap;
    map.style.width = String(mapWidth) + "px";

    // map height
    // when user drags panel/map separator to the right (to left is ok),
    // document.documentElement.clientHeight is 17 pixels too small,
    // but the second time it is referenced, it is correct {a Twilight Zone bug}
    var mapHeight = document.documentElement.clientHeight;
    mapHeight = document.documentElement.clientHeight - _headerHeight - _toolbarHeight - _footerHeight;
    if (mapHeight < _minMap) mapHeight = _minMap;
    map.style.height = String(mapHeight) + "px";

    // panel dimensions
    var panelHeight = document.documentElement.clientHeight - _headerHeight - _toolbarHeight;
    panel.style.height = String(panelHeight) + "px";

    // panel resizer dimensions
    resizer.style.height = String(panelHeight) + "px";
    resizer.style.top = _headerHeight + _toolbarHeight;
    resizer.style.left = panel.style.width;
    
    // map info dimensions
    var infoTop = document.documentElement.clientHeight - _footerHeight;
    info.style.top = String(infoTop) + "px";
    info.style.left = map.style.left;
    info.style.width = map.style.width;

    // loading image position
    loading.style.left = String(panelWidth + (mapWidth / 2) - (_loadingWidth / 2)) + "px";
    loading.style.top = String(_headerHeight + (mapHeight / 2)) + "px";
    
    // redraw map
    if (_map != null)  // event, not initial call by StartApp()
    {
        _map.resize();
        _map.reposition();
    }
}

function BeginPanelDrag(event)
{
    if (!event) event = window.event;
    event.cancelBubble = true;
    event.preventDefault();
    if (event.stopPropagation) event.stopPropagation();

    // clientX can be 0 to 10 more than resizer left
    var resizer = document.getElementById("resizer");
    var left = Number(resizer.style.left.substring(0, resizer.style.left.length - 2));
    _offsetX = event.clientX - left;

    HideMap();

    // connect mousemove and mouseup events
    window.onresize = null;
    _mousemove = dojo.connect(document, "mousemove", PanelDrag);
    _mouseup = dojo.connect(document, "mouseup", EndPanelDrag);
}

function PanelDrag(event)
{
    if (!event) event = window.event;
    event.cancelBubble = true;
    event.preventDefault();
    if (event.stopPropagation) event.stopPropagation();

    var button = event.button;  // IE
    if (!button) button = event.which;  // Firefox
    if (button == 0) EndPanelDrag(event);
    
    // adjust clientX by mousedown X
    var width = event.clientX - _offsetX;

    // limit adjustment
    if (width < 20) return;
    if (width > document.documentElement.clientWidth - _scrollbarWidth - _minMap) return;

    var panel = document.getElementById("panel");
    panel.style.width = width + "px";

    var resizer = document.getElementById("resizer");
    resizer.style.left = width + "px";
}

function EndPanelDrag(event)
{
    if (!event) event = window.event;
    event.cancelBubble = true;
    event.preventDefault();
    if (event.stopPropagation) event.stopPropagation();

    dojo.disconnect(_mousemove);
    dojo.disconnect(_mouseup);

    ResizeMap();
    window.onresize = ResizeMap;

    setTimeout("ShowMap();", 2000);
}    

