﻿var pageWidth;
var pageHeight;

// -----------------------------------------------
//                     expand
// Method to expand an envelope defined by the xmin,
// xmax, ymin and ymax fields.  Use the factor
// parameter to determine the expand factor.
// -----------------------------------------------
function expand(xmin, xmax, ymin, ymax, factor)
{
    var newXmin;
    var newXmax;
    var newYmin;
    var newYmax;
    var diffX;
    var diffY;
    var deltaX;
    var deltaY;

    // Calculate the new extent
    diffX = parseFloat(xmax) - parseFloat(xmin);
    diffY = parseFloat(ymax) - parseFloat(ymin);

    deltaX = parseFloat(factor) * diffX;
    deltaY = parseFloat(factor) * diffY;

    newXmin = parseFloat(xmin) - deltaX;
    newXmax = parseFloat(xmax) + deltaX;
    newYmin = parseFloat(ymin) - deltaY;
    newYmax = parseFloat(ymax) + deltaY;

    // Return an envelope that was expand by the percentage determined
    // in the factor parameter
    return new ESRI.ADF.Geometries.Envelope(parseFloat(newXmin), parseFloat(newYmin), parseFloat(newXmax), parseFloat(newYmax))
}

// -----------------------------------------------
//                   getWidth
// -----------------------------------------------
function getWidth()
{
    // get browser window dimensions
    pageWidth = window.innerWidth;
    if (pageWidth == null)
    {
        if (document.documentElement && document.documentElement.clientWidth) {
            pageWidth = document.documentElement.clientWidth
        }
        else 
        {
            pageWidth = document.body.clientWidth;
        }
    }
}

// -----------------------------------------------
//                   getHeight
// -----------------------------------------------
function getHeight()
{
    
    pageHeight = window.innerHeight;
    if (pageHeight == null)
    {
        if (document.documentElement && document.documentElement.clientHeight)
        {
            pageHeight = document.documentElement.offsetHeight;
        }
        else
        {
            pageHeight = document.body.clientHeight;
        }
    }
}

// -----------------------------------------------
//                resizeMapContent
// -----------------------------------------------
function resizeMapContent()
{
    //Call the function to resize the left panel
    resizeLeftPanel();

    //Call the function to resize Map
    resizeMap();   
}

// -----------------------------------------------
//                 resizeLeftPanel
// -----------------------------------------------
function resizeLeftPanel()
{
    getHeight();

    //Calculate left panel height
    //Get the left panel object
    var leftPanel = document.getElementById("NewLeftPanel");

    var leftPanelHeight = pageHeight - leftPanel.offsetTop - 2;

    if (leftPanelHeight < 425)
    {
        leftPanelHeight = 425;
    }

    //Asign left panel height
    leftPanel.style.height = leftPanelHeight + "px";

    resizeResultPanel();
}

// -----------------------------------------------
//                resizeResultPanel
// -----------------------------------------------
function resizeResultPanel()
{
    getHeight;

    var resultsPanel = document.getElementById("Results");

    // Check if we are in IE or Firefox
    var msie = (document.all && !window.opera) ? true : false;
    
    // Calculate the top offset above the result panel
    var resultsPanelOffsetTop = 0;
    if (msie)
    {
        // In IE, add the parent node offset
        resultsPanelOffsetTop = resultsPanel.offsetTop + resultsPanel.parentNode.offsetTop;
    }
    else
    {
        // Firefox, the top offset includes the parent node
        resultsPanelOffsetTop = resultsPanel.offsetTop
    }
    
    // Calculate the new height of the result panel
    var finalHeight = pageHeight - resultsPanelOffsetTop - 4;

    // In case we get a negative value, when the browser window is to
    // small, we set a minimum value of 50 pixels.
    if (finalHeight < 50)
    {
        finalHeight = 50;
    }
    
    //Asign results panel height
    resultsPanel.style.height = finalHeight + "px";
}

// -----------------------------------------------
//                  resizeMap
// -----------------------------------------------
function resizeMap()
{
    getWidth();
    
    //Get the map object
    var Map1 = document.getElementById("Map1");        
    //Get the map panel object
    var mapPanel = document.getElementById("MapHandler");

    //Calculate the width of the map
    var mapWidth = pageWidth - mapPanel.offsetLeft
    if (mapWidth < 400)
    {
        mapWidth = 400;
        var newPageWidth = mapPanel.offsetLeft + 400;
        var headerPanel = document.getElementById("header");
        var toolBarPanel = document.getElementById("LinkBar");
        headerPanel.style.width = newPageWidth + "px";
        toolBarPanel.style.width = newPageWidth + "px";
    }
    else
    {
        var headerPanel = document.getElementById("header");
        var toolBarPanel = document.getElementById("LinkBar");
        headerPanel.style.width = pageWidth + "px";
        toolBarPanel.style.width = pageWidth + "px";
    }

    Map1.style.width = mapWidth + "px";
    mapPanel.style.width = mapWidth + "px";


    //Get the left panel object
    var leftPanel = document.getElementById("NewLeftPanel");
    mapPanel.style.height = leftPanel.offsetHeight + "px";
    Map1.style.height = leftPanel.offsetHeight + "px";
}

// -----------------------------------------------
//                  bDetection
// -----------------------------------------------
function bDetection()
{
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
    { //test for MSIE x.x;
        var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion >= 8)
            browserType = "IE7";
        else if (ieversion >= 7)
            browserType = "IE7";
        else if (ieversion >= 6)
            browserType = "IE6";
    }
    else
    {
        browserType = "other";
    }

}

// -----------------------------------------------
//                 showPopUpInfo
// -----------------------------------------------
function showPopUpInfo(e)
{
    //Line that takes in consideration if the browser is IE or FF
    //in the case of IE, the e arguments should be empty and then
    //it takes the window.event. If the browser is FF, the e argument
    //will contain the event. The rest of the codes also take in 
    //consideration from wich browser the code is excute.
    var evt = e || window.event;

    var posx = 0;
    var posy = 0;
    
    if (evt.pageX || evt.pageY)
    {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY)
    {
        posx = evt.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
    }
   
    // posx and posy contain the mouse position relative to the document
    // Do something with this information
    var popUpInfo = document.getElementById("popUpInfo");

    //Add a gap between the pointer and the popup
    posx = posx + 5;
    posy = posy + 5;


    //popUpInfo.style.height = leftPanelHeight + "px";
    popUpInfo.style.display = '';
    popUpInfo.style.top = posy + "px";
    popUpInfo.style.left = posx + "px";
}

// -----------------------------------------------
//                 hidePopUpInfo
// -----------------------------------------------
function hidePopUpInfo()
{
    var popUpInfo = document.getElementById("popUpInfo");
    popUpInfo.style.display = 'none';
}

//------------------------------------------------
//           showPopupGlossary
// Function fired when link  
// is clicked, open popupextender
//------------------------------------------------
function showPopupGlossary(id, element, offSetX, evt)
{
    var Popup = $find('GlossaryPopupControl_' + id);
    
    // Set offSet X 
    Popup.set_OffsetX(offSetX);
    
    // set the parentElementID to the parentID...the popup is positioned on this
    Popup._popupBehavior.set_parentElement(element);

    // show
    evt.cancelBubble = true;
    Popup.showPopup();

}

// -----------------------------------------------
//             HideResource
// Method used to show or hide map and satellite image
// -----------------------------------------------
function HideResource(clickedButton)
{
    if (clickedButton.style.fontWeight == 'bold')
    {
        return;
    }

    // Recive toc object
    var toc = $find("Toc1");

    // Set variable of service name
    var mapServiceName = "sama_web_fills";
    var satelliteServiceName = "sama_web_flysask";
    
    // Declare service div variable
    var mapServiceDivId
    var satelliteServiceDivId

    // Get the Hidden box object
    var HBX_ButtonClicked = document.getElementById('HBXButtonClicked');

    // Get all the child nodes of the toc contents
    var serviceNodes = document.getElementById("Toc1_contents").childNodes;

    // For each child nodes
    for (i = 0; i < serviceNodes.length; i++)
    {

        // Format the inner text to identiy the title of each service
        var _innerText = serviceNodes[i].innerText || serviceNodes[i].textContent;
        _innerText = _innerText.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\n/g, '').replace(/\r/g, '');


        // If it's the map service
        if (_innerText == mapServiceName)
        {
            // Get the id of the div
            mapServiceDivId = serviceNodes[i].id;
        }

        // If it's the satellite service
        if (_innerText == satelliteServiceName)
        {
            // Get the id of the div
            satelliteServiceDivId = serviceNodes[i].id;
        }

    }

    if (!(satelliteServiceDivId && mapServiceDivId))
    {
        // Don't go any further if we didn't find the 2 divs
        return;
    }

    // Depending on the context 
    if (clickedButton.value == "Map" )
    {

        // Set the value of the hidden box
        HBX_ButtonClicked.value = clickedButton.value;

        //Turn of satellite 
        document.getElementById(satelliteServiceDivId + '_CheckBox').checked = false;
        toc._nodeChecked(satelliteServiceDivId);

        //Turn on map
        document.getElementById(mapServiceDivId + '_CheckBox').checked = true;
        toc._nodeChecked(mapServiceDivId);
    }
    else if (clickedButton.value == "Satellite")
    {

        //Set value to the hbx_button
        HBX_ButtonClicked.value = clickedButton.value;

        //Turn on map
        document.getElementById(mapServiceDivId + '_CheckBox').checked = false;
        toc._nodeChecked(mapServiceDivId);

        //Turn off satellite 
        document.getElementById(satelliteServiceDivId + '_CheckBox').checked = true;
        toc._nodeChecked(satelliteServiceDivId);
   
    }
}


