/*  
	This code should go in the main file, shown here as an example.
	
	var allPoints = new Array();
	var mapFile = "";
	var currentZoomLevel = 5;
	var bDrawnLine = false; // this is true if we've already drawn the less detailed line.
	// initialize the map


    map.addControl(new GLargeMapControl());
	 map.addControl(new GMapTypeControl());

	loadFromFile('http://www.amgentourofcalifornia.com/docroot/maps/stage1-small.xml', currentZoomLevel);
*/

	function drawOverlays() {
		for (x=0; x<allOverlays.length; x++) {
			map.addOverlay(allOverlays[x]);
		}
	}


	function pausecomp(millis) {
		date = new Date();
		var curDate = null;

		do {
		 var curDate = new Date(); 
		}
		while(curDate-date < millis);
	}



	function loadFromFile(fname, zoomLevel) {
	var request = GXmlHttp.create();
	request.open('GET', fname, true);
	request.onreadystatechange = function() {
	 if (request.readyState == 4) {
		allPoints = "";
		allPoints = new Array();			
		mapFile = fname;
		var xmlDoc = request.responseXML;
		var points = xmlDoc.documentElement.getElementsByTagName("point");
		for (var i = 0; i < points.length; i++) {
 				var point = new GPoint(parseFloat(points[i].getAttribute("lat")),
					   parseFloat(points[i].getAttribute("lng")));
			allPoints.push(point);
		}
		drawLine();
		currentZoomLevel = zoomLevel;
 	 }
	}
	request.send(null);

	}

	// function to redraw the line
	function drawLine() {
		// if we're using the small data set, we can afford to just draw the entire line without checking points for 
		// current viewing visibility, saving us time.
		if ((mapFile.indexOf('-') > 0) && (map.getZoomLevel() > 4)) {
		 if (!bDrawnLine) {
			// clear the map
			map.clearOverlays();
			drawOverlays();
			var line = new GPolyline(allPoints, "#0000ff", 3, 1);
			map.addOverlay(line);	
			bDrawnLine = true;		
		 }

		}
		else if ((mapFile.indexOf('-') < 0 ) && (map.getZoomLevel() <=4)) {
		// clear the map
		map.clearOverlays();
		drawOverlays();
		var points = new Array();
		var pString = map.getBoundsLatLng().toString().replace('Bounds(', '').replace(')','');	
		var pArray = pString.split(',');
		var lines = new Array();

		// if this is true we need to draw individual segments, instead of the whole line
		var bDrawSegments = false; 
		for (x = 1; x < allPoints.length; x++) {
			// we want to check to make sure this point is within the current viewing window, because if its not,
			//	we don't want to show it or draw a line. This way we can have huge lines without crashing the browser.
			//	However, if the zoom level is low enough, then the browser is smart enough to not draw all points, so we
			//	can skip this check.
			var zoomLevel = map.getZoomLevel();
			if (zoomLevel < 5) {
				// check to see if this point fits in the current window. 
				var point = allPoints[x];
				var ppoint = allPoints[x-1];
				// Boolean definitions, to make this easier to read. 
				var bPointWithinBounds = ( ( (point.x > pArray[0]) && (point.y > pArray[1]) ) && ( (point.x < pArray[2]) && (point.y < pArray[3]) ) );
				var bPPointWithinBounds = ( ( (ppoint.x > pArray[0]) && (ppoint.y > pArray[1]) ) && ( (ppoint.x < pArray[2]) && (ppoint.y < pArray[3]) ) );			 

				// condition one, both points are within the bounds, so draw it
				if (bPointWithinBounds && bPPointWithinBounds) {
					var ps = new Array();
					ps[0] = ppoint;
					ps[1] = point;
					var line = new GPolyline(ps, "#0000ff", 3, 1);
					lines.push(line);
					bDrawSegments = true;
					
				}
				// condition two, one of the two points is within bounds, so draw it
				else if (  bPointWithinBounds || bPPointWithinBounds) {
					var ps = new Array();
					ps[0] = ppoint;
					ps[1] = point;
					var line = new GPolyline(ps, "#0000ff", 3, 1);
					lines.push(line);
					bDrawSegments = true;
				}

			}
			else // if we're at zoom level 9 or above, then just draw all the points 
				points = allPoints;

		} 
		if (bDrawSegments) { // we've had at least one successful point pair, then draw the lines array
			for (x = 0; x < lines.length; x++) {
				map.addOverlay(lines[x]);
			}
		}
		else { // no successful point pair, or too far zoomed out, so just draw the entire point set
			var line = new GPolyline(points, "#0000ff", 3, 1);
			map.addOverlay(line);
		}
		bDrawnLine = false;
	 } // end optimize check
	}

	// populate points based on zoom level
	function handleZoom(oldZoomLevel, newZoomLevel) {
		// if we're currently using the detailed set of points, and just moved past zoom level 5, use the 
		// less detailed points
		if ((oldZoomLevel < 5) && (newZoomLevel > 4)) {
			//alert("switching to small dataset [" + oldZoomLevel + "->" + newZoomLevel + "]");
			var newFile = mapFile.substring(0, mapFile.lastIndexOf('.'));
			newFile = newFile + "-small.xml";
			loadFromFile(newFile, newZoomLevel);
		}
		// if we're currently using the smaller pointset, and just went detailed, use the detailed map
		if ((oldZoomLevel > 4) && (newZoomLevel < 5)) {
			//alert("switching to large dataset [" + oldZoomLevel + "->" + newZoomLevel + "]");
			var newFile = mapFile.substring(0, mapFile.lastIndexOf('-'));
			newFile = newFile + ".xml";
			loadFromFile(newFile, newZoomLevel);
		 }
	}