function TrackView()
{
	this.r = null;
	this.vehicles = new Array();
	this.vehpos = "";
	this.posrow = 0;
	this.curTrackName = "MissionBay";
	this.trackviewpanel;
	this.curTrack = new Object();
	//Tracks
	this.Detroit = new Object();
	this.MissionBay = new Object();
	this.Madison = new Object();
	this.posDataUpdater = null;
	this.vehPosUpdater = null;
	this.url = "";

	this.BGimage = null;

	this.loadTrackView = function(panelName, url) {
		this.url = url;
		this.r = Raphael(panelName);
		
		this.trackviewpanel = document.getElementById(panelName);
		this.BGimage = this.r.image("",0,0, this.trackviewpanel.offsetWidth, this.trackviewpanel.offsetHeight);
		this.setUpTracks();
		this.posDataUpdater = new PeriodicalExecuter(this.loadVehiclePositions, 10);
		this.vehPosUpdater = new PeriodicalExecuter(this.updateVehiclePositions, 0.42);
	}

	this.loadVehiclePositions = function()
	{
		new Ajax.Request(this.url,
	  {
		method:'get',
		parameters: {nocache: (new Date()).getTime()},
		onSuccess: function(transport){
		  this.vehpos += transport.responseText;
		}
	  });
	}

	this.clearVehicles = function()
	{
		//if(this.vehPosUpdater != null)
		//	this.vehPosUpdater.stop();
		this.vehpos = "";
		this.posrow = 0;
		//this.vehicles.clear();
		//if(this.vehPosUpdater != null)
		//	this.vehPosUpdater = new PeriodicalExecuter(this.updateVehiclePositions, 0.40);
	}

	this.changeCurrentTrack = function(trackName)
	{
		if(trackName == "MissionBay")
			this.curTrack = this.MissionBay;
		else if(trackName == "Detroit")
			this.curTrack = this.Detroit;
		else if(trackName == "Madison")
			this.curTrack = this.Madison;
		
		this.trackviewpanel.style.width = this.curTrack.bgWidth + "px";
		this.trackviewpanel.style.height = this.curTrack.bgHeight + "px";
		this.r.setSize(this.curTrack.bgWidth, this.curTrack.bgHeight);
		this.trackviewpanel.style.background = "url('../trackview/"+this.curTrack.imageUrl+"')";
		this.BGimage.setAttr("src", "../trackview/"+this.curTrack.imageUrl).setAttr("width", this.curTrack.bgWidth).setAttr("height", this.curTrack.bgHeight);
	}

	this.updateVehiclePositions = function()
	{
		var rows = this.vehpos.split("\n");
		if(rows.length > 0)
		{
			if(this.posrow >= rows.length - 1)
			{
				this.posrow = rows.length - 1;
				return;
			}

			var veh = rows[this.posrow].split(",");
			for(var i = 0; i < veh.length; i++)
			{
				if(this.posrow == 0)
				{	
					if(this.vehicles[i] != null)
					{
						this.vehicles[i].vehShape.remove();
						this.vehicles[i].tailPath.remove();
					}
					this.vehicles[i] = new Object();
					this.vehicles[i].tailPoints = new Array();
					this.vehicles[i].tailPath = this.r.path().attr("stroke", "#ffffff").attr("stroke-width", 2);
					this.vehicles[i].vehShape = this.r.circle(3,3,2).attr("fill", this.genHex());
				}
				var pos = veh[i].split(":");
				var point = new Object();
				point.x = (Math.abs(this.curTrack.MinX) + parseFloat(pos[0])) / (Math.abs(this.curTrack.MinX) + this.curTrack.MaxX) * this.curTrack.bgWidth;
				point.y = (Math.abs(this.curTrack.MinY) + parseFloat(pos[1])) / (Math.abs(this.curTrack.MinY) + this.curTrack.MaxY);
				point.y = (1.0 - point.y) * this.curTrack.bgHeight;
				
				this.vehicles[i].tailPoints.push(point);
				if(this.vehicles[i].tailPoints.length > 4)
					this.vehicles[i].tailPoints.splice(0, 1);
				if(this.vehicles[i].tailPoints.length == 4) 
				{
					var tailpath = "M"+this.vehicles[i].tailPoints[3].x.toFixed(0) + " " + this.vehicles[i].tailPoints[3].y.toFixed(0);
					tailpath += "L"+ this.vehicles[i].tailPoints[2].x.toFixed(0) + " " + this.vehicles[i].tailPoints[2].y.toFixed(0);
					tailpath += "L"+ this.vehicles[i].tailPoints[1].x.toFixed(0) + " " + this.vehicles[i].tailPoints[1].y.toFixed(0);
					tailpath += "L"+ this.vehicles[i].tailPoints[0].x.toFixed(0) + " " + this.vehicles[i].tailPoints[0].y.toFixed(0);
					//this.vehicles[i].tailPath.attr("path", tailpath);
					this.vehicles[i].tailPath.animate({path:tailpath}, 500);
				}
				this.vehicles[i].vehShape.animate({cx:point.x, cy:point.y}, 500);
			}
			this.posrow++;
		}
	}

	this.setUpTracks = function()
	{
		//Mission Bay
		this.MissionBay.MinX = -800.5314;
		this.MissionBay.MaxX = 1608.2965;
		this.MissionBay.MinY = -828.4051;
		this.MissionBay.MaxY = 775.2752;
		this.MissionBay.imageUrl = "MissionBayBG.png";
		this.MissionBay.bgWidth = 640;
		this.MissionBay.bgHeight = 424;
		//Detroit
		this.Detroit.MinX = -1282.186;
		this.Detroit.MaxX = 1172.8;
		this.Detroit.MinY = -247.1333;
		this.Detroit.MaxY = 1052.3168;
		this.Detroit.bgWidth = 810;
		this.Detroit.bgHeight = 424;
		this.Detroit.imageUrl = "DetroitBG.png";
		//Madison
		this.Madison.MinX = -1151.6295;
		this.Madison.MaxX = 1150.0076;
		this.Madison.MinY = -602.6304;
		this.Madison.MaxY = 618.4437;
		this.Madison.bgWidth = 810;
		this.Madison.bgHeight = 424;
		this.Madison.imageUrl = "MadisonBG.png";
	}

	this.genHex = function(){
		colors = new Array(14);
		colors[0]="0";
		colors[1]="1";
		colors[2]="2";
		colors[3]="3";
		colors[4]="4";
		colors[5]="5";
		colors[5]="6";
		colors[6]="7";
		colors[7]="8";
		colors[8]="9";
		colors[9]="a";
		colors[10]="b";
		colors[11]="c";
		colors[12]="d";
		colors[13]="e";
		colors[14]="f";

		digit = new Array(5);
		color="#";
		for (i=0;i<6;i++){
			digit[i]=colors[Math.round(Math.random()*14)];
			color = color+digit[i];
		}
		return color;
	}
}
