var debug = false;

//--- XML Framework -------------------------------

function loadXmlDoc(requestUrl, callbackFunction, args) {
    
    var uniqueUrl;
    
    if (requestUrl.indexOf("?") < 0) {
		uniqueUrl = requestUrl + "?unique=" + Math.ceil(Math.random() * 10000000000);
	}
	else {
		uniqueUrl = requestUrl + "&unique=" + Math.ceil(Math.random() * 10000000000);
	}
		
    
    var request = GXmlHttp.create();
    request.open("GET", uniqueUrl, true);
    
    request.onreadystatechange = function () {

			// If the request's status is "complete"
			if (request.readyState == 4) {
				
				if (request.status == 200) {

					// Pass the XML payload of the response to the
					// handler function
					callbackFunction(request.responseXML, args);

				} else {
					
					// An HTTP problem has occurred
					
					if (debug)
					    alert("HTTP error: " + request.status);
				}
			}
		}
    request.send(null);
}



// ---- CUSTOM OBJECTS ------------

function Label(point, text) {
	this.initialize = function (map) {
		var div = document.createElement("div");
		div.innerHTML = text;
		div.className = "map-label";
		map.getPane(G_MAP_MAP_PANE).appendChild(div);
		this.div = div;
		this.map = map;
	}
	
	this.remove = function () {
		this.div.parentNode.removeChild(this.div);
	}
	
	this.copy = function () {
		return new Label(point, text);
	}
	
	this.redraw = function (force) {
		if (force) {
			var c1 = this.map.fromLatLngToDivPixel(point);
			this.div.style.fontSize = (8 * Math.pow(1.2, this.map.getZoom())) + '%';
			this.div.style.left = (c1.x - this.div.clientWidth / 2) + 'px';
			this.div.style.top = (c1.y - this.div.clientHeight / 2) + 'px';
		}
	}
}

Label.prototype = new GOverlay();

//----- Drawing Functions -----------------------

var d2r = Math.PI / 180;   // degrees to radians
var r2d = 180 / Math.PI;   // radians to degrees
var earthsradius = 3963; // 3963 is the radius of the earth in miles

function drawCircle(lat, lng, radius)
{
   var points = 48;           // radius in miles

   // find the raidus in lat/lon
   var rlat = (radius / earthsradius) * r2d;
   var rlng = rlat / Math.cos(lat * d2r);

   var extp = new Array();
   for (var i=0; i < points+1; i++) // one extra here makes sure we connect the
   {
      var theta = Math.PI * (i / (points/2));
      ex = lng + (rlng * Math.cos(theta)); // center a + radius x * cos(theta)
      ey = lat + (rlat * Math.sin(theta)); // center b + radius y * sin(theta)
      extp.push(new GPoint(ex, ey));
   }

   map.addOverlay(new GPolyline(extp, "#ff0000", 2));
}

