/**
 *   监控打点、路线线条选择、框选功能加载js事件
 * */
function MapUtil() {}

//初始化
MapUtil.init=function(){
	MapUtil();
};
//根据经纬度查询Graphic
MapUtil.getGraphicByName=function(name,color) {
	for (var i=0,len=MapAPI.getFrameSelectGraphicsLayer().graphics.length; i< len;i++) {
		var graphic=MapAPI.getFrameSelectGraphicsLayer().graphics[i];
		var title=graphic.infoTemplate.title;
		console.log("graphic",graphic)
		if (title==name) {
			MapAPI.getFrameSelectGraphicsLayer().remove(graphic)
			graphic.symbol.color={r:color.r,g:color.g,b:color.b,a:0.2};
			graphic.symbol.outline.color={r:color.r,g:color.g,b:color.b,a:1};
			MapAPI.getFrameSelectGraphicsLayer().add(graphic);
			return;
		}
	}
	$.showMsg("此位置无标注设备!");
	return null;
}
//根据经纬度查询Graphic
MapUtil.getGraphic=function(point) {
    for (var i=0,len=MapAPI.getCameraGraphicsLayer().graphics.length; i< len;i++) {
    	var graphic=MapAPI.getCameraGraphicsLayer().graphics[i];
    	var geometry=graphic.geometry;
    	  if (point.jd==geometry.x&&point.wd==geometry.y) {
              return graphic;
    	  }
    }
    $.showMsg("此位置无标注设备!");
    return null;
}
//地图打点功能
MapUtil.addPoint=function(data,html){	
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	// 根据状态改变颜色
	var videoImg = "resource/images/icons/video.png";
	if (data.isonline == "1") {
		videoImg = "resource/images/icons/video.png";
		if ((data.vqcstate != "1" && data.vqcstate != null) || (data.nosignalstatus != "1" && data.nosignalstatus) || (data.videoocclusionstatus != "1" && data.videoocclusionstatus != null) || (data.scenechangestatus != "1" && data.scenechangestatus != null) || (data.imgtoodarkstatus != "1" && data.imgtoodarkstatus  != null) || (data.imgoverbrightstatus != "1" && data.imgoverbrightstatus != null) || (data.imgclearstatus != "1" && data.imgclearstatus != null) || (data.imgcolorstatus != "1" && data.imgcolorstatus != null) || (data.recordstate != "1" && data.recordstate != null)) {
			videoImg = "resource/images/icons/video-yellow.png";
		}
	} else if(data.isonline == "0") {
		videoImg = "resource/images/icons/video-red.png";
	} else if (data.isonline == null) {
		videoImg = "resource/images/icons/video.png";
	} else {
		videoImg = "resource/images/icons/video-yellow.png";
	}
	if (data.imgUrl != null && data.imgUrl != "") {
		videoImg = data.imgUrl;
	}
	var symbol = new PictureMarkerSymbol({
		"url": videoImg,
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.tdmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.tdmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getCameraGraphicsLayer().add(graphic);
	return graphic;
};
//巡检未巡检

//地图打点功能
MapUtil.addXJPoint=function(data){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	// 根据状态改变颜色
	videoImg = "resource/images/icons/video_noxj.png";
	if (data.status == "1") {
		videoImg = "resource/images/icons/video_xj.png";
	}
	var symbol = new PictureMarkerSymbol({
		"url": videoImg,
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var graphic = new Graphic(point,symbol);
	MapAPI.getXjGraphicsLayer().add(graphic);
	return graphic;
};

// 建筑物标注
MapUtil.addJtjzwPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jzw.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.jzwmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.jzwmc,html);
	var graphic = new Graphic(point,symbol);
	MapAPI.getJtjzwGraphicsLayer().add(graphic);
	return graphic;
};

// 标志牌标注
MapUtil.addNhywjtbzPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jtbzp.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.bzmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.bzmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getNhywjtbzGraphicsLayer().add(graphic);
	return graphic;
}

// 交通标线标注
MapUtil.addJtbxPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jtbx.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.bxmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.bxmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getJtbxGraphicsLayer().add(graphic);
	return graphic;
}

// 交通护栏标注
MapUtil.addJthlPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jthl.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.hlmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.hlmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getJthlGraphicsLayer().add(graphic);
	return graphic;
}

// 交通信号灯标注
MapUtil.addJtxhdPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jtxhd.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.xhdmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.xhdmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getJtxhdGraphicsLayer().add(graphic);
	return graphic;
}

// 交通诱导屏标注
MapUtil.addJtydpPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "resource/images/icons/jtydp.png",
		"height": 20,
		"width": 20
	});
	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.ledmc,"xh":data.xh};
    var infoTemplate = new InfoTemplate(data.ledmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getJtydpGraphicsLayer().add(graphic);
	return graphic;
}

// 地磁、微波标注
MapUtil.adddetectorPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "",
		"height": 20,
		"width": 20
	});
	//判断是地磁还是微波
	if(data.detectortype=="GVD" )//地磁
	{
		if(data.state===undefined ||(data.state!=null &&data.state=="1")) {
			symbol.url = "resource/images/icons/dc.png";
		}
		else {
			symbol.url="resource/images/icons/dc-red.png";
		}
	}

	if( data.detectortype=="SMARTSENSOR" ){

		if(data.state===undefined ||(data.state!=null && data.state=="1")) {
			symbol.url = "resource/images/icons/wb.png";
		}
		else {
			symbol.url="resource/images/icons/wb-red.png";
		}
	}
	var point = new Point(data.longitude, data.latitude);
	var attr = {"x":data.longitude,"y":data.latitude,"name":data.name,"id":data.id};
	var infoTemplate = new InfoTemplate(data.name,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	if(data.detectortype=="GVD")//地磁
	{
		MapAPI.getJtdcGraphicsLayer().add(graphic);
	}else {

		MapAPI.getJtwbGraphicsLayer().add(graphic);
	}
	return graphic;
}

//雷达标
MapUtil.adddLdPoint=function(data,html){
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var symbol = new PictureMarkerSymbol({
		"url": "",
		"height": 20,
		"width": 20
	});

	if(data.state===undefined ||(data.state!=null &&data.state=="1")) {
		symbol.url = "resource/images/icons/ld.png";
	}
	else {
		symbol.url="resource/images/icons/ld-red.png";
	}

	var point = new Point(data.jd, data.wd);
	var attr = {"x":data.jd,"y":data.wd,"name":data.ldmc,"id":data.xh};
	var infoTemplate = new InfoTemplate(data.ldmc,html);
	var graphic = new Graphic(point,symbol,attr,infoTemplate);
	MapAPI.getJtldGraphicsLayer().add(graphic);
	return graphic;
}






//地图划线功能
MapUtil.addLine=function(data,html){	
	var PictureMarkerSymbol=MapAPI.getPictureMarkerSymbol();
	var Point=MapAPI.getPoint();
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var CartographicLineSymbol=MapAPI.getCartographicLineSymbol();
	var Color=MapAPI.getColor();
	var infoTemplate = new InfoTemplate(data.tdmc,html);
	var attr = {"geometry":data.geometry,"name":data.tdmc,"xh":data.xh};
	var myLine ={geometry:{"paths":data.geometry},
	    "symbol":{"color":[255,0,0],"width":3,"type":"esriSLS","style":"esriSLSSolid"},
	    "infoTemplate":infoTemplate,
	    "attributes":attr
	};
	var graphic = new Graphic(myLine);
	MapAPI.getRoadGraphicsLayer().add(graphic);
	return graphic;
};
//地图画多边形功能
MapUtil.addPolygon=function(data,html){	
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var infoTemplate = new InfoTemplate(data.tdmc,html);
	var attr = {"geometry":data.geometry,"name":data.tdmc,"xh":data.xh};
	var myLine ={geometry:{"rings":data.geometry},
		    "symbol":{"color":[255,0,0,70],"outline":{"color":[255,0,0],
		        "width":1,"type":"esriSLS","style":"esriSLSSolid"},
		        "type":"esriSFS","style":"esriSFSSolid"},
			"infoTemplate":infoTemplate,
			"attributes":attr
	};
	var graphic = new Graphic(myLine);
	MapAPI.getFrameSelectGraphicsLayer().add(graphic);
	return graphic;
};
//地图画多边形功能
MapUtil.addCircle=function(data,html){	
	var Graphic=MapAPI.getGraphic();
	var InfoTemplate=MapAPI.getInfoTemplate();
	var infoTemplate = new InfoTemplate(data.tdmc,html);
	var attr = {"geometry":data.geometry,"name":data.tdmc,"xh":data.xh};
	var myLine ={geometry:{"rings":data.geometry},
			"symbol":{"color":[255,0,0,70],"outline":{"color":[255,0,0],
				"width":1,"type":"esriSLS","style":"esriSLSSolid"},
				"type":"esriSFS","style":"esriSFSSolid"},
				"infoTemplate":infoTemplate,
				"attributes":attr
	};
	var graphic = new Graphic(myLine);
	MapAPI.getCircleGraphicsLayer().add(graphic);
	return graphic;
};
