//Step1Map 0.4
function Step1Map()
{
	function K_Function(){}
	//读取网址后缀的函数
	K_Function.GetQueryString=function(key)
	{
		var returnValue =""; 
		var sURL = window.document.URL;
		if (sURL.indexOf("?") > 0)
		{
			var arrayParams = sURL.split("?");
			var arrayURLParams = arrayParams[1].split("&");
			for (var i = 0; i < arrayURLParams.length; i++)
			{
				var sParam =  arrayURLParams[i].split("=");
				if ((sParam[0] ==key) && (sParam[1] != ""))
					returnValue=sParam[1];
			}
		}
		return returnValue;
	}
	K_Function.GetCallBack=function(obj,func)
	{
		return function(){return func.apply(obj,arguments)};
	}
	K_Function.InheritClass=function(a,b)
	{
		var c=function(){};
		c.prototype=b.prototype;
		a.prototype=new c();
	};
	//Cookie操作函数
	K_Function.GetCookieVal=function(offset)
	{
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
			endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	}
	K_Function.SetCookie=function(name, value)
	{
		var expdate = new Date();
		var argv = K_Function.SetCookie.arguments;
		var argc = K_Function.SetCookie.arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
		document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))+((secure == true) ? "; secure" : "");
	}
	K_Function.DelCookie=function(name)
	{
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval =K_GetCookie(name);
		document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
	}
	K_Function.GetCookie=function(name)
	{
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen)
		{
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
				return K_Function.GetCookieVal(j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
		return null;
	}
//显示经纬度网格线的标注类型
	function K_LatlngGrid()
	{
		this.overlays=[];
	}
	K_LatlngGrid.prototype= new GControl(true,true);
	K_LatlngGrid.prototype.initialize=function(map)
	{
		this.map=map;
		this.redraw();
		GEvent.bind(map,"move",this,this.redraw);
		return document.createElement("div");
	}
	K_LatlngGrid.prototype.remove=function()
	{
		var overlay;
		while(overlay=this.overlays.pop())
		{
			this.map.removeOverlay(overlay);
		}
	}
	K_LatlngGrid.prototype.redraw=function(a)
	{
		var bounds=map.getBounds();
		var span=bounds.toSpan();
		var scale=Math.log(Math.min(span.lng(),span.lat()))/Math.log(10);
		scale=(scale-Math.floor(scale)<0.5)?Math.pow(10,Math.floor(scale)):Math.pow(10,Math.floor(scale))*5;
		if(this.scale==scale && this.bounds.containsBounds(bounds)){return;}
		this.remove();
		this.scale=scale;
		var sw=new GLatLng(2*bounds.getSouthWest().lat()-bounds.getNorthEast().lat(),2*bounds.getSouthWest().lng()-bounds.getNorthEast().lng(),true);
		var ne=new GLatLng(2*bounds.getNorthEast().lat()-bounds.getSouthWest().lat(),2*bounds.getNorthEast().lng()-bounds.getSouthWest().lng(),true);
		this.bounds=new GLatLngBounds(sw,ne);
		for(var lng=Math.floor(sw.lng()/scale)*scale;lng<ne.lng();lng+=scale)
		{
			var polyline=new GPolyline([new GLatLng(sw.lat(),lng),new GLatLng(sw.lat()+(ne.lat()-sw.lat())/3,lng),new GLatLng(sw.lat()+(ne.lat()-sw.lat())*2/3,lng),new GLatLng(ne.lat(),lng)],"red",1,0.5);
			map.addOverlay(polyline);
			this.overlays.push(polyline);
		}
		for(var lat=Math.floor(sw.lat()/scale)*scale;lat<ne.lat();lat+=scale)
		{
			var polyline=new GPolyline([new GLatLng(lat,sw.lng()),new GLatLng(lat,sw.lng()+(ne.lng()-sw.lng())/3),new GLatLng(lat,sw.lng()+(ne.lng()-sw.lng())*2/3),new GLatLng(lat,ne.lng())],"red",1,0.5);
			map.addOverlay(polyline);
			this.overlays.push(polyline);
		}
		return;
	};
	K_LatlngGrid.prototype.getDefaultPosition=function()
	{
		return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(10,10))
	};
//加载一个JS文件的对象
	function K_JsLoader(parseHandle)
	{
		this.parseHandle=parseHandle;
	}
	K_JsLoader.prototype.load=function(src)
	{
		if(!this.jsFile)
		{
			this.jsFile=document.createElement("script");
			this.jsFile.type="text/javascript";
			this.jsFile.defer=true;
			document.body.insertBefore(this.jsFile,document.body.firstChild);
			GEvent.bindDom(this.jsFile,"readystatechange",this,this.onReadyStateChange);
			GEvent.bindDom(this.jsFile,"load",this,this.onLoad);
		}
		this.jsFile.src=src;
		this.running=true;
	}
	K_JsLoader.prototype.onLoad=function(e)
	{
		if(window._OLR)
		{
			GEvent.trigger(this,"loaded",K_JsLoader.parseObject(window._OLR));
			if(this.parseHandle)
				this.parseHandle.call(null,window._OLR);
		}
		else
		{
			GEvent.trigger(this,"error");
		}
		window._OLR=null;
		if(!document.all && this.jsFile && this.jsFile.parentNode==document.body)
		{
			this.jsFile.removeAttribute("src");
			document.body.removeChild(this.jsFile);
			delete this.jsFile;
		}
		this.running=false;
	}
	K_JsLoader.parseObject=function(obj)
	{
		return obj;
	}
	K_JsLoader.prototype.onReadyStateChange=function(e)
	{
		if(!this.jsFile || this.jsFile.readyState!="loaded")
		{
			return;
		}
		this.onLoad();
	}
	K_JsLoader.getChild=function(arr,name)
	{
		for(var i=0;i<arr.c.length;i++)
		{
			if(arr.c[i].name==name)
			{
				return arr.c[i];
			}
		}
	}
//这是51ditu坐标系统对象
	function K_51dituProjection()
	{
		this.imgSize=200;
		this.imgNtuSize=256;
	}
	K_51dituProjection.prototype=new GProjection(); 
	K_51dituProjection.load=function(latlng)
	{
		K_51dituProjection.loading=true;
		K_51dituProjection._latlng=[latlng[0],latlng[1]];
		if(!K_51dituProjection.loader)
		{
			K_51dituProjection.loader=new K_JsLoader();
			GEvent.addListener(K_51dituProjection.loader,"loaded",K_51dituProjection.onLoad);
		}
		K_51dituProjection.loader.load("http://www.step1.cn/51ditu/services/NTL_.aspx?ps="+latlng[0]+","+latlng[1]+"");
	}
	K_51dituProjection.onLoad=function(points)
	{
		if(points.length==1)
		{
			K_51dituProjection.spLatlng=K_51dituProjection._latlng;
			K_51dituProjection._latlng=null;
			K_51dituProjection.spNtu=points[0];
			GEvent.trigger(K_51dituProjection,"change");
			K_51dituProjection.loading=false;
		}
	}
	K_51dituProjection.LatlngToNtu=function(latlng)
	{
		if(K_51dituProjection.spLatlng)
		{
			if(Math.pow(K_51dituProjection.spLatlng[0]-latlng[0],2)+Math.pow(K_51dituProjection.spLatlng[1]-latlng[1],2)>100000000 && !K_51dituProjection.loading)
			{
				K_51dituProjection.load(latlng);
			}
			return [latlng[0]-K_51dituProjection.spLatlng[0]+K_51dituProjection.spNtu[0],latlng[1]-K_51dituProjection.spLatlng[1]+K_51dituProjection.spNtu[1]];
		}
		else
		{
			if(!K_51dituProjection.loading)
			{
				K_51dituProjection.load(latlng);
			}
			return [latlng[0],latlng[1]];
		}
	}
	K_51dituProjection.NtuToLatlng=function(ntu)
	{
		if(K_51dituProjection.spLatlng)
		{
			if(Math.pow(K_51dituProjection.spLatlng[0]-ntu[0],2)+Math.pow(K_51dituProjection.spLatlng[1]-ntu[1],2)>100000000 && !K_51dituProjection.loading)
			{
				K_51dituProjection.load(ntu);
			}
			return [ntu[0]+K_51dituProjection.spLatlng[0]-K_51dituProjection.spNtu[0],ntu[1]+K_51dituProjection.spLatlng[1]-K_51dituProjection.spNtu[1]];
		}
		else
		{
			if(!K_51dituProjection.loading)
			{
				K_51dituProjection.load(ntu);
			}
			return [ntu[0],ntu[1]];
		}
	}
	K_51dituProjection.get51dituZoom=function(zoom)
	{
		return 17-zoom;
	}
	K_51dituProjection.prototype.fromLatLngToPixel=function(point,zoom)
	{
		var zoomUnits=Math.pow(2,K_51dituProjection.get51dituZoom(zoom))*this.imgNtuSize/this.imgSize;
		var ntu=K_51dituProjection.LatlngToNtu([point.lng()*100000,point.lat()*100000]);
		return new GPoint(ntu[0]/zoomUnits,-ntu[1]/zoomUnits)
	};
	K_51dituProjection.prototype.fromPixelToLatLng=function(point,zoom,flag)
	{
		var zoomUnits=Math.pow(2,K_51dituProjection.get51dituZoom(zoom))*this.imgNtuSize/this.imgSize;
		var latlng=K_51dituProjection.NtuToLatlng([point.x*zoomUnits,-point.y*zoomUnits]);
		return new GLatLng(latlng[1]/parseFloat(100000),latlng[0]/parseFloat(100000),flag)
	};
	K_51dituProjection.prototype.tileCheckRange=function(tile,zoom,tilesize)
	{
		return true;
	};

	K_51dituProjection.prototype.getWrapWidth=function(zoom)
	{
		return Math.pow(2,zoom+8);
	};

//这是调用51ditu图片的地图类型对象
	function K_51dituSpec()
	{
		GTileLayer.call(this,new GCopyrightCollection("51ditu.com"),3,17);
		this.imgURL=["http://dimg.51ditu.com/"];
	}
	K_Function.InheritClass(K_51dituSpec,GTileLayer); 
	K_51dituSpec.prototype.getTileUrl=function(point,zoom)
	{
		var z=K_51dituProjection.get51dituZoom(zoom);
		var nGrade=parseInt(Math.ceil((12-z)/4));
		var nPreRow=0,nPreCol=0,nPreSize=0;  
		var path="";
		for(var i=0;i<nGrade;i++)
		{
			var nSize=1<<(4*(nGrade-i));
			var nRow =parseInt((point.x-nPreRow*nPreSize)/nSize);
			var nCol =parseInt((-point.y-1-nPreCol*nPreSize)/nSize);
			path+=((nRow>9)?nRow:"0"+nRow)+""+((nCol>9)?nCol:"0"+nCol)+"/";
			nPreRow = nRow;
			nPreCol = nCol;
			nPreSize = nSize;
		}
		var id=(((point.x)&((1<<20)-1))+(((-point.y-1)&((1<<20)-1))*Math.pow(2,20))+(((z)&((1<<8)-1))*Math.pow(2,40)));
		return this.imgURL[(point.x+point.y)%this.imgURL.length]+z+"/"+path+id+".png";
	}
	K_51dituSpec.prototype.isPng=function(){return true;}
	window.K_51ditu_MAP=new GMapType([new K_51dituSpec()],new K_51dituProjection(),"51ditu",{tileSize:200,shortName:51,alt:"显示51ditu.com数据"});


	window.K_Function=K_Function;
	window.K_LatlngGrid=K_LatlngGrid;
	window.K_JsLoader=K_JsLoader;
	window.K_51dituProjection=K_51dituProjection;
	window.K_51dituSpec=K_51dituSpec;
}
Step1Map();
