// define global variables
var arItems = new Array();

// setup variable arrays for populations
for (var i=0; i<arCategory.length; i++) {
	category = arCategory[i][0];
	arItems[i] = new Array();
	fnDataExtract(i);
}





// use the Google Maps API to parse my locally stored KML files
function fnDataExtract(categoryID) {

	// determine the local files URL
	var URLcategory = "data/"+category+".xml";
	GDownloadUrl(URLcategory, function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("Placemark");

		// loop every item and grab name and split description into an array of separate lines
		for (var i=0; i<markers.length; i++) {
			var name = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
			var description = markers[i].getElementsByTagName("description")[0].firstChild.nodeValue;

			// grab coordinates and split them into latitude and longitude
			var coordinates = markers[i].getElementsByTagName("coordinates")[0].firstChild.nodeValue;
			var arCoordinates = coordinates.split(",");
			var latitude = arCoordinates[1];
			var longitude = arCoordinates[0];

			// write all data into a single multi-dimensional array
			arItems[categoryID][i] = new Array();
			arItems[categoryID][i][0] = name;
			arItems[categoryID][i]["latitude"] = latitude;
			arItems[categoryID][i]["longitude"] = longitude;

			// breakdown the description into tagged content stripped of <br> tags
			var arVideo = description.split("[video] ");
			if (arVideo[1]) {arItems[categoryID][i]["video"] = arVideo[1].split("<br>")[0];}

			var arId = arVideo[0].split("[id] ");
			if (arId[1]) {arItems[categoryID][i]["id"] = arId[1].split("<br>")[0];}

			var arCuisine = arId[0].split("[cuisine] ");
			if (arCuisine[1]) {arItems[categoryID][i]["cuisine"] = arCuisine[1].split("<br>")[0];}

			var arWeb = arId[0].split("[web] ");
			if (arWeb[1]) {arItems[categoryID][i]["web"] = arWeb[1].split("<br>")[0];}

			var arTelephone = arCuisine[0].split("[tel] ");
			if (arTelephone[1]) {arItems[categoryID][i]["telephone"] = arTelephone[1].split("<br>")[0];}

			// chunk address into sepearate lines
			arItems[categoryID][i]["arDescription"] = arTelephone[0].split("<br>");
			
			// cut trailing empty entries off the array
			if (arItems[categoryID][i]["arDescription"][eval(arItems[categoryID][i]["arDescription"].length-1)] == "") {
				arItems[categoryID][i]["arDescription"].splice(arItems[categoryID][i]["arDescription"].length-1, 1)
			}
		}
	});
}

