// Areas by Id
var countdownA = document.getElementById('refreshCount');
var standingsA = document.getElementById('standings');
var roadA = document.getElementById('road');
var weatherTrackA = document.getElementById('weatherTrack');
var highlightsA = document.getElementById('events');
var topviewA = document.getElementById('topview');

// Global variables
var standingsData;
var trackData;
var weatherData;
var gearboxStatusArray	= new Array("OK","Malfunctioning","Terminal failure");
var carStatusArray		= new Array("OK","Problem reported","Retired");
var engineStatusArray	= new Array("OK","Malfunctioning","Terminal failure");
var tyresStatusArray	= new Array("OK","Wearing out","Blown tyre");

var countValue = 1;
var basexmlfile = '../get_live_xml.php?';
var xmlfile	= basexmlfile;
var minute = 1;
var league_id = 1;
var focusCarId =0;
var highlightedRowObj;
var isIE = false;
var browserStyleClass = "FF";
var livechat;
function start(){
	//initRain();
	//makeRain();
	//raindrop();
	
	if (window.ActiveXObject) {
        isIE = true;
		browserStyleClass = "IE";
	}
	// initializing
	countdownA 	= document.getElementById('refreshCount');
	standingsA = document.getElementById('standings');
	roadA = document.getElementById('road');
	weatherTrackA = document.getElementById('weatherTrack');
	
	highlightsA = document.getElementById('events');
	highlightsA.className = browserStyleClass;
	
	pitstandA = document.getElementById('pitstand');
	pitstandA.className = pitstandA.className+" pitstand"+browserStyleClass;
	pitstandlA = document.getElementById('pitstandl');
	pitstandlA.className = pitstandlA.className+" pitstandl"+browserStyleClass;
	pitstandrA = document.getElementById('pitstandr');
	pitstandrA.className = pitstandrA.className+" pitstandr"+browserStyleClass;
	
	topviewA = document.getElementById('topview');
	
	//new Draggable('chatContainer');

	countdown();
}

function countdown()
{
	countValue = countValue-1;
	if (countValue > 0  ) 	{ refreshDisplay = 'Refresh in ' + countValue + ' seconds';		}
	if (countValue == 0 ) 	{ refreshDisplay = 'Refreshing'; 								}
	if (countValue < 0 ) 	{ refreshDisplay = refreshDisplay + '..'; countValue = 60; refreshAll();}
//	if (countValue < -9 ) 	{ refreshDisplay = "Page was unable to auto-refresh. Please try to access page manually.";}
	countdownA.innerHTML = refreshDisplay;
	var counter = setTimeout("countdown()", 1000)
/*	if(positionsData.length>0){updatePositions();}*/
}

function refreshAll()
{
	getData();
}

function getData()
{
	minute = parseInt(minute)+1;
	if(minute>=0 && minute <=120){
	form_url();
	var url = xmlfile;
//	alert(url);
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
	}
}

// handle onreadystatechange event of req object
function processReqChange()
{
//	alert("process req change, \n ready state="+req.readyState+", status="+req.status);
	// only if req shows "completed"
	if (req.readyState == 4)
	{
		// only if "OK"
		if (req.status == 200)
		{
			//	alert(req.getAllResponseHeaders());
			minuteData		= req.responseXML.getElementsByTagName("minute");
			minute			= getElementTextNS("", "minute", req.responseXML, 0);
			standingsData	= req.responseXML.getElementsByTagName("titm"); 
			trackData 		= req.responseXML.getElementsByTagName("track");
		    weatherData		= req.responseXML.getElementsByTagName("weather");
		    highlightsData 	= req.responseXML.getElementsByTagName("hitm");

/*			race_start_timeData = req.responseXML.getElementsByTagName("race_start_time");
			race_start_time	= getElementTextNS("", "race_start_time", req.responseXML, 0)
		    trackData 		= req.responseXML.getElementsByTagName("track");
		    weatherData		= req.responseXML.getElementsByTagName("weather");
		    positionsData	= req.responseXML.getElementsByTagName("titm");//positions_item
		    timingData 		= req.responseXML.getElementsByTagName("titm");*/

			if(trackData)
			{
				refreshTrack();
				refreshStandings();
				refreshHighlights();
				refreshTopView();

				focusCar(focusCarId) ;

/*				refreshTrack();
				refreshWeather();
				refreshPositions();
				refreshTiming();*/
			}
		}
		else
		{
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
			return false;
		}
	}
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;
        }
    } else {
        return "n/a";
    }

}


function form_url()
{
	xmlfile = basexmlfile;
	if(minute>0)
	{
		xmlfile = xmlfile+'&minute='+minute;
	}
	if(league_id>0)
	{
		xmlfile = xmlfile+'&league_id='+league_id;
	}
}
function prev(jump)
{
	minute = parseInt(minute)-jump-1;
	form_url();
	countValue=1;
}

function next(jump)
{
	minute = parseInt(minute)+jump-1;
	form_url();
	countValue=1;
}
function pickLeague(leagueId)
{
	league_id=leagueId;
	form_url();
	countValue=1;
	focusCarId=0;
}

function secs2time(seconds)
{
	if(seconds>0)
	{
		var seconds_10 		= Math.floor((seconds/10	)%10);
		var seconds_1 		= Math.floor((seconds		)%10);
		var seconds_dot1 	= Math.floor((seconds*10	)%10);
		var seconds_dot10 	= Math.floor((seconds*100	)%10);
		var seconds_dot100 	= Math.floor((seconds*1000	)%10);

		if(seconds>=60)
		{
			return seconds_10+''+seconds_1+'.'+seconds_dot1+''+seconds_dot10+''+seconds_dot100 ;
		}
		else if(seconds>=10)
		{
			return seconds_10+''+seconds_1+'.'+seconds_dot1+''+seconds_dot10+''+seconds_dot100 ;
		}
		else
		{
			return seconds_1+'.'+seconds_dot1+''+seconds_dot10+''+seconds_dot100 ;
		}
	}
	return '0.000';
}

function highlightRow(obj)
{
	if(highlightedRowObj)
	{
		highlightedRowObj.className = "normal";
	}
	highlightedRowObj = obj;
	obj.className = "selected";
}

function focusCar(carId)
{
	var topOffsetArray = Array();
	focusCarId = carId;
	var roadOutput="";
	var gapsOutput="";

	// finding focus car data
	for (var i = 0; i < standingsData.length; i++)
	{
		if(getElementTextNS("", "id", standingsData[i], 0)==carId)
		{
			var focusPercentage = getElementTextNS("", "prct", standingsData[i], 0);
			topOffsetArray[i] = 0;
			break;
		}
	}

	// putting everything into an array
	for (var i = 0; i < standingsData.length; i++)
	{
		topOffsetArray[i] = (focusPercentage-parseFloat(getElementTextNS("", "prct", standingsData[i], 0)))*100;
	}

	var output1 = "";
	for (var i = 0; i < standingsData.length; i++)
	{
		output1 += i+": "+topOffsetArray[i]+"\n";
	}

	// adjusting the array
	for (var i = 0; i < topOffsetArray.length; i++)
	{
		carHeight=100;
		if(minute==0)
		{
			carHeight = 100;
		}
		for(var j = 0; j<topOffsetArray.length; j++)
		{
			if(topOffsetArray[j]>0 && topOffsetArray[i]>=0 && topOffsetArray[j]>topOffsetArray[i] && i!=j)
			{
				topOffsetArray[j] = topOffsetArray[j]+carHeight;
			}
			if(topOffsetArray[j]<0 && topOffsetArray[i]<=0 && topOffsetArray[j]<topOffsetArray[i] && i!=j)
			{
				topOffsetArray[j] = topOffsetArray[j]-carHeight;
			}
		}
	}

	var output2 = "";
	for (var i = 0; i < standingsData.length; i++)
	{
		output2 += i+": "+topOffsetArray[i]+"\n";
	}
//	alert(focusPercentage+"\n\n"+output1+"\n"+output2);

	// outputting data
	for (var i = 0; i < standingsData.length; i++)
	{
		var leftOffset = 75;
//		if(topOffsetArray[i]<400 && topOffsetArray[i]>-400)
//		{
			//get gaps
			if(i==0)
			{
				var leader_done_laps = getElementTextNS("", "laps", standingsData[i], 0);
				if(leader_done_laps<1){	var gap = '';}
				else if(leader_done_laps==1){	var gap = leader_done_laps+' lap';}
				else{var gap = leader_done_laps+' laps';}
				var frontGap = "("+gap+")";
				var last_laptime = getElementTextNS("", "ltm", standingsData[i], 0);
			}
			else{
				if(getElementTextNS("", "rtd", standingsData[i], 0) == 'TRUE')
				{
					var gap = getElementTextNS("", "rtdR", standingsData[i], 0);
					var frontGap = getElementTextNS("", "rtdR", standingsData[i], 0);
					var last_laptime = "Lap "+getElementTextNS("", "laps", standingsData[i], 0);
				}
				else{
					if(leader_done_laps > getElementTextNS("", "laps", standingsData[i], 0))
					{
						var gap = "("+getElementTextNS("", "gp", standingsData[i], 0)+')';
					}
					else
					{
						var gap = "+"+getElementTextNS("", "gp", standingsData[i], 0);
					}
					var frontGap = "(+"+secs2time( getElementTextNS("", "gpS", standingsData[i], 0)-getElementTextNS("", "gpS", standingsData[i-1], 0) )+")";
					var last_laptime = getElementTextNS("", "ltm", standingsData[i], 0);
				}
			}
			
			//position cars
			//is retired?
			if(getElementTextNS("", "rtd", standingsData[i], 0)=='TRUE')
			{
				var leftOffset = 195;
			}
			
			text_align = 'left';
			text_left = 0;
			// start
			if(minute<=0)
			{
				topOffset = 300+topOffsetArray[i]-85*i;
				topOffset1= topOffset;
				if(i%2 == 1)
				{
					topOffset1-=0;
					leftOffset = 140;
					//text_align = 'right';
					//text_left = -160;
				}
				roadOutput += "<div class=\"car\" style=\"top:"+topOffset1+"px; left:"+leftOffset+"px;\"><div class=\"car_image\"><img src=\"../images/cars/liverace/"+getElementTextNS("", "lvr", standingsData[i], 0)+"_r.png\" width=\"35\" height=\"88\" alt=\""+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\" title=\""+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\"/></div><div class=\"car_text\" style=\"top:85px; text-align:"+text_align+"; left:"+text_left+"px;\">"+(i+1)+". "+getElementTextNS("", "nm", standingsData[i], 0).substring(0,1)+"."+getElementTextNS("", "srnm", standingsData[i], 0)+"<br>"+last_laptime+" "+frontGap+"</div></div>";
			}
			else{ //race
				topOffset = 300+topOffsetArray[i];
				roadOutput += "<div class=\"car\" style=\"top:"+topOffset+"px; left:"+leftOffset+"px;\"><div class=\"car_image\"><img src=\"../images/cars/liverace/"+getElementTextNS("", "lvr", standingsData[i], 0)+"_r.png\" width=\"35\" height=\"88\" alt=\""+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\" title=\""+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\"/></div><div class=\"car_text\" style=\"top:85px; text-align:"+text_align+"; left:"+text_left+"px;\">"+(i+1)+". "+getElementTextNS("", "nm", standingsData[i], 0).substring(0,1)+"."+getElementTextNS("", "srnm", standingsData[i], 0)+"<br>"+last_laptime+" "+frontGap+"</div></div>";
			}

			//old gaps part
			if(topOffset==400){
				gapsOutput += "<div class=\"box\" style=\"top:0px; left:"+leftOffset+"px;\"><strong>"+getElementTextNS("", "pos", standingsData[i], 0)+". "+getElementTextNS("", "nm", standingsData[i], 0).substring(0,1)+"."+getElementTextNS("", "srnm", standingsData[i], 0)+"<br/>"+gap+"</strong></div>";
			}
			else{
				gapsOutput += "<div class=\"box\" style=\"top:0px; left:"+leftOffset+"px;\">"+getElementTextNS("", "pos", standingsData[i], 0)+". "+getElementTextNS("", "nm", standingsData[i], 0).substring(0,1)+"."+getElementTextNS("", "srnm", standingsData[i], 0)+"<br/>"+gap+"</div>";
			}
//			gapsOutput += "<div class=\"gap\">&laquo; +"+gap+" &raquo;</div>";
//		}
	}
	if(minute==0)
	{
		//roadOutput = "<img src=\"../images/back/pit_lane.jpg\" alt=\"\" title=\"\" style=\"position:absolute;top:'"+(topOffsetArray[0])+"px;left:0px\">"+roadOutput;
	}
	roadA.innerHTML = roadOutput;
}

function refreshStandings()
{
	
	var standingsOutput = 	""+
		"<table>"+
			"<thead>"+
				"<tr>"+
					"<th class=\"center\" colspan=\"2\">Pos</th>"+
					"<th class=\"left\">Manager</th>"+
					"<th>L.</th>"+
					"<th class=\"right\">Gap</th>"+
					"<th class=\"right\">Front</th>"+
					"<th class=\"right\">Lap Time</th>"+
					"<th class=\"right\">Fastest</th>"+
					"<th>P.</th>"+
					"<th>Status</th>"+
				"</tr>"+
			"</thead>"+
			"<tbody>";
	for (var i = 0; i < standingsData.length; i++)
	{
		var is_retired = getElementTextNS("", "rtd", standingsData[i], 0);
		var gap_laps =  getElementTextNS("", "gpL", standingsData[i], 0);
		if(i==0)
		{
			if(focusCarId ==0){focusCarId=getElementTextNS("", "id", standingsData[i], 0);}
			var leader_done_laps = getElementTextNS("", "laps", standingsData[i], 0);
			var gap = '';
			var frontGap = '';
			var last_laptime = getElementTextNS("", "ltm", standingsData[i], 0);
		}
		else
		{
			if(is_retired == 'TRUE')
			{
				var gap = getElementTextNS("", "rtdR", standingsData[i], 0);
				var frontGap = "";
				var last_laptime = "Lap "+getElementTextNS("", "laps", standingsData[i], 0);
			}
			else{
				if(leader_done_laps > getElementTextNS("", "laps", standingsData[i], 0))
				{
					var gap = '('+getElementTextNS("", "gp", standingsData[i], 0)+')';
				}
				else
				{
					var gap = getElementTextNS("", "gp", standingsData[i], 0);
				}
				var frontGap = secs2time( getElementTextNS("", "gpS", standingsData[i], 0)-getElementTextNS("", "gpS", standingsData[i-1], 0) );
				var last_laptime = getElementTextNS("", "ltm", standingsData[i], 0);
			}
		}

		//var position_change = getElementTextNS("", "ppos", standingsData[i], 0)-getElementTextNS("", "pos", standingsData[i], 0);
		var position_change = 0;
		var position_change_with_image = "<img src=\"../images/icons/position_no_change.gif\" width=\"7\" height=\"7\"/>";
		if(is_retired == 'TRUE'){position_change=0;}
		//if(position_change>0 && getElementTextNS("", "ppos", standingsData[i], 0)>0){	position_change_with_image = "<img src=\"../images/icons/position_plus.gif\" width=\"7\" height=\"7\" alt=\""+Math.abs(position_change)+"\" title=\""+Math.abs(position_change)+"\"/>";	}
		//if(position_change<0 && getElementTextNS("", "ppos", standingsData[i], 0)>0){	position_change_with_image = "<img src=\"../images/icons/position_minus.gif\" width=\"7\" height=\"7\" alt=\""+Math.abs(position_change)+"\" title=\""+Math.abs(position_change)+"\"/>";	}

		var isFastestLap = "";
		if(last_laptime==getElementTextNS("", "fLap", standingsData[i], 0)){isFastestLap=" fastest";}else{isFastestLap="";}
		if(getElementTextNS("", "sB", standingsData[i], 0)==1)
		{
			position_change_with_image += "<img src=\"../images/live/position_block.gif\" height=\"7\" width=\"7\" alt=\"Block\" title=\"Block\">";
		}
		
		standingsOutput += ""+
		"<tr class=\"normal\" onmousedown=\"javascript:highlightRow(this); focusCar("+getElementTextNS("", "id", standingsData[i], 0)+")\");\">"+
			"<td class=\"row"+((i%2)+1)+" right nopadding "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">"+ getElementTextNS("", "pos", standingsData[i], 0) + "</td>"+
			"<td class=\"row"+((i%2)+1)+" left nopadding "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">"+position_change_with_image + "</td>"+
			"<td class=\"row"+((i%2)+1)+" "+( ((i%2)+1)==1?browserStyleClass:"" )+"\" nowrap=\"nowrap\"><div class=\"shortname\"><img src=\"../images/cars/icons/"+ getElementTextNS("", "lvr", standingsData[i], 0) +".gif\" height=\"10\" width=\"10\" alt="+ getElementTextNS("", "lvr", standingsData[i], 0) +" title="+ getElementTextNS("", "lvr", standingsData[i], 0) +"> "+ getElementTextNS("", "nm", standingsData[i], 0).substr(0,1) + "." + getElementTextNS("", "srnm", standingsData[i], 0) + "</td>"+
			"<td class=\"row"+((i%2)+1)+" center "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + getElementTextNS("", "laps", standingsData[i], 0) + "</td>"+
			"<td class=\"row"+((i%2)+1)+" right "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + gap + "</td>"+
			"<td class=\"row"+((i%2)+1)+" right "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + frontGap + "</td>"+
			"<td class=\"row"+((i%2)+1)+" right "+( ((i%2)+1)==1?browserStyleClass:"" )+""+isFastestLap+"\">" + last_laptime + "</td>"+
			"<td class=\"row"+((i%2)+1)+" right "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + getElementTextNS("", "fLap", standingsData[i], 0) + "</td>"+
			"<td class=\"row"+((i%2)+1)+" center "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + getElementTextNS("", "pits", standingsData[i], 0) + "</td>"+
			"<td class=\"row"+((i%2)+1)+" center "+( ((i%2)+1)==1?browserStyleClass:"" )+"\">" + 
			"<img src=\"../images/live/status_car_"+ getElementTextNS("", "sC", standingsData[i], 0) +".png\" height=\"13\" width=\"11\" alt=\"Car status: "+ carStatusArray[getElementTextNS("", "sC", standingsData[i], 0)] +"\" title=\"Car status: "+ carStatusArray[getElementTextNS("", "sC", standingsData[i], 0)] +"\">" +
			"<img src=\"../images/live/status_engine_"+ getElementTextNS("", "sE", standingsData[i], 0) +".png\" height=\"13\" width=\"11\" alt=\"Engine status: "+ engineStatusArray[getElementTextNS("", "sE", standingsData[i], 0)] +"\" title=\"Engine status: "+ engineStatusArray[getElementTextNS("", "sE", standingsData[i], 0)] +"\">" +
			"<img src=\"../images/live/status_gears	_"+ getElementTextNS("", "sG", standingsData[i], 0) +".png\" height=\"13\" width=\"11\" alt=\"Gearbox status: "+ gearboxStatusArray[getElementTextNS("", "sG", standingsData[i], 0)] +"\" title=\"Gearbox status: "+ gearboxStatusArray[getElementTextNS("", "sG", standingsData[i], 0)] +"\">" +
			"<img src=\"../images/live/status_tyre_"+ getElementTextNS("", "sT", standingsData[i], 0) +".png\" height=\"13\" width=\"11\" alt=\"Tyres status: "+ tyresStatusArray[getElementTextNS("", "sT", standingsData[i], 0)] +"\" title=\"Tyres status: "+ tyresStatusArray[getElementTextNS("", "sT", standingsData[i], 0)] +"\">" +
			"</td>"+
		"</tr>";

	}

	standingsOutput +=	"</tbody></table>";
	standingsA.innerHTML = standingsOutput;
}

function refreshTrack()
{
	var weatherTrackOutput = '';
	
	weatherTrackOutput += ""+
		"<div id=\"item\"><img src=\"../images/weather/" + getElementTextNS("", "sky", weatherData[0], 0) +"_ssmall.png\" width=\"32\" height=\"32\" alt=\"weather\" title=\"weather\" /></div>"+
		"<div id=\"item\"><strong>Temp</strong>: " + getElementTextNS("", "temperature", weatherData[0], 0) +"&deg;C<br/><strong>Rain</strong>: " + getElementTextNS("", "wetness", weatherData[0], 0) +" mm</div>"+
		"<div id=\"item\"><strong>Pit lane</strong>: "+ getElementTextNS("", "pit_lane_length", trackData[0], 0) +" m<br/><strong>Fastest</strong>: "+ getElementTextNS("", "fastest_lap_time", trackData[0], 0) +"</div>"+
		"<div id=\"item\"><strong>Laps</strong>: "+getElementTextNS("", "laps_number", trackData[0], 0)+"<br/><strong>Lap</strong>: "+ getElementTextNS("", "lap_length", trackData[0], 0) +" km.</div>";
//	alert(weatherTrackOutput);
	weatherTrackA.innerHTML = weatherTrackOutput;
}

function refreshHighlights()
{
	highlightsOutput = '';
	for (var i = 0; i < highlightsData.length; i++)
	{
		var manager_name = getElementTextNS("", "name", highlightsData[i], 0);
		var manager_surname = getElementTextNS("", "srnm", highlightsData[i], 0);
		var text = getElementTextNS("", "txt", highlightsData[i], 0);
        highlightsOutput += "<span class=\"nick\">" + manager_name.substring(0,1) +"."+ manager_surname +"</span> "+ text +"<br/>\n";
    }

	highlightsOutput += "";
	highlightsA.innerHTML = highlightsOutput;
}

function refreshTopView()
{
	var topviewOutput = "";
	var distanceSum = 0;
	trackArray = allTracksArray[getElementTextNS("", "track_id", trackData[0], 0)];
	for(var i = 1; i < trackArray.length; i++)
	{
		distanceSum += Math.sqrt(Math.pow(trackArray[i][0]-trackArray[i-1][0],2)+Math.pow(trackArray[i][1]-trackArray[i-1][1],2));
		trackArray[i][2] = distanceSum;
	}
	for(var i = 0; i < trackArray.length; i++)
	{
		trackArray[i][3] = trackArray[i][2]/distanceSum*100;
	}
	for (var i = standingsData.length-1; i >= 0; i--)
	{
		var is_retired = getElementTextNS("", "prct", standingsData[i], 0);
		topviewOutput += "";
		for(var j = 1; j < trackArray.length; j++)
		{
			if( trackArray[j][3] > getElementTextNS("", "prct", standingsData[i], 0) && trackArray[j-1][3] < getElementTextNS("", "prct", standingsData[i], 0) )
			{
				var localPercentage = ( getElementTextNS("", "prct", standingsData[i], 0)-trackArray[j-1][3] ) / (trackArray[j][3]-trackArray[j-1][3]);
				topviewOutput += "<img src=\"../images/cars/icons/"+ getElementTextNS("", "lvr", standingsData[i], 0) + ".gif\" alt=\""+getElementTextNS("", "pos", standingsData[i], 0)+". "+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\" title=\""+getElementTextNS("", "pos", standingsData[i], 0)+". "+getElementTextNS("", "nm", standingsData[i], 0)+" "+getElementTextNS("", "srnm", standingsData[i], 0)+"\" style=\"position:absolute;top:"+(trackArray[j-1][1]+(trackArray[j][1]-trackArray[j-1][1])*localPercentage-5)+"px;left:"+(trackArray[j-1][0]+(trackArray[j][0]-trackArray[j-1][0])*localPercentage-5)+"px\">";
			}
		}
	}
	
	topviewA.style.background= "url(../images/tracks/live/"+getElementTextNS("", "track_id", trackData[0], 0)+".jpg)";
	topviewA.innerHTML = topviewOutput;
}
