// JScript File
var mapRightHolder = 0;
var mapBottomHolder = 0;
var mapLeftHolder = 0;
var mapTopHolder = 0;

//Current browser window size
function FindWindowSize()
{
    var winSize = new Array();
    
    if (BrowserDetect.browser == 'Explorer')
    {
       winSize[0] = document.documentElement.clientWidth;
       winSize[1] = document.documentElement.clientHeight; 
    }
    else
    {
       winSize[0] = window.innerWidth;
       winSize[1] = window.innerHeight; 
    }
    
    return winSize;
}

function StoreCurrentMapPosition() {
    //9.3 (Min 5/26/09)
    //    var rect = calcElementPosition(map.containerDivId);
    map = $find('Map1');
    var rect = calcElementPosition(map._containerDiv.id);
    
    mapRightHolder = rect.left + rect.width;
    mapBottomHolder = rect.top + rect.height;
    mapLeftHolder = rect.left;
    mapTopHolder = rect.top;
}

function CheckFloatingPanelPosition() {

    //9.3 (Min 5/26/09)
    map = $find('Map1');
    var box = calcElementPosition(map._containerDiv.id);
    
    if (box.width == 0)
        return;
        
    var mapRight = box.left + box.width;
    var mapBottom = box.top + box.height;

    for (var i = 0; i < ESRI.ADF.UI._FloatingPanels.length; i++)
    {
        var fp = ESRI.ADF.UI._FloatingPanels[i];
        if (fp!=null && fp.length>0 && !FloatingPanels[fp].docked)
        {
            var rect = calcElementPosition(fp);
            if (rect.width == 0)
                continue;
              
            var newLeft = rect.left;
            var newTop = rect.top;
            var doMove = false;
              
            var panelRight = rect.left + rect.width;
            var panelBottom = rect.top + rect.height;
              
            var deltaRight = mapRightHolder - panelRight;
            var deltaBottom = mapBottomHolder - panelBottom;
             
            if (panelRight > mapRight || Math.abs(deltaRight) < 15)
            {
                newLeft = mapRight - rect.width - 10;
                doMove = true;
            }
            
            if (panelBottom > mapBottom || Math.abs(deltaBottom) < 15)
            {
                newTop = mapBottom - rect.height - 10;
                doMove = true;
            }  
            
            if (doMove)
            {
                if (fp == 'Magnifier1')
                {
                    var mag = esriMagnifiers[fp];
                    mag.hideMapImage();
                }
                
                floatingPanel = document.getElementById(fp);                
                startWindowDrag(fp);
                moveTo(newLeft, newTop);
                endWindowDrag(null);
            }                    
        }
    }
}

function ShowMap(mapDivId, placeHolderDivId) {

    var mapDiv = document.getElementById(mapDivId);
    if (mapDiv != null)
    	mapDiv.className = '';

    var placeHolderDiv = document.getElementById(placeHolderDivId);    
    if (placeHolderDiv != null)
    	placeHolderDiv.className = 'hide';   
}

function ConstrictPanel(elementID, resizeElementID, maxheight)
{
    var resElement = document.getElementById(resizeElementID);
    
    var divElement = document.getElementById(elementID);
    var currentHeight = 0;
    
    for(var i=0; i < divElement.childNodes.length; i++)
    {
		var child = divElement.childNodes[i];
		if (child.tagName == 'DIV')
		{
		    currentHeight += child.offsetHeight;
		}
	}
    
    if (currentHeight > maxheight)
    {        
        divElement.style.width = '100%';
        divElement.style.height = '100%';
        resElement.style.height = maxheight + 'px';
        resElement.style.overflow = 'auto';
    }
    else
    {
        resElement.style.height = currentHeight + 'px';
        resElement.style.overflow = 'hidden';      
    }
}

function ArrayElementSize(DivArray)
{
    var elementSize = new Array();
    elementSize[0] = 0;
    elementSize[1] = 0;
    
    for (i=0; i<DivArray.length; i++)
    { 
        if (DivArray[i])
        {
            elementSize[0] += DivArray[i].offsetWidth * 1;
            elementSize[1] += DivArray[i].offsetHeight * 1;
        }
    }
    
    return elementSize;
}



/////////////////// Busy Indicator - show pending tiles in window.status /////////////
var statusTimeout;

function BusyIndicator_Initialize()
{
    map.pendingTiles.add_onRequestsPending(showBusyIndicator);
    map.pendingTiles.add_onRequestsRemove(showPendingTiles);
    map.pendingTiles.add_onRequestsCompleted(hideBusyIndicator);
}

function showBusyIndicator(sender)
{
    //showLayer('BusyIndicator');
    if (sender != null)
    {
        showPendingTiles(sender);
    }  
}

function showPendingTiles(sender)
{
    if (sender != null)
    {
        window.clearTimeout(statusTimeout);
        window.status = 'Pending Tiles: ' + sender.pendingTiles.length;
    }
}

function hideBusyIndicator(sender)
{
    //hideLayer('BusyIndicator');
    if (sender != null)
    {
        window.status = 'Map Download Complete';
        //statusTimeout = window.setTimeout('ClearStatus()', 2000);
    }
}

function ClearStatus()
{
    if (window.status == 'Map Download Complete')
        window.status = '';
}

///////////////////////////////////////////////////////////////////////////////////////
