var map;
var dwb_center_point;
var iconDefs = [
  { "name" : "A",
	"fname" : "/express/img/a.gif",
	"width" : 21,
	"height" : 35
  },
  { "name" : "B",
	"fname" : "/express/img/b.gif",
	"width" : 21,
	"height" : 35
  }];

var customIcons = {};
function initialize() 
{
	if (GBrowserIsCompatible()) 
	{
		// prepare icons
        for (var i = 0; i < iconDefs.length; i++) {
          var iconTemp = new GIcon(G_DEFAULT_ICON);
          iconTemp.image = iconDefs[i].fname;
          iconTemp.iconSize = new GSize(iconDefs[i].width, iconDefs[i].height);
          iconTemp.iconAnchor = new GPoint(iconDefs[i].width/2, iconDefs[i].height/2);
          customIcons[iconDefs[i].name] = iconTemp;
        }

		// Create and Center a Map
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(30.260698198224393, 120.15798568725586), 15);

		// bind a search control to the map, suppress result list
		/* 自定义搜索结果显示
			var options = {
			resultList : document.getElementById("results")
			};
			map.addControl(new google.maps.LocalSearch(options), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,355)));
		*/
		//var detailaddrs = getValue($("addr"));
		var options = {
			suppressZoomToBounds : true//搜索结果阻止初始化缩放级别
			//searchFormHint : detailaddrs,
			//onIdleCallback : function() { alert("search control is idle");},
			//onSearchCompleteCallback : function(searcher){
            //                      alert(searcher.results.length + " results");	
			//},
		};

	    var url = window.location.href;
		if(url.indexOf("index2")!=-1){
			map.addControl(new google.maps.LocalSearch(options), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(5,445)));
		}
		else{
			map.addControl(new google.maps.LocalSearch(options), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(5,587)));
		}

		var customUI = map.getDefaultUI();
		// Remove MapType.G_HYBRID_MAP
		customUI.maptypes.satellite = false;
		//customUI.maptypes.hybrid = false;
		customUI.maptypes.physical = false;
		map.setUI(customUI);
	}
}


var marker;//起点A
var marker2;//终点B
var whichIcon;//图标
var clickevent;//地图单击事件

//定位
function setCenter(lat,lng,type,icon)
{
	//alert(lat+" "+lng);
	if (lat*lng==0)//使用中心点坐标，以便标注
    {
		var currentPoint = map.getCenter();
		lat = map.getCenter().lat();
		lng = map.getCenter().lng();	
		whichIcon = icon;
    }
	else
	{
		lat /= 1000000;
		lng /= 1000000;
		//whichIcon = "old";
	}
	
	var latlng = new GLatLng(lat, lng);		
	if(icon=="A"){
		if (marker)
		{
			map.removeOverlay(marker)
		}
		whichIcon = "A";
		marker = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});	
		map.addOverlay(marker);
		GEvent.addListener(marker, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];
			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			calRange(px*1000000,py*1000000,type);
			if(alabel){
				map.removeOverlay(alabel);
			}
			alabel = new ELabel(new GLatLng(px,py), "取件地址", "style1",new GSize(5,-15));
			map.addOverlay(alabel);
			submitLatlng(px,py,type);
			//bounds();
			fromtoline();
			calfee();
		});
		map.setCenter(latlng,15);
	}
	else{
		if (marker2)
		{
			map.removeOverlay(marker2)
		}
		whichIcon = "B";
		marker2 = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});		
		map.addOverlay(marker2);
		GEvent.addListener(marker2, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];
			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			calRange(px*1000000,py*1000000,type);
			if(blabel){
				map.removeOverlay(blabel);
			}
			blabel = new ELabel(new GLatLng(px,py), "送件地址", "style1",new GSize(5,-15));
			map.addOverlay(blabel);
			submitLatlng(px,py,type);
			//bounds();
			fromtoline();
			calfee();
		});
		map.setCenter(latlng);
	}

	var sxy = latlng+"";
	sxy = sxy.substring(1,sxy.length-1);
	var px = sxy.split(", ")[0];
	var py = sxy.split(", ")[1];
	px = Math.round(px*1000000)/1000000;
	py = Math.round(py*1000000)/1000000;
	submitLatlng(px,py,type);

	
}

//定位
function setCenter2(lat,lng)
{
	if (lat*lng==0)//使用中心点坐标，以便标注
    {
		/*var currentPoint = map.getCenter();
		lat = map.getCenter().lat();
		lng = map.getCenter().lng();	
		whichIcon = "new";*/
    }
	else
	{
		lat /= 1000000;
		lng /= 1000000;
		//whichIcon = "old";
	}
	
	var latlng = new GLatLng(lat, lng);		
 
	var sxy = latlng+"";
	sxy = sxy.substring(1,sxy.length-1);
	var px = sxy.split(", ")[0];
	var py = sxy.split(", ")[1];
	px = Math.round(px*1000000)/1000000;
	py = Math.round(py*1000000)/1000000;

  
	map.setCenter(latlng);
}


//标注用户位置
function markBuild(type,icon)
{
    if (true)//使用中心点坐标，以便标注
    {
		whichIcon = icon;
    }
	var latlng = new GLatLng(map.getCenter().lat(),map.getCenter().lng());	
    if(icon=="A"){
		

		if (marker)
		{
			map.removeOverlay(marker)
		}
		marker = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});	
		map.addOverlay(marker);

		if(alabel){
			map.removeOverlay(alabel);
		}
		alabel = new ELabel(new GLatLng(map.getCenter().lat(),map.getCenter().lng()), "取件地址", "style1",new GSize(5,-15));
		map.addOverlay(alabel);
		
		/**添加地图点击事件监听**/
		if(clickevent){
			GEvent.removeListener(clickevent);
		}
		clickevent = GEvent.addListener(map, "click", function(overlay,point) {
		    if(marker){
				//alert("marker:"+point);
				//map.setCenter(point);
				var lng = point.x;
				var lat = point.y;
				markBuild2(lat,lng,type,icon);
			}
		});
		/**添加地图点击事件监听end**/

		GEvent.addListener(marker, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];

			if(alabel){
				map.removeOverlay(alabel);
			}
			alabel = new ELabel(new GLatLng(px,py), "取件地址", "style1",new GSize(5,-15));
			map.addOverlay(alabel);

			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			$("frombtn").innerText="提交取件地址";
			$("frombtn").onclick = function(){
				biaozhuok('from');
			}
			calRange(px*1000000,py*1000000,type);
			submitLatlng(px,py,type);
			fromtoline();
			calfee();
		});
	
	}
	else{
		if (marker2)
		{
			map.removeOverlay(marker2)
		}
		marker2 = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});		
		map.addOverlay(marker2);

		if(blabel){
			map.removeOverlay(blabel);
		}
		blabel = new ELabel(new GLatLng(map.getCenter().lat(),map.getCenter().lng()), "送件地址", "style1",new GSize(5,-15));
		map.addOverlay(blabel);

		/**添加地图点击事件监听**/
		if(clickevent){
			GEvent.removeListener(clickevent);
		}
		clickevent = GEvent.addListener(map, "click", function(overlay,point) {
		    if(marker2){
				//alert("marker2:"+point);
				//map.setCenter(point);
				var lng = point.x;
				var lat = point.y;
				markBuild2(lat,lng,type,icon);
			}
		});

		/**添加地图点击事件监听end**/

		GEvent.addListener(marker2, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];

			if(blabel){
				map.removeOverlay(blabel);
			}
			blabel = new ELabel(new GLatLng(px,py), "送件地址", "style1",new GSize(5,-15));
			map.addOverlay(blabel);

			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			$("tobtn").innerText="提交收件地址";
			$("tobtn").onclick = function(){
				biaozhuok('to');
			}
			calRange(px*1000000,py*1000000,type);
			submitLatlng(px,py,type);
			fromtoline();
			calfee();
		});
		
	}
	
		var sxy = latlng+"";
		sxy = sxy.substring(1,sxy.length-1);
		var px = sxy.split(", ")[0];
		var py = sxy.split(", ")[1];
		px = Math.round(px*1000000)/1000000;
		py = Math.round(py*1000000)/1000000;
		submitLatlng(px,py,type);
		map.setCenter(latlng);

		fromtoline();
		calfee();
		//map.openInfoWindowHtml(this.getLatLng(),html);
		
}

function markBuild2(lat,lng,type,icon)
{
	//alert("lat:"+lat+" lng:"+lng+" type:"+type+" icon="+icon);
    if (true)//使用中心点坐标，以便标注
    {
		whichIcon = icon;
    }
	var latlng = new GLatLng(lat,lng);	
    if(icon=="A"){
		if (marker)
		{
			map.removeOverlay(marker)
		}
		marker = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});	
		map.addOverlay(marker);

		if(alabel){
			map.removeOverlay(alabel);
		}
		alabel = new ELabel(new GLatLng(lat,lng), "取件地址", "style1",new GSize(5,-15));
		map.addOverlay(alabel);
		

		GEvent.addListener(marker, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];

			if(alabel){
				map.removeOverlay(alabel);
			}
			alabel = new ELabel(new GLatLng(px,py), "取件地址", "style1",new GSize(5,-15));
			map.addOverlay(alabel);

			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			$("frombtn").innerText="提交取件地址";
			$("frombtn").onclick = function(){
				biaozhuok('from');
			}
			calRange(px*1000000,py*1000000,type);
			submitLatlng(px,py,type);
			fromtoline();
			calfee();
		});
	
	}
	else{
		if (marker2)
		{
			map.removeOverlay(marker2)
		}
		marker2 = new GMarker(latlng, {icon:customIcons[whichIcon], draggable: true, bouncy:true});		
		map.addOverlay(marker2);

		if(blabel){
			map.removeOverlay(blabel);
		}
		blabel = new ELabel(new GLatLng(lat,lng), "送件地址", "style1",new GSize(5,-15));
		map.addOverlay(blabel);

		GEvent.addListener(marker2, "mouseout", function() {
			var sxy = this.getLatLng()+"";
			sxy = sxy.substring(1,sxy.length-1);
			var px = sxy.split(", ")[0];
			var py = sxy.split(", ")[1];

			if(blabel){
				map.removeOverlay(blabel);
			}
			blabel = new ELabel(new GLatLng(px,py), "送件地址", "style1",new GSize(5,-15));
			map.addOverlay(blabel);

			px = Math.round(px*1000000)/1000000;
			py = Math.round(py*1000000)/1000000;
			
			$("tobtn").innerText="提交收件地址";
			$("tobtn").onclick = function(){
				biaozhuok('to');
			}
			calRange(px*1000000,py*1000000,type);
			submitLatlng(px,py,type);
			fromtoline();
			calfee();
		});
		
	}
	
		var sxy = latlng+"";
		sxy = sxy.substring(1,sxy.length-1);
		var px = sxy.split(", ")[0];
		var py = sxy.split(", ")[1];
		px = Math.round(px*1000000)/1000000;
		py = Math.round(py*1000000)/1000000;
		submitLatlng(px,py,type);
		//map.setCenter(latlng);

		fromtoline();
		calfee();
		//map.openInfoWindowHtml(this.getLatLng(),html);
		
}

function fromtoline(){
	if($("fromlng").value!=0 && $("tolng").value!=0){
			if(polyline){
				polyline.hide();
			}
			var lng1 = $("fromlng").value;
			var lat1 = $("fromlat").value;
			var lng2 = $("tolng").value;
			var lat2 = $("tolat").value;
			polyline = new GPolyline([ 
			  new GLatLng(lat1/1000000, lng1/1000000), 
			  new GLatLng(lat2/1000000, lng2/1000000) 
			], "red", 3); 
			
			map.addOverlay(polyline);
			var clat = (Math.round(lat1)+Math.round(lat2))/2;
			var clng = (Math.round(lng1)+Math.round(lng2))/2;
			//setCenter2(clat,clng);
	}
}


function submitLatlng(px,py,type){
	$(type+'lng').value = py*1000000;
	$(type+'lat').value = px*1000000;  
}

function biaozhuok(type){
	if(type=="from"){
		alert("取件地址提交成功！");
		$("exp_addrfrom_tip").style.display="none";
	}
	if(type=="to"){
		alert("收件地址提交成功！");
		$("exp_addrto_tip").style.display="none";
	}

}


