//MapPanel.js

MapPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject,newMapId)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject,newMapId);
};

MapPanel.prototype   = new GuiWidget();
MapPanel.constructor = MapPanel;
MapPanel.superclass  = GuiWidget.prototype;

MapPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject,newMapId)
{
  MapPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "mapPanel");
  this.clickMode = '';
  if (newMapId!=null)
  {
    this.map = OBJECT_MANAGER.getControl(newMapId);
    this.mapId = newMapId;
  }
  else
  {
    this.map = newMapObject;
    this.mapId = newMapObject.id;
  }
  //Map frame objects
  this.mapFrameUL = new GuiWidget(this.id+"ULCORNER", this.element, 0, 0, 0, 25, 25, true, "mapUL");
  this.mapFrameUC = new GuiWidget(this.id+"UCENTER",  this.element, 25, 0, 0, this.width() - 50, 25, true, "mapUC");
  this.mapFrameUR = new GuiWidget(this.id+"URCORNER", this.element, this.width()-25, 0, 0, 25, 25, true, "mapUR");
  this.mapFrameCL = new GuiWidget(this.id+"ULCORNER", this.element, 0, 25, 0, 25, this.height() - 50, true, "mapCL");
  this.mapFrameCR = new GuiWidget(this.id+"URCORNER", this.element, this.width()-25, 25, 0, 25, this.height() - 50, true, "mapCR");
  this.mapFrameBL = new GuiWidget(this.id+"BLCORNER", this.element, 0, this.height() - 25, 0, 25, 25, true, "mapBL");
  this.mapFrameBC = new GuiWidget(this.id+"BCENTER",  this.element, 25, this.height() - 25, 0, this.width() - 50, 25, true, "mapBC");
  this.mapFrameBR = new GuiWidget(this.id+"BRCORNER", this.element, this.width()-25, this.height()-25, 0, 25, 25, true, "mapBR");

  //Pan arrow buttons
  this.panUL = new ImageButton(this.id+"panUL", this.mapFrameUL.element, 0, 0, 0, 25, 25, true, "mapULArrow", "");
  this.panUL.clickEvent = function(e)
  {
    this.map.pan('NW');
  };

  this.panUC = new ImageButton(this.id+"panUC", this.mapFrameUC.element, 0, 0, 0, 100, 25, true, "mapUCArrow", "");
  this.panUC.element.cbe.moveTo("N");
  this.panUC.clickEvent = function(e)
  {
    this.map.pan('N');
  };
  this.panUR = new ImageButton(this.id+"panUR", this.mapFrameUR.element, 0, 0, 0, 25, 25, true, "mapURArrow", "");
  this.panUR.clickEvent = function(e)
  {
    this.map.pan('NE');
  };

  this.panCL = new ImageButton(this.id+"panCL", this.mapFrameCL.element, 0, 0, 0, 25, 100, true, "mapCLArrow", "");
  this.panCL.element.cbe.moveTo("CEN");
  this.panCL.clickEvent = function(e)
  {
    this.map.pan('W');
  };

  this.panCR = new ImageButton(this.id+"panCR", this.mapFrameCR.element, 0, 0, 0, 25, 100, true, "mapCRArrow", "");
  this.panCR.element.cbe.moveTo("W");
  this.panCR.clickEvent = function(e)
  {
    this.map.pan('E');
  };

  this.numZoomBarDivs = (this.panCR.element.cbe.top() - 50) / 11;
  this.zoomBar = new ZoomBar(this.id+"zoomBar", this.mapFrameCR.element, 0, 0, 1, true, this.map, 0.01, this.numZoomBarDivs);

  this.panBL = new ImageButton(this.id+"panBL", this.mapFrameBL.element, 0, 0, 0, 25, 25, true, "mapBLArrow", "");
  this.panBL.clickEvent = function(e)
  {
    this.map.pan('SW');
  };

  this.panBC = new ImageButton(this.id+"panBC", this.mapFrameBC.element, 0, 0, 0, 100, 25, true, "mapBCArrow", "");
  this.panBC.element.cbe.moveTo("S");
  this.panBC.clickEvent = function(e)
  {
    this.map.pan('S');
  };

  this.panBR = new ImageButton(this.id+"panBR", this.mapFrameBR.element, 0, 0, 0, 25, 25, true, "mapBRArrow", "");
  this.panBR.clickEvent = function(e)
  {
    this.map.pan('SE');
  };

  //Element containing the map image and toolbar
  this.mapWindow = new GuiWidget(this.id+"MAPWINDOW", this.element, 25, 25, 0, this.width() - 50, this.height() - 50, true, "mapWindow");

  this.toolbarRadioButtonGroup = new RadioButtonGroup(this.id+".toolbarRadioButtonGroup");
  this.toolbar = new Panel(this.id+".toolbar", this.mapWindow.element, 25, 0, 1, this.width() - 50, 27, true, "mapToolbar", false);
  this.toolbar.element.cbe.moveTo("N");

  this.statusbar = new Panel(this.id+".statusbar", this.mapWindow.element, 0, this.mapWindow.height() - 13, 1, this.mapWindow.width(), 13, true, "mapStatusBar", false);
  this.statusbar.element.cbe.moveTo("S");
  this.statusbar.element.cbe.innerHtml("Powered by <i>Freeance</i> - <a href='http://www.tdc-group.com'>TDC Group Inc</a>");
  this.toolbarLeftButtonPanel = new Panel(this.id+".toolbarLeftButtonPanel", this.toolbar.element, 8, 0, 0, 180, 27, true, "mapLeftToolbar", false);
  this.toolbarLeftButtonPanel.element.cbe.moveTo("NW");

  var toolbarXOffset = 0;
  if ((OBJECT_MANAGER.getGuiValueById('extensions')) && (OBJECT_MANAGER.getGuiValueById('extensions')['bookmarks']))
    if (OBJECT_MANAGER.getGuiValueById('extensions')['bookmarks'].enabled)
    {
      this.bookmarkButton = new ImageButton(this.id+".addBookmarkButton", this.toolbarLeftButtonPanel.element, toolbarXOffset, 0, 0, 25, 25, true, "bookmark", "Add Bookmark");
      this.bookmarkButton.clickEvent = function()
      {
        bookmarkName = prompt('Enter a name for the new bookmark','');
        if (bookmarkName != null)
          if (bookmarkName == '')
            alert('You must enter a name!');
          else
            this.map.addBookmark(bookmarkName);
      };
      toolbarXOffset += 25;
    }

  this.printButton = new ImageButton(this.id+"printButton", this.toolbarLeftButtonPanel.element, toolbarXOffset, 0, 0, 25, 25, true, "print", "Print");
  this.printButton.clickEvent = function()
  {
    this.map.print( );
  };
  toolbarXOffset += 25;

  if ((OBJECT_MANAGER.getGuiValueById('extensions')) && (OBJECT_MANAGER.getGuiValueById('extensions')['exportMap']))
    if(OBJECT_MANAGER.getGuiValueById('extensions')['exportMap'].enabled)
    {
      this.exportMapButton = new ImageButton(this.id+'exportMapButton',this.toolbarLeftButtonPanel.element, toolbarXOffset, 0, 0, 25, 25, true, "exportMapButton", "Export Map");
      this.exportMapButton.clickEvent = function()
      {
        this.map.exportMap();
      };
      toolbarXOffset += 25;
    }

  if ((OBJECT_MANAGER.getGuiValueById('extensions')) && (OBJECT_MANAGER.getGuiValueById('extensions')['email']))
    if(OBJECT_MANAGER.getGuiValueById('extensions')['email'].enabled)
    {
      var emailConfig = OBJECT_MANAGER.getGuiValueById('emailConfig');
      if (emailConfig.allowMapImages)
      {
        this.attachMapsButton = new ImageButton(this.id+'attachMapsButton',this.toolbarLeftButtonPanel.element, toolbarXOffset, 0, 0, 25, 25, true, "attachAnim", "Attach Maps to EMail");
        this.attachMapsButton.mapPanel = this;
        this.attachMapsButton.clickEvent = function()
        {
          var emailPanel = document.getWidgetById("emailPanel");
          if (!emailPanel) return;
          var desc = prompt('Description:','');
          if (desc == null)
            return;
          // NEW STUFF STARTS HERE
          var mapFName = '';
          var vmapFName = '';
          if (emailConfig.mapImageHandle == 'http://')
            mapFName = emailPanel.URLToHttp(this.mapPanel.map.mapImage.imageNode.src);
          else
            mapFName = emailPanel.URLToFName(this.mapPanel.map.mapImage.imageNode.src);
          emailPanel.addBodyImageAttachment(desc+' [Main Map Image]',emailConfig.mapImageHandle,mapFName);
          if (this.mapPanel.map.vmapPresent)
          {
            if (emailConfig.vmapImageHandle == 'http://')
              vmapFName = emailPanel.URLToHttp(this.mapPanel.map.vmapImage.imageNode.src);
            else
              vmapFName = emailPanel.URLToFName(this.mapPanel.map.vmapImage.imageNode.src);
            // NEW STUFF ENDS HERE
            emailPanel.addBodyImageAttachment(desc+' [Vicinity Map Image]',emailConfig.vmapImageHandle,vmapFName);
          }
          // flip to email tab
          document.getWidgetById('mapTabPanel').setActiveTabById('emailTab');
        };
        toolbarXOffset += 25;
      }
    }
  
  this.toolbarRightButtonPanel = new Panel(this.id+"toolbarRightButtonPanel", this.toolbar.element, 8, 0, 0, 50, 27, true, "mapRightToolbar", false);
  this.toolbarRightButtonPanel.element.cbe.moveTo("NE");
  this.helpButton = new ImageButton(this.id+".helpButton", this.toolbarRightButtonPanel.element, 25, 0, 0, 25, 25, true, "help", "Help");
  this.helpButton.element.cbe.moveTo("E");
  this.helpButton.clickEvent = function(e)
  {
    helpWindow = window.open("./onlineHelp/index.html");
  };

  this.toolbarMapButtonPanel = new Panel(this.id+".toolbarMapButtonPanel", this.toolbar.element, 8, 0, 0, 205, 27, true, "mapToolbar2", false);
  this.toolbarMapButtonPanel.element.cbe.moveTo("CEN");

  this.identifyButton = new RadioButton(this.id+".identifyButton", this.toolbarMapButtonPanel.element, 0, 0, 0, 25, 25, true, "identify", "Select Features", this.toolbarRadioButtonGroup);
  this.identifyButton.mapPanel = this;
  this.identifyButton.clickEvent = function(e)
  {
    this.mapPanel.clickMode = 'Select';
    this.mapPanel.mapImage.setClass('mapIdent');
  };

  this.zoomBoxButton = new RadioButton(this.id+".zoomBoxButton", this.toolbarMapButtonPanel.element, 25, 0, 0, 25, 25, true, "zoomBox", "Zoom to Window", this.toolbarRadioButtonGroup);
  this.zoomBoxButton.mapPanel = this;
  this.zoomBoxButton.clickEvent = function(e)
  {
    this.mapPanel.clickMode = 'ZoomBox';
    this.mapPanel.mapImage.setClass('mapZoom');
  };

  this.zoomInButton = new RadioButton(this.id+".zoomInButton", this.toolbarMapButtonPanel.element, 50, 0, 0, 25, 25, true, "zoomIn", "Zoom In", this.toolbarRadioButtonGroup);
  this.zoomInButton.mapPanel = this;
  this.zoomInButton.clickEvent = function(e)
  {
    this.mapPanel.clickMode = 'ZoomIn';
    this.mapPanel.mapImage.setClass('mapZoom');
  };
  this.zoomOutButton = new RadioButton(this.id+".zoomOutButton", this.toolbarMapButtonPanel.element, 75, 0, 0, 25, 25, true, "zoomOut", "Zoom Out",this.toolbarRadioButtonGroup);
  this.zoomOutButton.mapPanel = this;
  this.zoomOutButton.clickEvent = function(e)
  {
    this.mapPanel.clickMode = 'ZoomOut';
    this.mapPanel.mapImage.setClass('mapZoom');
  };
  this.panButton = new RadioButton(this.id+".panButton", this.toolbarMapButtonPanel.element, 100, 0, 0, 25, 25, true, "pan", "ReCenter",this.toolbarRadioButtonGroup);
  this.panButton.mapPanel = this;
  this.panButton.clickEvent = function(e)
  {
    this.mapPanel.clickMode = 'Center';
    this.mapPanel.mapImage.setClass('mapPan');
  };
  this.redrawButton = new ImageButton(this.id+".redrawButton", this.toolbarMapButtonPanel.element, 125, 0, 0, 25, 25, true, "redraw2", "Redraw",this.toolbarRadioButtonGroup);
  this.redrawButton.clickEvent = function(e)
  {
    this.map.refresh();
  };
  this.backButton = new ImageButton(this.id+".backButton", this.toolbarMapButtonPanel.element, 150, 0, 0, 25, 25, true, "back", "Back");
  this.backButton.clickEvent = function(e)
  {
    this.map.back();
  };
  this.extentButton = new ImageButton(this.id+".extentButton", this.toolbarMapButtonPanel.element, 175, 0, 0, 25, 25, true, "initialExtent", "Zoom to Initial Extent");
  this.extentButton.clickEvent = function(e)
  {
    this.map.zoomInitialExtent();
  };

  this.toolbarRadioButtonGroup.setActiveButton(1);
  this.zoomBoxButton.setActive();
  this.clickMode = 'ZoomBox';

  this.mapImage = new MapImage(this.id+".mapImage", this.mapWindow.element, 0, this.toolbar.height(), 0, this.width() - 50, this.mapWindow.height() - this.toolbar.height() - this.statusbar.height(), true, this, this.map, 'MASTER');
  this.mapImage.setClass('mapZoom');
  this.loadImageGraphic = new ImagePanel(this.id+"LoadImage", this.mapWindow.element, 0, this.toolbar.height(), 1, this.mapImage.width(), this.mapImage.height(), false, './'+GuiWidget.THEME_PATH+GuiWidget.THEME+'/images/mapLoad.gif', false, null, null);
  this.setMapObject(this.map);
};

MapPanel.prototype.setMapObject = function(newMapObject)
{
  this.map = newMapObject;
  if (this.map != null)
  {
    this.map.mapPanel = this;
    this.map.setMapLoadImage(this.loadImageGraphic);
    this.map.setMapImage(this.mapImage);
    this.zoomBar.setMapObject(this.map);
  }
  this.panUL.map = this.map;
  this.panUC.map = this.map;
  this.panUR.map = this.map;
  this.panCL.map = this.map;
  this.panCR.map = this.map;
  this.panBL.map = this.map;
  this.panBC.map = this.map;
  this.panBR.map = this.map;
  if (this.bookmarkButton)
    this.bookmarkButton.map = this.map;
  this.printButton.map = this.map;
  if (this.exportMapButton)
    this.exportMapButton.map = this.map;
  this.redrawButton.map = this.map;
  this.backButton.map = this.map;
  this.extentButton.map = this.map;
};

MapPanel.prototype.setStatusbarText = function(newCaption)
{
  this.statusbar.element.cbe.innerHtml(newCaption);
};

MapPanel.prototype.resize = function(newWidth, newHeight)
{
  this.element.cbe.resizeTo(newWidth, newHeight);

  this.zoomBar.hide();

  this.mapFrameUL.setPosition(0, 0);
  this.mapFrameUC.setPosition(25, 0);
  this.mapFrameUR.setPosition(this.width()-25, 0);
  this.mapFrameCL.setPosition(0, 25);
  this.mapFrameCR.setPosition(this.width()-25, 25);
  this.mapFrameBL.setPosition(0, this.height() - 25);
  this.mapFrameBC.setPosition(25, this.height() - 25);
  this.mapFrameBR.setPosition(this.width()-25, this.height()-25);

  this.mapFrameUC.resize(this.width() - 50, 25);
  this.mapFrameCL.resize(25, this.height() - 50);
  this.mapFrameCR.resize(25, this.height() - 50);
  this.mapFrameBC.resize(this.width() - 50, 25);

  this.panUC.element.cbe.moveTo("N");
  this.panBC.element.cbe.moveTo("S");
  this.panCL.element.cbe.moveTo("W");
  this.panCR.element.cbe.moveTo("E");

  this.mapWindow.resize(this.width() - 50, this.height() - 50);
  this.statusbar.resize(this.mapWindow.width(), 13);
  this.statusbar.element.cbe.moveTo("S");
  this.toolbar.resize(this.width() - 50, 27);
  this.toolbarMapButtonPanel.element.cbe.moveTo("CEN");
  this.toolbarRightButtonPanel.element.cbe.moveTo("NE");
  this.mapImage.resizeTo(this.width() - 50, this.mapWindow.height() - this.toolbar.height() - this.statusbar.height());
  this.loadImageGraphic.resize(this.mapImage.width(), this.mapImage.height());

  this.numZoomBarDivs = (this.panCR.element.cbe.top() - 50) / 11;
  this.zoomBar = new ZoomBar(this.id+"zoomBar", this.mapFrameCR.element, 0, 0, 1, true, this.map, 0.01, this.numZoomBarDivs);

  if (this.resizeEvent)
    this.resizeEvent(newWidth, newHeight);
};


