	function pp(x,y){this.x = x; this.y = y;}

	function MathInit(ZeroMap)
	{
		var nImageSizeX = GetImageSizeX(ZeroMap.m_fMapZoom);
		var nImageSizeY = GetImageSizeY(ZeroMap.m_fMapZoom);
		var dOneGridSize = GetOneGridSize(ZeroMap.m_nMapScale);
		
		ZeroMap.m_dWorldRatioX = parseFloat(nImageSizeX / dOneGridSize);
		ZeroMap.m_dWorldRatioY = parseFloat(nImageSizeY / dOneGridSize);
		ZeroMap.m_dScreenRatioX = parseFloat(dOneGridSize / nImageSizeX);
		ZeroMap.m_dScreenRatioY = parseFloat(dOneGridSize / nImageSizeY);
		
	};
	
	function WorldToScreen(dX , dY , ZeroMap)
	{
		var nX;
		var nY;
		
		var ddx = dX - ZeroMap.m_dMapX;
		var ddy = dY - ZeroMap.m_dMapY;
			
		ddx = parseFloat(ddx * ZeroMap.m_dWorldRatioX);
		ddx = ddx + ZeroMap.m_nHalfScreenX;
		ddy = parseFloat(ZeroMap.m_nScreenY) - ( ddy * ZeroMap.m_dWorldRatioY) ;
		
		
		ddy = ddy - parseFloat(ZeroMap.m_nHalfScreenY);
	
		nX = Math.round(ddx);
		nY = Math.round(ddy);   
		
		var tmpt = new pp; 
		tmpt.x = nX;
		tmpt.y = nY;
		return tmpt;
	};
	
	function ScreenToWorld(nX , nY , ZeroMap)
	{
		var dX;
		var dY;
		var ddx = parseFloat(nX - ZeroMap.m_nHalfScreenX);
		nY = ZeroMap.m_nScreenY - nY;
		var ddy = parseFloat(nY - ZeroMap.m_nHalfScreenY);
		
				
		ddx *= ZeroMap.m_dScreenRatioX;
		ddy *= ZeroMap.m_dScreenRatioY;
	
		dX = ddx + ZeroMap.m_dMapX;
		dY = ddy + ZeroMap.m_dMapY;
		
		var tmpt = new pp; 
		tmpt.x = dX;
		tmpt.y = dY;
		return tmpt;
	};
	
	function GetOneGridSize(nScale)
	{
		switch(nScale)
		{
		    case 0: return 0.003125; break; 
            case 1: return 0.006250; break; 
            case 2: return 0.012500; break; 
            case 3: return 0.016666666666666666666666666666667; break; 
            case 4: return 0.020833333333333333333333333332667; break; 
            case 5: return 0.025000; break; 
            case 6: return 0.032500; break; 
            case 7: return 0.050000; break; 
            case 8: return 0.062500; break; 
            case 9: return 0.125000; break; 
            case 10: return 0.187500; break; 
            case 11: return 0.250000; break; 
            case 12: return 0.333333; break; 
            case 13: return 0.500000; break; 
            case 14: return 0.666667; break; 
            case 15: return 0.833333; break; 
            case 16: return 1.000000; break; 
            case 17: return 2.000000; break; 
            case 18: return 8.000000; break; 
		}
		return 0.006250;
	};
	
	
	function GetRealSize(nScale)
	{
		switch(nScale)
		{
			case 0: return 1250.0; break; 
            case 1: return 2500.0; break; 
            case 2: return 5000.0; break; 
            case 3: return 7500.0; break; 
            case 4: return 10000.0; break; 
            case 5: return 15000.0; break; 
            case 6: return 19500.0; break; 
            case 7: return 25000.0; break; 
            case 8: return 37500.0; break; 
            case 9: return 50000.0; break; 
            case 10: return 75000.0; break; 
            case 11: return 100000.0; break; 
            case 12: return 200000.0; break; 
            case 13: return 500000.0; break; 
            case 14: return 800000.0; break; 
            case 15: return 1000000.0; break; 
            case 16: return 2000000.0; break; 
            case 17: return 4000000.0; break; 
            case 18: return 6000000.0; break; 
		}
	};
	
/*		double ddy = max(bound.dX2 - bound.dX1, bound.dY2 - bound.dY1);
	double temp_WMaxy = GetTotSec(ddy);
	double dWidth = 1.2 * temp_WMaxy;
	int screen = min(m_stTransInfo.m_ptScreenSize.x, m_stTransInfo.m_ptScreenSize.y);
	double WidthRatio = (dWidth * 30.0) / screen;
	double dPixPerMap = 1.0 / WidthRatio;
	double dScale = 1.0 / (dPixPerMap * 0.00026458333333333);
	return dScale;
}*/
	function GetBoundaryToScale(x1,y1,x2,y2,ZeroMap)
	{
		var ddy = Math.max(x2 - x1, y2 - y1);
		var temp_WMaxy = GetTotSec(ddy);
		var dWidth = 1.2 * temp_WMaxy;
		var screen = Math.min(ZeroMap.m_nScreenX, ZeroMap.m_nScreenY);
		var WidthRatio = (dWidth * 30.0) / screen;
		var dPixPerMap = 1.0 / WidthRatio;
		var dScale = 1.0 / (dPixPerMap * 0.00026458333333333);

		return dScale;
	};
	
	function GetTotSec(Dgree)
	{
	
		var x_tdgree = parseInt(Dgree);
		var xtmin = (Dgree - parseFloat(x_tdgree)) * 100000000.0;
		var xmin = parseInt(xtmin);
		var x_temp = (xmin * 6);
		var x_min = parseInt(x_temp / 10000000);
		var x_tt = parseInt(x_min * 10000000);
		var x_zz = parseInt(x_temp - x_tt);
		var sx_temp = parseInt(x_zz * 60);
		var x_sec = parseInt(sx_temp / 10000000);
		var dTotsec = parseFloat(x_tdgree * 3600) + parseFloat(x_min * 60) + parseFloat(x_sec);
		return dTotsec;
	};
	
	
	function GetStartPos(nScale)
	{
		var tmpt = new pp; 
		switch(nScale)
		{
			case 0: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 1: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 2: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 3: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 4: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 5: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 6: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 7: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 8: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 9: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 10: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 11: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 12: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 13: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 14: {tmpt.x = 124.0; tmpt.y = 33.0;} break; 
            case 15: {tmpt.x = 121.0; tmpt.y = 32.0;} break; 
            case 16: {tmpt.x = 121.0; tmpt.y = 32.0;} break; 
            case 17: {tmpt.x = 121.0; tmpt.y = 32.0;} break; 
            case 18: {tmpt.x = 124.0; tmpt.y = 33.0;} break;
		}
		return tmpt;
	};
	
	function GetScaleToZoomRatio(dScale , dOneGridSize)
	{
		var dPixPerMap = (1.0 / dScale) / 0.00026458333333333;
		var dPixPerRatio = 1.0 / dPixPerMap;
		var dWidth = (600.0 * dPixPerRatio) / 30.0;
		var temp_WMaxy = GetTotSec(dOneGridSize);
		return (dWidth / temp_WMaxy);
	};
	
	function GetImageSizeX(fMapZoom)
	{
		return parseInt(1024 *  fMapZoom);
	};
	
	function GetImageSizeY(fMapZoom)
	{
		return parseInt(1024 * fMapZoom);
	};
	
	function fnxco(olat)
	{
		var fnxco = 0.0;
		var EE = 0.006674372231315;
		var A  = 1.005037306048555;
		var B  = 0.005047849240300;
		var C  = 0.000010563786831;
		var D  = 0.000000020633322;
		
		fnxco = (6377397.155) * ( 1 - EE ) 
			* ( ( A * olat ) - 0.5 * B * Math.sin(2*olat) + 0.25 
			* C * Math.sin(4*olat) -1. / 6. * D * Math.sin(6*olat) );
		return fnxco;
	};
	
	function rad(x) {return (x) * 3.141592653589793 / 180.0}
	
	function WGS84ToTM(x,y)
	{
		var tmpt = new pp; 
		var b = 0.0, e_prime = 0.0, N = 0.0, origin_lat = 0.0, nn = 0.0, t = 0.0, xy_lon0 = 0.0 , e = 0.0, n = 0.0, lat = 0.0, lon  = 0.0, EE = 0.006674372231315;
		lon = x;
		lat = y;
		xy_lon0 = 127.0 + 10.405 / 3600.0;
		origin_lat = rad(38.0);
		lon -= xy_lon0;
		lon = rad(lon);
		lat = rad(lat);
		b = fnxco(lat) - fnxco(origin_lat);
		N = (6377397.155) / Math.sqrt(1-EE*Math.pow(Math.sin(lat),2.0));
		e_prime = EE / (1-EE);
		nn = e_prime*Math.pow(Math.cos(lat),2.0);
		t = Math.tan(lat);
		e = 1.0 * (lon * N * Math.cos(lat) + Math.pow(lon,3.0) / 6.0 * N *Math.pow(Math.cos(lat),3.0)
		* ( 1 - t * t + nn) + Math.pow(lon,5.0) / 120.0 * N * Math.pow(Math.cos(lat),5.0) 
		* ( 5 - 18 * t * t + Math.pow(t,4.0) ) );
		n = 1.0 * ( b + Math.pow(lon, 2.0) / 2.0 * N * Math.sin(lat) * Math.cos(lat)
		+ Math.pow(lon,4.0) / 24.0 * N * Math.sin(lat) * Math.pow(Math.cos(lat),3.0) 
		* ( 5 - t * t + 9 * nn) );
		tmpt.x = n + (500000);
		tmpt.y = e + (200000);
		return tmpt;
	};
	
	
	function TMToWGS84(ix , iy)
	{
		var n0=0.0,phi1=0.0, BN=0.0, nm=0.0, error=0.0, origin_lat=0.0, nn=0.0, M=0.0, N=0.0, t=0.0;
		var lon=0.0, lat=0.0;
		var xy_lon0=0.0;
		var a = 6377397.155;
		lat = ix- 500000.0;
		lon = iy - 200000;
	
		xy_lon0=127.0+10.405/3600.0;
		origin_lat = rad(38.0);
		n0=fnxco(origin_lat);
		phi1=origin_lat;
	
		do {
			BN=fnxco(phi1);
			nm=n0+lat/1.0;
			error=(BN-nm)*Math.pow((1-0.006674372231315*Math.sin(phi1)*Math.sin(phi1)), 1.5)/(a*(1-0.006674372231315));
			phi1-=error;
		}while(Math.abs(error)>10e-10);
	
		nn=0.006674372231315/(1-0.006674372231315)*Math.pow(Math.cos(phi1),2.0);
		M = a*(1-0.006674372231315)/Math.pow((1-0.006674372231315*Math.pow(Math.sin(phi1),2.0)),1.5);
		N = a/Math.sqrt(1-0.006674372231315*Math.pow(Math.sin(phi1),2.0));
		t=Math.tan(phi1);
	
		lat=phi1-lon*lon*t/(2*M*N*1.0*1.0) 	
			+Math.pow(lon, 4.0)*t/(24*M*Math.pow(N, 3.0)+Math.pow(1.0, 4.0))*(5+3*t*t+nn-9*t*t*nn);
			lon=lon/(N*1.0*Math.cos(phi1))-Math.pow(lon,3.0)/(6*Math.pow(N, 3.0)*Math.pow(1.0, 3.0)*Math.cos(phi1))*(1+2*t*t+nn)
			+Math.pow(lon, 5)/(120*Math.pow(N, 5.0)*Math.pow(1.0, 5.0)*Math.cos(phi1))*(5+28*t*t+24*Math.pow(t, 4.0));
	
		var tmpt = new pp; 
		tmpt.x=lon*180./3.141592653589793+xy_lon0;
		tmpt.y=lat*180./3.141592653589793;
		return tmpt;
	};
	
	function GetScaleToZoomRatio(dscale , ZeroMap)
    {
	    //³Ñ¾î¿Â ½ºÄÉÀÏ °ª¿¡ ÇØ´çµÇ´Â ÁöµµÀÇ ¸éÀû°ªÀ» ÃÊ´ÜÀ§·Î È¯»êÇÏ¿© ±¸ÇÑ´Ù.
	    var dPixPerMap = parseFloat( (1.0 / dscale) / 0.00026458333333333 );
	    var dPixPerRatio = parseFloat( 1.0 / dPixPerMap );

	    var dWidth = parseFloat( (600.0 * dPixPerRatio) / 30.0 );//ÃÊ´ç 30m
	    //ÀüÃ¼ ÁöµµÀÇ ¸éÀûÀ» ÃÊ´ÜÀ§·Î È¯»ê ÇÏ¿© ±¸ÇÑ´Ù.
	    var temp_WMaxy = parseFloat( GetTotSec( GetOneGridSize(ZeroMap.m_nMapScale) ) );
	    //½ºÄÉÀÏ¿¡ ÇØ´çÇÏ´Â Áöµµ ¸éÀû / ÀüÃ¼ Áöµµ¸éÀû  = ½ºÄÉÀÏ¿¡ ´ëÇÑ ÁöµµÀÇ È®´ë, Ãà¼Ò ºñÀ²
	    var zoomratio = dWidth / temp_WMaxy;
	    return zoomratio;
    }

	function GetRealScale(dScale , ZeroMap)
	{
	    var test = GetScaleToZoomRatio(dScale , ZeroMap);
	    dScale *= test;
		if(dScale < 1250.0)return 0;
		else if(dScale >= 1250.0 && dScale < 2500.0)return 1;
		else if(dScale >= 2500.0 && dScale < 5000.0)return 2;
		else if(dScale >= 5000.0 && dScale < 7500.0)return 3;
		else if(dScale >= 7500.0 && dScale < 10000.0)return 4;
		else if(dScale >= 10000.0 && dScale < 15000.0)return 5;
		else if(dScale >= 15000.0 && dScale < 19500.0)return 6;
		else if(dScale >= 19500.0 && dScale < 25000.0)return 7;
		else if(dScale >= 25000.0 && dScale < 37500.0)return 8;
		else if(dScale >= 37500.0 && dScale < 75000.0)return 9;
		else if(dScale >= 75000.0 && dScale < 100000.0)return 10;
		else if(dScale >= 100000.0 && dScale < 200000.0)return 11;
		else if(dScale >= 200000.0 && dScale < 500000.0)return 12;
		else if(dScale >= 500000.0 && dScale < 800000.0)return 13;
		else if(dScale >= 800000.0 && dScale < 1000000.0)return 14;
		else if(dScale >= 1000000.0 && dScale < 2000000.0)return 15;
		else if(dScale >= 2000000.0 && dScale < 4000000.0)return 16;
		else if(dScale >= 4000000.0 && dScale < 6000000.0)return 17;
		else return 18;
	};


