//Extent History Objects and Functions
// Variables to store the current Map Extents to be use to calculate the current Map Cordinates on the Browser 
// Status Bar.
var currentMapMinX, currentMapMaxX, currentMapMinY, currentMapMaxY;

function EnvelopeObject()
{
    this.minx = 0;
    this.miny = 0;
    this.maxx = 0;
    this.maxy = 0;
}

function SavedView()
{
    this.MapService = "";
    this.Extent = new EnvelopeObject();
    this.LayerIDs = new Array();
    this.LayerVisibility = new Array();
}


function ExtentObject(maxNumberExtents)
{
    this.PreviousExtentArray = new Array();
    this.NextExtentArray = new Array();
    this.maxExtents = maxNumberExtents;
    this.DisableZoomNext = true;
    this.DisableZoomPrevious = true;

    //Functions
    
    this.SaveExtent = function(minx, miny, maxx, maxy){
        
        //If Current Extent Exists
        if (currentExtent != null)
        {
            //Verify if the Extent is the same as the current Extent
            if (!this.RepeatedExtent(minx, miny, maxx, maxy))
            {
                //Save Current Extent
                if (previousNextExtentAction == "")
                {
                    previousNextExtentAction = "";
                    this.PreviousExtentArray.push(currentExtent);
                    this.NextExtentArray = new Array();
                    
                    //If number of extents is greater than the maximum number of extents to be stored
                    //Remove the oldest one
                    if (this.PreviousExtentArray.length > this.maxExtents)
                    {
                        var removeExtent = new EnvelopeObject();
                        removeExtent = this.PreviousExtentArray.shift();
                        removeExtent = null;
                    }
                }
                //Move through extent history array -> Does not store a new extent
                else if (previousNextExtentAction == "NEXT")
                {
                    previousNextExtentAction = "";
                    this.PreviousExtentArray.push(currentExtent);
                }
                //Move through extent history array -> Does not store a new extent
                else if (previousNextExtentAction == "PREV")
                {
                    previousNextExtentAction = "";
                    this.NextExtentArray.push(currentExtent);
                }

                // Enable/Disable Next Extent
                if (this.NextExtentArray.length > 0)
                {
                    this.DisableZoomNext = false;
                }
                else
                {
                    this.DisableZoomNext = true;
                }

                // Enable/Disable Previous Extent
                if (this.PreviousExtentArray.length > 0)
                {
                    this.DisableZoomPrevious = false;
                }
                else
                {
                    this.DisableZoomPrevious = true;
                }
            }
        }
        //Create Current Extent
        currentExtent = new EnvelopeObject();
        
        //Save New Current Extent
        currentExtent.minx = minx;
        currentExtent.miny = miny;
        currentExtent.maxx = maxx;
        currentExtent.maxy = maxy;
    }

    // Check if same extent as current extent
    // Added by Castillo 10/01/2007
    // It seems that page is going many times though Map_ExtentChanged Method which calls the save extent function,
    // resulting in multiple almost exact extents saved.
    this.RepeatedExtent = function(minx, miny, maxx, maxy)
    {
        var sameExtent = false;
        var repeatedExtent = 0;
        
        if (currentExtent.minx == minx)
        {
            repeatedExtent++;
        }
        if (currentExtent.miny == miny)
        {
            repeatedExtent++;
        }
        if (currentExtent.maxx == maxx)
        {
            repeatedExtent++;
        }
        if (currentExtent.maxy == maxy)
        {
            repeatedExtent++;
        }
        
        if (repeatedExtent >= 2)
        {
            sameExtent = true;
        }
        
        return sameExtent;

    }
    
    // Get Previous Extent
    this.GetPreviousExtent = function(){
        if ((this.PreviousExtentArray) && (!this.DisableZoomPrevious))
        {
            if(this.PreviousExtentArray.length > 0)
            {

                // Pop envelope from Previous
                pExtent = new EnvelopeObject();
                pExtent = this.PreviousExtentArray.pop();

                //Enable/Disable Previous Zoom                
                if (this.PreviousExtentArray.length == 0) 
                {
                    this.DisableZoomPrevious = true;
                }
                else
                {
                    this.DisableZoomPrevious = false;
                }
                
                //Enable Next Zoom                
                this.DisableZoomNext = false;
                
                // Return the popped extent
                return pExtent;
                
            }
            else
            {
                this.DisableZoomPrevious = true;
                return;
            }

        }
        else
        {
            this.DisableZoomPrevious = true;
            return;
        }
    }

    // Get Previous Extent
    this.GetNextExtent = function(){
        if ((this.NextExtentArray) && (!this.DisableZoomNext))
        {
            if(this.NextExtentArray.length > 0)
            {

                // Pop envelope from Previous
                pExtent = new EnvelopeObject();
                pExtent = this.NextExtentArray.pop();
                
                //Enable/Disable Next Zoom                
                if (this.NextExtentArray.length == 0) 
                {
                    this.DisableZoomNext = true;
                }
                else
                {
                    this.DisableZoomNext = false;
                }
                
                //Enable Previous Zoom                
                this.DisableZoomPrevious = false;

                // Return the popped extent
                return pExtent;
                
            }
            else
            {
                this.DisableZoomNext = true;
                return;
            }

        }
        else
        {
            this.DisableZoomNext = true;
            return;
        }
    }
    
}

//========================================================================================================
//                          ZOOM TO PREVIOUS AND NEXT EXTENT - FUNCTIONS                                //
//========================================================================================================

// TODO: Call it from default.aspx
// Create Extent History Object
var mapExtentHistory = new ExtentObject(5);
var currentExtent = null;
var previousNextExtentAction = "";

// <summary>
// Zooms to Previous Map Extent
// </summary>
// <history>
// Juliana C. – Woolpert, Inc. (Created) 3/22/2007
// </history>
function PreviousExtent()
{

    if (getSessionLapse() < maximumLapseTime) {
        //reset ESRI session counter 
        lastResponseReceivedTime=new Date();

        if (!mapExtentHistory.DisableZoomPrevious)
        {
           ShowLoading();
                
            var extentToZoom = mapExtentHistory.GetPreviousExtent();
            previousNextExtentAction = "PREV";
            //alert("Zoom to Previous Extent");    
            var argument = "ControlType=Map&Eventarg=PreviousExtent&MinX=" + extentToZoom.minx + "&MinY=" + extentToZoom.miny + "&MaxX=" + extentToZoom.maxx + "&MaxY=" + extentToZoom.maxy;
	        var context = map.controlName;
	        eval(extentHistoryCallback);
        }
        else
        {
            previousNextExtentAction = "";
            Ext.MessageBox.alert('Zoom to Previous Extent', 'There is no previous extent defined.', null);        
        }

    }
    else
    {
        showLapseAlert();
    }
}

// <summary>
// Zooms to Next Map Extent
// </summary>
// <history>
// Juliana C. – Woolpert, Inc. (Created) 3/22/2007
// </history>
function NextExtent()
{
    if (getSessionLapse() < maximumLapseTime) {
        //reset ESRI session counter 
        lastResponseReceivedTime=new Date();

        if (!mapExtentHistory.DisableZoomNext)
        {
        
            ShowLoading();    
        
            var extentToZoom = mapExtentHistory.GetNextExtent();
            previousNextExtentAction = "NEXT";
            var argument = "ControlType=Map&Eventarg=NextExtent&MinX=" + extentToZoom.minx + "&MinY=" + extentToZoom.miny + "&MaxX=" + extentToZoom.maxx + "&MaxY=" + extentToZoom.maxy;
	        var context = map.controlName;
	        eval(extentHistoryCallback);
        }
        else
        {
            previousNextExtentAction = "";
            Ext.MessageBox.alert('Zoom to Next Extent', 'There is no next extent defined.', null);        
        }

    }
    else
    {
        showLapseAlert();
    }
}

