// 
// msexplor - Copyright (C) 2007 I.T. Beyond Pty Ltd.
// iMapPlot_AJAX Control
//
//-------------------------------------------------------------------------
// AJAX MapServer Library customised and adapted from original code by
// 20050927 - Copyright (C) 2005-2006 Simone Manca <simone.manca@gmail.com>
// http://datacrossing.crs4.it/en_Documentation_mscross.html
//-------------------------------------------------------------------------
//
// 10 Sep 2007 - I.T. Beyond Pty Ltd
// Adapted for use with prototype 1.5 library
// Added JSON support for AJAX data elements
// Major support updates and significant code changes to support places integration
// Support for absolute position of map elements
// Unused code removed
// Popup Info converted to inline <div> for places support
//
// 7 Jan 2008 - I.T. Beyond Pty Ltd
// Added support for search system passing ElementID - Extent and triggering the aspnet form submit.
// The search method loads the object in Zoom box mode by default, also added new method to display zoombox
//
var pixel_img = new Image(); pixel_img.src = '/iMapPlot/img/pixel.gif';
var dragObj = new Object();
dragObj.zIndex = 0;

// msMap class prototype
function msMap(DivTag, ControlType, p_protocol, h_Display)
{
  // Private vars
  var i        = this;
  var _tagMain = DivTag;
  var _map_w   = parseInt( _tagMain.style.width );
  var _map_h   = parseInt( _tagMain.style.height );

  var _protocol = p_protocol; // 'mapservercgi', 'wms'
  if (_protocol == null) { _protocol = 'mapservercgi'; }

  var _map_w_bord = 0; var _map_h_bord = 0;

  var _control = ControlType;
  var _display = h_Display;
  if (_display == null) {_display = 'external';}
  var _searchElementID = '';
  var _locn    = 'http://' + top.location.host;
  var _cgi     = _locn + '/cgi-bin/mapserver/mapserv.exe';
  var _mode    = 'map';
  var _layers  = '';
  var _map_file; var _args;
  var _attachedMsMap = null; var _referenceMap  = null;
  var _report = null;
  var _loading_counter = 0;
  var _read_cookie = false;
  
  var _ceControl = null;
  var _ceDist = null;

  var _ext_Xmin_orig; var _ext_Xmax_orig;
  var _ext_Ymin_orig; var _ext_Ymax_orig;
  var _ext_Xmin = null; var _ext_Xmax = null;
  var _ext_Ymin = null; var _ext_Ymax = null;
  
  var _zoombox_x_first; var _zoombox_y_first;
  var _zoombox_x_last;  var _zoombox_y_last;
  var _pixel_w; var _pixel_h;

  // Double buffering
  var _tagMap   = document.createElement('img');
  var _tagMap_B = document.createElement('img');
  this.getTagMap = function(){return _tagMap;}
  this.getTagEvent = function(){return _tagEvents;}

  var _tagZoombox   = document.createElement('div');
  var _tagReference = document.createElement('div');
  var _tagLoading   = document.createElement('img');
  var _tagOverlay   = document.createElement('div'); // Overlay Layer
  var _tagPoints    = document.createElement('div'); // Points Overlay container
  var _tagEvents    = document.createElement('div');
  var _tagCoords    = document.createElement('div');

  var _toolbars = new Array(); // Array of toolbars

  this.getToolbar = function(p){return _toolbars[p];}
  var _iconLoading = '/iMapPlot/img/Map_Loading3.gif';

  // WMS.GetMap protocol specific
  var _wms_imageformat = 'image/png';
  var _wms_projection = 'EPSG:4326';
  this.setWmsImageFormat = function(p){_wms_imageformat = p;}
  this.setWmsProjection = function(p){_wms_projection = p;}

  // Disable text/images selection.
  if (Prototype.Browser.IE)
  {
    _tagMain.onselectstart = function(){return false;};
    _tagMain.ondrag = function(){return false;};
  } else
  { _tagMain.style.setProperty("-moz-user-select", "none", ""); }

  // Point Overlay array
  var _pointsOverlayArray = new Array();
  this.setReport = function(p)
  {
    if (_report != null)
    { i.setReportNull(); }
    _report = p;
  }
  this.addPointOverlay = function(p_point, p_redraw)
  {
    p_point.setMap(i);
    _pointsOverlayArray.push(p_point);

    if ( (p_redraw == null) || (p_redraw == true) )
    { i.overlayPointsResort() }
  }
  this.getMainTag = function() { return _tagMain; }
  this.setSearchElementID = function(id) {_searchElementID = id;} 
  this.setReportNull = function()
  {
    if ($('PlaceInfo') != null) {
     if ($('info_content') != null) $('info_content').remove();
    }
   delete _report; _report = null;
  }

  i.show_loading_image = function(p)
  {
    if (p == true)
    {
      _loading_counter++;
      _tagLoading.style.display = '';
    }
    if (p == false)
    {
      _loading_counter--;  if (_loading_counter < 0) { _loading_counter = 0; }
      { _tagLoading.style.display = 'none'; }
    }
  }

  // Active Debug mode
  this.debug = function()
  {
    var db = document.createElement('a');
    db.oncontextmenu    = function(){return false;};
    setZindex(db, '110');
    db.style.position   = 'absolute';
    db.style.fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
    db.style.fontSize   = '11px';
    db.style.left       = i.width()-72+'px';
    db.style.top        = i.height()-13+'px';
    db.style.fontWeight = 'normal';
    db.style.textDecoration = 'overline';
    db.appendChild(document.createTextNode("Debug INFO"));
    _tagMain.appendChild(db);
    Event.observe(db, 'click', function(){ prompt('Debug INFO',i.get_map_url()) } );
  }

  // Check if a box (x, y, width, height) is within a map
  this.isPointInMap = function( p_x, p_y, p_w, p_h )
  {
    if ( (p_x > (_ext_Xmax+i.wPixel2real(_map_w_bord))) ||
         ((p_x+p_w) < (_ext_Xmin-i.wPixel2real(_map_w_bord))) ) return false;
    if ( ((p_y-p_h) > (_ext_Ymax+i.hPixel2real(_map_h_bord))) ||
         ((p_y+p_h-p_h) < (_ext_Ymin-i.hPixel2real(_map_h_bord))) ) return false;
    return true;
  }

  this.setBorder = function(p)
  {
    _map_w_bord = p; _map_h_bord = p;

    // First image buffer (double buffer)
    _tagMap.style.width  = (i.width()+_map_w_bord+_map_w_bord)+'px';
    _tagMap.style.height = (i.height()+_map_h_bord+_map_h_bord)+'px';
    _tagMap.style.top    = (- _map_w_bord) +'px';
    _tagMap.style.left   = (- _map_h_bord) +'px';

    // Second image buffer (double buffer)
    _tagMap_B.style.width  = _tagMap.style.width;
    _tagMap_B.style.height = _tagMap.style.height;
    _tagMap_B.style.top    = _tagMap.style.top;
    _tagMap_B.style.left   = _tagMap.style.left;
  }
  this.getBorder = function() {return _map_w_bord;}

  this.addClickTool = function()
  { i.getToolbar(0).addMapTool('Select', i.setActionSelect, _iconSelectButton);  }

  this.width      = function() { return _map_w; }
  this.height     = function() { return _map_h; }
  this.control    = function() { return _control; }
  this.display    = function() { return _display; }
  this.setCgi     = function(path) { _cgi = path; }
  this.setMapFile = function(p_mapFile) { _map_file = 'map='+p_mapFile; }
  this.setMode    = function(p_mode) { _mode = p_mode; }
  this.setLayers  = function(p_layers) { _layers = p_layers }
  this.setArgs    = function(p_args) { _args = p_args; }
  this.attachMap  = function(myMap) { _attachedMsMap = myMap; }
  this.setReferenceMap = function(myMap) { _referenceMap = myMap; }
  this.setControlEnable = function(p_control, p_ctrllatdif) { _ceControl = p_control; _ceDist = p_ctrllatdif; }

  this.setZoomBox = function(p_Xmin, p_Xmax, p_Ymin, p_Ymax)
  {
    Xmin = i.xReal2pixel(p_Xmin); Ymin = i.yReal2pixel(p_Ymin);
    Xmax = i.xReal2pixel(p_Xmax); Ymax = i.yReal2pixel(p_Ymax);

    if ((Xmax - Xmin < 2) && (Ymax - Ymin < 2)) {
    // Small extent display as myIcon using ballon
      var myPoint = new pointOverlay(new msIcon(null, null), _display, p_Xmax, p_Ymax, new Array(null, "Selected Location"));
      i.addPointOverlay(myPoint, true);
    }
     
    _tagZoombox.style.left    = Xmin +'px';
    _tagZoombox.style.top     = Ymax +'px';
    _tagZoombox.style.width   = Xmax - Xmin +'px';
    _tagZoombox.style.height  = Ymin - Ymax +'px';
    _tagZoombox.style.display = '';
  }
  
  this.setReferenceBox = function(p_Xmin, p_Xmax, p_Ymin, p_Ymax)
  {
    Xmin = i.xReal2pixel(p_Xmin); Ymin = i.yReal2pixel(p_Ymin);
    Xmax = i.xReal2pixel(p_Xmax); Ymax = i.yReal2pixel(p_Ymax);

    _tagReference.style.left    = Xmin +'px';
    _tagReference.style.top     = Ymax +'px';
    _tagReference.style.width   = Xmax - Xmin +'px';
    _tagReference.style.height  = Ymin - Ymax +'px';
    _tagReference.style.display = '';
  }

  this.xReal2pixel = function(p_x)
  { return Math.round( _map_w * (p_x - _ext_Xmin) / (_ext_Xmax - _ext_Xmin) ); }
  this.yReal2pixel = function(p_y)
  { return Math.round( _map_h * (_ext_Ymax - p_y) / (_ext_Ymax - _ext_Ymin) ); }
  this.wPixel2real = function(p_w) { return (p_w * _pixel_w); }
  this.hPixel2real = function(p_h) { return (p_h * _pixel_h); }
  this.wReal2pixel = function(p_w) { return (p_w / _pixel_w); }
  this.hReal2pixel = function(p_h) { return (p_h / _pixel_w); }
  this.xPixel2Real = function(p_x) { return i.wPixel2real(p_x)+_ext_Xmin; }
  this.yPixel2Real = function(p_y) { return i.hPixel2real(_map_h-p_y)+_ext_Ymin; }

  this.setExtent  = function(Xmin, Xmax, Ymin)
  {
    Xmin = parseFloat(Xmin); Xmax = parseFloat(Xmax); Ymin = parseFloat(Ymin);
    _ext_Xmin = Xmin; _ext_Xmax = Xmax; _ext_Ymin = Ymin;
    _ext_Ymax = ((_map_h/_map_w)*(_ext_Xmax-_ext_Xmin)) + _ext_Ymin;
  }

    this.round = function(num, dec) {
	    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }

  this.setFullExtent = function(Xmin, Xmax, Ymin)
  {
    _ext_Xmin_orig = Xmin; _ext_Xmax_orig = Xmax; _ext_Ymin_orig = Ymin;
    _ext_Ymax_orig = ((_map_h/_map_w)*(_ext_Xmax_orig-_ext_Xmin_orig)) + _ext_Ymin_orig;
    if (_read_cookie == false)
    { i.fullExtentNoRedraw(); }	
  }

  this.setZoomboxFirst = function(x, y)
  { _zoombox_x_first = x; _zoombox_y_first = y; }

  this.setZoomboxWH = function(x, y)
  {
    _zoombox_x_last = x; _zoombox_y_last = y;
    _tagZoombox.style.left   = min(_zoombox_x_first, _zoombox_x_last) + 'px';
    _tagZoombox.style.top    = min(_zoombox_y_first, _zoombox_y_last) + 'px';
    _tagZoombox.style.width  = max(_zoombox_x_last,  _zoombox_x_first) -
                               min(_zoombox_x_last,  _zoombox_x_first) + 'px';
    _tagZoombox.style.height = max(_zoombox_y_last,  _zoombox_y_first) -
                               min(_zoombox_y_last,  _zoombox_y_first) + 'px';
    _tagZoombox.style.display = '';
  }

  this.zoomboxExtent = function()
  {
    _tagZoombox.style.display = 'none';

    var ll = min(_zoombox_x_last, _zoombox_x_first);
    var rr = max(_zoombox_x_last, _zoombox_x_first);
    var bb = max(_zoombox_y_last, _zoombox_y_first);
    var tt = min(_zoombox_y_last, _zoombox_y_first);

    _ext_Xmin += ll * _pixel_w;
    _ext_Xmax -= (_map_w - rr) * _pixel_w;
    _ext_Ymax -= tt * _pixel_h;
    _ext_Ymin += (_map_h - bb) * _pixel_h;

    if (ControlType == "search") {
        if (_searchElementID != null) {document.getElementById(_searchElementID).value = i.round(_ext_Xmin,3) + ' ' + i.round(_ext_Xmax,3) + ' ' + i.round(_ext_Ymin,3) + ' ' + i.round(_ext_Ymax,3);
        i.show_loading_image(true);
        document.aspnetForm.submit();}
        else
        alert(_searchElementID + ' not found on page')
    } else 
        i.redraw();
  }

  this.recalc_pixel_size = function()
  {
    _pixel_w  = (_ext_Xmax - _ext_Xmin) / _map_w;
    _pixel_h  = (_ext_Ymax - _ext_Ymin) / _map_h;
  }

  this.redraw = function(redrawAttached)
  {
    i.show_loading_image(true);
    i.recalc_map_size();

    // Set second buffer map image (Double buffer)
    _tagMap_B.src = i.get_map_url();

    if ( (_attachedMsMap != null) && (redrawAttached != false) )
    {
      _attachedMsMap.attachMap(i);
      _attachedMsMap.setExtent(_ext_Xmin, _ext_Xmax, _ext_Ymin);
      _attachedMsMap.redraw(false);
    }
    
    if ( _ceControl != null ) {
      if (_ext_Xmax - _ext_Xmin <= _ceDist) _ceControl.disabled = false;
      if (_ext_Xmax - _ext_Xmin > _ceDist) _ceControl.disabled = true;
    }
    if ( _referenceMap != null )	// Draw zoom box in the reference map
    { _referenceMap.setReferenceBox(_ext_Xmin, _ext_Xmax, _ext_Ymin, _ext_Ymax); } 
  }

  this.fullExtentNoRedraw = function()
  {
    _ext_Xmin = _ext_Xmin_orig; _ext_Xmax = _ext_Xmax_orig;
    _ext_Ymin = _ext_Ymin_orig; _ext_Ymax = _ext_Ymax_orig;
  }

  this.fullExtent = function() { i.fullExtentNoRedraw(); i.redraw(); }

  this.setActionZoombox = function()
    {_action = 'zoom'; _tagMap.style.cursor = "crosshair"; }

  this.setActionPan = function()
  { _tagMap.style.cursor = "move"; }

  this.setActionCoords = function()
  { _tagMap.style.cursor = "crosshair"; }

  this.setActionNone = function()
  {
    for (j=0; j<_toolbars.length; j++){_toolbars[j].hide();}
    _tagMap.style.cursor = "";
  }

  this.setActionZoomIn = function()
  {
    if ( isNaN(_tagLoading.style.display) )
    { i.zoomPerc(1.40); i.redraw(); }
  }

  this.setActionZoomOut = function()
  {
    if ( isNaN(_tagLoading.style.display) )
    { i.zoomPerc(0.30); i.redraw(); }
  }

  this.zoomPerc = function(p_perc)
  {
    var wx     = _ext_Xmax - _ext_Xmin;
    var wx_new = wx * p_perc;
    var kx     = (wx_new - wx) / 2;
    var wy     = _ext_Ymax - _ext_Ymin;
    var wy_new = wy * p_perc;
    var ky     = (wy_new - wy) / 2;
    i.setExtent(_ext_Xmin + kx, _ext_Xmax - kx, _ext_Ymin + ky);
  }

  this.mapLoaded = function()
  {
    // Swap image buffer (double buffering)
    var tmp = _tagMap;
    _tagMap = _tagMap_B;
    _tagMap_B = tmp;

    // Disegna gli overlay puntuali
    _tagOverlay.style.left = '0'; _tagOverlay.style.top  = '0';
    for(var j=0; j<_pointsOverlayArray.length; j++)
    { _pointsOverlayArray[j].redraw(); }
    if ( _report != null ) { _report.redraw(); }

    _tagMap.style.cursor = _tagMap_B.style.cursor;
    _tagMap.style.left = (- _map_w_bord) +'px';
    _tagMap.style.top  = (- _map_h_bord) +'px';
    _tagMap_B.style.display = 'none';
    _tagMap.style.display = '';

    // Hide "loading" image when map is loaded
    i.show_loading_image(false);
  }

  this.get_map_url = function()
  {
    var my_url;

    if (_protocol == 'mapservercgi')
    {
      var size = 'mapsize=' + (_map_w+_map_w_bord+_map_w_bord) + '+'
                            + (_map_h+_map_h_bord+_map_h_bord);
      var ext  = 'mapext=' + (_ext_Xmin-i.wPixel2real(_map_w_bord)) + '+'
                           + (_ext_Ymin-i.hPixel2real(_map_h_bord)) + '+'
                           + (_ext_Xmax+i.wPixel2real(_map_w_bord)) + '+'
                           + (_ext_Ymax+i.hPixel2real(_map_h_bord)) ;

      my_url = _cgi + '?mode=' + _mode + '&' + _map_file + '&' +
               ext + '&' + size + '&layers=' + _layers;
      // Opera9 Bug Fix (onload event don't work if image is in cache)
      if (Prototype.Browser.Opera) {my_url = my_url + '&' + Math.random();}
    }

    if (_protocol == 'wms')
    {
      var size = 'width='   + (_map_w+_map_w_bord+_map_w_bord) +
                 '&height=' + (_map_h+_map_h_bord+_map_h_bord);
      var ext  = 'BBOX=' + (_ext_Xmin-i.wPixel2real(_map_w_bord)) + ','
                         + (_ext_Ymin-i.hPixel2real(_map_h_bord)) + ','
                         + (_ext_Xmax+i.wPixel2real(_map_w_bord)) + ','
                         + (_ext_Ymax+i.hPixel2real(_map_h_bord)) ;
      var imgtype = 'FORMAT=' + _wms_imageformat;
      var proj = 'SRS=' + _wms_projection;
      var lay = 'LAYERS=' + _layers.replace(/\ /g,",");

      my_url = _cgi + '?VERSION=1.1.1&REQUEST=GetMap&' + proj + '&' + lay +
                      '&STYLES=&' + ext + '&' + imgtype + '&' + size;
    }
    if (ControlType == "search") {_args +=  '&map_logo_status=off';}
    return my_url + '&' + _args;
  }

  this.setPan = function(x, y)
  {
    i.recalc_pixel_size();
    var x_real = x * _pixel_w; var y_real = y * _pixel_h;
    _ext_Xmin = _ext_Xmin - x_real; _ext_Xmax = _ext_Xmax - x_real;
    _ext_Ymin = _ext_Ymin + y_real; _ext_Ymax = _ext_Ymax + y_real;
    i.redraw();
  }

  this.recalc_map_size = function()
  {
    i.recalc_pixel_size();

    if ( _pixel_w > _pixel_h )
    { // Modify only Y (box width > height)
      var middle = ((_ext_Ymax - _ext_Ymin) / 2) + _ext_Ymin;
      var new_h = (_map_h / _map_w) * (_ext_Xmax - _ext_Xmin);
      _ext_Ymin = middle - (new_h / 2);
      _ext_Ymax = middle + (new_h / 2);
    } else
    { // Modify only X (box width < height)
      var middle = ((_ext_Xmax - _ext_Xmin) / 2) + _ext_Xmin;
      var new_w = (_map_w / _map_h) * (_ext_Ymax - _ext_Ymin);
      _ext_Xmin = middle - (new_w / 2);
      _ext_Xmax = middle + (new_w / 2);
    }
    i.recalc_pixel_size();
  }

  this.spatialPointQueryWMSurl = function(p_server, p_x, p_y, p_layers)
  {
    if (p_server == null) {p_server = _cgi;}
    var ext  = (_ext_Xmin-i.wPixel2real(_map_w_bord)) + ','
               + (_ext_Ymin-i.hPixel2real(_map_h_bord)) + ','
               + (_ext_Xmax+i.wPixel2real(_map_w_bord)) + ','
               + (_ext_Ymax+i.hPixel2real(_map_h_bord)) ;
    var c; if(p_server.indexOf('?')==-1){c='?';}else{c='&';}
    var url = p_server + c + 'SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo' +
              '&INFO_FORMAT=text/plain' + //gml' +
              '&LAYERS=' + p_layers +
              '&QUERY_LAYERS=' + p_layers +
              '&x=' + p_x + '&y=' + p_y +
              '&bbox=' + ext +
              '&width=' +_map_w+ '&height=' +_map_h;
    return url;
  }

  this.init = function()
  {
    _tagMain.oncontextmenu  = function(){return false;};
    _tagMain.style.width    = i.width()+'px';
    _tagMain.style.height   = i.height()+'px';
    _tagMain.style.overflow = 'hidden';
    if (_tagMain.style.position != 'absolute') {_tagMain.style.position = 'relative';}

    _tagEvents.oncontextmenu = function(){return false;};
    setZindex(_tagEvents, '0');
    _tagEvents.style.position = 'absolute';
    _tagEvents.left           = '0';
    _tagEvents.top            = '0';

    // First buffer (double buffer)
    _tagMap.objRef = i;
    _tagMap.oncontextmenu  = function(){return false;};
    _tagMap.onmousedown = function(){return false;};  // Disable drag'n drop
    Event.observe(_tagMap, 'load', i.mapLoaded );
    setZindex(_tagMap, '0');
    _tagMap.galleryImg = "no";
    _tagMap.style.width    = (i.width()+_map_w_bord+_map_w_bord)+'px';
    _tagMap.style.height   = (i.height()+_map_h_bord+_map_h_bord)+'px';
    _tagMap.style.border   = '0 none';
    _tagMap.style.margin   = '0'; _tagMap.style.padding  = '0';
    _tagMap.style.position = 'absolute';
    _tagMap.style.top      = (- _map_w_bord) +'px';
    _tagMap.style.left     = (- _map_h_bord) +'px';
    _tagMap.style.display  = 'none';

    // Second buffer (double buffer)
    _tagMap_B.objRef = _tagMap.objRef;
    _tagMap_B.oncontextmenu = _tagMap.oncontextmenu;
    _tagMap_B.onmousedown = _tagMap.onmousedown;
    Event.observe(_tagMap_B, 'load', i.mapLoaded );
    setZindex(_tagMap_B, '0');
    _tagMap_B.galleryImg = "no";
    _tagMap_B.style.width    = _tagMap.style.width;
    _tagMap_B.style.height   = _tagMap.style.height;
    _tagMap_B.style.border   = _tagMap.style.border;
    _tagMap_B.style.margin   = _tagMap.style.margin;
    _tagMap_B.style.padding  = _tagMap.style.padding;
    _tagMap_B.style.position = _tagMap.style.position;
    _tagMap_B.style.top      = _tagMap.style.top;
    _tagMap_B.style.left     = _tagMap.style.left;
    _tagMap_B.style.display  = 'none';

    _tagReference.oncontextmenu    = function(){return false;};
    setZindex(_tagReference, '100');
    _tagReference.style.display    = 'none';
    _tagReference.style.position   = 'absolute';
    _tagReference.style.margin     = '0'; _tagReference.style.padding = '0';
    _tagReference.style.lineHeight = '0';
    _tagReference.style.border     = '1px solid #ff0000';
    _tagReference.style.background = '#ffa0a0';
    _tagReference.style.opacity    = '0.40';               // Gecko
    _tagReference.style.filter     = 'alpha(opacity=40)';  // Windows
    _tagReference.style.fontSize   = '1'; // 20061012 bugfix by Rodrigo

    _tagZoombox.oncontextmenu    = function(){return false;};
    setZindex(_tagZoombox, '100');
    _tagZoombox.style.position   = 'absolute';
    _tagZoombox.style.display    = 'none';
    _tagZoombox.style.border     = '1px dashed #000000';
    _tagZoombox.style.margin     = '0px'; _tagZoombox.style.padding = '0px';
    _tagZoombox.style.lineHeight = '0';
    _tagZoombox.style.background = '#606060';	         //'#f0f0f0';
    _tagZoombox.style.opacity    = '0.25';               // Gecko
    _tagZoombox.style.filter     = 'alpha(opacity=25)';  // Windows
    _tagZoombox.style.fontSize   = '1'; // 20061012 bugfix by Rodrigo

    // Overlay Layer
    _tagOverlay.oncontextmenu    = function(){return false;};
    setZindex(_tagOverlay, '30');
    _tagOverlay.style.position   = 'absolute';

    _tagPoints.oncontextmenu    = function(){return false;};
    setZindex(_tagPoints, '40');
    _tagPoints.style.position   = 'absolute';

    // "Loading" image tag
    _tagLoading.oncontextmenu    = function(){return false;};
    _tagLoading.onmousedown = function(){return false;};  // Disable drag'n drop
    setZindex(_tagLoading, '100');
    _tagLoading.style.position   = 'absolute';
    _tagLoading.style.display    = 'none';
    _tagLoading.style.border     = '0';
    _tagLoading.style.margin     = '0'; _tagLoading.style.padding    = '0';
    _tagLoading.style.lineHeight = '0';
    setAlphaPNG(_tagLoading, _iconLoading);
    _tagLoading.style.left = (_map_w - 130) / 2 + 'px';
    _tagLoading.style.top  = (_map_h - 122) / 2 + 'px';
    
    // Coords tag
    _tagCoords.oncontextmenu    = function(){return false;};
    _tagCoords.onmousedown = function(){return false;};  // Disable drag'n drop
    setZindex(_tagCoords, '110');
    _tagCoords.style.position   = 'absolute';
    _tagCoords.style.display    = 'block';
    _tagCoords.style.margin     = '0px'; _tagLoading.style.padding    = '8px';
    _tagCoords.style.left = (0) + 'px';
    _tagCoords.style.top  = (_map_h - 13) + 'px';
    _tagCoords.style.height = '14px';
    _tagCoords.style.width = '130px';

    // Double buffer
    _tagEvents.appendChild(_tagMap);
    _tagEvents.appendChild(_tagMap_B);

    _tagOverlay.appendChild(_tagPoints);

    _tagMain.appendChild(_tagEvents);
    _tagMain.appendChild(_tagOverlay);
    _tagMain.appendChild(_tagZoombox);
    _tagMain.appendChild(_tagReference);

// ---> ToolBar ---------------------------------------------------------------
    if (ControlType != null)
    {
      _toolbars.push(new msToolbar(i, ControlType, true));
      _tagMain.appendChild(_toolbars[0].getTag());
      _tagMain.appendChild(_tagCoords);
    }
// <--- ToolBar ---------------------------------------------------------------
    _tagMain.appendChild(_tagLoading);
   
   var flag = true; if (Prototype.Browser.Opera){flag=false;}
   Event.observe(_tagMain, 'mousemove', i.coordGo, flag) 
  }
  
  this.coordGo = function(event)
  {
    var coodinfo = document.createElement('div');
     coodinfo.style.background = '#a0a0a0';
    coodinfo.style.opacity    = '0.8';               // Gecko
    coodinfo.style.filter     = 'alpha(opacity=80)';  // Windows
    coodinfo.style.fontSize   = '11px'; 
    if (_tagCoords.childNodes[0]) {_tagCoords.removeChild(_tagCoords.childNodes[0]);}
    coodinfo.innerHTML = '&nbsp;' + Math.round(i.xPixel2Real(i.getClick_X(event)-1)*1000000)/1000000 + ' &nbsp; ' + Math.round(i.yPixel2Real(i.getClick_Y(event)-1)*1000000)/1000000
    _tagCoords.appendChild(coodinfo);
  }

  this.getClick_X = function(p_event)
  { return Event.pointerX(p_event) - Position.cumulativeOffset(i.getTagMap())[0] - _map_w_bord; }
  this.getClick_Y = function(p_event)
  { return Event.pointerY(p_event) - Position.cumulativeOffset(i.getTagMap())[1] - _map_w_bord; }

  this.zoomStart = function(event)
  {
    var el;
    dragObj.elNode = _tagMap;
    var x = i.getClick_X(event);
    var y = i.getClick_Y(event);
    if ( isNaN(_tagLoading.style.display) )
    {  
      var flag = true; if (Prototype.Browser.Opera){flag=false;}
      Event.observe(document, 'mousemove', i.zoomGo, flag) 
      Event.observe(document, 'mouseup', i.zoomStop, flag) 
      i.setZoomboxFirst(x, y);
    }
  }

  this.zoomGo = function(event)
  {
    var x = i.getClick_X(event);
    var y = i.getClick_Y(event);
    i.setZoomboxWH(x, y);
  }

  this.zoomStop = function(event)
  {
    var flag = true; if (Prototype.Browser.Opera){flag=false;}
    Event.stopObserving(document, "mousemove", i.zoomGo, flag);
    Event.stopObserving(document, "mouseup", i.zoomStop, flag);
    i.zoomboxExtent();
  }

  this.dragStart = function(event)
  {
    var el;
    dragObj.elNode = _tagMap;
    var x = i.getClick_X(event) + Position.cumulativeOffset(i.getTagMap())[0];
    var y = i.getClick_Y(event) + Position.cumulativeOffset(i.getTagMap())[1];

    // Save starting positions of cursor and element.
    dragObj.cursorStartX = x; dragObj.cursorStartY = y;
    dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
    dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
    if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
    if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

    if ( isNaN(_tagLoading.style.display) )
    {
      var flag = true; if (Prototype.Browser.Opera){flag=false;}
      Event.observe(document, 'mousemove', i.dragGo, flag) 
      Event.observe(document, 'mouseup', i.dragStop, flag) 
    }
  }

  this.dragGo = function(event)
  {
    var xx = i.getClick_X(event) + Position.cumulativeOffset(i.getTagMap())[0];
    var yy = i.getClick_Y(event) + Position.cumulativeOffset(i.getTagMap())[1];

    // Move map by the same amount the cursor has moved.
    dragObj.elNode.style.left = (dragObj.elStartLeft + xx - dragObj.cursorStartX) + "px";
    dragObj.elNode.style.top  = (dragObj.elStartTop  + yy - dragObj.cursorStartY) + "px";
    // Move Overlay layer
    _tagOverlay.style.left = parseInt(dragObj.elNode.style.left) +_map_w_bord +'px';
    _tagOverlay.style.top  = parseInt(dragObj.elNode.style.top)  +_map_h_bord +'px';
  }

  this.dragStop = function(event)
  {
    // Stop capturing mousemove and mouseup events.
    var flag = true; if (Prototype.Browser.Opera){flag=false;}
    Event.stopObserving(document, "mousemove", i.dragGo, flag);
    Event.stopObserving(document, "mouseup", i.dragStop, flag);

    var xx, yy;
    var x = i.getClick_X(event) + Position.cumulativeOffset(i.getTagMap())[0];
    var y = i.getClick_Y(event) + Position.cumulativeOffset(i.getTagMap())[1];
    // Move drag element by the same amount the cursor has moved.
    xx = (dragObj.elStartLeft + x - dragObj.cursorStartX);
    yy = (dragObj.elStartTop  + y - dragObj.cursorStartY);
    // Add buffer size
    xx += _map_w_bord;
    yy += _map_h_bord;

    if ((xx != 0) || (yy != 0)) {i.setPan(xx, yy);}
  }

  // WFS protocol
  this.loadPointsOverlayWFS = function(p_serv, p_name, p_icon)
  {
    i.show_loading_image(true);  // Show "loading" image
    var _hasResults = false;
    var url = p_serv+'&SERVICE=WFS&VERSION=1.0.0&REQUEST=getfeature&TYPENAME='+p_name;
    new Ajax.Request(url, { method:'get', 
        onSuccess: function(transport){ 
            var response = transport.responseText || "no response text";
            var mydata = parsePointsFromGML(response);
            
            var _hasResults = i.setOverlayPoints(mydata, p_icon); },           
        onFailure: function(){ alert('Something went wrong...') } 
    });    
    return _hasResults;
  }
  
  this.loadPointsOverlayJSON = function(p_serv, p_name, p_lon, p_lat, p_countid, p_icon)
  {
    i.show_loading_image(true);  // Show "loading" image

    var url = p_serv;
    if (p_name != null) url+='&poi='+p_name;
    if (p_lon != null) url+='&x='+p_lon;
    if (p_lat != null) url+='&y='+p_lat;

   new Ajax.Request(url, { method:'get', 
        onSuccess: function(transport){ 
           var response = transport.responseText || "no response text";
            var mydata = parsePointsFromJSON(response, p_countid);
            var _hasResults = i.setOverlayPoints(mydata, p_icon);
            if ($('PlaceName') != null) {
                if (_hasResults) { $('PlaceName').style.background = 'white'; } else 
                { $('PlaceName').style.background = 'red'; }}
            },            
        onFailure: function(){ alert('Something went wrong...'); return false; } 
    }); 
  }
  
  this.removeOverlayPoints = function()
  {
    i.setReportNull();
    _pointsOverlayArray.splice(0, _pointsOverlayArray.length);
    // Empty _tagPoints icons
    var kids = _tagPoints.childNodes;
    for(var j=kids.length-1; j>=0; j--) {_tagPoints.removeChild(kids[j]);}
  }

  this.overlayPointsResort = function()
  {
    // Z sorting
    _pointsOverlayArray.sort(function(a,b){if (a.getY()>b.getY()){return -1;}else{return 1;}});

    // Empty _tagPoints icons
    var kids = _tagPoints.childNodes;
    for(var j=kids.length-1; j>=0; j--) {_tagPoints.removeChild(kids[j]);}

    for(var j=0; j<_pointsOverlayArray.length; j++)
    {_tagPoints.appendChild(_pointsOverlayArray[j].getShd());}

    for(var j=0; j<_pointsOverlayArray.length; j++)
    {_tagPoints.appendChild(_pointsOverlayArray[j].getImg());}

    // Redraw...
    for(var j=0; j<_pointsOverlayArray.length; j++)
    {_pointsOverlayArray[j].redraw();}
  }

  this.setOverlayPoints = function(p, p_icon)
  {
    var retVal = false;
    i.setReportNull();
    for(var j=0; j<p[0].length; j++)
    {
      retVal = true;
      var myPoint = new pointOverlay(p_icon, _display, p[0][j], p[1][j], p[2][j]);
      i.addPointOverlay(myPoint, false);
    }
    i.overlayPointsResort()
    i.show_loading_image(false);
 //   alert('setOverlayPoints: ' + retVal);
    return retVal;
  }

  i.init();
}

function min(a, b) { if ( a < b ) { return a; } else { return b; } }
function max(a, b) { if ( a > b ) { return a; } else { return b; } }

function setPos(p_obj, p_x, p_y)
{
  p_obj.style.left    = p_x+'px';
  p_obj.style.top     = p_y+'px';
  p_obj.style.display = '';
}

// pointOverlay class prototype
function pointOverlay( p_icon, p_type, p_x, p_y, p_item_value )
{
  var _msMap = null;
  var _img   = null;	//document.createElement('img');
  var _shd   = null;	//document.createElement('img');
  var _x     = parseFloat(p_x);	// Real coord X
  var _y     = parseFloat(p_y);	// Real coord Y
  var _icon  = p_icon;
  var _item_value = p_item_value;
  var _offsetX = 0;  var _offsetY = 0;

  if (_icon == null) {_icon = new msIcon(null, null);}

  // Functions...
  this.setMap = function(m) { _msMap = m; }
  this.getMap = function()  { return _msMap; }
  this.getImg = function()  { return _img; }
  this.getShd = function()  { return _shd; }
  this.getX   = function()  { return _x; }
  this.getY   = function()  { return _y; }
  this.getHtmlAttributes = function()
  {
    var pl = ''
    var url = '\"/Places/Show.aspx?pwp=' + _item_value[0] + '\"'
    if (_msMap.control() == 'search') url = '\"/Shop/AllItems.aspx?me=' + _x + ' ' + _x + ' ' + _y + ' ' + _y + '\"'
    var ret = '';
    if ( typeof _item_value[1] != 'undefined' ) ret += '<div><a class=\"bm\" style=\"font-size:11.5px\" href=' + url + '>' + _item_value[1] + '</a></div>';
    if ( typeof _item_value[4] != 'undefined' ) ret += '<div style=\"margin-top:2px\">' + _item_value[4] + '</div>';
    if ( typeof _item_value[2] != 'undefined' ) ret += '<div><a href=' + url + '>' + _item_value[2] + '</a></div>';
    if (typeof thisPlace != 'undefined') { pl = thisPlace; ret += _item_value[3]; ret +=  'km from ' + pl ;}
    if (_msMap.control() != 'search') ret += '<div><a class=\"lba\" href=\"/Places/Show.aspx?pwp=' + _item_value[0] + '\">View this Place...</a></div>';
    ret += '<div><a class=\"lba\" href=\"/Shop/AllItems.aspx?me=' + _x + ' ' + _x + ' ' + _y + ' ' + _y + '\">View Maps & Books...</a></div>';
    ret += '</div>';
    return ret;
  }
  this.getInfoX = function() { return _msMap.xReal2pixel(_x); }
  this.getInfoY = function() { return Math.round(_msMap.yReal2pixel(_y) - (parseInt(_img.offsetHeight)/2) ); }
  this.getWidth  = function() { return parseInt(_img.style.width); }
  this.getHeight = function() { return parseInt(_img.style.height); }

  this.redraw = function()
  {
    if ( _msMap.isPointInMap( _x - _msMap.wPixel2real(_offsetX),
                              _y + _msMap.hPixel2real(_offsetY),
                              _msMap.wPixel2real(_offsetX),
                              _msMap.hPixel2real(_offsetY) ) )
    {
      setPos(_img, _msMap.xReal2pixel(_x) - _offsetX,
                   _msMap.yReal2pixel(_y) - _offsetY);
      setPos(_shd, _msMap.xReal2pixel(_x) - _offsetX,
                   _msMap.yReal2pixel(_y) - _offsetY);
    } else
    { this.setVisible(false); }
  }

  this.setVisible = function(p_bool)
  {
    var str = null;
    if (p_bool) {str = '';} else {str = 'none';}
    _img.style.display = str;
    _shd.style.display = str;
  }

  this.showReport = function()
  {
    var pnt = new msReport(this, p_type);
    _msMap.setReport(pnt);
  }

  // Initialization...
  _img = _icon.getImage(); _shd = _icon.getShadow();
  _offsetX = _icon.getShiftX() -1;
  _offsetY = _icon.getShiftY() -1;

  _img.objRef = this;

Event.observe(_img, 'click' , function(event){pointOverlayEvent(event);});
}

function setAlphaPNG(p_imgTag, p_src)
{
  if (Prototype.Browser.IE)
  {
    p_imgTag.src = pixel_img.src;
    p_imgTag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"+
                            "(src='"+p_src+"',sizingMethod='image')";
      //# "image": Keep the original size of the image.
      //# "scale": Stretch or compress the image to the container boundaries.
      //# "crop": Crop the image to the container dimensions.
  } else
  { p_imgTag.src = p_src; }
}

function setAlphaBackgroundPNG( p_Tag, p_src )
{
  if (Prototype.Browser.IE)
  {
    p_Tag.style.backgroundImage = 'none';
    p_Tag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"+
                         "(src='"+p_src+"',sizingMethod='scale')";
  } else
  { p_Tag.style.backgroundImage = "url('"+p_src+"')"; }
}

function setZindex(p_tag, p)
{
  if (p_tag.setAttribute)
  { p_tag.setAttribute('style', 'z-index:'+p+';'); } else
  { p_tag.style.zIndex = p; }
}

// msIcon class prototype
function msIcon( p_img, p_shd, p_offsetX, p_offsetY )
{
  var _img_name = p_img;
  var _shd_name = p_shd;
  var _offsetX = 1;	// Distanza del target dall'angolo
  var _offsetY = 1;	// alto-sinistra.

  if ( p_offsetX != null ) { _offsetX = p_offsetX; }
  if ( p_offsetY != null ) { _offsetY = p_offsetY; }
  if ( _img_name == null )
  {
    _img_name = '/iMapPlot/img/mm_20_red.png';
    _shd_name = '/iMapPlot/img/mm_20_shadow.png';
    _offsetX  = 6; _offsetY = 19;
  }
  if (_img_name == '') {_img_name='/iMapPlot/img/pixel.gif';}
  if (_shd_name == '') {_shd_name='/iMapPlot/img/pixel.gif';}

  this.getShiftX = function() { return _offsetX; }
  this.getShiftY = function() { return _offsetY; }
  this.getImage = function()
  {
    var tmp = document.createElement('img');
    tmp.oncontextmenu  = function(){return false;};
    tmp.onmousedown = function(){return false;};  // Disable drag'n drop
    setZindex(tmp, '110');
    tmp.style.position = 'absolute';
    tmp.style.cursor   = 'pointer';
    setAlphaPNG(tmp, _img_name);
    return tmp;
  }
  this.getShadow = function()
  {
    var tmp = document.createElement('img');
    tmp.oncontextmenu  = function(){return false;};
    tmp.onmousedown = function(){return false;};
    setZindex(tmp, '109');
    tmp.style.position = 'absolute';
    setAlphaPNG(tmp, _shd_name);
    return tmp;
  }
}

// msReport class prototype
function msReport(p_pnt, p_type)
{
  var _content = document.createElement('div');
  
  this.redraw = function() {}
  
  if ($('PlaceInfo') == null) { return }

  _content.setAttribute("id", "info_content")
  _content.innerHTML = p_pnt.getHtmlAttributes();
  if ($('PlaceHelp') != null) { $('PlaceHelp').style.display='none';}
  if (p_type == 'inMap') {
    var _scrollX = -20;
    var _scrollY = 0;
    var os_x = _scrollX;
    var os_y = _scrollY +50;
    $('PlaceInfo').style.left = p_pnt.getInfoX() -os_x +'px';
    $('PlaceInfo').style.top  = p_pnt.getInfoY() -os_y +'px';
    $('PlaceInfo').style.display = 'block';
 }
  $('PlaceInfo').appendChild(_content);
}

// pointOverlay Click Event
function pointOverlayEvent(e)
{
  var p;
  if (e.srcElement) { p = e.srcElement.objRef; }
  if (e.target)     { p = e.target.objRef; }
  p.showReport();
}

// .......................................

function parsePointsFromGML(myxml)
{
  var _coords = null;
  var _name = null;
  var prefix = "";
  var featureMember_Name = "featureMember";
  //var msGeometry_Name    = "msGeometry";
  var Point_Name         = "Point";
  var _add               = 0; // Mozilla utilizza gli indici di "childNodes" + 1.
  var _molt              = 1; // Mozilla moltiplica per 2

  if (Prototype.Browser.IE)   // IE
  {
    featureMember_Name = "gml:"+featureMember_Name;
    // msGeometry_Name    = "ms:"+msGeometry_Name;
    Point_Name         = "gml:"+Point_Name;
  } else
  if (window.XMLHttpRequest)  // Mozilla, Safari,...
  {
    _add  = 1;
    _molt = 2;
  } 

  var _data = new Array();
  _data[0] = new Array();
  _data[1] = new Array();
  _data[2] = new Array();
  _data[3] = new Array();

  // For each point in GML file...
  var count = myxml.getElementsByTagName(featureMember_Name).length;
  for(var i=0; i<count; i++)
  {
    _coords =  myxml.getElementsByTagName(featureMember_Name)[i].
               childNodes[0+_add].childNodes[0+_add].
               childNodes[0+_add].
               childNodes[0+_add].childNodes[0].nodeValue;
    var tmp = new Array(); tmp = _coords.split(',');
    var names = new Array(); var values = new Array();

    // Per ogni attributo alfanumerico...
    var size = (myxml.getElementsByTagName(featureMember_Name)[0].
               childNodes[0+_add].childNodes.length - _add) / _molt;

    for (var j=2; j<size; j++)
    {
      nam = myxml.getElementsByTagName(featureMember_Name)[i].
                  childNodes[0+_add].childNodes[(j * _molt) +_add].tagName;
      var nam = nam.split(":");

      val = myxml.getElementsByTagName(featureMember_Name)[i].
                 childNodes[0+_add].childNodes[(j * _molt) +_add].
                 childNodes[0].nodeValue;

      names.push(nam[1]);
      values.push(val);
    }
/////////////////////////////////////

    _data[0][i] = tmp[0];
    _data[1][i] = tmp[1];

    _data[2][i] = names;
    _data[3][i] = values;
  }

  return _data;
}

function parsePointsFromJSON(json, p_countid)
{
 var place;
  var _data = new Array();
  _data[0] = new Array();
  _data[1] = new Array();
  _data[2] = new Array();

  jsonArray = eval('(' + json + ')');
  var count = jsonArray.length;
  if (p_countid != null) { document.getElementById(p_countid).value=count;}
  for(var i=0; i<count; i++)
  {
    place = jsonArray[i];
    var values = new Array();
    values.push(place.id);
    values.push(place.n);
    if (place.i == 1) {values.push('<img style=\"padding-top:2px;padding-bottom:3px;\" src=\"/images/Places/' + place.id + '_0__TN125x100.jpg\" alt=\"' + place.n + '\" />');} else {values.push('');}
    values.push(place.d);
    var Imgline = '';  var pp = place.p;
    for(var x=0; x<pp.length; x++)
    {
        if (pp[x].indexOf('.gif') > 0)
            Imgline += '<img src=\"/images/PointPlace/Symbols/' + pp[x] + '\"  alt=\"' + pp[x].substr(0, pp[x].indexOf('-')) + '\" /> ';
        else
            Imgline += pp[x] + ' ';
    }
    values.push(Imgline);
    _data[0][i] = dc(place.a);
    _data[1][i] = dc(place.b);
    _data[2][i] = values;
  }
  return _data;
}

// msTool class prototype
function msTool(p_title, p_event_button, p_icon, p_event_map)
{
  var _tag = document.createElement('img');
  var _map; var _toolbar;

  var _event_button = null;	//p_event_button;
  if (p_event_button != null)
  { _event_button = function(e){p_event_button(e, _map);} }

  var _event_map = null;
  if (p_event_map != null)
  {
    _event_map = function(e)
    {
      var xx = _map.getClick_X(e); var yy = _map.getClick_Y(e);
      p_event_map(e, _map, xx, yy, _map.xPixel2Real(xx), _map.yPixel2Real(yy));
    }
  }

  this.eventClick = function(e)
    {
      if (_event_map != null)
      {
        _toolbar.removeMapEvents();  // Remove all events
        Event.observe(_map.getTagEvent(), 'mousedown', _event_map);
      }
      if (_event_button != null) {_event_button();}
    }

  _tag.className = 'mscross_tool';
  _tag.oncontextmenu = function(){return false;};
  _tag.onmousedown = function(){return false;};  // Disable drag'n drop
  Event.observe(_tag, 'click', this.eventClick);
  setAlphaPNG(_tag, p_icon);
  _tag.title = p_title;
  setZindex(_tag, '200');
  _tag.style.margin = '0'; _tag.style.padding = '0';
  _tag.style.position = 'absolute';
  _tag.style.cursor = 'pointer';
  _tag.style.display = 'none';

  this.getTag = function(){return _tag;}
  this.setMap = function(p){_map=p;}
  this.setToolbar = function(p){_toolbar=p;}
  this.haveMapEvent = function(){if (_event_map == null){return false;} return true;}
  this.removeMapEvent = function()
  {
    if (_event_map != null)
    { 
    Event.stopObserving(_map.getTagEvent(), "mousedown", _event_map);
    }
  }
}

// msToolbar class prototype
function msToolbar(p_msMap, _control, _default)
{
  var _tagToolbar = document.createElement('div');
  var _toolbarArray = new Array();
  var _msMap = p_msMap;
  var _tagMap = _msMap.getTagMap();

  // Toolbar Default Icons...
  var _iconFullExtentButton = '/iMapPlot/img/alpha_button_fullExtent.png';
  var _iconZoomboxButton    = '/iMapPlot/img/alpha_button_zoombox.png';
  var _iconPanButton        = '/iMapPlot/img/alpha_button_cross.png';
  var _iconZoominButton     = '/iMapPlot/img/alpha_button_zoomIn.png';
  var _iconZoomoutButton    = '/iMapPlot/img/alpha_button_zoomOut.png';

  this.getTag = function(){return _tagToolbar;}
  this.hide = function(){_tagToolbar.style.display = 'none';}

  this.removeMapEvents = function()
  { for (i=0; i<_toolbarArray.length; i++){_toolbarArray[i].removeMapEvent();} }

  this.addMapTool = function(p_tool)
  {
    p_tool.setMap(_msMap);
    p_tool.setToolbar(this);
    _toolbarArray.push(p_tool);
    _tagToolbar.appendChild(p_tool.getTag());
    this.redraw();
  }

  this.redraw = function()
  {
    if ( (_control == 'standard')      ||
         (_control == 'standardRight') ||
         (_control == 'standardCornerRight') )
    {
      box.style.left   = (parseInt(_tagMap.style.width)-(40+_msMap.getBorder()*2)) +'px';
      box.style.top    = '0px';
      box.style.width  = '40px';
      box.style.height = _tagMap.style.height;
      for (i=0; i<_toolbarArray.length; i++)
      { setPos(_toolbarArray[i].getTag(), parseInt(box.style.left)+5, (i*40)+5); }
    }
    if ( (_control == 'standardLeft') || (_control == 'standardCornerLeft') )
    {
      for (i=0; i<_toolbarArray.length; i++){setPos(_toolbarArray[i].getTag(), 3, (i*40)+5 );}
      box.style.left   = '0px';
      box.style.top    = '0px';
      box.style.width  = '40px';
      box.style.height = _tagMap.style.height;
    }
    if (_control == 'standardUp')
    {
      for (i=0; i<_toolbarArray.length; i++){setPos(_toolbarArray[i].getTag(), (i*40)+5, 5 );}
      box.style.left   = '0px';
      box.style.top    = '0px';
      box.style.width  = _tagMap.style.width;
      box.style.height = '40px';
    }
  }

  _tagToolbar.oncontextmenu = function(){return false;};
  setZindex(_tagToolbar, '100');
  _tagToolbar.style.position = 'absolute';

  box = document.createElement('div');
  box.oncontextmenu  = function(){return false;};
  setZindex(box, '100');
  box.style.position = 'absolute';
  box.style.display  = '';
  box.style.border     = '0px';
  box.style.margin     = '0px';
  box.style.padding    = '0px';
  box.style.background = '#404040';
  box.style.lineHeight = '0';
  box.style.opacity    = '0.20';               // Gecko
  box.style.filter     = 'alpha(opacity=20)';  // Windows
  _tagToolbar.appendChild(box);

  if (_default == true)
  {
  if (_control == 'search') {
    var t_zoom    = new msTool('Zoom', _msMap.setActionZoombox, _iconZoomboxButton, function(e, map, x, y){map.zoomStart(e);});
    this.addMapTool(t_zoom);
  } else {  
    var t_fullext = new msTool('Full Extent', _msMap.fullExtent, _iconFullExtentButton);
    var t_pan     = new msTool('Pan', _msMap.setActionPan, _iconPanButton, function(e, map, x, y){map.dragStart(e);});
    var t_zoom    = new msTool('Zoom', _msMap.setActionZoombox, _iconZoomboxButton, function(e, map, x, y){map.zoomStart(e);});
    var t_zoomin  = new msTool('Zoom In', _msMap.setActionZoomIn, _iconZoominButton);
    var t_zoomout = new msTool('Zoom Out', _msMap.setActionZoomOut, _iconZoomoutButton);
    this.addMapTool(t_fullext);
    this.addMapTool(t_pan)
    this.addMapTool(t_zoom);
    this.addMapTool(t_zoomin);
    this.addMapTool(t_zoomout);
  }}

  this.redraw();

  // Activate first button with map function
  for (i=0; i<_toolbarArray.length; i++)
  {
    if (_toolbarArray[i].haveMapEvent() == true)
    {
      _toolbarArray[i].eventClick();
      break;
    }
  }
}
