﻿
var IsIE = document.all?true:false

/* Height & Widths / X & Ys */
var BrowserHeight = 0;
var BrowserWidth = 0;
var MouseX = 0;
var MouseY = 0;
/* End Height & Widths / X & Ys */

/* Page HTML Elements */
var HeadPanel = null;
var PageContentWrapper = null;
var LeftPanelContainer = null;
var LeftPanel = null;
var SplitPanel = null;
var RightPanel = null;
var LeftPanelHead = null;
var LeftPanelTOC = null;
var OverviewMapRectangle = null;
var PreviousRightPanelWidth = 0;
var PreviousContentPanelHeight = 0;
/* End Page HTML Elements */

var LeftPanelMinimumWidth = 375;
var LeftPanelWidth = LeftPanelMinimumWidth;

var RightPanelLeftOffsetX = LeftPanelMinimumWidth;
var RightPanelRightOffsetX = 0;

var splitMoving = false;

var splitMovingFunction = null;

// set window resize event handler
function startUp()
{
    window.onresize = AdjustMapSizeHandler;
}


// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustSizesPostBrowserResize();",1000);
}

// function to set initial sizes of page elements
function setPageElementSizes() 
{
    // set body style 
    if (document.documentElement) {
        document.documentElement.style.overflow = "hidden";
        document.documentElement.style.height = "100%"; 
    } else {
        document.body.style.overflow = "hidden";
        document.body.style.height = "100%";
    }
    
    // Set Data Members
    // get browser window dimensions
    getBrowserDimensions();
    
    // get necessary elements
    HeadPanel = document.getElementById("_headPanel");
    PageContentWrapper = document.getElementById("_pageContentWrapper");
    LeftPanelContainer = document.getElementById("_leftPanelContainer");
    LeftPanel = document.getElementById("_leftPanelCell");
    SplitPanel = document.getElementById("_splitPanelCell");
    RightPanel = document.getElementById("_rightPanel");
    LeftPanelHead = document.getElementById("_leftPanelHeadCell");
    LeftPanelTOC = document.getElementById("_leftPanelTOCCell");
    OverviewMapRectangle = document.getElementById("_overviewMapRectangle");

    // find content area height
    var contentAreaHeight = BrowserHeight - HeadPanel.clientHeight;
    
    // set heights
    PageContentWrapper.style.height = contentAreaHeight + "px";
    LeftPanel.style.height = contentAreaHeight + "px";
    SplitPanel.style.height = contentAreaHeight + "px";
    RightPanel.style.height = contentAreaHeight + "px";
    var tocHeight = contentAreaHeight - LeftPanelHead.clientHeight;
    LeftPanelTOC.style.height = tocHeight + "px";
    
    // set widths
    RightPanel.style.width = (BrowserWidth - LeftPanelContainer.clientWidth) + "px";

    AdjustSizesPostBrowserResize();
}

function getBrowserDimensions()
{
    if(IsIE) 
    {
        //IE 6+ in 'standards compliant mode'
        BrowserHeight = document.documentElement.clientHeight;;
        BrowserWidth = document.documentElement.clientWidth;
    }
    else
    {
        //Non-IE
        BrowserHeight = window.innerHeight;
        BrowserWidth = window.innerWidth;
    }
}

function getMousePosition(e)
{
    if (IsIE) 
    { 
        // grab the x-y pos.s if browser is IE
        MouseX = event.clientX + document.body.scrollLeft
        MouseY = event.clientY + document.body.scrollTop
    } else {  
        // grab the x-y pos.s if browser is NS
        MouseX = e.pageX
        MouseY = e.pageY
    }  
}


function startSplitResizeDrag(e) {
    if (!splitMoving) 
    {
        splitMovingFunction = document.onmousemove;
        splitMoving = true;
    }
    
    document.onmouseup = stopSplitResizeDrag;
    
    if (LeftPanel.style.display != "none") 
    {
        // get browser window dimensions
        getBrowserDimensions();
        
        // set the data members of MouseX & MouseY
        getMousePosition(e); 
        
        RightPanelLeftOffsetX = MouseX - LeftPanel.clientWidth;
        
        var rightPanelPosition = findPosition(RightPanel);
        RightPanelRightOffsetX = rightPanelPosition.Left - MouseX; 
        document.onmousemove = moveSplit;
    }
    return false;  
}

function moveSplit(e) 
{
    // set the data members of MouseX & MouseY
    getMousePosition(e); 
    getBrowserDimensions(e);
    
    var eventButton = (IsIE) ? event.button : e.which;
    if (0 == eventButton) 
        stopSplitResizeDrag(e)
    
    LeftPanelWidth =  MouseX - RightPanelLeftOffsetX;
    
    if (LeftPanelWidth > (BrowserWidth - SplitPanel.clientWidth - 2))
        LeftPanelWidth = BrowserWidth - SplitPanel.clientWidth - 2;
    
    //Stop the left panel from shrinking below the minimum set value
    if (LeftPanelWidth < LeftPanelMinimumWidth) 
        LeftPanelWidth = LeftPanelMinimumWidth; 
    
    LeftPanel.style.width = LeftPanelWidth + "px";
    LeftPanelTOC.style.width = LeftPanelWidth + "px";
    
    //Make sure the right panel doesn't completely dissappear
    var rightPanelWidth =  BrowserWidth - SplitPanel.clientWidth - LeftPanelWidth; 
    if (rightPanelWidth<1) rightPanelWidth = 1; 
    
    //Set size and position of right panel
    RightPanel.style.width = rightPanelWidth + "px";
    RightPanel.style.left = (LeftPanelWidth + SplitPanel.clientWidth + 1) + "px";
    
    return false;
}

function stopSplitResizeDrag(e) 
{
    document.onmousemove = splitMovingFunction;
    
    //Detach stopSplitResizeDrag from on mouse up as we aren't moving any more.
    document.onmouseup = null;
    
    splitMoving = false;    
    
    //The following function was to resize things inside the left panel.
    //I don't think we need to do that anymore.
    //CheckLeftPanelChildWidths();
    
    AdjustSizesPostBrowserResize();  
    
    return false;
}

// function for adjusting element sizes when brower is resized
function AdjustSizesPostBrowserResize() 
{
   // set element widths 
    LeftPanel.style.width = LeftPanelWidth + "px";
    LeftPanelTOC.style.width = LeftPanelWidth + "px";
    
   // get browser window dimensions 
   getBrowserDimensions();
   
    // calc dimensions needed for right panel
    var rightPanelWidth = BrowserWidth - SplitPanel.clientWidth - LeftPanelWidth; 
    var contentPanelHeight = BrowserHeight - HeadPanel.clientHeight;
    
    if (rightPanelWidth < 5) rightPanelWidth = 5;
    if (contentPanelHeight < 5) contentPanelHeight = 5;
    
    RightPanel.style.width =  rightPanelWidth + "px";
    // Note: Don't use LeftPanelContainer.clientWidth as it could be bigger than LeftPanelWidth due to super-long layer names.
    RightPanel.style.left = (LeftPanelWidth + SplitPanel.clientWidth) + "px";
    
    // set heights on elements 
    PageContentWrapper.style.height = contentPanelHeight  + "px";
    LeftPanel.style.height = contentPanelHeight  + "px";
    SplitPanel.style.height = contentPanelHeight  + "px";
    RightPanel.style.height = contentPanelHeight  + "px";
    var tocHeight = contentPanelHeight - LeftPanelHead.clientHeight;
    LeftPanelTOC.style.height = tocHeight + "px";
    
    // resize the map 
    window.setTimeout("resizeTheMap(" + rightPanelWidth + ", " + contentPanelHeight + ", false);", 500);    
    
    // update the OverviewMapRectangle size - note this doesn't always work because the OverviewMapRectangle is in an UpdatePanel.
    if (false)
    {
        if ((PreviousRightPanelWidth != 0) && (PreviousRightPanelWidth != rightPanelWidth))
        {
            var previousOverviewMapWidth = parseInt(OverviewMapRectangle.style.width);
            if (previousOverviewMapWidth > 0)
            {
                var overviewMapWidth = (rightPanelWidth / PreviousRightPanelWidth) * previousOverviewMapWidth;
                
                // Never make the rectangle bigger than full extent
                if (overviewMapWidth > 180) overviewMapWidth = 180;
                
                OverviewMapRectangle.style.width = overviewMapWidth + "px";
            }
        }
        PreviousRightPanelWidth = rightPanelWidth;
        
        if ((PreviousContentPanelHeight != 0) && (PreviousContentPanelHeight != contentPanelHeight))
        {
            var previousOverviewMapHeight = parseInt(OverviewMapRectangle.style.height);
            if (previousOverviewMapHeight > 0)
            {
                var overviewMapHeight = (contentPanelHeight / PreviousContentPanelHeight) * previousOverviewMapHeight;
                
                // Never make the rectangle bigger than full extent
                if (overviewMapHeight > 124) overviewMapHeight = 124;
                
                OverviewMapRectangle.style.height = overviewMapHeight + "px";
            }
        }
        PreviousContentPanelHeight = contentPanelHeight;
    }
    
    return false;
}

/* Element Position Object */
function EmentPosition(top, left)
{
    this._top = top;
    this._leftPos = left;
}
EmentPosition.prototype._left;
EmentPosition.prototype.Left = function(){ return this._left; }
EmentPosition.prototype._top;
EmentPosition.prototype.Top = function(){ return this._top; }
/* End Element Position Object */

function findPosition(obj)
{
    var currentLeft = 0;
    var currentTop = 0;
    
    if (obj.offsetParent) 
    {
        do {
			currentLeft += obj.offsetLeft;
			currentTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
    }
    
    return new EmentPosition(currentTop, currentLeft);
}

// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {
    if (resizeExtent==null) 
    {
        resizeExtent = true;
    }
    
    // See http://quomon.com/question_how_check_javascript_variable_defined_891.aspx
    if (window.map != undefined)
    {
        map.resize(width, height, resizeExtent);
    }
    else
    {
        var breakpoint = true;
    }
    
    return false; 
}