// JavaScript Document
var centre=70;
var altimetre={id_alti100:null, id_alti1000:null};

var cap_disp= {
		  id_valeur:new Array,
		  id_ligne:null
	  };
var attitude={id:null,id_back:null,point_delay:null};


function init_instruments() {
 // ALTIMETRE
 
 
      var frag = document.createDocumentFragment(true);
      for (var i = 0; i < 10; i++) {
        var ligne = document.createElementNS(svgns, 'line');
		var teta=Math.PI*i/5;
		var x1=centre+53*Math.sin(teta);
		var x2=centre+64*Math.sin(teta);
		var y1=centre-53*Math.cos(teta);
		var y2=centre-64*Math.cos(teta);
        ligne.setAttribute('x1', x1);
        ligne.setAttribute('y1', y1);
        ligne.setAttribute('x2', x2);
		ligne.setAttribute('y2', y2);
		ligne.setAttribute('stroke-opacity', '1');
		ligne.setAttribute('stroke-width', '3');
        ligne.setAttribute('stroke', 'white');
		
		var textNode = document.createTextNode(i, true);
		var chiffre = document.createElementNS(svgns, 'text');
		x1=-3+centre+45*Math.sin(teta);
		y1=5+centre-45*Math.cos(teta);
        chiffre.setAttribute('x', x1);
        chiffre.setAttribute('y', y1);
		chiffre.appendChild(textNode);
        frag.appendChild(ligne);
		frag.appendChild(chiffre);
      }
      // now append the DocumentFragment to the DOM
      //var svg = document.getElementsByTagNameNS(svgns, 'svg')[0];
	  var svg = document.getElementById('svg_alti');
      svg.appendChild(frag); // DocumentFragment disappears leaving circles
	  altimetre.id_alti100=document.getElementById('alti100');
	  altimetre.id_alti1000=document.getElementById('alti1000');
	  
	   // CAP
	  
      var frag = document.createDocumentFragment(true);
      for (var i = 0; i < 9; i++) {
        var ligne = document.createElementNS(svgns, 'line');
		var teta=Math.PI*(-10+2*i)/180;
		var x1=centre-60*Math.sin(teta);
		var y1=120;
		ligne.setAttribute('stroke-opacity', '1');	
		ligne.setAttribute('stroke-width', '2');
        ligne.setAttribute('x1', x1);
        ligne.setAttribute('y1', y1);
        ligne.setAttribute('x2', x1);
		ligne.setAttribute('y2', y1-10);
        ligne.setAttribute('stroke', 'white');
		frag.appendChild(ligne);
		
		
      }
	  cap_disp.id_ligne = document.getElementById('cap_ligne');
      cap_disp.id_ligne.appendChild(frag); // DocumentFragment disappears leaving circles
	  cap_disp.id_valeur.push(document.getElementById('svgcap1'),document.getElementById('svgcap2'),document.getElementById('svgcap3'));  //texte cap
	  
	  //Attitude
	  attitude.id=document.getElementById('attitude');
	  attitude.id_back=document.getElementById('attitude_back');

 	
}


function update_instruments() {  // Update instruments et divers RC
	disp_alti(helico.alti);
	disp_attitude(helico.heading,helico.tilt,helico.roll);
	update_map();info_wp();
	
	//Filtrage Vitesses
	v_reel.vx=frc(v_cons.vx,v_reel.vx,v_reel.rc);
	v_reel.vz=frc(v_cons.vz,v_reel.vz,v_reel.rc);
	v_reel.vr=frc(v_cons.vr,v_reel.vr,v_reel.rc);
	v_reel.vy=frc(v_cons.vy,v_reel.vy,v_reel.rc);
	
	
	//Affichage des vitesses
			var vxs=Math.floor(v_cons.vx*10)/10;
			var vys=Math.floor(v_cons.vy*10)/10;
			if (v_cons.vx<0) {
				document.getElementById('VH1').innerHTML="Vx: "+ vxs+ "<br />Vy: "+vys;
				document.getElementById('VH2').innerHTML="";
				
			} else {
				document.getElementById('VH2').innerHTML="Vx: "+ vxs+ "<br />Vy: "+vys;
				document.getElementById('VH1').innerHTML="";
			}
			var newPosY=68-V_log(v_cons.vx);
			document.getElementById('badin').style.top = newPosY + 'px';
			var newPosX=53+V_log(v_cons.vy);
			document.getElementById('badin').style.left = newPosX + 'px';
			
			
			document.getElementById('VZ').innerHTML="Vz: "+Math.floor(v_cons.vz*10)/10;	
			newPosY=71-V_log(v_cons.vz);
			document.getElementById('montee').style.top = newPosY + 'px';
			
			document.getElementById('VR').innerHTML="Vr: "+Math.floor(v_cons.vr*10)/10;
			newPosX=55+V_log(v_cons.vr);
			document.getElementById('rotation').style.left = newPosX + 'px';
			
			//Auto / Manuel
			if (helico.timer_auto--<0) {
				helico.timer_auto=-1;
				if(!helico.auto) helico_auto(true);
			}
	setTimeout('update_instruments();',200);
	
}
//ALTIMETRE
function disp_alti(alti) {
	
	teta=Math.PI*alti/500;
	var x2=centre+60*Math.sin(teta);
	var y2=centre-60*Math.cos(teta);
	 altimetre.id_alti100.setAttribute('x2', x2);
	 altimetre.id_alti100.setAttribute('y2', y2);
	 
	 
	 var teta=Math.PI*alti/5000;
	x2=Math.floor(centre+30*Math.sin(teta-0.2));
	y2=Math.floor(centre-30*Math.cos(teta-0.2));
	var x3=centre+40*Math.sin(teta);
	var y3=centre-40*Math.cos(teta);
	var x4=centre+30*Math.sin(teta+0.2);
	var y4=centre-30*Math.cos(teta+0.2);
	var points="70,70,"+x2+","+y2+","+x3+","+y3+","+x4+","+y4;
	altimetre.id_alti1000.setAttribute('points', points);
}

function disp_attitude(cap_,tilt,roll) {
	
	var j=0;
	for (i=0;i<9;i++) {
		var teta=10*Math.floor(cap_/10)-40+10*i;
		var tetar=Math.PI*(teta-cap_)/180;
		var x1=centre-48*Math.sin(tetar);
		var y2=110;
		if (teta%30==0) {
			cap_disp.id_valeur[j].setAttribute('x', x1-8);
			cap_disp.id_valeur[j].childNodes[0].nodeValue=Math.floor(teta);
			y2=105;j++; 
		
		}
		cap_disp.id_ligne.childNodes[i].setAttribute('x1', x1);
		cap_disp.id_ligne.childNodes[i].setAttribute('x2', x1);
		cap_disp.id_ligne.childNodes[i].setAttribute('y2', y2);
	}
 	tilt=Math.PI*tilt/180;
	roll=Math.PI*roll/180;
	var t=centre-70*Math.sin(tilt);
	var r=70*Math.tan(roll);
	var y1=t+r;
	var y2=t-r;
	var points="0,"+y1+",140,"+y2+",140,140,0,140";
	attitude.id.setAttribute('points', points);
	attitude.point_delay= points;
	setTimeout('disp_attitude_delayed();',50);
}
function disp_attitude_delayed() { //2 eme image, evite flick sur IE
	attitude.id_back.setAttribute('points', attitude.point_delay);
}

function info_wp() {
	nb_wp=(nb_wp+1)%10;
	if (nb_wp==0 || scene.update_on==false) { 
			var lat1=Math.PI*lati[scene.idx_lat]/180;
			var lng1=Math.PI*lngi[scene.idx_lat]/180;
			var lat2=Math.PI*helico.lat/180;
			var lng2=Math.PI*helico.lng/180;
			var dist =1.852* 60*180*Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2)*Math.cos(lng1-lng2))/Math.PI;	
			if (dist >10) {
				dist=Math.floor(dist);
			} else {
				dist=Math.floor(dist*10)/10;
			}
			
			document.getElementById("distance").innerHTML="Dist. : "+dist+ " km"; //dist		
			document.getElementById("ground_alti").innerHTML="Ground Alt. : "+Math.floor(helico.alti_sol)+ " m";
			
	}
	
	
}
//Manettte GAZ
var Gaz={
 isClicked:false,
 objectToDrag:null,
 ecartX:0,
 ecartY:0,
 curX:0,
 curY:0};

function getPositionCurseur(e){
	//ie
	if(document.all){
		Gaz.curX = event.clientX;
		Gaz.curY = event.clientY;
	}
	
	//netscape 4
	if(document.layers){
		Gaz.curX = e.pageX;
		Gaz.curY = e.pageY;
	}
	
	//mozilla
	if(document.getElementById){
		Gaz.curX = e.clientX;
		Gaz.curY = e.clientY;
	}
	
}

function downAction(p_obj,e){
	
	Gaz.isClicked = true;
	Gaz.objectToDrag =p_obj;
	getPositionCurseur(e);
	Gaz.ecartX = Gaz.curX - parseInt(Gaz.objectToDrag.style.left);
	Gaz.ecartY = Gaz.curY - parseInt(Gaz.objectToDrag.style.top);
	Gaz.objectToDrag.style.zIndex = 200;
	
}

function moveAction(p_obj, e){
	var newPosX;
	var newPosY;
	if(Gaz.isClicked){
		helico_auto(false);
		
		getPositionCurseur(e);		
		newPosX = Gaz.curX - Gaz.ecartX;
		newPosY = Gaz.curY - Gaz.ecartY;
		if (Gaz.objectToDrag.id=="montee") {
			newPosY=Math.max(16,newPosY); //Limite montee 71=neutre
			newPosY=Math.min(newPosY,126);
			
			newPosX=124;
			v_cons.vz=V_exp(71,newPosY); // Consigne
			
		}
		if (Gaz.objectToDrag.id=="rotation") {
			newPosX=Math.max(0,newPosX); //Limite  55=neutre
			newPosX=Math.min(newPosX,110);
			newPosY=1;
			v_cons.vr=V_exp(newPosX,55);
			
		}
		if (Gaz.objectToDrag.id=="badin") {
			newPosX=Math.max(0,newPosX); //Limite  53=neutre
			newPosX=Math.min(newPosX,108);
			v_cons.vy=V_exp(newPosX,53);
		
			newPosY=Math.max(13,newPosY); //Limite  68=neutre
			newPosY=Math.min(newPosY,123);
			v_cons.vx=V_exp(68,newPosY);
			
		}
		
		Gaz.objectToDrag.style.left = newPosX + 'px'; // 
		Gaz.objectToDrag.style.top = newPosY + 'px';
		
		
	}
}


function leaveAction(){
	if(Gaz.isClicked) {
		Gaz.isClicked = false;
		Gaz.objectToDrag.style.zIndex = 0;	
		document.getElementById('hidden_focus').focus(); //Pour reprendre le curseur
		
	}
}

function V_exp(pos,centre) {  // Conversion position en 2^n
	
			var v=0;
			var vl=(pos-centre)/5;
			if (vl>2) {
				v=Math.pow(2,vl)/8;
			} 
			if (vl<=2 && vl>0.2) {
				v=vl/4;
			}
			if (vl<-2) {
				v=-Math.pow(2,-vl)/8;
			} 
			if (vl>=-2 && vl<-0.2) {
				v=vl/4;
			}
			return v;
}
function V_log(v) {  // Conversion vitesse en position
			var pos_r=0
			
			if (v>0.5) {
				pos_r=5*Math.log(8*v)/Math.LN2;
			} 
			if (v<=0.5 && v>=-0.5) {
				pos_r=v*20;
			}
			if (v<-0.5) {
				pos_r=-5*Math.log(-8*v)/Math.LN2;
			} 
			
			return pos_r;
}
function helico_auto(t){
	helico.auto=t;
	
	if(t){
		helico.timer_auto=-1;
		document.getElementById("voyant_b").style.visibility="visible";
		document.getElementById("voyant_r").style.visibility="hidden";
	}else{
		helico.timer_auto=150;
		document.getElementById("voyant_r").style.visibility="visible";
		document.getElementById("voyant_b").style.visibility="hidden";
	}
}

