/*
/* PigeonObject.js
*  assume google maps 2.x API loaded
*  
*/


function createMarker(point, icon, html)
{
		var marker  = new PdMarker(point,icon);
		marker.setTooltip(html);
		marker.setDetailWinHTML(html);
		marker.setHoverImage("icons/radio_map_on.png");
		
		//document.getElementById("kevin").innerHTML = ""+ point.x+","+ point.y;

		return marker;
}


function pigeon(id){
	this.id = id;
	
	 this.CO;
	 this.NOX;
	 this.Temp;
	 this.Signal;
	 this.Alt;
	 this.Voltage;
	 this.Time;
	 
	 this.MPH;
	 this.NOXvalue;
	 this.COvalue;
	 this.PPM;
	 this.totalDistance = 0;

	 this.Gpoint = new GLatLng(0, 0);
	
	this.pigeon_marker;
	//for the marker
		
	//icon stuff
	var icon = new GIcon();
	icon.iconSize = new GSize(30, 30);
	icon.iconAnchor = new GPoint(15, 15);
	icon.infoWindowAnchor = new GPoint(5, 5);
	icon.image = "icons/radio_map_off.png";
		
	this.pigeon_marker = createMarker(this.Gpoint, icon, "");
	
	this.markers = new Array();
	this.points = [];
	
	this.gLine1 = new GOverlay();
	this.gLine2 = new GOverlay();
	
	this.html;
	
	
	this.setPigeonMarker  = function( markerOn){
		if (markerOn){
			this.pigeon_marker.setImage("icons/pigeon_active.png");
			this.pigeon_marker.setHoverImage("icons/pigeon_normal.png");
		}else{
			this.pigeon_marker.setImage("icons/pigeon_normal.png");
			this.pigeon_marker.setHoverImage("icons/pigeon_active.png");
		}
	
	}
	
	this.setPigeonMarkerPosition  = function( point, isActive){
		map.removeOverlay(this.pigeon_marker);
		icon.image = (isActive)?"icons/pigeon_active.png":"icons/pigeon_normal.png";
		this.pigeon_marker = createMarker(point, icon, this.html);
		
		if (isActive)
			this.pigeon_marker.setHoverImage("icons/pigeon_normal.png");
		else
			this.pigeon_marker.setHoverImage("icons/pigeon_active.png");
			
		map.addOverlay(this.pigeon_marker);
		
	}

	this.setPoint = function(Lat,  Lon,  Alt,  CO,  NOX,  Temp,  Signal,  Voltage,  Time, DateTime, isActive){
		var newGpoint = new GLatLng(Lat, Lon);
		//document.getElementById("kevin").innerHTML = samplenum;
		
		this.CO = CO;
		this.NOX = NOX;
		this.Temp = Temp;
		this.Signal = Signal;
		this.Alt = Alt;
		this.Voltage = Voltage;
		
	
		
		if (this.Gpoint.x == 0 || this.Gpoint.y == 0){ 
			//unkown distance
			var Distance = 0;
		}else{	
			var Distance  =  newGpoint.distanceFrom(this.Gpoint);
			//convert to miles
			Distance *= .000621371192;  
		}
		
		this.totalDistance += Distance;
		
		this.Gpoint  = new GLatLng(Lat, Lon);
		//document.getElementById("kevin").innerHTML = ""+ this.Gpoint.x+","+ this.Gpoint.y;
		var dtime =  Time - this.Time;

		if ((this.points.length > 1)&&(dtime > 0)){
			this.MPH = (Distance / dtime) * 3600;	
		} else{
			//first point
			
			this.MPH = 0;
		}
		
		var temp_calibrate = T_a*Temp + T_b;
		
		//set to 590 for now to remove temp calibration
		 var adc1_cali = CO - 590;//temp_calibrate;
																 
		this.PPM = .0005*Math.exp(adc1_cali*.04);//CO_a["0"]*Math.exp(adc1_cali*CO_b["0"]);
		this.PPM = (this.PPM < 0.01) ? "< .01" : this.PPM.toFixed(3);
		
		//set the values
		this.COvalue = adc1_cali/270.0;
		this.COvalue = (this.COvalue > 1) ? 1 : this.COvalue;
		this.COvalue = (this.COvalue < 0) ? 0 : this.COvalue;
		
		this.NOXvalue = (300-this.NOX) / 300.0;
		this.NOXvalue = (this.NOXvalue > 1) ? 1 : this.NOXvalue;
		this.NOxvalue = (this.NOXvalue < 0) ? 0 : this.NOXvalue;
		
		if (this.NOXvalue < .33)
			this.NOXstr = "LOW"
		else if (this.NOXvalue > .33 && this.NOXvalue < .66)
			this.NOXstr = "MEDIUM"
		else	
			this.NOXstr = "HIGH"
		
	/*	var html =	"<div style='font-family: sans-serif; width:12em; font-size: small;'>" +
										"lat = "+ this.Gpoint.x + 
										"<br/>lon = "+ this.Gpoint.y + 
										"<br/>alt = " + Alt + 
										"<br/>sensor1 = " + CO +
										"<br/>sensor2 = " + NOX +
										 "<br/>temp = " + Temp +
										 "<br>distance = " + Distance.toFixed(2) +
										"<br/>MPH = " + this.MPH.toFixed(1)+"mph (" + ((.621371192*this.avgMPH).toFixed(1))+" km/h)"
										"<br/>TempCali = " + adc1_cali+ 
										"<br/>PPM = " + this.PPM.toFixed(3) +
										
										document.getElementById("speed_data").innerHTML =  p.MPH.toFixed(1)+"mph (" + ((.621371192*p.MPH).toFixed(1))+" km/h)";
					document.getElementById("distance_data").innerHTML =  p.totalDistance.toFixed(2)+"mi ("+ ((.621371192*p.totalDistance).toFixed(2))+" km)";
					document.getElementById("altitude_data").innerHTML =  (3.2808399*p.Alt).toFixed(2)+"ft ("+p.Alt+"m)";

										
										
										"</div>";*/
										
			this.html =	"<div style='font-family: sans-serif; width:14em; font-size: small;'>" +
										"<center>" + DateTime + "</center>"+
										"CO: " + this.PPM + "ppm" +
										"<br/>NOX: " + this.NOXstr +
										"<br/>Alt = " +(3.2808399*this.Alt).toFixed(2)+"ft ("+this.Alt+"m)" +
										 "<br>Distance = " + this.totalDistance.toFixed(2)+"mi ("+ ((1.609344*this.totalDistance).toFixed(1))+" km)" +
										"<br/>Speed = " +  this.MPH.toFixed(1)+"mph (" + ((1.609344*this.MPH).toFixed(1))+" km/h)" +
										"</div>";								
		
		//for the line
		this.points.push(this.Gpoint);
		
		
		icon.image = "icons/radio_map_off.png";
		var marker = createMarker(this.Gpoint, icon, this.html);
		
		
		if (Distance > .01 || (this.markers.length==0)){
		//only add points if we've gone somewhere or first point
			//map.addOverlay(marker);
			//if (this.markers.length > 0)
			//	this.markers[this.markers.length-1].setImage("icons/radio_map_off.png");
			this.markers[this.markers.length] = marker;
			map.addOverlay(this.markers[this.markers.length-1]);
		}	
		
		//switch to pigeon and move to point
		//this.setPigeonMarker(isActive);	
		//this.markers[this.markers.length-1].setPoint(this.Gpoint);
		if (FF){
			//if (this.pigeon_marker)
			//	this.pigeon_marker.setPoint(this.Gpoint);
			//else	
			//	map.addOverlay(this.pigeon_marker);
				
			/*if (this.markers[this.markers.length-2])
				this.markers[this.markers.length-2].setImage("icons/radio_map_off.png");
				
			if (isActive)
				this.markers[this.markers.length-1].setImage("icons/pigeon_active.png");
			else
				this.markers[this.markers.length-1].setImage("icons/pigeon_nornmal.png");*/
					
		}else{
			map.removeOverlay(this.pigeon_marker);
			icon.image = (isActive)?"icons/pigeon_active.png":"icons/pigeon_normal.png";
			this.pigeon_marker = createMarker(this.Gpoint, icon, this.html);
			
			if (isActive)
				this.pigeon_marker.setHoverImage("icons/pigeon_normal.png");
			else
				this.pigeon_marker.setHoverImage("icons/pigeon_active.png");
				
			map.addOverlay(this.pigeon_marker);
		}
		
		this.Time = Time;
		
		//Signal
		var imagenum = Math.round(this.Signal/6) -1;
		 imagenum = (imagenum <0)? 0: imagenum;
		//document.getElementById("kevin").innerHTML = '<img src=\"icons/signal_'+imagenum +'.gif \">';
		
		//Battery
		imagenum = Math.round((this.Voltage-3300)/88 -1);
		imagenum = (imagenum <0)? 0: imagenum;
		imagenum = (imagenum >8)? 8: imagenum;
		//document.getElementById("kevin").innerHTML += ' <img src=\"icons/battery_'+imagenum +'.gif \">';
		//document.getElementById("kevin").innerHTML = "ta="+ T_a+" tb="+ T_b+"C0_a"+CO_a+"C0_b"+CO_b;
	}
	
	this.addLine = function(){
		this.gLine1 = new GPolyline(this.points, "#660000",4);
		this.gLine2 = new GPolyline(this.points, "#F0C030",2);
		map.addOverlay(this.gLine1);
		map.addOverlay(this.gLine2);
		map.removeOverlay(this.markers);
		
		
	
	}
	
	this.removeLine = function(){
		map.removeOverlay(this.gLine1);
		map.removeOverlay(this.gLine2);
		
		//for (i=0; i< markers.length; i++)
		//	map.removeOverlay(this.markers[i]);
		if (this.markers[this.markers.length-1])
				this.markers[this.markers.length-1].setImage("icons/radio_map_off.png");
	
	}
	
}
