﻿//// JScript File

var mapminx; 
var mapminy; 
var mapmaxx; 
var mapmaxy;
var mouseX;
var mouseY;
var xDistance;
var iWidth;
var iHeight;
var yDistance;
var mapX;
var mapY;   
    
function getCoords(){
    if (mapminx == null){
        setTimeout("ChangeClient('GetCoords')", 100);
    }
}    
    
function updateCoords(message){
        
    var ext = message.split(":--:"); 

    var map = Maps[ext[0]];
    iWidth = map.viewWidth;
    iHeight = map.viewHeight;  

    mapminx = parseFloat(ext[1]); 
    mapminy = parseFloat(ext[2]); 
    mapmaxx = parseFloat(ext[3]); 
    mapmaxy = parseFloat(ext[4]);
    xDistance = Math.abs(mapminx - mapmaxx);
    yDistance = Math.abs(mapmaxy - mapminy);   
    
    setMapMouseMoveFunction(map);
}

function setMapMouseMoveFunction(map){    
    map.divObject.onmousemove = myOnMouseMove;
    map.moveFunction = myOnMouseMove;
    map.tempMoveFunction = myOnMouseMove; 
 }

function setVirtualEarthClickFunction(divid){    
    map = Maps[divid];
    MapPoint(map.controlName, "VirtualEarth", false);
    map.divObject.onclick = launchVirtualEarth;
}

function setGoogleStreetViewClickFunction(divid){    
    map = Maps[divid];
    MapPoint(map.controlName, "GoogleStreetView", false);
    map.divObject.onclick = launchGoogleStreetView;
}

function launchVirtualEarth(e)
{
    e = e || window.event;
    var img = document.getElementById("Map1"); 
    var x = e.clientX?(e.clientX):img.offsetLeft; 
    var y = e.clientY?(e.clientY):img.offsetTop; 
    x = x - findPosX(img) + document.documentElement.scrollLeft; 
    y = y - findPosY(img) + document.documentElement.scrollTop; 
    getMapXY(x,y); 
	var latLonDdStr = convertSP(mapX, mapY);
	var ll = latLonDdStr.split("|");
	var LonDD = ll[0];
	var LatDD = ll[1];
	LatDD = (Math.round(LatDD * 100000)) / 100000;
	LonDD = (Math.round(LonDD * 100000)) / 100000;
	var url = "http://maps.mckinneytexas.org/VirtualEarth.aspx?lat=" + LatDD + "&long=" + LonDD;
	var aWindow=window.open(url,"Google","SCROLLBARS=NO,STATUS=NO,RESIZE=NO,WIDTH=525,HEIGHT=325");
}

function launchGoogleStreetView(e)
{
    e = e || window.event;
    var img = document.getElementById("Map1"); 
    var x = e.clientX?(e.clientX):img.offsetLeft; 
    var y = e.clientY?(e.clientY):img.offsetTop; 
    x = x - findPosX(img) + document.documentElement.scrollLeft; 
    y = y - findPosY(img) + document.documentElement.scrollTop; 
    getMapXY(x,y); 
	var latLonDdStr = convertSP(mapX, mapY);
	var ll = latLonDdStr.split("|");
	var LonDD = ll[0];
	var LatDD = ll[1];
	LatDD = (Math.round(LatDD * 100000)) / 100000;
	LonDD = (Math.round(LonDD * 100000)) / 100000;
	var url = "http://maps.mckinneytexas.org/GoogleStreetView.aspx?lat=" + LatDD + "&long=" + LonDD;
	var aWindow=window.open(url,"Google","SCROLLBARS=NO,STATUS=NO,RESIZE=NO,WIDTH=525,HEIGHT=325");
}


function myOnMouseMove(e){ 
    e = e || window.event; 
    var img = document.getElementById("Map1"); 
    var x = e.clientX?(e.clientX):img.offsetLeft; 
    var y = e.clientY?(e.clientY):img.offsetTop; 
    x = x - findPosX(img) + document.documentElement.scrollLeft; 
    y = y - findPosY(img) + document.documentElement.scrollTop; 
    getMapXY(x,y); 

	var latLonDdStr = convertSP(mapX, mapY);
	var ll = latLonDdStr.split("|");
	var LonDD = ll[0];
	var LatDD = ll[1];
	LatDD = (Math.round(LatDD * 100000)) / 100000;
	LonDD = (Math.round(LonDD * 100000)) / 100000;
	window.status = "Latitude: " + LatDD + " Longitude: " + LonDD;
}

function getMapXY(xIn,yIn) {
    mouseX = xIn;
    pixelX = xDistance / iWidth;
    mapX = pixelX * mouseX + mapminx;
    mouseY = iHeight - yIn;
    pixelY = yDistance / iHeight;
    mapY = pixelY * mouseY + mapminy;
     
}					

function findPosX(obj) {
    var curleft = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x) {
        curleft += obj.x;
    }

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;

    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }

    return curtop;
}

//This code is to convert map coordinates in Texas state plane NAD83 survey feet to Latitude and Longitude value-- Haripriya Madabushi
//As the mouse click or move gives you only map coordinates in survey feet; and Latitude and Longitude values are needed for opening Google and Virtual earth windows- Haripriya Madabushi
//custom function in aimsCustom.js for converting WA state plane to lat/long


// Code to convert from map units to decimal degrees.
// Code based on asp code from Gerry Daumiller, Montana State Library 8-23-02 email. ASP code on web at http://nris.state.mt.us/gis/projection/projection.html.  Translated to javascript by Jeff Miller, Jefferson County, WA.

// The formulae this program is based on are from "Map Projections,
// A Working Manual" by John P. Snyder, U.S. GeoLogical Survey
// Professional Paper 1395, 1987, pages 295-298

// Set up the coordinate system parameters.  Need to edit these based on state plane zone.  This for WA North and US Survey Feet.


function convertSP(uX,uY) {
/*
a = 20925604.48;   		//major radius of ellipsoid, map units (NAD 83)
ec = 0.08181905782;  		//eccentricity of ellipsoid (NAD 83)
angRad = 0.01745329252;  	//number of radians in a degree
pi4 = 3.141592653582 / 4;  	//Pi / 4
p0 = 47 * angRad; 	 	//latitude of origin
p1 = 47.5 * angRad;  		//latitude of first standard parallel
p2 = 48.73333333333 * angRad;  	//latitude of second standard parallel
m0 = -120.833333333 * angRad;  	//central meridian
x0 = 1640416.67; 		//False easting of central meridian, map units
*/

//nad83 texas state plane
a = 20925604.48;   		//major radius of ellipsoid, map units (NAD 83)
ec = 0.08181905782;  		//eccentricity of ellipsoid (NAD 83)
angRad = 0.01745329252;  	//number of radians in a degree
pi4 = 3.141592653582 / 4;  	//Pi / 4
p0 = 31.66666667 * angRad;  	 	//latitude of origin
p1 = 32.13333333 * angRad;  		//latitude of first standard parallel
p2 = 33.96666667 * angRad;   	//latitude of second standard parallel
m0 = -98.50000000 * angRad;   	//central meridian
x0 = 1968500.00000000; 		//False easting of central meridian, map units
y0 = 6561666.666667;


// Calculate the coordinate system constants.
with (Math) {
m1 = cos(p1) / sqrt(1 - (pow(ec,2)) * pow(sin(p1),2));  
m2 = cos(p2) / sqrt(1 - (pow(ec,2)) * pow(sin(p2),2));
t0 = tan(pi4 - (p0 / 2));
t1 = tan(pi4 - (p1 / 2));
t2 = tan(pi4 - (p2 / 2));
t0 = t0 / pow(((1 - (ec * (sin(p0)))) / (1 + (ec * (sin(p0))))),ec/2);  
t1 = t1 / pow(((1 - (ec * (sin(p1)))) / (1 + (ec * (sin(p1))))),ec/2);
t2 = t2 / pow(((1 - (ec * (sin(p2)))) / (1 + (ec * (sin(p2))))),ec/2);
n = log(m1 / m2) / log(t1 / t2);
f = m1 / (n * pow(t1,n)); 
rho0 = a * f * pow(t0,n);

// Convert the coordinate to Latitude/Longitude.

// Calculate the Longitude.
uX = uX - x0;
//added this in HM
uY = uY - y0;
pi2 = pi4 * 2;

rho = sqrt(pow(uX,2) + pow((rho0 - uY),2));  
theta = atan(uX / (rho0 - uY));
txy = pow((rho / (a * f)),(1 / n));
lon = (theta / n) + m0;
uX = uX + x0;

// Estimate the Latitude
lat0 = pi2 - (2 * atan(txy));

// Substitute the estimate into the iterative calculation that
// converges on the correct Latitude value.
part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));

while ((abs(lat1 - lat0)) > 0.000000002) {
  lat0 = lat1;
  part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
  lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
  }

// Convert from radians to degrees.
Lat = lat1 / angRad;
Lon = lon / angRad;

//alert(lat);
//alert(lon);

//Round the latitude and longitude
//lat = (CLng(lat * 100000)) / 100000;
//lon = (CLng(lon * 100000)) / 100000;

//Lat = lat.toPrecision(7);
//Lon = lon.toPrecision(8);

return Lon + "|" + Lat;

}
}
//End of Code for converting map units to decimal degrees
