
// Needs object of OGCDataHolder, TocData, Name of the variable holding this object, cfgParams, and Id of 
// an object which will be used for assigning hspc and vspc.

function MapData (pOgcData, pId, pCfgParams, isPortal) {

   this.hspc = -1;// horizontal map offset
   this.vspc = -1;// vertical map offset

   this.id      = pId;
   this.ogcData = pOgcData;
   this.tocData = null;// Must be assigned later.

   this.tbarActive = (pCfgParams.displayToolbar == 'y' ? true : false);

   this.counter     = 0;

   // ------------ Map variables ---------------------------

   this.tmod = 1;
   this.toolname = 'zoomin';
   this.cursor   = 'crosshair';

   this.startLeft   = pCfgParams.bbox.minx;
   this.startRight  = pCfgParams.bbox.maxx;
   this.startTop    = pCfgParams.bbox.maxy;
   this.startBottom = pCfgParams.bbox.miny;

   this.lastLeft   = pCfgParams.bbox.minx;
   this.lastRight  = pCfgParams.bbox.maxx;
   this.lastTop    = pCfgParams.bbox.maxy;
   this.lastBottom = pCfgParams.bbox.miny;

   this.cfgParams   = pCfgParams;

   this. aspectRatio = Math.abs (this.startLeft - this.startRight)/ Math.abs (this.startTop - this.startBottom);

    // map extents. . . dynamically updated
   this.eLeft = this.startLeft;
   this.eRight = this.startRight;
   this.eTop = this.startTop;
   this.eBottom = this.startBottom;

    // CUSTOMIZE: You can assign different full-extent coordinates.
   this.fullLeft = this.eLeft;
   this.fullRight = this.eRight;
   this.fullTop = this.eTop;
   this.fullBottom = this.eBottom;

   //CUSTOMIZE: You can assign different limit or max extents
   this.limitLeft = this.eLeft;
   this.limitRight = this.eRight;
   this.limitTop = this.eTop;
   this.limitBottom = this.eBottom;


   // map size . . . dynamically updated
   this.iWidth  = 512;
   this.iHeight = 305;

   // Map height and width in pixels. CUSTOMIZE: TBD: These may be dynamically assigned.
   this.mWidth = 512;
   this.mHeight= 305;

   // Map pane height and width
   this.pWidth = 520;
   this.pHeight= 310;

    this.xDistance = Math.abs(this.eRight-this.eLeft);
    this.yDistance = Math.abs(this.eTop-this.eBottom);

    this.pixelX = this.xDistance/this.iWidth;
    this.pixelY = this.yDistance/this.iHeight;

    this.mapX = this.eLeft;
    this.mapY = this.eTop;

    this.fullWidth = Math.abs(this.fullRight - this.fullLeft);
    this.fullHeight = Math.abs(this.fullTop - this.fullBottom);

    this.mapScaleFactor = 0;
    this.myscale = 0;

    this.toolMode = 1;

   //------------------ Click and mouse position -----
    // Global vars to save mouse position
    this.mouseX=0;
    this.mouseY=0;
    this.x1=0;
    this.y1=0;
    this.x2=0;
    this.y2=0;
    this.zleft=0;
    this.zright=0;
    this.ztop=0;
    this.zbottom=0;

    this.totalMeasure=0;
    this.currentMeasure=0;
    this.lastTotMeasure=0;

    // variables for interactive clicks
    this.clickCount = 0;
    this.clickPointX = new Array();
    this.clickPointY = new Array();
    this.clickMeasure = new Array();

    // type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
    this.clickType = 1;

    this.panning=false;
    this.zooming=false;

   // ------------ Map functions ---------------------------

   this.getMapLayers = o_getMapLayers;
   this.boxIt = o_boxIt;

   // Found in navigation.js
   this.updateMapImages = o_updateMapImages;
   this.resetBoxes      = o_resetBoxes;
   this.setVariables = o_setVariables;
   this.aspectRatioCorrection = o_aspectRatioCorrection;

   this.getMapXY = o_getMapXY;

   if (isPortal == true) {
       if (gIsNav)
           this.getImageXY = o_getImageXYNAV;
       else
           this.getImageXY = o_getImageXYOTHERS;
   }
   else {
       if (gIsNav)
           this.getImageXY = o_getImageXYNAV2;

       else
           this.getImageXY = o_getImageXYOTHERS;
   }

   this.mouseStuff = o_mouseStuff;
   this.stopZoomBox = o_stopZoomBox;
   this.startZoomBox = o_startZoomBox;
   this.stopZoomOutBox = o_stopZoomOutBox;

   this.setClip = o_setClip;
   this.startPan = o_startPan;
   this.stopPan = o_stopPan;
   this.identify = o_identify;
   this.setZoomColor = o_setZoomColor;

   this.startZoomOutBox = o_startZoomOutBox;
   this.stopZoomOutBox = o_stopZoomOutBox;

   //------ Found in map.js
   this.saveLastExtent = o_saveLastExtent;
   this.fullExtent = o_fullExtent;
   this.startExtent = o_startExtent;
   this.zoomBack = o_zoomBack;
   this.zoomToEnvelope = o_zoomToEnvelope;
   this.zoomScale = o_zoomScale;
   this.setExtent = o_setExtent;
   this.setFullExtent = o_setFullExtent;
   this.checkFullExtent = o_checkFullExtent;
   this.showRetrieveData = o_showRetrieveData;
   this.hideRetrieveData = o_hideRetrieveData;
   this.showRetrieveMap = o_showRetrieveMap;
   this.hideRetrieveMap = o_hideRetrieveMap;
   this.calcDistance = o_calcDistance;
   this.convertUnits = o_convertUnits;

   //-------- Found in click.js ---
   this.clickFunction = o_clickFunction;
   this.updateMeasureBox = o_updateMeasureBox;
   this.getMeasureBoxHtml = o_getMeasureBoxHtml;
   this.changeUnitsIE = o_changeUnitsIE;
   this.changeUnits = o_changeUnits;
   this.convertDistance = o_convertDistance;
   this.clickAddPoint = o_clickAddPoint;
   this.resetClick = o_resetClick;
   this.setCursor = o_setCursor;
}

//--------------- draw map layers ----------------------------

function o_getMapLayers (tocData) 
{

    var j= tocData.groupLayerCounts + 1;// Total number of layer. Creating layers and giving them a name is
				       // reversed so that top TOC layer become most visible layer
                                       // Extra layer it can be for ArcIMS generated map or any other map of your choice
                                       // for pan operations etc.

    var pixelUrl = gMyMTKHost+ 'images/pixel.gif';

    var layerHtml = '';

    // NOTE: Layers must be created in the reverse order initially
    // displayed on the TOC. or this will not work.

    //*********************************************************************
    //*  START of Code to Add OGC Permanent Foreground Layers    *
    //*********************************************************************

    // This code simply loops through
    // the array of permanent foregrounds creating a layer for each.

    var content;

    for (var i=this.ogcData.OGCPermNumForegrounds-1;i>=0;i--) {

            j = j - 1;
	    content = '<img name="theImage' + this.id + j + '" alt="" src="' +pixelUrl+'" hspace=0 vspace=0 border=0 width=' + this.mWidth + ' height=' + this.mHeight +' >';

            // First time all created layers are not visible. They shall be visible when Map Pane is displayed
	    layerHtml = layerHtml + getLayerHTML ("theMap" + this.id + j,this.hspc,this.vspc,this.mWidth,this.mHeight,false,content, " ", false);

	    this.ogcData.OGCPermFGs[i].layerPos = j;
    }

    //*********************************************************************
    //*			  END of Code to Add OGC Permanent Foreground Layers      *
    //*********************************************************************

    //*********************************************************************
    //*		  START of Code to Add OGC Dynamic Foreground Layers    *
    //*********************************************************************

    // This code simply loops through the array of dynamic foregrounds
    // creating a layer for each.

    for (var i=this.ogcData.OGCNumForegroundsMax-1; i>=0;i--) {
	    j = j -1;
	    content = '<img name="theImage' + this.id + j + '" alt="" src="' +pixelUrl+'" hspace=0 vspace=0 border=0 width=' + this.mWidth + ' height=' + this.mHeight + '> ';
	    layerHtml = layerHtml + getLayerHTML ("theMap" + this.id + j,this.hspc,this.vspc,this.mWidth,this.mHeight,true,content," ", false);
	    this.ogcData.OGCFGs[i].layerPos = j;
    }

    //*********************************************************************
    //*			  END of Code to Add OGC Dynamic Foreground Layers      *
    //*********************************************************************
    j = j-1;

    layerPos = j;// Initialize layerPos with j.

    // TBD: Check, if you can replace map.gif with a meaningful image from a OGC web map service.
    // This layer is good for when User is doing Pan. However you have to specify a OGC web map service for it.
    // Right now it is just an overhead.

    content = '<img name="theImage'+this.id + j+ '" src="'+gMyMTKHost+'images/map.gif" hspace=0 vspace=0 border=0 width=' + this.mWidth + ' height=' + this.mHeight + '> ';// NBIIOGC
    layerHtml = layerHtml + getLayerHTML ("theMap" + this.id + j,this.hspc, this.vspc,this.mWidth,this.mHeight,true,content,"", false);


    this.iWidth   = this.mWidth;
    this.iHeight  = this.mHeight;

    // zoom/selection box
    content = '<img name="zoomImageTop" src="' +pixelUrl+'" width=1 height=1>';
    layerHtml = layerHtml + getLayerHTML ("zoomBoxTop" + this.id,this.hspc,this.vspc,this.mWidth,this.mHeight,false,content,"background-color:"+gZoomBoxColor+";", false);

    content = '<img name="zoomImageLeft" src="' +pixelUrl+'" width=1 height=1>';
    layerHtml = layerHtml + getLayerHTML ("zoomBoxLeft" + this.id,this.hspc,this.vspc,this.mWidth,this.mHeight,false,content, "background-color:"+gZoomBoxColor+";", false);

    content = '<img name="zoomImageRight" src="' +pixelUrl+'" width=1 height=1>';
    layerHtml = layerHtml + getLayerHTML ("zoomBoxRight" + this.id,this.hspc,this.vspc,this.mWidth,this.mHeight,false,content, "background-color:"+gZoomBoxColor+";", false);

    content = '<img name="zoomImageBottom" src="' +pixelUrl+'" width=1 height=1>';
    layerHtml = layerHtml + getLayerHTML ("zoomBoxBottom" + this.id,this.hspc,this.vspc,this.mWidth,this.mHeight,false,content, "background-color:"+gZoomBoxColor+";", false);


    // loading splashs
    content = '<img name="LoadingData" src="'+gMyMTKHost+'images/loadData.gif" width=273 height=30 HSPACE=0 VSPACE=0 BORDER=0 ALT="Retrieving Map">';
    layerHtml = layerHtml + getLayerHTML ("LoadData" + this.id, this.hspc + gLoadBannerLeft, this.vspc + gLoadBannerTop,273,30,false,content, " ", false);

    content = '<img name="Loading" src="'+gMyMTKHost+'images/loadMap.gif" width=273 height=30 HSPACE=0 VSPACE=0 BORDER=0 ALT="Retrieving Map">';
    layerHtml = layerHtml + getLayerHTML ("LoadMap" + this.id,  this.hspc + gLoadBannerLeft, this.vspc + gLoadBannerTop,273,30,false,content, " ", false);

    // measure display
    // delete once workuing
    content = this.getMeasureBoxHtml (0, 0, 'DEGREES');// Check for right scalebar units.
    layerHtml = layerHtml + getLayerHTML ("measureBox" + this.id,this.hspc,this.vspc+this.mHeight+4,500,25,false,content, "overflow:visible", false);

    return layerHtml;
}

function o_boxIt(theLeft,theTop,theRight,theBottom) {

	clipLayer("zoomBoxTop" + this.id,theLeft,theTop,theRight,theTop+2);
	clipLayer("zoomBoxLeft" + this.id,theLeft,theTop,theLeft+2,theBottom);
	clipLayer("zoomBoxRight" + this.id,theRight-2,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom" + this.id,theLeft,theBottom-2,theRight,theBottom);	
	
	showLayer("zoomBoxTop" + this.id);
	showLayer("zoomBoxLeft" + this.id);
	showLayer("zoomBoxRight" + this.id);
	showLayer("zoomBoxBottom" + this.id);
}
