function Participant(data)
{
	//initialize leaderboard stuff
	for (var n in arguments[0]) { this[n] = arguments[0][n]; }
	this.tr = $("#row_"+this.imei);
	this.markertoggleelement = $("#row_"+this.imei+" .markertoggle");
	this.speedelement = $("#row_"+this.imei+" .speed");
	this.distanceelment = $("#row_"+this.imei+" .to_finish");
	this.positionelement = $("#row_"+this.imei+" .position");
	this.gpoints = new Array();
	$("#row_"+this.imei).data('participant', this);
}


Participant.prototype.getSpeed = function()
{
	//update content also
	var currentpoint = this.currentpoint();
	var speed = 0.00;
	if (race.units == "m")
	{
		speed = Math.round(currentpoint.speed*5.39956803)/10;
	}
	else
	{
		speed = Math.round(currentpoint.speed*10)/10;
	}
	return speed;
};

Participant.prototype.getDistance2Finish = function()
{
	var distance_to_finish = this.currentpoint().distance_to_finish;
	if (race.units == "m")
	{
		distance_to_finish = distance_to_finish * 0.539956803;
	}
	return distance_to_finish.toFixed(2);
};

Participant.prototype.getCoveredPercent = function()
{
	return 100 - (this.currentpoint().distance_to_finish * 100 / race.racedistance);
}

Participant.prototype.currentpoint = function()
{
	if (this.gpoints && this.gpoints.length > 0 && this.index >= 0)
	{
		return this.gpoints[this.index];
	}
	return null;
};

Participant.prototype.getFlotData = function()
{
	var data = new Array();
	var d=new Date(race.start_epoch*1000);
	var offset = d.getTimezoneOffset()*60*2000;

	var step = Math.round(this.gpoints.length/300);
	
	if (step > 1)
	{

		for (var i=step+1; i < this.gpoints.length; i+=step)
		{
			var speed = this.getMeanSpeed(i-step, i);
			speed[0] *= 1000;
			speed[0] -= offset;
			data.push(speed);
		}
	}
	else
	{
		for (var i=1; i < this.gpoints.length; i++)
		{
			var speed= new Array((this.gpoints[i].time*1000)-offset, this.gpoints[i].speed);
			data.push(speed);
		}
	}
	var bla = {color: this.color, data: data};
	return bla;
}

Participant.prototype.getMeanSpeed = function(start, end)
{
	var points = this.gpoints.slice(start, end)
	if (points.length == 0)
		return 0;

	var realstart = parseInt(points[0].time);
	var realend = parseInt(points[points.length-1].time);
	var delta = realend - realstart;

	var speed = google.maps.geometry.spherical.computeLength(points) / delta;
	// convert to km/h
	speed = speed * 60 * 60 / 1000;
	var time = (realstart + delta);
	var speed_time = new Array(time, speed);
	return speed_time;
}


