// loaded in the Head of map.aspx
// script-wide variables
var LastX = -1;							// the last place the ruler was clicked: initial values
var LastY = -1;
var cKmToMiles = 0.621369;				// conversion from km to miles
var dragging = false;

// set up English strings used in these scripts. They may be overridden by translate.js
var _miles_this_click = " miles this click";
var _km_this_click = " km this click";
var _ft = "ft";
var _Language = "english";

function GetDiv(divname) {
// this is a constructor function for a reference to the properties of a division.
// pass in the name of the division.
  if(document.getElementById) {
    this.obj=document.getElementById(divname).style;
  } else {
   this.obj=eval(divname + ".style");
  }
}

function pagecode() {
  // code to run when the page is loaded
  // called from BODY onload of Map.aspx
  // use the logo height for positioning the minimap and minibox
  //currentdiv = new GetDiv("divminimap");
  //currentdiv.obj.top = parseInt(currentdiv.obj.top) + parseInt(document.images[0].height);
  //currentdiv.obj.visibility = "visible";

  //currentdiv = new GetDiv("divminibox");
  //currentdiv.obj.top = parseInt(currentdiv.obj.top) + parseInt(document.images[0].height);
  //currentdiv.obj.visibility = "visible";
}

function tooldown() {
  // called from docdblclick & docmouseup
  // return true if a tool is in use
  var atooldown = false;
  if(document.getElementById('ZoomInToolControl1_Image').src.indexOf("Inactive") == -1){atooldown = true};
  if(document.getElementById('ZoomOutToolControl1_Image').src.indexOf("Inactive") == -1){atooldown = true};
  if(document.getElementById('PanToolControl1_Image').src.indexOf("Inactive") == -1){atooldown = true};
  if(document.getElementById('DistanceToolControl1_Image').src.indexOf("Inactive") == -1){atooldown = true};
  return atooldown;
}

function mapbounds(elementname) {
  // a constructor for the bounds object used by GetPointsString and docmouseup
  var thismap = document.getElementById(elementname);
  if(thismap.origin) {
    // the map has been assigned properties
    this.x = thismap.origin.x;
    this.y = thismap.origin.y;
    this.right = thismap.offsetWidth + thismap.origin.x;
    this.bottom = thismap.offsetHeight + thismap.origin.y;
  } else {
    // this is the first click of a new session so make it impossible to hit the map
    this.x = 0;
    this.y = 0;
    this.right = 0;
    this.bottom = 0;    
  }
}

function showloading() {
  // called by docmouseup & submitdocument & docdblclick
  // show the 'loading' graphic
  var bardiv = new GetDiv("divbar");
  bardiv.obj.visibility = "visible";
  return true;
}

function hideloading() {
  // called by show_help
  // hide the 'loading' graphic
  var bardiv = new GetDiv("divbar");
  bardiv.obj.visibility = "hidden";
  return true;
}

function docdblclick(e) {
  // called by the dblclick of the document as docdblclick(event)
  // shows the loading graphic if the double-click was in the bounds of the map
  var map1bounds = new mapbounds("MapControl1_Image");	// get the map bounds
  if(e.clientX >= map1bounds.x && e.clientX <= map1bounds.right && e.clientY >= map1bounds.y && e.clientY <= map1bounds.bottom) {
  // if(e.srcElement.id == "MapControl1_Image" || e.srcElement.id == "MapInfoWebEntity") {
    // the double-click was on the map, so if a tool is selected, show the loading graphic
    if(tooldown) {
      showloading();
    }
  }
}

function docmouseup(e) {
  // called by the onclick event of the document as onclick(event)
  // does all the non-standard processing
  // save a hidden field to say if the pointsel or label tool is active
  if(e.srcElement) {
    if(e.srcElement.id == "ApplyChanges" || e.srcElement.id == "SearchButton" || e.srcElement.id == "PrintTool" ||
        e.srcElement.id == "DefaultFindButton" || e.srcElement.id.substr(0,4) == "Show") {
        // one of these buttons has been pressed  so show the 'loading' graphic
        showloading();
        return true;
     }
  }
  var toolname = "";
  var circlepoints;
  var map1bounds = new mapbounds("MapControl1_Image");	// get the map bounds
  if(e.clientX < map1bounds.x || e.clientX > map1bounds.right || e.clientY < map1bounds.y || e.clientY > map1bounds.bottom) {
  // the line below doesn't work for a radius circle because it has no .id
  // if(e.srcElement.id != "MapControl1_Image" && e.srcElement.id != "MapInfoWebEntity") {
    // the click was not in the bounds of the map, so do nothing.
    return true;
  }
  var MapZoom = document.Form1.MapZoom.value;  
  if(document.Form1.MapControl1_Image.MapTools.CurrentTool) {
	// there is a tool selected so get it
    toolname = document.Form1.MapControl1_Image.MapTools.CurrentTool.Name;
  }
  if(toolname == "MapInfoWebDistance") {
    // code to display the distance between clicks of the ruler.
    // it captures the click even if you click on the line itsself.
    // the ruler tool uses onmouseup, so we do too. 
    if(LastX != -1) {
      // this is a second or subsequent click
      if(e.type == "dblclick") {
        // this is the end of a polyline, so show the 'loading' graphic
        showloading();
        return true;
      }
      var ThisX = e.clientX - LastX;
      var ThisY = e.clientY - LastY;
      var ThisClick = Math.sqrt((ThisX * ThisX) + (ThisY * ThisY));
	  if(ThisClick != 0) {
	    ThisClick = (MapZoom / (map1bounds.right - map1bounds.x)) * ThisClick;
	    if(document.Form1.UnitOfMeasure.value == "imperial") {
			document.Form1.distances.value = Math.round((ThisClick * cKmToMiles) * 100)/100 + _miles_this_click;
			if(MapZoom < 3) {
				var DistFt = Math.round((ThisClick * cKmToMiles * 5280) * 100)/100 + _ft;
				document.Form1.distances.value = document.Form1.distances.value + ", " + DistFt;
			}
		} else {
			document.Form1.distances.value = Math.round(ThisClick * 100)/100 + _km_this_click;
			if(MapZoom < 3) {
				var DistKm = Math.round((ThisClick * 1000) * 100)/100 + " m";
				document.Form1.distances.value = document.Form1.distances.value + ", " +  DistKm;
			}			
		}
      }
    }
    LastX = e.clientX;
    LastY = e.clientY;
    return true;
  }
  
  if(tooldown()) {
    // show the 'loading' graphic
    showloading();
  }
}

function switchunits() {
// called by the onmousedown of the mile_km control tool button
// change the UnitOfMeasure control and submit the map
  if(document.Form1.UnitOfMeasure.value == "imperial") {
    document.Form1.UnitOfMeasure.value = "metric";
  } else {
    document.Form1.UnitOfMeasure.value = "imperial";
  }
  showloading();
  // create a hidden field for SelectableLayers  
  HideSelectableLayers();
  // submit this page
  document.Form1.submit(); 
}

function HideSelectableLayers() {
	// set up a hidden SelectableLayers field
	// without setting up the hidden field, SelectableLayers is emptied by a submit()
	// called throughout this script

	MapInfoWebCreateSelectableLayerField("MapControl1");
}
