// control for "XUELONG Online" website
// @author ast@whu.edu.cn
//--------xuelong plan line-------------------------------------------------------------------------

function GxlPlanControl() 
{
  var me = this;
  me.enabled_ = false;
  me.TIP_TITLE_SHOW = '单击这里显示计划航线';
  me.TIP_TITLE_HIDE = '单击这里去掉计划航线';
  me.TIP_POINT_SHOW = '单击显示规划航点';
  me.TIP_POINT_HIDE = '单击隐藏规划航点';
  me.BACKGROUND_IMAGE_ = 'images/ruler_background.gif';
  me.xuelong_plan_line=new Object();
  me.xuelong_plan_markers=[];

  me.ENABLE_BUTTON_IMAGE_ = 'images/plan_enabled.png';
  me.DISABLE_BUTTON_IMAGE_ = 'images/plan_disabled.png';

}

GxlPlanControl.prototype = new GControl();

/**
 * 初始化控件
 */
GxlPlanControl.prototype.initialize = function(map) 
{
  var me = this;
  var container = document.createElement('div');
  me.setButtonStyle_(container);


  // “加载计划航线”按钮
  var btnEnable = document.createElement('img');
  btnEnable.width = btnEnable.height = 19;
  btnEnable.src = me.DISABLE_BUTTON_IMAGE_;
  btnEnable.title = me.TIP_TITLE_SHOW;
  GEvent.addDomListener(btnEnable, 'click', 
    function() {
      me.xlPlan();
    }
  );
  container.appendChild(btnEnable);

  var chkpoint = document.createElement('input');
  chkpoint.setAttribute("type","checkbox");
  chkpoint.setAttribute("id","xlplanpoint");
  chkpoint.setAttribute("title",me.TIP_POINT_SHOW);
  chkpoint.style.display = 'none';

  GEvent.addDomListener(chkpoint, 'click', 
    function() {
      me.xlPoint();
    }
  );
  container.appendChild(chkpoint);
  
  // 初始化内部变量
  map.xlPlanControl_ = me;
  me.map_ = map;
  me.btnEnable_ = btnEnable;
  me.chkpoint_ = chkpoint;

  map.getContainer().appendChild(container);
  return container;
}


/**
 * 设置控件的格式
 */
GxlPlanControl.prototype.setButtonStyle_ = function(ctrlarea) 
{
  ctrlarea.style.backgroundImage = 'url(' + this.BACKGROUND_IMAGE_ + ')';
  ctrlarea.style.font = 'small Arial';
  ctrlarea.style.padding = '4px';
  ctrlarea.style.border = '1px solid #888888';
  ctrlarea.style.cursor = 'pointer';
}

/**
 * 返回控件的默认位置
 */
GxlPlanControl.prototype.getDefaultPosition = function() 
{
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(8, 48));
}

/**
 * 返回控件是否已启用
 */
GxlPlanControl.prototype.isEnabled = function() 
{
  return this.enabled_;
}

GxlPlanControl.prototype.xlPlan = function() 
{
  var me = this;
  me.enabled_= ! me.enabled_;
  if(me.enabled_)//显示雪龙计划航线
  {
  	me.btnEnable_.src = me.ENABLE_BUTTON_IMAGE_;
  	me.btnEnable_.title = me.TIP_TITLE_HIDE;

  	me.chkpoint_.style.display="";
  	me.chkpoint_.setAttribute("title",me.TIP_POINT_HIDE);
  	me.chkpoint_.checked=true;
  	me.get_xuelong_plan(false);
  }
  else
  {
  	me.btnEnable_.src = me.DISABLE_BUTTON_IMAGE_;
  	me.btnEnable_.title = me.TIP_TITLE_SHOW;

  	me.chkpoint_.style.display="none";
  	me.get_xuelong_plan(true);
	$("#_memodiv").html("雪龙计划航线已去掉");

  }
}

GxlPlanControl.prototype.xlPoint = function() 
{
  var me = this;
  if(me.chkpoint_.checked)//显示雪龙计划航迹点
  {
  	me.chkpoint_.setAttribute("title",me.TIP_POINT_HIDE);
  	for(var i=0;i<me.xuelong_plan_markers.length;i++)
	{
		me.xuelong_plan_markers[i].show();
	}

  }
  else
  {
  	me.chkpoint_.setAttribute("title",me.TIP_POINT_SHOW);
  	for(var i=0;i<me.xuelong_plan_markers.length;i++)
	{
		me.xuelong_plan_markers[i].hide();
	}

  }
}

GxlPlanControl.prototype.get_xuelong_plan = function(bDel) 
{
  var me=this;	
  if(me.xuelong_plan_markers.length < 1)
  {
  	
	var np=200;//最多？个计划点位

	var ajurl="ajaxxuelongplan.php";
	var para="key=chinare2009";
	para +="&npoints="+np;
	para +="&"+getBound();

$.ajax({
	type: "GET",
	url:ajurl,
	dataType: "html",
	data:para,
	beforeSend: function()
	{
		$("#_memodiv").html("<img src=images/loading.gif>");
	},
	error: function(request)
	{
		$("#_memodiv").html("获取雪龙计划航线出错");
	},
	success: function(msg)
	{
		var xys=msg.split("\n");

		if (xys.length > 1)
		{
			var xy=[];
			for(var i=0;i<xys.length;i++)
			{
				var latlng=xys[i].split(",");
				var gpt=new GLatLng(latlng[2],latlng[3]);
				xy.push(gpt);
				if(latlng[6])//have name,then add marker
				{
  				        mm=createIconMarker(gpt, "_","blue",0,latlng[6],"预定计划:<br/>时间:"+latlng[1]+"<br />航速:"+latlng[4]+"节;航向:"+latlng[5]+"度");
      					me.xuelong_plan_markers.push(mm);
      					me.map_.addOverlay(mm);
				}	
			}
			var polyOptions = {geodesic:true};
			me.xuelong_plan_line=new GPolyline(xy,"#ffff00", 5,0.6,polyOptions);
			me.map_.addOverlay(me.xuelong_plan_line);
			
			var lenxl=Math.round(me.xuelong_plan_line.getLength());			
			$("#_memodiv").html("雪龙计划航线长度 "+formatLength_(lenxl)+"["+me.xuelong_plan_markers.length+"pts]");
			
			xy.length=0;
		}
		else	$("#_memodiv").html("没有找到雪龙计划航线!");
		
	}
	});
	
  }
  
  var np=me.xuelong_plan_markers.length;
  if(np < 1) return;
  
  for(var i=0;i< np;i++)
  {
	if(bDel)//隐藏
	{
		me.xuelong_plan_markers[i].hide();
	}
	else
	{
		me.xuelong_plan_markers[i].show();
	}
  }
  
  if(np < 2) return;

	if(bDel)//仅仅是删除当前航线
	{
		 
		me.map_.removeOverlay(me.xuelong_plan_line);
		
		$("#_memodiv").html("雪龙计划航线已经去掉");
	}
	else
	{
		me.map_.addOverlay(me.xuelong_plan_line);
			
		var lenxl=Math.round(me.xuelong_plan_line.getLength());			
		$("#_memodiv").html("雪龙计划航线长度 "+formatLength_(lenxl)+"["+np+"pts]");
	}
	
}


//--------station-------------------------------------------------------------------------
// 南极考察站点浏览的google地图控件
// author：ast@whu.edu.cn
//----------------------------------------------------------------------------
function GStationControl() 
{
  var me = this;
  me.enabled_=false;
  me.TIP_TITLE_SHOW = '单击显示各国南极考察站点';
  me.TIP_TITLE_HIDE = '单击隐藏各国南极考察站点';

  me.BACKGROUND_IMAGE_ = 'images/ruler_background.gif';
  me.station_markers=[];
  
  me.ENABLE_BUTTON_IMAGE_ = 'images/station_enabled.png';
  me.DISABLE_BUTTON_IMAGE_ = 'images/station_disabled.png';

}

GStationControl.prototype = new GControl();

/**
 * 初始化控件
 */
GStationControl.prototype.initialize = function(map) 
{
  var me = this;
  var container = document.createElement('div');
  me.setButtonStyle_(container);

  // “加载考察站点”按钮
  var btnEnable = document.createElement('img');
  btnEnable.width = btnEnable.height = 19;
  btnEnable.src = me.DISABLE_BUTTON_IMAGE_;
  btnEnable.title = me.TIP_TITLE_SHOW;
  GEvent.addDomListener(btnEnable, 'click', 
    function() {
      me.showStations();
    }
  );
  container.appendChild(btnEnable);
  
  // 初始化内部变量
  map.stationControl_ = me;
  me.map_ = map;
  me.btnEnable_ = btnEnable;

  map.getContainer().appendChild(container);
  return container;
}


/**
 * 设置控件的格式
 */
GStationControl.prototype.setButtonStyle_ = function(ctrlarea) 
{
  ctrlarea.style.backgroundImage = 'url(' + this.BACKGROUND_IMAGE_ + ')';
  ctrlarea.style.font = 'small Arial';
  ctrlarea.style.padding = '4px';
  ctrlarea.style.border = '1px solid #888888';
  ctrlarea.style.cursor = 'pointer';
}

/**
 * 返回控件的默认位置
 */
GStationControl.prototype.getDefaultPosition = function() 
{
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(8, 88));
}

GStationControl.prototype.showStations = function() 
{
  var me = this;
  me.enabled_ = ! me.enabled_;
  if(me.enabled_)//显示站点
  {
  	me.btnEnable_.src = me.ENABLE_BUTTON_IMAGE_;
  	me.btnEnable_.title = me.TIP_TITLE_HIDE;
   	me.get_stations(false);
  }
  else
  {
  	me.btnEnable_.src = me.DISABLE_BUTTON_IMAGE_;
  	me.btnEnable_.title = me.TIP_TITLE_SHOW;
  	me.get_stations(true);
  }
}

GStationControl.prototype.get_stations = function(bDel) 
{
  var me=this;
	
  if(me.station_markers.length < 1)//还没有获取站点
  {
	var np=200;//最多？个

	var ajurl="ajaxstation.php";
	var para="key=chinare2009";
	para +="&npoints="+np;
	para +="&"+getBound();

$.ajax({
	type: "GET",
	url:ajurl,
	dataType: "html",
	data:para,
	beforeSend: function()
	{
		$("#_memodiv").html("<img src=images/loading.gif>");
	},
	error: function(request)
	{
		$("#_memodiv").html("获取考察站点出错");
	},
	success: function(msg)
	{
		var xys=msg.split("\n");

		if (xys.length > 1)
		{
			var ns=xys.length;
			for(var i=0;i<ns;i++)
			{
				var latlng=xys[i].split(",");
				var gpt=new GLatLng(latlng[6],latlng[7]);
				var zl=parseInt(latlng[8]);
				var zi=0;
				if(latlng[1] == "长城站" || latlng[1] == "中山站" || latlng[1] == "昆仑站") zi=100;
				
			        mm=createIconMarker(gpt, "_","green",zl,latlng[1]+" / "+latlng[2],"<table width=238px><tr><td width='90%'>站点类型:"+latlng[0]+"<br />所属国家:"+latlng[4]+" / "+latlng[5]+"<br />建站日期:"+latlng[3]+"</td><td valign='top' width='10%'><img src='flag_image/"+latlng[5]+".gif' /></td></tr></table>",zi);

      				me.station_markers.push(mm);
      				me.map_.addOverlay(mm);
			}
			$("#_memodiv").html("总计加载 "+ns+" 个考察站点");
			me.map_.setZoom(3);
			me.map_.panTo(new GLatLng(-69.373518,76.369979));
		}
		else	$("#_memodiv").html("没有找到考察站点!");
		
	}
	});
  }
  var ns=me.station_markers.length;
  
  if(ns < 1) return;
  
  for(var i=0;i<ns;i++)
  {
	if(bDel)//隐藏站点
	{
		me.station_markers[i].hide();
	}
	else
	{
		me.station_markers[i].show();
	}
  }
  if(bDel)//隐藏站点
  	$("#_memodiv").html("考察站点已经去掉");
  else
  	$("#_memodiv").html("总计加载 "+ns+" 个考察站点");
  
	
}
