// mapTools.js
// MapTools.js - map interaction (pan / zoom / identify / etc.)
//

function controlNotEnabled() {
	alert("This feature is not yet enabled");
}

function setCurrentExtent(min_x,min_y,max_x,max_y) {
  minx = min_x;
  miny = min_y;
  maxx = max_x;
  maxy = max_y;
}

function setNewExtent(min_x,min_y,max_x,max_y) {
  newMinx = min_x;
  newMiny = min_y;
  newMaxx = max_x;
  newMaxy = max_y;
}

function doZoomFullExtent() {
    document.LocatorForm.DoWhat.value="zoomfull";
	submitForm();	
}

function setZoomBoxSettings() {
  //alert("setZoomBoxSettings()");
  // Set up event capture for mouse movement
  if (isNav && is5up) {
    document.captureEvents(Event.MOUSEMOVE);
    document.captureEvents(Event.MOUSEDOWN);
    document.captureEvents(Event.MOUSEUP);
    document.onmousemove = getMouse;
    document.onmousedown = mapTool;
    document.onmouseup = chkMouseUp;
  } else if (isNav4) {
    // Otherwise the buttons don't work
    getLayer("thmaxy").captureEvents(Event.MOUSEMOVE);
    getLayer("thmaxy").captureEvents(Event.MOUSEDOWN);
    getLayer("thmaxy").captureEvents(Event.MOUSEUP);
    getLayer("thmaxy").onmousemove = getMouse;
    getLayer("thmaxy").onmousedown = mapTool;
    getLayer("thmaxy").onmouseup = chkMouseUp;
  } else {
    document.onmousemove = getMouse;
    document.onmousedown = mapTool;
    document.onmouseup = chkMouseUp;
  }
}

function disableClick() {
  if (isNav && is5up) {
    document.captureEvents(Event.MOUSEMOVE);
    document.captureEvents(Event.MOUSEDOWN);
    document.captureEvents(Event.MOUSEUP);
    document.onmousemove = disable;
    document.onmousedown = disable;
    document.onmouseup = disable;
  } else if (isNav4) {
    // Otherwise the buttons don't work
    getLayer("thmaxy").captureEvents(Event.MOUSEMOVE);
    getLayer("thmaxy").captureEvents(Event.MOUSEDOWN);
    getLayer("thmaxy").captureEvents(Event.MOUSEUP);
    getLayer("thmaxy").onmousemove = disable;
    getLayer("thmaxy").onmousedown = disable;
    getLayer("thmaxy").onmouseup = disable;
  } else {
    document.onmousemove = disable;
    document.onmousedown = disable;
    document.onmouseup = disable;
  }
}

// Check for mouseup
function chkMouseUp(e) { 
  if (zooming || panning) {
    if (mouseX<0)
      mouseX = 0;
    if (mouseX>iWidth)
      mouseX = iWidth;
    if (mouseY<0)
      mouseY = 0;
    if (mouseY>iHeight)
      mouseY = iHeight;
      mapTool(e);
  }
}

// Perform appropriate action with mapTool
function mapTool (e) {
  //alert("mapTool");
  if (activeTool == "setActive") return true;
  getImageXY(e);
// alert("mx="+mouseX+" iW="+iWidth+" my="+mouseY+" iH="+iHeight+" z?="+zooming+" p?="+panning);
  if ((!zooming) && (!panning) && (mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
    if (activeTool == "pan" ) {
      startPan(e);
    } else if (activeTool == "info") {
       identify(e);
    } else if (activeTool == "measure") {
		doMeasure(e);
    } else    
       startZoomBox(e);
    return false;    
  } else if (zooming) {
    getMouse(e);
    stopZoomBox(e);
    hideZoomBox();
  } else if (panning) {
    getMouse(e);
    stopPan(e);
  }
  return true;
}

// Convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
  // If we're completely zoomed out, the values might not be correct otherwise
  reaspect();
  mouseX = xIn;
  var pixelX = (maxx-minx) / iWidth;
  mapX = pixelX * mouseX + minx;
  mouseY = iHeight - yIn;
  var pixelY = (maxy-miny) / iHeight;
  mapY = pixelY * mouseY + miny;
}

function getImageXY(e) {
  if (isNav) {
    mouseX=e.pageX;
    mouseY=e.pageY;
  } else {
    mouseX=event.clientX + document.body.scrollLeft;
    mouseY=event.clientY + document.body.scrollTop;
  }
  // Subtract offsets from page left and right
  mouseX = mouseX-hspc;
  mouseY = mouseY-vspc;
}	

// Recenter map is the default option
function recenter(e) {
  hideZoomBox();
  getMapXY(mouseX,mouseY);
  var widthHalf = Math.abs(maxx - minx) / 2;
  var heightHalf = Math.abs(maxy - miny) / 2;
  newMinx = mapX - widthHalf;
  newMaxx = mapX + widthHalf;
  newMaxy = mapY + heightHalf;
  newMiny = mapY - heightHalf;

  if (activeTool == "zoomin") {
      addPercent(-50);
  } else if (activeTool == "zoomout") {
      addPercent(75);
  } 
  // update the extent
  setCurrentExtent(newMinx, newMiny, newMaxx, newMaxy);        
  submitForm(); //in ManagueUI.js
}

// Get the coords at mouse position
function getMouse(e) {
  window.status="";
  getImageXY(e);

  if (zooming) {
    if (mouseX<0)
      mouseX = 0;
    if (mouseX>iWidth)
      mouseX = iWidth;
    if (mouseY<0)
      mouseY = 0;
    if (mouseY>iHeight-bottomBorderHeight)
      mouseY = iHeight-bottomBorderHeight;
    x2=mouseX;
    y2=mouseY;
    setClip();
    return false;
  } else if (panning) {
    x2=mouseX;
    y2=mouseY;
    panMouse();	
    return false;
  } 
}

// Start zoom in.... box displayed
function startZoomBox(e) {
//  alert("startZoomBox(e)");
  if (extentOutsideMap) {
    hideZoomBox();
    //alert("extentOutsideMap=true");
    return;
  }
  getImageXY(e);	
  // Keep it within the MapImage
  if ((mouseX<iWidth) && (mouseY<iHeight)) { //-bottomBorderHeight)) {
    if (!zooming) {
      x1=mouseX;
      y1=mouseY;
      x2=x1+1;
      y2=y1+1;
      zooming=true;
      clipLayer("zoomBoxTop",x1,y1,x2,y2);
      clipLayer("zoomBoxLeft",x1,y1,x2,y2);
      clipLayer("zoomBoxRight",x1,y1,x2,y2);
      clipLayer("zoomBoxBottom",x1,y1,x2,y2);
      showZoomBox();
    }
  } else {
    if (zooming) {
      stopZoomBox(e);
    } 
  }
  return false;	
}

// Stop zoom box display... zoom in
function stopZoomBox(e) {
  //alert("stopZoomBox(e)");
  zooming=false;
  var width = Math.abs(maxx - minx);
  var height = Math.abs(maxy - miny); 
  if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
      // If the zoom box is too small
      recenter(e);
  } else {
      var pixelX = width / iWidth;
      var theY = iHeight - zmaxy;
      var pixelY = height / iHeight;
      var url = "error.html";
      if (activeTool == "zoomin") {
        newMinx = mouse2GeoX(iWidth,zminx); 
        newMiny = mouse2GeoY(iHeight,zminy); 
        newMaxx = mouse2GeoX(iWidth,zmaxx); 
        newMaxy = mouse2GeoY(iHeight,zmaxy);         
        geoConstraints(newMinx, newMiny, newMaxx, newMaxy);
      }
      if (activeTool == "zoomout") {
	newMinx = minx - (width*2/2);
	newMaxx = maxx + (width*2/2);
	newMaxy = maxy + (height*2/2);
	newMiny = miny - (height*2/2);				       
      }
      if (activeTool!="selectbyrect") {
        // update the extent
        setCurrentExtent(newMinx, newMiny, newMaxx, newMaxy); 
            window.setTimeout('submitForm();', 50); //submit the form
      } else {
        useLimitExtent = true;
        queryString = "";
        selMinx = mouse2GeoX(iWidth,zminx); 
        selMiny = mouse2GeoY(iHeight,zmaxy); 
        selMaxx = mouse2GeoX(iWidth,zmaxx); 
        selMaxy = mouse2GeoY(iHeight,zminy);
        url = getFeatures(0, 0, selMinx, selMiny, selMaxx, selMaxy, "");
        window.open(url,"GML_Export");
      }      
  } 			
  return true;
}

function clearMap() {
	frmGC=document.LocatorForm;  
	frmGC.AddressX.value = "";
	frmGC.AddressY.value = "";
	frmGC.AddressLabel.value = "";
	frmGC.SchoolInfo.value = "";
	frmGC.SchoolID.value = "";
	frmGC.SchoolName.value = "";
	setNewExtent(minx,miny,maxx,maxy); //make sure the extent variables are set ("newMinX", etc.)
	//clear the match results ...
	frmGC.MatchPanelVis.value = "hidden";
	chore = "ClearMap";	
	submitForm();
}

function doGeocode() {
	frmGC=document.LocatorForm;  
	strNum = frmGC.AddNum.value;
	strName = frmGC.AddStreet.value;
	strType = frmGC.AddType.value;
//	alert(strNum + " " + strName + " " + strType);
	setNewExtent(minx,miny,maxx,maxy); //make sure the extent variables are set ("newMinX", etc.)
	//clear the last address point and school information
	frmGC.AddressX.value="";
	frmGC.AddressY.value="";
	frmGC.AddressLabel.value = "";
	frmGC.SchoolInfo.value = "";
	frmGC.SchoolID.value = "";
	frmGC.SchoolName.value = "";	
	chore = "FindAddress";
	submitForm();
}

function findSchool() {
	frmGC=document.LocatorForm; 
	frmSchool=document.SchoolForm;
	if ((frmGC.AddressX.value == "") || (frmGC.AddressY.value == "")) {
		alert("Please zoom to an address before locating a school.");
		return;
	}
	idxYear = frmSchool.whichYear.selectedIndex;
	idxLevel = frmSchool.schoolLevel.selectedIndex;
	frmGC.SearchYear.value=frmSchool.whichYear.options[idxYear].value;
	frmGC.SearchLevel.value=frmSchool.schoolLevel.options[idxLevel].value;
	frmGC.idxSelYear.value=idxYear;
	frmGC.idxSelLevel.value=idxLevel;
	setNewExtent(minx,miny,maxx,maxy); //make sure the extent variables are set ("newMinX", etc.)
	chore = "FindSchool";
	submitForm();
}
function zoomToAddress(xCoord, yCoord, labelText, selRowNum) {
//this function will zoom the map to the extent of the point (x/y coords) passed in
	//build an envelope centered on the point
	//alert("x="+xCoord+" y="+yCoord);
	ptMinx = xCoord - (zoomWidth / 2); //zoomWidth and zoomHeight are defined in "Globals.js"
	ptMaxx = xCoord + (zoomWidth / 2);
	ptMiny = yCoord - (zoomHeight / 2);
	ptMaxy = yCoord + (zoomHeight / 2);
	//alert("xmin="+ptMinx+" xmax="+ptMaxx+" ymin="+ptMiny+" ymax="+ptMaxy);
 	//set the new min/max x/y variables to the new extent ...
	newMinx = ptMinx;
	newMiny = ptMiny;
	newMaxx = ptMaxx;
	newMaxy = ptMaxy;
	//set the desired task on the server
	chore = "ShowAddress";
	frmGC=document.LocatorForm;  	
	frmGC.AddressX.value = xCoord;
	frmGC.AddressY.value = yCoord;
	frmGC.AddressLabel.value = labelText;
	frmGC.idxSelAddressRow.value = selRowNum;
	submitForm();
	return true;	
}

// Clip zoom box layer to mouse coords
function setClip() {	
  if (x1>x2) {
    zmaxx=x1;
    zminx=x2;
  } else {
    zminx=x1;
    zmaxx=x2;
  }
  if (y1>y2) {
    zminy=y1;
    zmaxy=y2;
  } else {
    zmaxy=y1;
    zminy=y2;
  }
	
  if ((x1 != x2) && (y1 != y2)) {
    var ovBoxSize = 1;
    clipLayer("zoomBoxTop",zminx, zmaxy, zmaxx, zmaxy+ovBoxSize);
    clipLayer("zoomBoxLeft",zminx, zmaxy, zminx+ovBoxSize,  zminy);
    clipLayer("zoomBoxRight",zmaxx-ovBoxSize, zmaxy, zmaxx, zminy);
    clipLayer("zoomBoxBottom",zminx, zminy-ovBoxSize, zmaxx, zminy);
  }
}

// Start pan.... image will move
function startPan(e) {
  if (extentOutsideMap) {
    hideZoomBox();
 //   alert("extentOutsideMap=true");
    return;
  }
  getImageXY(e);
  // Keep it within the MapImage
  if ((mouseX<iWidth) && (mouseY<iHeight)) {
    if (panning) {
      stopPan(e);
    } else {
      x1=mouseX;
      y1=mouseY
      x2=x1+1;
      y2=y1+1;
      panning=true;
    }
  } 
  return false;
}

// Stop moving image.... pan 
function stopPan(e) {
  showLayer("loadLayer");
  //document.mapImage.src=background.src;
  if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
    // The move is too small
    recenter(e);
    //alert("x1 "+x1+" x2 "+x2+" y1 "+y1+" y2 "+y2+" newMinx "+newMinx+" newMiny "+newMiny+" newMaxx "+newMaxx+" newMaxy "+newMaxy);
  } else  {
    hideLayer("theMap");  
    panning=false;
    var width = Math.abs(maxx - minx);
    var height = Math.abs(maxy - miny);
    var tempLeft=minx;
    var tempRight=maxx;
    var tempTop=maxy;
    var tempBottom=miny;
    var ixOffset = x2-x1;
    var iyOffset = y1-y2;
    pixelX = width / iWidth;
    var theY = iHeight - zmaxy;
    pixelY = height / iHeight;
    var xOffset = pixelX * ixOffset;
    var yOffset = pixelY * iyOffset;
    newMaxy = maxy - yOffset;
    newMaxx = maxx - xOffset;
    newMinx = minx - xOffset;
    newMiny = miny - yOffset;
    //alert("x1 "+x1+" x2 "+x2+" y1 "+y1+" y2 "+y2+" newMinx "+newMinx+" newMiny "+newMiny+" newMaxx "+newMaxx+" newMaxy "+newMaxy);
    // update the extent
    setCurrentExtent(newMinx, newMiny, newMaxx, newMaxy); 
//    moveLayer("theMap",hspc,vspc);
    clipLayer("theMap", 0, 0, iWidth, iHeight);    
    frmGC=document.LocatorForm;
    window.setTimeout('submitForm();', 50); //submit the form
    setTimeout('showLayer("theMap")', 600);

  }		
  return true;	
}

// Move map image with mouse
function panMouse() {
  var xMove = x2-x1;
  var yMove = y2-y1;
  var cLeft = -(xMove);
  var cTop = -(yMove);
  var cRight = iWidth + hspc;
  var cBottom = iHeight + vspc;
  if (xMove>0) {
    cLeft = 0;
    cRight = iWidth - xMove;
  }
  if (yMove>0) {
    cTop = 0;
    cBottom = iHeight - yMove;
  }
  clipLayer("MapImg2",cLeft,cTop,cRight,cBottom);
//  moveLayer("MapImg2",xMove+hspc,yMove+vspc);
  moveLayer("MapImg2",xMove + 20,yMove +10);
 
  return false;
}

// identify feature
function identify(e) {
//build a small rectangle to perform the identify with ...
   x1=mouseX-7;
   y1=mouseY-5;
   x2=x1+12;
   y2=y1+10;
// clipLayer("zoomBoxBottom",x1, y1, x2, y2);
 // showLayer("zoomBoxBottom");   
//get the map coords for the lower left corner 
  getMapXY(x1,y1);
  idMinX = mapX;
  idMinY = mapY;
//get the map coords for the upper right corner  
  getMapXY(x2,y2);
  idMaxX = mapX;
  idMaxY = mapY;
 // alert("idminx="+idMinX+" miny="+idMinY+" maxx="+idMaxX+" maxy="+idMaxY);
 //set the desired task on the server
  chore = "IDStreet";
  //store the ID click (x/y coords)
  document.LocatorForm.IDXMin.value = idMinX;
  document.LocatorForm.IDYMin.value = idMinY;
  document.LocatorForm.IDXMax.value = idMaxX;
  document.LocatorForm.IDYMax.value = idMaxY;
  //store the current map extent
  setNewExtent(minx,miny,maxx,maxy); //make sure the extent variables are set ("newMinX", etc.)
  submitForm(); //submit the form
}

function showPopup(page, popName, winWidth, winHeight) {
//a simple function that will display a new page in a separate browser. The browser will not have the toolbar, location, or status bar displayed.
	popWin = window.open(page, popName, "toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,width="+winWidth+",height="+winHeight+",top=200,left=200");
}

function printMap() {
	schoolInfo = document.LocatorForm.SchoolInfo.value;
	mapURL = document.images["imgMap"].src;
	url = "PrintMap.aspx?SchoolInfo="+schoolInfo+"&MapImage="+mapURL;

	showPopup(url, "print", 660, 600)
}

function showPrintForm() {
	showPopup("/WebApplicationBaseCamp/ElPasoSiteMap/PrintMap.aspx", "printform", 400, 100);
}

function hideZoomBox() {
  hideLayer("zoomBoxTop");
  hideLayer("zoomBoxLeft");
  hideLayer("zoomBoxRight");
  hideLayer("zoomBoxBottom");
}

function addPercent(value) {

  // add %
  value = 1 + value/100;
	
  var width = Math.abs(newMaxx-newMinx);
  var height = Math.abs(newMaxy-newMiny);
  var cx = newMinx + width/2;
  var cy = newMiny + height/2;
	
  newMinx = cx - width*value/2;
  newMiny = cy - height*value/2;
  newMaxx = cx + width*value/2;
  newMaxy = cy + height*value/2;
}

function showZoomBox() {
  showLayer("zoomBoxTop");
  showLayer("zoomBoxLeft");
  showLayer("zoomBoxRight");
  showLayer("zoomBoxBottom");
}

function reaspect() {
  // Check if extent is too big or too small
  width = maxx - minx;
  height = maxy - miny;
  if (height == 0) height = 0.0001;
    fullWidth = fullMaxx - fullMinx;
    fullHeight = fullMaxy - fullMiny;
    if (fullHeight == 0) fullHeight = 0.0001; 

  if (width/fullWidth > height/fullHeight)
    percent = width/fullWidth;	
  else 
    percent = height/fullHeight;
  if (percent < minScale) {
    midX = minx + ((maxx-minx) / 2); 
    midY = miny + ((maxy - miny) / 2); 
    minx = midX - (fullWidth / 2 * minScale);
    maxy = midY + (fullHeight / 2 * minScale);
    maxx = midX + (fullWidth / 2 * minScale);
    miny = midY - (fullHeight / 2 * minScale);
    width = maxx - minx;
    height = maxy - miny;
  } else if (percent > maxScale) {
    midX = minx + ((maxx-minx) / 2);
    midY = miny + ((maxy - miny) / 2);
    minx = midX - (fullWidth / 2 * maxScale);
    maxy = midY + (fullHeight / 2 * maxScale);
    maxx = midX + (fullWidth / 2 * maxScale);
    miny = midY - (fullHeight / 2 * maxScale);
    width = maxx - minx;
    height = maxy - miny;
  }

  // If we move or change the extent of the map 
  // and the map is somewhere at the side
  // place it inside full extent 

  // Stretch the image to the maxx size
  ratio = iWidth / iHeight;
  if (ratio == 0) ratio = 0.0001; 
  if (width/height < ratio) {
    // Stretch x 
    mid = minx + width/2;
    minx = mid - (height * ratio)/2;
    maxx = mid + (height * ratio)/2;
    width = maxx - minx;
  } else {
    // Stretch y
    mid = miny + height/2;
    miny = mid - (width / ratio)/2;
    maxy = mid + (width / ratio)/2;
    height = maxy - miny;
  }

  // Now position the image maxx
  if ((minx + width > fullMaxx) && (maxx - width >= fullMinx)) {
    minx = fullMaxx - width;
    maxx = fullMaxx;
  }
  if ((maxx - width < fullMinx) && (minx + width <= fullMaxx)) {
    minx = fullMinx;
    maxx = fullMinx + width; 
  }
  if ((minx + width >= fullMaxx) && (maxx - width <= fullMinx)) {
    minx = (fullMaxx+fullMinx)/2 - width/2;
    maxx = (fullMaxx+fullMinx)/2 + width/2;
  }

  if ((miny + height > fullMaxy) && (maxy - height >= fullMiny)) {
    miny = fullMaxy - height; 
    maxy = fullMaxy;
  }
  if ((maxy - height < fullMiny) && (miny + height <= fullMaxy)) {
    miny = fullMiny;
    maxy = fullMiny + height; 
  }
  if ((miny + height >= fullMaxy) && (maxy - height <= fullMiny)) {
    miny = (fullMaxy+fullMiny)/2 - height/2;
    maxy = (fullMaxy+fullMiny)/2 + height/2;
  }
}

// ******* geoConstraints(obRef) *************
// Check for the geographical and proportions constraints of the bounding box
function geoConstraints(x1, y1, x2, y2)
{  	
  // check min and max values
  if (x1>x2){
  	var tmpX1=x1;
  	x1=x2;
	x2=tmpX1;
	}
  if (y1>y2){
  	var tmpY1=y1;
  	y1=y2;
	y2=tmpY1;
	}				
  // check first if current BBox obeys the initial proportions	  
  var initPropor = (fullMaxx - fullMinx) / (fullMaxy - fullMiny);    
  var Propor = (x2 - x1) / (y2 - y1);   
  if (initPropor!=Propor)
  	{
		var sizeX = x2 - x1;
		var sizeY = y2 - y1;
		if (sizeY<sizeX){
			var midY= y1 + (sizeY)/2;
			var newSizeY= (sizeX)/initPropor;
			y1= midY - newSizeY / 2;
			y2= midY + newSizeY / 2;
			}
		else{
			var midX= x1 + (sizeX)/2;
			var newSizeX= (sizeY)*initPropor;
			x1= midX - newSizeX / 2;
			x2= midX + newSizeX / 2;
			}			
	}
  
  // check if both BBox axis are inside the initial BBox
  // check X-axis 
  var SizeX = x2 - x1;
  if (x1<fullMinx)
     {      
	      x1 = fullMinx;
          x2 = x1 + SizeX;
          if (x2>fullMaxx) { x2=fullMaxx}
     }
  else
	{
       if (x2>fullMaxx)
          {
            x2 = fullMaxx;
            x1 = x2 - SizeX;
          }
     }
  // check Y-axis 
  var SizeY = (y2 - y1);
  
  if (y1<fullMiny)
     {
       y1 = fullMiny;
       y2 = y1 + SizeY;
       if (y2>fullMaxy) {y2=fullMaxy}
     }
  else
     {
       if (y2>fullMaxy)
          {
            y2= fullMaxy;
            y1= y2 - SizeY;
          }
     }
  setNewExtent(x1,y1,x2,y2)   
}

// *** Mouse to geographical conversion function
function mouse2GeoX(width,mX) {
  return minx + mX * (maxx - minx)/width;
}
function mouse2GeoY(height,mY) {
  return maxy - mY * (maxy - miny)/height;
}

// ***  Geographical to mouse conversion function
function geo2MouseX(obRef,cX) {
  return Math.round(obRef.width * (cX - x1)/ (x2 - x1) )
}
function geo2MouseY(obRef,cY) { 
  return Math.round( obRef.height * (y2 - cY) / ( y2 - y1 )); 
}

function checkImageStatus(url) {
    if(url.indexOf("png") != -1 ) {
       return true;
    } else {
       return false;
    }
} 
