//get the center point of the map in geographic coordinates
//returns a JSON object with longitude and latitude
function getGeoCenterPoint() {
    //get the current map extent
    var xMin = Math.round(map.extent.xmin);
    var yMin = Math.round(map.extent.ymin);
    var xMax = Math.round(map.extent.xmax);
    var yMax = Math.round(map.extent.ymax);
    
    //get the map center
    var xCenter = xMin + ((xMax - xMin) / 2);
    var yCenter = yMin + ((yMax - yMin) / 2);

    //get the center, upper right, and lower left map points in longitude & latitude
    var geoCenter = convertSPtoLL(xCenter, yCenter);
    
    return geoCenter;
}

//convert state plane to longitude/latitude
//returns a JSON object with Longitude, Latitude
function convertSPtoLL(spX,spY) {
    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 = 34.3333 * angRad; 	 	//latitude of origin
    p1 = 35.25 * angRad;  		//latitude of first standard parallel
    p2 = 36.416666 * angRad;  	//latitude of second standard parallel
    m0 = -86.00 * angRad;  	    //central meridian
    x0 = 1968500;               //False easting of central meridian, map units
    
    // 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.
	    spX = spX - x0;
	    pi2 = pi4 * 2;

	    rho = sqrt(pow(spX,2) + pow((rho0 - spY),2));  
	    theta = atan(spX / (rho0 - spY));
	    txy = pow((rho / (a * f)),(1 / n));

	    lon1 = (theta/n) + m0;
		
	    spX = spX + 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 = lon1 / angRad;
		
		//round points to 5 decimal places
        Lat = (Math.round(Lat * 100000)) / 100000;
        Lon = (Math.round(Lon * 100000)) / 100000;    		
		
		var geoPoint = { "longitude":Lon, "latitude":Lat}
        return geoPoint;
    }
}
