<!--
/* Map Builder, (c) Edd Read, 2007
	
   Map Builder API Functions
	
   Built as part of a final-year project supporting the degree of
   Master Of Engineering.
   
   All code is by Edd Read, apart from where specified otherwise.
   
*/


/* Performs a geocoding on a place name and runs lat,lng on a function */
function geoCode(placeName,func) {
	
	if (window.XMLHttpRequest) {
		httpGetObj = new XMLHttpRequest();
	} else {
		httpGetObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	httpGetObj.open("GET",'http://gis.eddread.co.uk/QuickSearch/?q='+escape(placeName),true);
	
	var latLng = null;
	
	httpGetObj.onreadystatechange = function() {
		if (httpGetObj.readyState == 4) {
			return func(httpGetObj.responseText);
		}
	};

	httpGetObj.send(null);
	
}

/* Pause execution */
function pause(ms) {
	var date = new Date();
	var curDate = null;
	do { 
		curDate = new Date();
	} while(curDate-date < ms);
}



var map;
/* Loads the map - function should be called as part of the <body> onload event */
function loadMapBuilder(startC, startZ,mapType) {
	if (GBrowserIsCompatible()) {
	
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

		eval('startPoint = new GLatLng('+startC+')');
		map.setCenter(startPoint,startZ);
		if (mapType) map.setMapType(mapType);
		
	}
}




/* Adds a marker to the map */
function createMarker(point,infoHTML,icon) {
	/* Set up icon */
	if (icon == null) {
		thisIcon = new GIcon(G_DEFAULT_ICON);
	} else {
		thisIcon = icon;
	}
	
	var marker = new GMarker(point,thisIcon);			  		
	GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(infoHTML);
		});
	map.addOverlay(marker);
}	

/* Loads and parses an MBML feed */
function loadFeed(url,onBeforeLoad,onLoad,icon) {

	GDownloadUrl(url, function(data, responseCode) {
		if(responseCode == 200) {
			if (onBeforeLoad) eval(onBeforeLoad);
			var xml = GXml.parse(data);
			var points = xml.documentElement.childNodes;
			for (var i = 0; i < points.length; i++) {
				var lat = null;
				var lon = null;
				var title = null;
				var details = new Array();
				
				var point = points[i].childNodes;
				
				for (var j=0; j<point.length; j++) {
					
					if (point[j].nodeName=='LocationDetails') {
					
						/* We're in <LocationDetails>, try to get lat long */
						locDetails = point[j].childNodes;
						for (var k=0; k<locDetails.length; k++) {
							if (locDetails[k].nodeName=='coords') {
								lat = parseFloat(locDetails[k].getAttribute('lat'));
								lon = parseFloat(locDetails[k].getAttribute('long'));
							}
						}
					} 
					else if (point[j].nodeName=='title') {
						title = point[j].firstChild.nodeValue;
					}
					else if (point[j].nodeName=='icon') {
						iconStr = point[j].firstChild.nodeValue;
						try {
							eval('icon = '+iconStr+'');
						} catch(e) { /* do nothing */ }
					}
					else if (point[j].nodeName=='detail') {
						detail = new Array();
						if (point[j].getAttribute('name')) {
							detail['name'] = point[j].getAttribute('name');
							detail['value'] = (point[j].firstChild ? point[j].firstChild.nodeValue : '');
							details.push(detail);
						}
					}
					
				}
				if (lat!=null) {

					/* Construct HTML */
					var MarkerHTML = '<div class="infoBox">';
					if (title!=null) MarkerHTML+='<div class="title">'+title+'</div>';
					/* details */
					for (var j=0; j<details.length; j++) {
						MarkerHTML += '<div class="detail">'+details[j]['name']+':&nbsp;<span>'+details[j]['value']+'</span></div>';
					}
					MarkerHTML+='</div>';
					
					createMarker(new GLatLng(parseFloat(lat),parseFloat(lon)),MarkerHTML,icon);
				}
			}
			
			
		} else if(responseCode == -1) {
			alert("We couldn't download or parse your XML file. Please check it is retrievable!");
		} else { 
			alert("We couldn't download or parse your XML file. Please check it is retrievable!");
		}
		
		if (onLoad) eval(onLoad);
	
	});

}


/* Allows map to be centred on a given named location */
function findAndCentre(place) {
	
	if (place!='') {
		geoCodeURL = 'http://gis.eddread.co.uk/QuickSearch/?q='+place;
		GDownloadUrl(geoCodeURL, function(data, responseCode) {
										  
			if(responseCode == 200) {
				var lat = null;
				var lon = null;
				if (data!=',') {
					data=data.split(',');
					lat = data[0];
					lon = data[1];
					map.setCenter(new GLatLng(parseFloat(lat),parseFloat(lon)),map.getZoom());
					return true;
				}
			}
			
		});
		
	}
	
	return false;
	
}


/* Detects an 'enter' keypress within the text-input fields */
function detectEnter(e,action) {
	var characterCode;
	if (e && e.which) { 
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
	if (characterCode == 13)
		return true;
	else
		return false;
}




/* Browser Detect Script
   (c) eddie traversa etraversa@dhtmlnirvana.com
   http://dhtmlnirvana.com/ */
function Is() {
	agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.ns = ((agent.indexOf('mozilla') != -1) &&
		(agent.indexOf('spoofer') == -1) &&
		(agent.indexOf('compatible') == -1) &&
		(agent.indexOf('opera') == -1) &&
		(agent.indexOf('webtv') == -1) &&
		(agent.indexOf('hotjava') == -1));
	this.ns2 = (this.ns && (this.major == 2));
	this.ns3 = (this.ns && (this.major == 3));
	this.ns4 = (this.ns && (this.major == 4));
	this.ns6 = (this.ns && (this.major >= 5));
	this.ie = ((agent.indexOf("msie") != -1) &&
		(agent.indexOf("opera") == -1));
	this.ie3 = (this.ie && (this.major < 4));
	this.ie4 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 4") != -1));
	this.ie5 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 5.") != -1) &&
		(agent.indexOf("msie 5.5") == -1) &&
		(agent.indexOf("mac") == -1));
	this.iem5 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 5.") != -1) &&
		(agent.indexOf("mac") != -1));
	this.ie55 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 5.5") != -1));
	this.ie6 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 6.") != -1));
	this.ie7 = (this.ie && (this.major == 4) &&
		(agent.indexOf("msie 7.0") != -1));
	this.nsdom = (this.ns4 || this.ns6);
	this.ie5dom = (this.ie5 || this.iem5 || this.ie55);
	this.iedom = (this.ie4 || this.ie5dom || this.ie6);
	this.w3dom = (this.ns6 || this.ie6 ||this.ie7);
}
var is = new Is();




//-->
