﻿function SL() { }

SL.Load = function() {

    SL.contentContainer = document.getElementById("content");
    SL.infopaneContainer = document.getElementById("infopane");
    SL.mapContainer = document.getElementById("map_container");

    SL.addressTextbox = document.getElementById("search_address");
    SL.goButton = document.getElementById("search_go");
    SL.boundariesList = document.getElementById("navigation_boundaries");

    SL.resultsP = document.getElementById("results_p");
    SL.searchingP = document.getElementById("searching_p");
    SL.errorP = document.getElementById("error_p");

    SL.addressTextbox.value = "";
    SL.addressTextbox.onkeyup = function(e) { SL.OnAddressKeyPress(e); };
    SL.goButton.onclick = function(e) { SL.Geocode(); };
    SL.boundariesList.onchange = function(e) { SL.ChangeBoundaries(); };

    SL.isSearching = false;

    SL.InitCollapsiblePanels();

    // If this line is executed before the map object
    // has been created in the mark up the map will not initialize correctly. 
    // Therefore it is necessary to set a timeout for this method to fire.
    // I've set the timeout to 500 milliseconds.
    
    setTimeout(function() { SL.SetInitialExtent(); }, 500);

    SL.Resize();
};

SL.InitCollapsiblePanels = function()
{
    SL.collapseImage = document.getElementById("collapse_image");
    SL.expandImage = document.getElementById("expand_image");
    
    SL.searchPanel = document.getElementById("search_head");
    SL.resultsPanel = document.getElementById("results_head");
    SL.helpPanel = document.getElementById("help_head");
    
    SL.searchPanel._body = document.getElementById("search_body");
    SL.searchPanel._body.style.display = "block";
    SL.searchPanel._img = SL.searchPanel.getElementsByTagName("img")[0];
    SL.searchPanel._img._panel = SL.searchPanel;
    SL.searchPanel._img.onclick = function(e) { SL.ToggleCollapsiblePanel(this._panel); };
    
    SL.resultsPanel._body = document.getElementById("results_body");
    SL.resultsPanel._body.style.display = "none";
    SL.resultsPanel._img = SL.resultsPanel.getElementsByTagName("img")[0];
    SL.resultsPanel._img._panel = SL.resultsPanel;
    SL.resultsPanel._img.onclick = function(e) { SL.ToggleCollapsiblePanel(this._panel); };
    
    SL.helpPanel._body = document.getElementById("help_body");
    SL.helpPanel._body.style.display = "block";
    SL.helpPanel._img = SL.helpPanel.getElementsByTagName("img")[0];
    SL.helpPanel._img._panel = SL.helpPanel;
    SL.helpPanel._img.onclick = function(e) { SL.ToggleCollapsiblePanel(this._panel); };
};

SL.ToggleCollapsiblePanel = function(panel)
{
    if(panel._body.style.display == "none")
    {
        panel._body.style.display = "block";
        panel._img.src = SL.collapseImage.src;
    } else {
        panel._body.style.display = "none";
        panel._img.src = SL.expandImage.src;
    }
};

SL.ShowResultsP = function(paragraph)
{
    SL.resultsP.style.display = "none";
    SL.searchingP.style.display = "none";
    SL.errorP.style.display = "none";
    
    paragraph.style.display = "block";
    
    if(SL.resultsPanel._body.style.display == "none")
    {
        SL.resultsPanel._body.style.display = "block";
        SL.resultsPanel._img.src = SL.collapseImage.src;
    }
};

SL.Resize = function()
{
    var clientSize = SL.GetClientSize();
    var bodyHeight = document.body.offsetHeight;
    var bodyWidth = document.body.offsetWidth;
    var contentHeight = SL.contentContainer.offsetHeight;
    var mapWidth = SL.mapContainer.offsetWidth;
    
    contentHeight = ((clientSize.y - bodyHeight) + contentHeight);
    mapWidth = clientSize.x - SL.mapContainer.offsetLeft;
    
    SL.contentContainer.style.height = contentHeight + "px";
    SL.infopaneContainer.style.height = contentHeight + "px";
    SL.mapContainer.style.height = contentHeight + "px";
    SL.mapContainer.style.width = mapWidth + "px";
    
    window.clearTimeout(SL.resizeTimer);
    SL.resizeTimer = window.setTimeout("SL.ResizeMap();", 500);
};

SL.ResizeMap = function() {

    var map = document.getElementById("map");

    // remove the height / width styles from the map
    if ($("#map") != null) {

        var styleArray = $("#map").attr("style").split(";");
        var newStyleArray = "";
        var styleElement = "";

        if (styleArray != null && styleArray.length > 0) {

            for (var i = 0; i < styleArray.length; i++) {

                styleElement = styleArray[i].toString().toLowerCase();

                // if the string height / width is not found within the
                // style array index i, then concatenate the string back together
                if (styleElement.indexOf("height") == -1 && styleElement.indexOf("width") == -1) {

                    if (newStyleArray.length > 0) {

                        newStyleArray += ";" + styleArray[i];
                    }
                    else {

                        newStyleArray += styleArray[i];
                    }
                }
            }
        }

        newStyleArray += ";height:" + SL.mapContainer.offsetHeight + "px;";
        newStyleArray += ";width:" + SL.mapContainer.offsetWidth + "px;";
        
        // reset the map's style without the in-line height / width
        $("#map").attr({ style: newStyleArray });
    }
};

SL.OnAddressKeyPress = function(e)
{
    var evt = SL.GetEvent(e);
    
    if(evt.keyCode == 13)
    {
        SL.Geocode();
    }
    
    return true;
};

SL.StartSearch = function()
{
    SL.isSearching = true;
    SL.goButton.disabled = true;
    SL.ShowResultsP(SL.searchingP);
};

SL.ProcessCallbackResult = function(response, context)
{
    if(SL.isSearching)
    {
        SL.goButton.disabled = false;
        SL.isSearching = false;
        
        if(SL.resultsPanel._body.style.display == "none")
        {
            SL.resultsPanel._body.style.display = "block";
            SL.resultsPanel._img.src = SL.collapseImage.src;
        }
    }
    
    /* This function implemented by ArcGIS Web ADF */
    processCallbackResult(response, context);
};

SL.PostBackError = function(argument, context)
{
    if(SL.isSearching)
    {
        SL.goButton.disabled = false;
        SL.isSearching = false;
        
        if(SL.resultsPanel._body.style.display == "none")
        {
            SL.resultsPanel._body.style.display = "block";
            SL.resultsPanel._img.src = SL.collapseImage.src;
        }
    }
    
    SL.ShowResultsP(SL.errorP);
};

SL.Geocode = function()
{
    /* These two functions and variable implemented by ArcGIS Web ADF */
    if(getSessionLapse() > maximumLapseTime)
    {
        showLapseAlert();
    } else {
        SL.StartSearch();
        /* Arguments: eventArgumentString, context (should be the ID of map control) */
        Go("geocode|" + SL.addressTextbox.value, "map");
    }
};

SL.ZoomToBoundary = function(index)
{
    /* These two functions and variable implemented by ArcGIS Web ADF */
    if(getSessionLapse() > maximumLapseTime)
    {
        showLapseAlert();
    } else {
        SL.boundariesList.selectedIndex = index;
        /* Arguments: eventArgumentString, context (should be the ID of map control) */
        Go("zoomtoboundary|" + index, "map");
    }
};

SL.ChangeBoundaries = function()
{
    /* These two functions and variable implemented by ArcGIS Web ADF */
    if(getSessionLapse() > maximumLapseTime)
    {
        showLapseAlert();
    } else {
        /* Arguments: eventArgumentString, context (should be the ID of map control) */
        Go("changeboundaries|" + SL.boundariesList.selectedIndex, "map");
    }
};

SL.GetEvent = function(evt)
{
    return ((evt) ? evt : window.event);
};

SL.GetClientSize = function()
{
    var size = {x:0, y:0};
    
    if(self.innerWidth)
    {
        size.x = self.innerWidth;
        size.y = self.innerHeight;
    } else if(document.documentElement && document.documentElement.clientWidth) {
        size.x = document.documentElement.clientWidth;
        size.y = document.documentElement.clientHeight;
    } else if(document.body) {
		size.x = document.body.clientWidth;
		size.y = document.body.clientHeight;
	}
    
    return size;
};

SL.SetInitialExtent = function() {

    var map = $find("map");

    if (map != undefined && map != null) {

        map.refresh();

        //these are hard coded values for ARJIS Map Extent
        var initialExtentEnvelope = new ESRI.ADF.Geometries.Envelope(parseInt($("#initialExtentXMin").val(), 10), parseInt($("#initialExtentYMin").val(), 10), parseInt($("#initialExtentXMax").val(), 10), parseInt($("#initialExtentYMax").val(), 10));

        map.zoomToBox(initialExtentEnvelope, true);
        //alert("Initial Extent set: Min (" + parseInt($("#initialExtentXMin").val(), 10).toString() + ", " + parseInt($("#initialExtentYMin").val(), 10) + ") Max (" + parseInt($("#initialExtentXMax").val(), 10) + ", " + parseInt($("#initialExtentYMax").val(), 10) + ")");
    }
    else {

        //alert("Initial Extent not set.");
    }
};

var __oldonload = window.onload;
var __oldonresize = window.onresize;

if(typeof window.onload != "function")
{
    window.onload = SL.Load;
    
} else {
    window.onload = function()
    {
        __oldonload();
        SL.Load();
    };
}

if(typeof window.onresize != "function")
{
    window.onresize = SL.Resize;
} else {
    window.onresize = function()
    {
        __oldonresize();
        SL.Resize();
    };
}