

// AIS
var Ship=new Array;
var AIS_lat='';
var AIS_lng='';
var AIS_name='';
var AIS_mmsi='';
var AIS_cap='';
var AIS_type='';

var tAIS_mmsi=new Array;
var tAIS_lat=new Array;
var tAIS_lng=new Array;
var tAIS_cap=new Array;
var tAIS_name=new Array;
var tAIS_type=new Array;

var AIS__view=false;
var last_lat_cam=0;
var last_lng_cam=0;




/* AIS */
/********/
 function set_AIS(){

	  tAIS_mmsi=tAIS_mmsi.concat( AIS_mmsi.split(","));
	  tAIS_lat=tAIS_lat.concat(AIS_lat.split(","));
	  tAIS_lng=tAIS_lng.concat(AIS_lng.split(","));
	  tAIS_cap=tAIS_cap.concat(AIS_cap.split(","));
	  tAIS_name=tAIS_name.concat(AIS_name.split(","));
	  tAIS_type=tAIS_type.concat(AIS_type.split(","));
	  init_AIS_ship();
 }

 
function init_AIS_ship() {
	if(tAIS_name.length>0) {
				var dejala=false;
				 for (var k=0;k<Ship.length;k++) {
					 if(Ship[k].mmsi==tAIS_mmsi[0]) {
						 dejala=true;
						 k=Ship.length;
						 
					 }
				 }
				var nom=tAIS_name[0];tAIS_name.shift();
				var lat_ref=tAIS_lat[0];tAIS_lat.shift();
				var lng_ref=tAIS_lng[0];tAIS_lng.shift();
				var cap_ref=tAIS_cap[0];tAIS_cap.shift();
				var type_=tAIS_type[0];tAIS_type.shift();
				var mmsi=tAIS_mmsi[0];tAIS_mmsi.shift();
				
			if (dejala==false && mmsi!="") { // initialise un bateau
				
				
				lat_ref=1*lat_ref;
				lng_ref=1*lng_ref;
				cap_ref=1*cap_ref;
				var i=Ship.length;
				
				
		
				var loc_ = null;
				var scal_=null   ;
				var orient_=null;
					
					var lineString = ge.createLineString('');
					
					lineString.setAltitudeMode( ge.ALTITUDE_ABSOLUTE ) ;
					lineString.getCoordinates().pushLatLngAlt( lat_ref, lng_ref, 0);
					lineString.getCoordinates().pushLatLngAlt( lat_ref, lng_ref, 10000);			
					// create the linestring placemark and add it to Earth
					var lineStringPlacemark = ge.createPlacemark('');
					lineStringPlacemark.setGeometry(lineString);
					ge.getFeatures().appendChild(lineStringPlacemark);
		
				//modele 3d
					var	placemark = ge.createPlacemark('');
						
						
										
						//Le model
					var	model = ge.createModel('');
						ge.getFeatures().appendChild(placemark);
						loc_ = ge.createLocation('');
						model.setLocation(loc_);
						scal_=ge.createScale  	( '' )   ;
						model.setScale	(scal_);
						orient_=ge.createOrientation('');
						model.setOrientation(orient_);
						var	link_ = ge.createLink('');
						
						var h="http://www.sea-seek.com/3d/images/models/"+mod_file[type_];
						link_.setHref(h); // model 3d
						model.setLink(link_);
						Ship[i]=new objet_ship(mmsi,lat_ref,lng_ref,-0.5,loc_,scal_,orient_,link_,model,cap_ref,mod_cap0[type_],mod_scal0[type_],type_,false,placemark,lineStringPlacemark); // Une altitude bnegative permet voir sous eau 
		
				 model.setAltitudeMode(ge.ALTITUDE_ABSOLUTE);
				var scale=Ship[i].scal0;
				Ship[i].scal.set(scale,scale,scale);
				Ship[i].loc.setLatitude(Ship[i].lat);
				Ship[i].loc.setLongitude(Ship[i].lng);
				Ship[i].loc.setAltitude(Ship[i].alti);
				Ship[i].orient.setHeading((Ship[i].heading+Ship[i].cap0)%360);
				
				placemark.setName(nom+' - Head:'+cap_ref);
				placemark.setGeometry(model);
				placemark.setVisibility(false);
			}
	}
}



function objet_ship(un_mmsi,une_latitude,une_longitude,une_alti,une_location,une_scale,une_orient,un_link,un_model,u_heading,_cap0,_scale0,_type,_highres,_place_mod,_place_ligne) {
      this.mmsi=un_mmsi;
	  this.lat=une_latitude;
      this.lng=une_longitude;
	  this.alti=une_alti;
	  this.alti0=une_alti;
	  this.alti_sol=une_alti;

	  this.loc=une_location;
	  this.scal=une_scale;
	  this.orient=une_orient;
	  this.tilt=0;
	  this.type=_type;
	  this.link_=un_link;
	  this.model=un_model;
	 
	  this.heading=u_heading;

	  this.cap0=_cap0;
	  this.scal0=_scale0; 
	  
	  this.place_mod=_place_mod; 
	  this.place_ligne=_place_ligne; 
	  this.highres=_highres; 

 }
 function AIS_view() {
	 AIS__view=!AIS__view;
	 if (AIS__view) {
		 for (i=0;i<Ship.length;i++) {
						Ship[i].place_mod.setVisibility(false);
						Ship[i].place_ligne.setVisibility(AIS__view);
						Ship[i].highres=false;
		}
		 distance_camera();
		 var mes='AIS - '+lg_('Hide ships','Masquer','','');
		 
	 } else {
	 	for (i=0;i<Ship.length;i++) {
						Ship[i].place_mod.setVisibility(AIS__view);
						Ship[i].place_ligne.setVisibility(AIS__view);
						Ship[i].highres=AIS__view;
		}
		 var mes='AIS - '+lg_('View ships','Visualiser','','');
		 
 	}
	document.getElementById("bt_AIS").value=mes;
 }
 function distance_camera() {
	 if (AIS__view) {
			 var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_ABSOLUTE);
				
				var range=lookAt.getRange();
				var latc=lookAt.getLatitude();
				var lngc=lookAt.getLongitude();
				for (i=0;i<Ship.length;i++) {
						var dist=range+100000*Math.abs(Ship[i].lat-latc)+50000*Math.abs(Ship[i].lng-lngc)
						if (dist<10000 && Ship[i].highres==false) {chg_model(i,true);	}
						if (dist>10000 && Ship[i].highres) {chg_model(i,false);	}
				}
				
				var lati=Math.floor(latc);
				var lngi=Math.floor(lngc);
		
				if (  lati!= last_lat_cam && lngi!=last_lng_cam ) { // 
					last_lat_cam=lati;last_lng_cam=lngi;
					var message_='call_AIS='+idx_zone+'&latc='+latc+'&lngc='+lngc;  // demande paramètres AIS, on s'est déplacé		
					go_(message_);
					
				} 
				for (var k=0;k<5;k++) {
					init_AIS_ship();
				}
				  setTimeout("distance_camera();",500);
				
	 }
 }
 function chg_model(i,hi_res) {  // Alterne entre modele ou ligne
						Ship[i].place_mod.setVisibility(hi_res);
						Ship[i].place_ligne.setVisibility(!hi_res);
						Ship[i].highres=hi_res;
 }
