function toggleTravelTimes(){

	if(document.legendForm.checkbox_tt.checked == false)
	{	
		removeGetAwayPolyLine();
	}
	else
	{	
		drawGetAwayPolyLine();
		refreshGetAwayPolyLine();
	}
}

function removeGetAwayPolyLine()
{

	//alert("remove");

	for (var i = 0; i < markerGroups["traveltimes"].length; i++) {
       		
       		var polyline = new GPolyline;


       		var polyline = markerGroups["traveltimes"][i];
        	if (polyline.isHidden()) 
        	{
          		//polyline.show();
        	} 
        	else 
        	{
          		polyline.hide();
          		//polyline.remove();
        	}
        }

}

function refreshGetAwayPolyLine()
{

	//alert("refresh");
	
	var bounds1 = new GLatLngBounds();
	var bounds2 = new GLatLngBounds();
	

	for (var i = 0; i < markerGroups["traveltimes"].length; i++) {
       		
       		var polyline = new GPolyline;
       		var polyline = markerGroups["traveltimes"][i];

		bounds1 = map.getBounds();
		bounds2 = polyline.getBounds();		


          	if(bounds1.intersects(bounds2))
          	{
          		polyline.show();
          	}
          	else
          	{
          		polyline.hide();
		}
        }


}

function drawGetAwayPolyLine()
{

	if(!travelTimesDrawn)
	{
				
				//alert("draw");
				randomNum = Math.floor(Math.random()*10000000);

				var IncdientsUrl = "./data/Polytraveltimes.xml?" + randomNum.toString();




					//This gets the request from
					var request = GXmlHttp.create();
					request.open("GET", IncdientsUrl, true);


				      request.onreadystatechange = function() {


							if (request.readyState == 4) {
								var xmlDoc = request.responseXML;

								// obtain the array of markers and loop through it
								var markers = xmlDoc.documentElement.getElementsByTagName("Points");



								for (var i = 0; i < markers.length; i++) {

											var url = markers[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue;
											var polyencode = markers[i].getElementsByTagName("POLYENCODE")[0].childNodes[0].nodeValue;
											var levels = markers[i].getElementsByTagName("LEVELS")[0].childNodes[0].nodeValue;

											var polyline = new GPolyline;

											var polyline = returnPolyline(url,"#000000",12,polyencode,levels,32,0.0,4);



											markerGroups["traveltimes"].push(polyline);

											var bounds1 = new GLatLngBounds();
											bounds1 = map.getBounds();

											var bounds2 = new GLatLngBounds();
											bounds2 = polyline.getBounds();

											//if(bounds1.intersects(bounds2))
											//{
												map.addOverlay(polyline);
											//}




								}

								polyline = null;
								
								travelTimesDrawn = true;
								
							}
				      }


			      request.send(null);

		}
		else{
		

			refreshGetAwayPolyLine();
		
		}

}

function returnPolyline(url,color,weight,polyencode,levels,zoomfactor,opacity,numlevels)

{

	var polyline = new GPolyline.fromEncoded({
	color: color,
	weight: weight,
	points: polyencode,
	levels: levels,
	zoomFactor: zoomfactor,
	opacity: opacity,
	numLevels: numlevels
	});


	GEvent.addListener(polyline, "mouseover", function() {
	    	//mapDivElement.style.cursor = "pointer";
		document.getElementById("map-canvas").style.cursor = "pointer";

	});


	GEvent.addListener(polyline, "click", function(latlng)
	{
	var html = "<div  align=\"left\" class=\"bodytext\"><iframe src =\"" + url + "\" width=\"240\" height=\"200\" FRAMEBORDER=0></iframe></div>";
	map.openInfoWindowHtml(latlng, html);
	moveWanted = 0;
	WaitandTurnOffMoveWanted();
	});

	return polyline;

}

