/***************************************************************
This file supports the Dart Map Section
It is AJAX-a-riffic!
****************************************************************/

var xmlHttp;
var xmlHttp2;
var sURL;
var map;
var sLoc;
var fLat = 43.636763;
var fLng = -95.000606;
var fRadius = 233;
var iQty = 5;
var fInc = .1;
var iZoom = 5;

var bCMark = 1;


function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		GEvent.addListener(map, "moveend", function() {
			//var center = map.getCenter();
			//document.getElementById("message").innerHTML = center.toString();
		});
		
		map.setCenter(new GLatLng(fLat, fLng), iZoom);
	}
} //end load

function vPlotNow() {
	if (fRadius < 20) { iZoom = 8; }
	if (fRadius >= 20 && fRadius < 100) { iZoom = 7; }
	if (fRadius >= 100 && fRadius < 300) { iZoom = 5; }
	if (fRadius >= 300 && fRadius < 600) { iZoom = 4; }
	if (fRadius >= 600 && fRadius < 1200) { iZoom = 3; }
	if (fRadius >= 1200 && fRadius < 2000) { iZoom = 2; }
	if (fRadius >= 2000) { iZoom = 1; }
	
	if (fInc < .1) { 	iZoom = 11; }

	map.setCenter(new GLatLng(fLat, fLng), iZoom);
	
	reqBigList();
}

function reqBigList() {
	
	map.clearOverlays();
	bCMark = 1;
	
	// REQUEST BigList
	sURL = "dartmap.php?lat=" + fLat + "&lng=" + fLng + "&tdist=" + fRadius + "&qty=" + iQty + "&inc=" + fInc;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleBigListStateChange;
	xmlHttp.open("GET", sURL, true);
	xmlHttp.send(null);
}

function gotBigList() {
	var responseXML = xmlHttp.responseXML;
	var currNode = responseXML.getElementsByTagName("points").item(0);
	currNode = currNode.firstChild;
	processPoint(currNode);
	while (currNode = currNode.nextSibling) {
		processPoint(currNode);
	}
	
	vDisplayThink("Blank");
}

function processPoint(currNode) {
	sName = currNode.firstChild.firstChild.nodeValue;
	iLat = currNode.firstChild.nextSibling.firstChild.nodeValue;
	iLng = currNode.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
	sPlace = currNode.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
	
	var sLinkURL = "http://maps.google.com/maps?f=q&source=s_q&hl=en&q=" + escape(sLoc) + "+to+" + escape(sPlace);
	
	sHTML = "<div style=padding:10px><b>"  + sName + "</b><br /><br />" + iLat + ", " + iLng + "<br /><br /><b><a style=\"text-decoration:none\" target=\"_blank\" href = \"" + sLinkURL + "\">" + sPlace + "</a></b></div>";
	addMarker(iLat, iLng, sHTML);
} //end processPoint

function addMarker(iLat, iLng, sHTML) {
	var point = new GLatLng(iLat, iLng);
	map.addOverlay(createMarker(point, sHTML));
}

function createMarker(point, sHTML) {
	var myIcon = new GIcon();
	if (bCMark == 1) {
		myIcon.image = "http://www.very-appealing.com/misc/dartmap/markerRed.png";
		bCMark = 0;
	} else {
		myIcon.image = "http://www.very-appealing.com/misc/dartmap/dart.png";
	}

	myIcon.iconAnchor = new GPoint(10, 34);
	myIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	myIcon.infoWindowAnchor = new GPoint(10, 0);
	
	
	var marker = new GMarker(point, myIcon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(sHTML);
	});
	return marker;
}



function vFire() {
	
	sLoc = document.getElementById("start").value;
	iQty = document.getElementById("qty").value;
	fRadius = document.getElementById("radius").value;
	fInc = document.getElementById("inc").value;

	// VALIDATION
	if 	(bIsNumeric(fRadius) && fRadius > 1) {
		processAddr(sLoc);
	} else {
		vError("Radius must be a number greater than 1");
	}
	
	vDisplayThink("Thinking");
} //end vFire

function processAddr(sLoc) {
	sURL = "geocode.php?addr=" + escape(sLoc);
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleAddrValidation;
	xmlHttp.open("GET", sURL, true);
	xmlHttp.send(null);
} //end processAddr

function validateAddr() {
	var responseXML = xmlHttp.responseXML;
	var currNode = responseXML.getElementsByTagName("status").item(0);
	if (currNode.firstChild.nodeValue == "200") {
		fLat = currNode.nextSibling.firstChild.nodeValue;
		fLng = currNode.nextSibling.nextSibling.firstChild.nodeValue;
		sDesc = currNode.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
		vPlotNow();
	} else {
		vError("Invalid Starting Address!");	
	}
} //end validateAddr



function vError(sMsg) {
	alert("Error: " + sMsg);	
}


function bIsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}

function vDisplayThink(sIMG) {
	/* Clear out the div */
	while (document.getElementById("thinking").hasChildNodes())
	{
	  document.getElementById("thinking").removeChild(document.getElementById("thinking").firstChild);
	}
	
	var theIMG = document.createElement("img");
	theIMG.setAttribute("src", sIMG + ".gif");
	theIMG.setAttribute("alt", sIMG);
	
	document.getElementById("thinking").appendChild(theIMG);
}



/****************** AJAX ***********************/
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function handleBigListStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			gotBigList();
		}
		else {
			alert ("AJAX error!");
		}
	}
}

function handleAddrValidation() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			validateAddr();
		}
		else {
			alert ("AJAX error!");
		}
	}
}
