/**
 * Created by yexb on 2017-08-02
 */
$(document).ready(function () {
	//初始化地图
	initMap();
});
var map;
var win =false;
var blfirstload = true;//首次登录界面
var lineindex;
var activesearchInfoWindow = null;//活跃的InfoWindow
var activepoint = null;//活跃的InfoWindow(point)

//聚合Styles
var myStyles = [{
    url: '../images/img/clusterer20.png',
    size: new BMap.Size(20, 20),
    textColor: '#006A6A',
    opt_textSize: 10
}, {
    url: '../images/img/clusterer30.png',
    size: new BMap.Size(30, 30),
    textColor: '#006A6A',
    opt_textSize: 12
}, {
    url: '../images/img/clusterer40.png',
    size: new BMap.Size(40, 40),
    textColor: '#006A6A',
    opt_textSize: 14
}];

var mapTab = 0;
function openEventStatis(){
	var tjclick=$("#tjclick").val();
	if(tjclick == 'on'){
    	var tjsrc=$("#tjsrc").val();
		tjsrc=filterXSS(tjsrc);
    	document.getElementById("tj").src = tjsrc;
    	$("#tjclick").val('off');
	}
	$('#myTab li:eq(1) a').tab('show');
}

function openEventSys(){
	var fxclick=$("#fxclick").val();
	if(fxclick == 'on'){
    	var fxsrc=$("#fxsrc").val();
		fxsrc=filterXSS(fxsrc);
    	document.getElementById("fx").src = fxsrc;
    	$("#fxclick").val('off');
	}
	$('#myTab li:eq(2) a').tab('show');
}

function openDevRunning(){
	var sbclick=$("#sbclick").val();
	if(sbclick == 'on'){
    	var sbsrc=$("#sbsrc").val();
		sbsrc=filterXSS(sbsrc);
    	document.getElementById("sb").src = sbsrc;
    	$("#sbclick").val('off');
	}
	$('#myTab li:eq(3) a').tab('show');
}


/********************************************
JS初始化百度地图
*******************************************/
var markerClusterer;
var markers = [];//需要聚合的监测点
function initMap() {
	map =  getGISMap("rightContent",true,true,12);
	
   /**********************************************
   用地图的加载事件（移动事件）涵盖所有新算法
	*********************************************/
	//地图加载完毕事件
	map.addEventListener("tilesloaded",function(){
	  if(blfirstload){
		//数据获取
       	AnalysisLineMaps(false);//无需强制获取数据
       	blfirstload = false;
       	setTimeout( function(){pantoPoint(lineindex);},1 );
	  }
	  controlMapElements(map);
	});
	
	//地图移动事件
	map.addEventListener("moveend",function(){
	   controlMapElements(map);
	});
	
	//地图单击事件
	map.addEventListener("click",function(){
		if(!win){
			if(activesearchInfoWindow != null){
				   activesearchInfoWindow.close();
			   }
		}
		win =false;
	   
	});
   
    markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
    //更改一些参数设置
    markerClusterer.setGridSize(40);//GridSize
    markerClusterer.setMaxZoom(14);//maxZoom
    markerClusterer.setStyles(myStyles);//重新设置聚合类型
}

function changeFlag(flag){
	win = flag;
}

/********************************************
归一化处理后重新刷新地图
*******************************************/
function refreshDatabaseMap(blFlag){
	parent.layer.msg("系统进行归一化处理，地图刷新请注意!",{icon:5,time:2000});
	if(activesearchInfoWindow != null){
		activesearchInfoWindow.close();
	}
	
	AnalysisLineMaps(blFlag);
}

/********************************************
终端状态改变后刷新地图
*******************************************/
function refreshDevice(data){
	parent.layer.msg("有终端状态变更，地图刷新请注意!",{icon:5,time:2000});
	var strLineId = "";
	if(activesearchInfoWindow != null){
		activesearchInfoWindow.close();
	}
	
	markerClusterer.removeMarkers(markers);
	$.each(data,function(index,item){
		strLineId = item.lineid;
		 for(var i = 0; i < markers.length; i++){
		   	  marker = markers[i];
		   	  if(marker.index == strLineId){
		   		refreshDeviceInfo(marker,i,item.devflag,item.status);
		   		break;
		   	  }
		 }
	});
	markerClusterer.addMarkers(markers);
}

//更新相对应的终端状态
function refreshDeviceInfo(marker,index,devflag,status) {
   //获取监测点的地址
   var lineMap =marker.lineMap;
   lineMap.devflag = devflag;//终端状态
   lineMap.status = status;//通讯状态
   //移除监测点
   markers.splice(index,1);
   //重新添加监测点
   markers.push(getPoint(lineMap));
}


/********************************************
获取所有监测点信息
*******************************************/
var myVar;
var gisinfo = new Object();
function AnalysisLineMaps(blFlag){
	markerClusterer.removeMarkers(markers);
	markers = [];
	if(blFlag && gisinfo != null){
		for(var i = 0;i <gisinfo.subMarkers.length; i++){
		     map.removeOverlay(subMarkers[i]);
		 }
		 for(var i = 0;i < gisinfo.subpointLines.length; i++){
		     map.removeOverlay(subpointLines[i]);
		 }
	}
	
	var dataarr =parent.parent.getLineMaps(blFlag,false);
	//把数据打到地图上去，供电公司，监测点，供电公司跟监测点连线
    gisinfo = drawElement(dataarr,map,1);
    markers = gisinfo.pointMarkers;
    markerClusterer.addMarkers(markers);
}

/************************************
*点击监测点实现定位
*line_index 监测点ID
************************************/
function pantoPoint(line_index){
	lineindex = line_index;
	var marker;
    for(var i = 0; i < markers.length; i++){
   	  marker = markers[i];
   	  if(line_index == marker.index){
   		  var point =  marker.getPosition();
  		  map.centerAndZoom(point, 15);  //设初始化地图
  		  marker.searchInfoWindow.open(point);
  		  activesearchInfoWindow = marker.searchInfoWindow;
		  activepoint = point;
  		  var lng=point.lng;
  		  var lat=point.lat;
  		  $("#longitude").val(lng);
  		  $("#latitude").val(lat);
  		  break;
   	  }
    }
}