
/******************************************************************************************************/
// CREATES THE "DOT" OBJECT THAT REPRESENTS A RUNNER
function Runner(color, name, clockTime, time, stepDistance, pace, place, splitSeconds, additionalData)
{
	this.color = color;					// this runner's color representation
	this.name = name;					// my name
	this.place = place;					// my finish place
	this.clockTime = clockTime;			// represented as hh:mm:ss
	this.time = time; 					// number of seconds to complete the event
	this.split1Sec = splitSeconds; 		// number of seconds to the first split mat
	this.stepDistance = stepDistance; 	// how far he/she travels each 'step'
	this.CUR_STEP = 0; 					// tracks the number of steps taken to draw the image (total steps varies depending upon time desired total drawing time)
	this.curLocation = 0; 				// in pixels
	this.indexInArray = 0;
	this.data = additionalData;			// AGE | SEX | CITY | STATE
	this.x = 0;
	this.y = 0;
	this.alive = true; // becomes false when this runner finishes

	this.myCurrentLATLONG = new GLatLng(rGPS[0][0], rGPS[0][1]);	
	this.myMark = placeMarker(null, this.myCurrentLATLONG, this.color, this);

	this.setColor = function(newColor)
	{
		this.color = newColor;
	};
	
	
	this.prepDot = function(index)
	{
		nextLocation = getXY(this.stepDistance, this.CUR_STEP, this.curLocation);
		this.x = nextLocation[0];
		this.y = nextLocation[1]-(index*16);
		jg.setColor(this.color); // user specified color		
		jg.paint();
		this.prepTopBox(index);
	};//------------------------------------------------------------------
	
	
	
	this.setIndexLocation = function(curIndexInArray)
	{
		this.indexInArray = curIndexInArray;
	};//---------------------------------------------
	
		
	this.populateInfoBox = function()
	{
		this.x = 6; // displays underneath the clock
		this.y = 28+(this.indexInArray*23);
		jg.setColor(this.color); // user specified color
		dispText = this.name;
		jg.drawString(dispText, this.x+20, this.y-4);	
		jg.drawString(this.getDistance(), this.x+164, this.y-4);
	//	jg.drawString(calculatePace(this.getNextSplitTime(),this.getNextSplitDistance()), this.x+200, this.y-4);
		jg.drawString(this.clockTime, this.x+210, this.y-4);					
		jg.drawImage("http://www.areep.com/results/maps/images/rm"+this.color +".gif", this.x, this.y-4, 16, 19);
		jg.paint();	
	};
		
	// not used yet
	this.updateDistanceInBox = function()
	{
		jg.drawString(this.getDistance(), this.x+164, this.y-4);	
	};//----------------------------------------------------
	
	this.prepTopBox = function(index)
	{
		return;
		this.x = 10; // displays underneath the clock
		this.y = 30+(index*16);
		jg.setColor(this.color); // user specified color
		dispText = this.name;
		if(!this.alive) // once finished, display time next to name
			dispText = this.name + " - " + clockTime;				
		jg.drawString(dispText, this.x+6, this.y-4);	
//		jg.fillEllipse(this.x, this.y, 5, 5);	
		jg.paint();
	};

	this.getDistance = function()
	{
		dist = (this.curLocation*ACTUAL_DISTANCE)/TOTAL_PIXEL_DISTANCE;
		if(dist >= ACTUAL_DISTANCE)
			dist = ACTUAL_DISTANCE;
		return roundNumber(dist, 2);
	};
	
	this.drawDot = function()
	{
//			jg.clear();
//		alert("Drawing for " + this.displayInformation());
//			var totalPixels = this.CUR_STEP*this.stepDistance;

		if(this.alive)
		{
			// determine if the pace should change
			this.calculateStepPixelDistance();
		
			nextLocation = getXY(this.stepDistance, this.curLocation);
			this.CUR_STEP++;
			this.curLocation += this.stepDistance;

			dist = this.getDistance();
			msCum = dist*(this.time*1000)/ACTUAL_DISTANCE; // number of ms it takes to get to this step's total distance 					
			setTimeByRunner(msCum); // updates the clock to display this time

			var point = nextLocation;
		
//			if(dist > 9) alert(this.name + ' is almost done. ' +  ACTUAL_DISTANCE  + ' ' + dist);

			myPano.setLocationAndPOV(point);

			if(dist >= ACTUAL_DISTANCE)
			{
				setTimeByRunner(this.time*1000); // updates the clock to display this time				
//				clearInterval(intervalID); IS_MOVING=false;
				if(place < 21)
					ending = NUM_ENDING[place];
				else
					ending = NUM_ENDING[place%10];
				alert(this.name + " finished " + place + ending + " in a time of " + clockTime);			
				this.alive = false;
				dist = ACTUAL_DISTANCE;
				point = this.myCurrentLATLONG;
			}
			this.myMark = placeMarker(this.myMark, point, this.color, this);
			this.myCurrentLATLONG = point;				
			this.populateInfoBox();
			//jg.paint();			
		}	
		else // indicates that he is dead (reached the finish)
		{
			this.populateInfoBox();		
			return false;	
		}
		return true;
	};
	
	
	
	this.displayInformation = function()
	{
		return "Name: " + name + "\n Time: " + clockTime + "\n Pace: " + pace + " - split1: " + this.split1Sec;
	}

	// returns my finish time in seconds
	this.myTimeInSeconds = function() { return time; }
	
	// returns the place i finished
	this.getFinClockTime = function() { return this.clockTime; }

	// sets the speed that i go through the simulation
	this.setSpeed = function(newSpeed) { this.stepDistance = newSpeed; }
	
	// returns the place i finished
	this.getPlace = function() { return this.place; }

	// returns my name
	this.getName = function() { return this.name; }
	
		
	this.calculateStepGPS= function()
	{
		var myTravTime = (TOTAL_TRAVERSE_TIME*this.time)/FASTEST_TIME;
		this.setSpeed(TOTAL_PIXEL_DISTANCE/(myTravTime/(REFRESH_RATE/1000)));					
	}//--------------------------------------------------


	// NEEDS TO BE UPGRADED TO GO THROUGH THE ARRAY OF SPLITS!!
	this.getNextSplitDistance = function()
	{
		if(this.getDistance() < SPLIT_DIST_1)
			return SPLIT_DIST_1;
		else
			return (ACTUAL_DISTANCE-SPLIT_DIST_1);
	}//------------------------------------------------

	// NEEDS TO BE UPGRADED TO GO THROUGH THE ARRAY OF SPLITS!!
	this.getNextSplitTime = function()
	{
		if(this.getDistance() < SPLIT_DIST_1)
			return this.split1Sec;
		else
			return this.time-this.split1Sec;
	}//------------------------------------------------

	this.calculateStepPixelDistance = function()
	{
		var myTravTime;

		if(this.getDistance() < SPLIT_DIST_1) /* need to move at the pace maintained up to split 1 */
		{
//			alert(calculatePace(this.split1Sec,SPLIT_DIST_1));		
			var curExtrapFinishTime = ACTUAL_DISTANCE * this.split1Sec / SPLIT_DIST_1; // expected finish time up to this point (in sec)
			myTravTime = (TOTAL_TRAVERSE_TIME*curExtrapFinishTime)/FASTEST_TIME;
		}
		else // move at the pace from here to the finish
		{
//			alert(calculatePace(this.time,ACTUAL_DISTANCE));
					
			var curExtrapFinishTime = ACTUAL_DISTANCE * (this.time-this.split1Sec) / (ACTUAL_DISTANCE-SPLIT_DIST_1); // expected finish time from this checkpoint to the finish
			myTravTime = (TOTAL_TRAVERSE_TIME*curExtrapFinishTime)/FASTEST_TIME;
		}
		this.setSpeed(TOTAL_PIXEL_DISTANCE/(myTravTime/(REFRESH_RATE/1000)));					
	}//--------------------------------------------------

}

/******************************************************************************************************/

 
