//地图初始化加载js，初始化地图和监控等设备，加载显示
dojoConfig = {		
    async: true,
    parseOnLoad: true,
    packages: [{
        name: 'tdlib',
        location: this.location.pathname.replace(/\/[^/]+$/, "") + "/js/tdlib"
    }]
};
	require([
		"esri/map",
		"esri/graphic",
		"esri/InfoTemplate",
		"esri/layers/GraphicsLayer",
		"esri/layers/ArcGISTiledMapServiceLayer",
		"esri/dijit/Scalebar",
		"dojo/parser",
		"dijit/layout/BorderContainer",
		"dijit/layout/ContentPane",
		"esri/symbols/PictureMarkerSymbol",
		"esri/symbols/SimpleMarkerSymbol",
		"esri/geometry/Point",
		"esri/geometry/Circle",
		"esri/symbols/Font",
		"esri/symbols/TextSymbol",
		"esri/SpatialReference",
		"dojo/domReady!",
		"dojo/dom-construct",
		"esri/dijit/InfoWindow",
		"esri/dijit/PopupTemplate",
		"esri/toolbars/draw",
		"esri/symbols/SimpleLineSymbol",
		"esri/Color",
		"esri/symbols/PictureFillSymbol",
		"esri/symbols/SimpleFillSymbol"
		], function(Map, Graphic,InfoTemplate, GraphicsLayer, tiled, Scalebar, parser, PictureMarkerSymbol, SimpleMarkerSymbol, Point, Circle, Font, TextSymbol, SpatialReference, domConstruct, InfoWindow,PopupTemplate,Draw,SimpleLineSymbol,Color,PictureFillSymbol,SimpleFillSymbol) {
		
		var map = new esri.Map("map");
		//测试环境
    	//var arcGISTiledMapServiceLayer=new esri.layers.ArcGISTiledMapServiceLayer("http://101.95.167.134:6080/arcgis/rest/services/NanHai_SL/MapServer");
		var arcGISTiledMapServiceLayer=new esri.layers.ArcGISTiledMapServiceLayer("http://101.95.167.134:6080/arcgis/rest/services/NanHai_SL/MapServer");
    	var arcGISTiledMapServiceLayer=new esri.layers.ArcGISTiledMapServiceLayer("http://192.168.182.37:6080/arcgis/rest/services/NH0614/MapServer");
		map.addLayer(arcGISTiledMapServiceLayer);
		
		//加入打点图层
		var cameraGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "camera"
		});
		var roadGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "road"
		});
		var frameSelectGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "frameSelect"
		});
		var circleGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "circle"
		});
		var jtjzwGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "jtjzw"
		});
		var nhywjtbzGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "nhywjtbz"
		});
		var jtbxGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "jtbx"
		});
		var jthlGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "jthl"
		});
		var jtxhdGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "jtxhd"
		});
		var jtydpGraphicsLayer = new esri.layers.GraphicsLayer({
			id: "jtydp"
		});
		map.addLayer(cameraGraphicsLayer);
		map.addLayer(roadGraphicsLayer);
		map.addLayer(frameSelectGraphicsLayer);
		map.addLayer(circleGraphicsLayer);
		map.addLayer(jtjzwGraphicsLayer);
		map.addLayer(nhywjtbzGraphicsLayer); // 交通标志牌
		map.addLayer(jtbxGraphicsLayer); // 交通标线
		map.addLayer(jthlGraphicsLayer); // 交通护栏
		map.addLayer(jtxhdGraphicsLayer); // 交通信号灯
		map.addLayer(jtydpGraphicsLayer); // 交通诱导屏
		MapAPI.setPictureMarkerSymbol(esri.symbol.PictureMarkerSymbol);
		MapAPI.setInfoTemplate(esri.InfoTemplate);
		MapAPI.setInfoWindow(esri.dijit.InfoWindow);
		MapAPI.setPopupTemplate(esri.dijit.PopupTemplate);
		MapAPI.setGraphic(esri.Graphic);
		MapAPI.setMap(map);
		MapAPI.setRoadGraphicsLayer(roadGraphicsLayer);
		MapAPI.setCameraGraphicsLayer(cameraGraphicsLayer);
		MapAPI.setCircleGraphicsLayer(circleGraphicsLayer);
		MapAPI.setJtjzwGraphicsLayer(jtjzwGraphicsLayer);
		MapAPI.setNhywjtbzGraphicsLayer(nhywjtbzGraphicsLayer); // 交通标志牌
		MapAPI.setJtbxGraphicsLayer(jtbxGraphicsLayer); // 交通标线
		MapAPI.setJthlGraphicsLayer(jthlGraphicsLayer); // 交通护栏
		MapAPI.setJtxhdGraphicsLayer(jtxhdGraphicsLayer); // 交通信号灯
		MapAPI.setJtydpGraphicsLayer(jtydpGraphicsLayer); // 交通诱导屏
		MapAPI.setFrameSelectGraphicsLayer(frameSelectGraphicsLayer);
		MapAPI.setPoint(esri.geometry.Point);
		MapAPI.setDraw(esri.toolbars.Draw);
		MapAPI.setSimpleLineSymbol(esri.symbol.SimpleLineSymbol);
		MapAPI.setPictureFillSymbol(esri.symbol.PictureFillSymbol);
		MapAPI.setColor(esri.Color);
		MapAPI.setSimpleFillSymbol(esri.symbol.SimpleFillSymbol);
		MapAPI.setCartographicLineSymbol(esri.symbol.CartographicLineSymbol);
		//初始化获取监控、路段等数据，在地图上加载
		loadCameraGraphicsLayer();
		loadRoadGraphicsLayer();
		loadFrameSelectGraphicsLayer();
		loadJtjzwGraphicsLayer();
		loadNhywjtbzGraphicsLayer(); // 交通标志牌
		loadJtbxGraphicsLayer(); // 交通标线
		loadJthlGraphicsLayer(); // 交通护栏
		loadJtxhdGraphicsLayer(); // 交通信号灯
		loadJtydpGraphicsLayer(); // 交通诱导屏
	});
	
// 加载所有相机 并打点
function loadCameraGraphicsLayer(){	
	MapAPI.getCameraGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'sbtdspsrAction!query_Sbtdspsr.do',
		success:function(res){		
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					/*var param = {'tdmc':item.tdmc,'tdtypename':item.tdtypename,'sbbh':item.sbbh,
							'tdbh':item.tdbh,'sfzlx':item.sfzlx,'sfzx':item.sfzx,
							'jd':item.jd,'wd':item.wd};
					var str=JSON.stringify(param);*/
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.tdmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.realtimeVideo(\""+item.xh+"\")'>视频</li>" +
//										"<li onclick='Label.artificialAlarm(\""+item.xh+"\")'>人工告警</li><br/>" +
										"<li onclick='Label.recordNav(\""+item.xh+"\")'>录像</li><br/>" +
										"<li onclick='Label.detailInformation(\""+item.xh+"\")'>详细信息</li>" +
										"<li onclick='removeCamera(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载建筑物标注
function loadJtjzwGraphicsLayer() {
	MapAPI.getJtjzwGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjtjzwxxbAction!listNhywjtjzwxxbByLoadMap.do',
		success:function(res){		
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.jzwmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
//										"<li onclick='Label.realtimeVideo(\""+item.xh+"\")'>视频</li>" +
//										"<li onclick='Label.artificialAlarm(\""+item.xh+"\")'>人工告警</li><br/>" +
//										"<li onclick='Label.recordNav(\""+item.xh+"\")'>录像</li><br/>" +
										"<li onclick='Label.detailInformationJtjzw(\""+item.xh+"\")'>详细信息</li>" +
										"<li onclick='removeJtjzw(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addJtjzwPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载标志牌
function loadNhywjtbzGraphicsLayer() {
	MapAPI.getNhywjtbzGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjtbzglAction!query_Nhywjtbzgl.do',
		success:function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.bzmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.detailInformationNhywjtbz(\""+item.xh+"\")'>详细信息</li>" +
										"<li onclick='removeNhywjtbz(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addNhywjtbzPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载交通标线
function loadJtbxGraphicsLayer(){
	MapAPI.getJtbxGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjtbxglAction!query_Nhywjtbxgl.do',
		success:function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.bxmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.detailInformationJtbx(\""+item.xh+"\")'>详细信息</li>" + // TODO
										"<li onclick='removeJtbx(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addJtbxPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载交通护栏
function loadJthlGraphicsLayer(){
	MapAPI.getJthlGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjthlglAction!query_Nhywjthlgl.do',
		success:function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.hlmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.detailInformationJthl(\""+item.xh+"\")'>详细信息</li>" + // TODO
										"<li onclick='removeJthl(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addJthlPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载交通信号灯
function loadJtxhdGraphicsLayer(){
	MapAPI.getJtxhdGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjtxhdglAction!query_Nhywjtxhdgl.do',
		success:function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.xhdmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.detailInformationJtxhd(\""+item.xh+"\")'>详细信息</li>" + // TODO
										"<li onclick='removeJtxhd(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addJtxhdPoint(item,html);
					}
				});	
			}
		}
	});
}

// 加载交通诱导屏
function loadJtydpGraphicsLayer(){
	MapAPI.getJtydpGraphicsLayer().clear();
	$.ajax({
		type:'post',
		dataType : 'json',
		url : 'nhywjtledydpglAction!query_Nhywjtledydpgl.do',
		success:function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item){
					var jd = item.jd;
					var wd = item.wd;
					if(jd==null || jd=='' || wd==null || wd==''){    // 无经纬度 不打点
						 
					}else{
						var html="<div class='bcontent'><div><span style='color:#75767A'>"+item.ledmc+"</span></div>" +
									"<ul  class='bcontentItem'>" +
										"<li onclick='Label.detailInformationJtydp(\""+item.xh+"\")'>详细信息</li>" + // TODO
										"<li onclick='removeJtydp(\""+item.xh+"\")'>删除</li>" +
									"</ul></div>";
						MapUtil.addJtydpPoint(item,html);
					}
				});	
			}
		}
	});
}

function loadRoadGraphicsLayer(){
	/*// 模拟路段数据 并显示路段图层
	var lineData=[
		{tdmc:"路段1",xh:"12",geometry:"120.725,30.853;120.786,30.871;120.892,30.896;120.892,30.914"},
		{tdmc:"路段2",xh:"13",geometry:"121.081,30.669;121.133,30.689;121.183,30.702;121.209,30.720;121.267,30.731;121.295,30.766;121.361,30.800"}
		];
	for(var i=0,len=lineData.length;i<len;i++){
		var result=[[]];
		var geometry=lineData[i].geometry;
		if(geometry!=null&&geometry.trim()!=''){
			var geometryArr=geometry.split(';');
			for(var j=0;j<geometryArr.length;j++){
				result[0].push([geometryArr[j].split(',')[0],geometryArr[j].split(',')[1]]);
			}
		}
		lineData[i].geometry=result;
		var html="<div class='bcontent'><div><span style='color:#75767A'>"+lineData[i].tdmc+"</span></div><ul  class='bcontentItem'><li onclick='Label.deleteRoad(\""+lineData[i].xh+"\")'>删除路段</li><li onclick=' Label.detailInfoRoad(\""+lineData[i].xh+"\")'>详细信息</li></ul></div>";
		MapUtil.addLine(lineData[i],html);
	}*/
	$.ajax({
		type : 'post',
		datatype : 'json',
		url : 'trafficroadgridinfoAction!query_Trafficroadgridinfo.do',
		data : {'remarks2':'1'},                     // 1代表路段；2代表区域
		success : function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item,index){
					var roadName = item.roadname;
					var roadPosition = item.roadgridposition;
					var result=[[]];         // 存放坐标点
					if(roadPosition!=null && roadPosition.trim()!=''){
						var positionArr = roadPosition.split(';');
						for(var i=0; i<positionArr.length; i++){
							result[0].push([positionArr[i].split(',')[0],positionArr[i].split(',')[1]]);
						}
					}
					var ld = item.remarks1==null ? '':item.remarks1;
					var ssld = ld + "(" +item.roadname+ ")";
					var html="<div class='bcontent'><div>" +
					"<span style='color:#75767A'>"+ssld+"</span>" +
					"</div><ul  class='bcontentItem'>" +
					"<li onclick=' Label.detailInfoRoad(\""+item.roadgridid+"\")'>详细信息</li>" +
					"<li onclick='Label.deleteRoad(\""+item.roadgridid+"\")'>删除路段</li>" +							
					"</ul></div>";
					
					item.tdmc = ld;				
					item.xh = item.roadgridid;
					item.geometry = result;
					MapUtil.addLine(item,html);
				})
			}
		}	
	})
}

function loadFrameSelectGraphicsLayer(){
	// 模拟框选数据 并显示框选图层
	/*var frameSelectData=[
		{tdmc:"框选1",xh:"12",geometry:"120.453,31.108;120.240,31.090;120.187,30.983;120.441,30.892;120.443,31.110"},
		{tdmc:"框选2",xh:"13",geometry:"120.048,30.666;120.063,30.637;120.055,30.616;120.045,30.605;120.040,30.585;120.022,30.558;120.015,30.539;120.019,30.519;120.020,30.50;120.176,30.520;120.163,30.664;120.048,30.664"}
		];
	for(var i=0,len=frameSelectData.length;i<len;i++){
		var result=[[]];
		var geometry=frameSelectData[i].geometry;
		if(geometry!=null&&geometry.trim()!=''){
			var geometryArr=geometry.split(';');
			for(var j=0;j<geometryArr.length;j++){
				result[0].push([geometryArr[j].split(',')[0],geometryArr[j].split(',')[1]]);
			}
		}
		frameSelectData[i].geometry=result;
		var html="<div class='bcontent'><div><span style='color:#75767A'>"+frameSelectData[i].tdmc+"</span></div><ul  class='bcontentItem'><li onclick='FrameSelect.deleteFrameSelect(\""+frameSelectData[i].xh+"\")'>删除区域</li><li onclick=' FrameSelect.detailInfoFrameSelect(\""+frameSelectData[i].xh+"\")'>详细信息</li></ul></div>";
		MapUtil.addPolygon(frameSelectData[i],html);
	}*/
	
	$.ajax({
		type : 'post',
		datatype : 'json',
		url : 'trafficroadgridinfoAction!query_Trafficroadgridinfo.do',
		data : {'remarks2':'2'},				// 1代表路段；2代表区域
		success : function(res){
			if(res&&typeof res!='string'){
				res.forEach(function(item,index){
					var roadName = item.roadname;
					var roadPosition = item.roadgridposition;
					var result=[[]];         // 存放坐标点
					if(roadPosition!=null && roadPosition.trim()!=''){
						var positionArr = roadPosition.split(';');
						for(var i=0; i<positionArr.length; i++){
							result[0].push([positionArr[i].split(',')[0],positionArr[i].split(',')[1]]);
						}
					}
					var ld = item.remarks1==null ? '':item.remarks1;
					var ssld = ld + "(" +item.roadname+ ")";				
					var html="<div class='bcontent'><div>" +
							 "<span style='color:#75767A'>"+ssld+"</span>" +
							 "</div><ul  class='bcontentItem'>" +
							 	"<li onclick=' FrameSelect.detailInfoFrameSelect(\""+item.roadgridid+"\")'>详细信息</li>" +
							 	"<li onclick='FrameSelect.deleteFrameSelect(\""+item.roadgridid+"\")'>删除区域</li>" +						 	
							 "</ul></div>";
					
					item.tdmc = ld;
					item.xh = item.roadgridid;
					item.geometry = result;
					MapUtil.addPolygon(item,html);
				})
			}
		}	
	})	
}

// 删除相机
function removeCamera(xh){
//	console.log("removeCamera: ",xh);
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'sbtdspsrAction!updateJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
						
				loadCameraGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	})
	
}

//删除建筑物
function removeJtjzw(xh){
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjtjzwxxbAction!updateJtjzwJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
						
				loadJtjzwGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	})
	
}

// 删除标志牌
function removeNhywjtbz(xh){
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjtbzglAction!updateJtbzJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
						
				loadNhywjtbzGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	});
}

// 删除交通标线
function removeJtbx(xh){
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjtbxglAction!updateJtbxJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
				loadJtbxGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	});
}

// 删除交通护栏
function removeJthl(xh) {
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjthlglAction!updateJthlJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
				loadJthlGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	});
}

// 删除信号灯
function removeJtxhd(xh) {
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjtxhdglAction!updateJtxhdJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
				loadJtxhdGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	});
}

//删除诱导屏
function removeJtydp(xh) {
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : {"xh":xh,"jd":'',"wd":''},
		url : 'nhywjtledydpglAction!updateJtydpJdWdByXh.do',
		success:function(res){
//			console.log("返回结果：",res);
			if(res){
				top.$.showMsg("删除成功！");
				MapAPI.getMap().infoWindow.hide();
				loadJtydpGraphicsLayer();
			}else{
				top.$.showMsg("删除失败！");
			}
		}		
	});
}