//<![CDATA[

function travelog ()
{	if (GBrowserIsCompatible())
	{	
//********** GLOBAL VARIABLES **********
		var visitdata;
		var baseIcon; 
		var baseIconSmall;
		var visitPins = new Array();
		var placePins = new Array();
		var smallplacePins = new Array();
		var sitePins = new Array();

//********** FUNCTIONS **********
		//set up map
		function setMap (size, home)
		{	map = new GMap2(document.getElementById("map"));
			map.addControl (new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10))); 
			map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,35)));
			map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5,20)));
			map.addMapType(G_NORMAL_MAP);
			map.addMapType(G_SATELLITE_MAP);
			map.addMapType(G_HYBRID_MAP);
			map.addMapType(G_PHYSICAL_MAP);
			map.setCenter(home, size, G_NORMAL_MAP);
//			map.enableScrollWheelZoom();
		}
		
		//custom pins
		function setCustomPinIcons ()
		{	// base pin icon (white)
			baseIcon = new GIcon();
			baseIcon.image = "supportfiles/pins/pinWhite.png"; 
			baseIcon.shadow = "supportfiles/pins/pinShadow.png"; 
			baseIcon.iconSize = new GSize(18, 29); 
			baseIcon.shadowSize = new GSize(18, 29); 
			baseIcon.iconAnchor = new GPoint(7, 25); 
			baseIcon.infoWindowAnchor = new GPoint(7, 25); 
			// small base pin icon (white)
			baseIconSmall = new GIcon();
			baseIconSmall.image = "supportfiles/pins/pinWhiteSmall.png"; 
			baseIconSmall.shadow = "supportfiles/pins/pinShadowSmall.png"; 
			baseIconSmall.iconSize = new GSize(12, 19); 
			baseIconSmall.shadowSize = new GSize(12, 19); 
			baseIconSmall.iconAnchor = new GPoint(5, 16); 
			baseIconSmall.infoWindowAnchor = new GPoint(5, 16); 
		}
		
		//create a Visit instance and store in tovisit[]
		function storeVisits ()
		{	for (var t = 0; t < visitdata.length; t++)
			{	// extract data
				var when = visitdata[t].getAttribute("when");
				var ref = visitdata[t].getAttribute("ref");
				var colour = visitdata[t].getAttribute("colour");
				var places = storePlaces(t);
				// create id
				var id = when+ref;
				// store data
				tovisit[id] = new Visit (when, ref, colour, places);
			}
		}
		
		//Visit constructor
		function Visit (when, ref, colour, places)
		{	// store data
			this.when = when;
			this.ref = ref;
			this.colour = colour;
			this.places = places;
			// calculate bounds of attached places, use to store map size and centre
			var b = new GLatLngBounds(0, 0);
			if (places.length == 1)	// only one place in the visit so use sites instead
			{	for (var i = 0; i < places[0].sites.length; i++)		{ b.extend(places[0].sites[i].point); }
			} else if (places[0].ishome == true)		// drive from home
			{	for (var i = 1; i < places.length; i++)
				{	b.extend(places[i].point);
				}
				b.extend(places[0].sites[0].point);
			} else	// multiple places
			{	for (var i = 0; i < places.length; i++)		{ b.extend(places[i].point); }
			}
			this.mapbounds = b;
			this.mapcentre = b.getCenter();
			return this;
		}

		//create a Place instance and store in tovisit[]
		function storePlaces (t)
		{	// create storage
			var places = new Array();
			for (var p = 0; p < visitdata[t].getElementsByTagName("place").length; p++)
			{	var tid = visitdata[t].getAttribute("when")+visitdata[t].getAttribute("ref");		var pid = p;
				// extract data
				var colour = visitdata[t].getAttribute("colour");
				if (visitdata[t].getElementsByTagName("place")[p].getAttribute("ishome") != undefined)
				{	var ishome = true;
				} else	{ var ishome = false; }
				var placename = GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("placename")[0]);
				var lat = parseFloat(GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("lat")[0]));
				var lng = parseFloat(GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("lng")[0]));
				var sites = storeSites(t, p, colour);
				// store data
				places[p] = new Place (tid, pid, colour, ishome, placename, lat, lng, sites);
			}
			return places;
		}
		
		//Place constructor
		function Place (tid, pid, colour, ishome, placename, lat, lng, sites, previous, next)
		{	// store data
			this.tid = tid;		this.pid = pid;
			this.colour = colour;
			this.ishome = ishome;
			this.placename = placename;
			this.lat = lat;
			this.lng = lng;
			this.sites = sites;
			// set place lat/lng
			this.point = new GLatLng(this.lat, this.lng);
			// create pins
			this.icon = new GIcon(baseIcon);							this.icon.image = "supportfiles/pins/pin" + colour + ".png"; 
			this.alticon = new GIcon(baseIcon);
			this.smallicon = new GIcon(baseIconSmall);			this.smallicon.image = "supportfiles/pins/pin" + colour + "Small.png"; 
			this.pin = new GMarker(this.point, {icon:this.icon, title:String(this.placename)});
			this.altpin = new GMarker(this.point, {icon:this.alticon, title:String(this.placename)});
			this.smallpin = new GMarker(this.point, {icon:this.smallicon, title:String(this.placename)});
			return this;
		}
		
		//create a Site instance and store in tovisit[]
		function storeSites (t, p, colour)
		{	// create storage
			var sites = new Array();
			for (var s = 0; s < visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("site").length; s++)
			{	var tid = visitdata[t].getAttribute("when")+visitdata[t].getAttribute("ref");		var pid = p;		var sid = s;
				// extract data
				var placename = GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("placename")[0]);
				var sitename = GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("site")[s].getElementsByTagName("sitename")[0]);
				var lat = parseFloat(GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("site")[s].getElementsByTagName("lat")[0]));
				var lng = parseFloat(GXml.value(visitdata[t].getElementsByTagName("place")[p].getElementsByTagName("site")[s].getElementsByTagName("lng")[0]));
				// store data
				sites[s] = new Site (tid, pid, sid, colour, placename, sitename, lat, lng);
			}
			return sites;
		}

		//Site constructor
		function Site (tid, pid, sid, colour, placename, sitename, lat, lng)
		{	// store data
			this.tid = tid;		this.pid = pid;		this.sid = sid;
			this.colour = colour;
			this.placename = placename;
			this.sitename = sitename;
			this.lat = lat;
			this.lng = lng;
			// set place lat/lng
			this.point = new GLatLng(this.lat, this.lng);
			// create pin
			this.icon = new GIcon(baseIconSmall);			this.icon.image = "supportfiles/pins/pin" + colour + "Small.png"; 
			this.pin = new GMarker(this.point, {icon:this.icon, title:String(this.sitename)});
			return this;
		}
		
		// set markers using MarkerManager
		function setPinMarkers ()
		{	// build arrays of markers
			var pm = 0;		var sm = 0;		var tm = 0;
			for (var t in tovisit)
			{	for (var p = 0; p < (tovisit[t].places.length); p++)
				{	placePins[pm] = setPlace (tovisit[t].places[p], "");
					smallplacePins[pm] = setPlace (tovisit[t].places[p], "small");
					pm++;
					if (tovisit[t].places[p].base == true)
					{	visitPins[tm] = setPlace (tovisit[t].places[p], "small");
						tm++;
					}
					for (var s = 0; s < (tovisit[t].places[p].sites.length); s++)
					{	sitePins[sm] = setPlace (tovisit[t].places[p].sites[s], "");
						sm++;
					}
				}
			}
			// add markers to manager
			mgr = new MarkerManager(map);
			if (visitPins.length == 0)
			{	mgr.addMarkers(smallplacePins, 0, 6);
				mgr.addMarkers(placePins, 7);
			} else
			{	mgr.addMarkers(visitPins, 0, 4);
				mgr.addMarkers(smallplacePins, 5, 6);
				mgr.addMarkers(placePins, 7);
			}
			mgr.addMarkers(sitePins, 7);
			// refresh map
			mgr.refresh();
		}

		// set event listeners for markers
		function setPlace (place, pinsize)
		{	if (pinsize == "small")
			{	GEvent.addListener(place.smallpin,"click", function() {map.openInfoWindowHtml(place.point, place.windowtext, WINDOW_OPTIONS);});
				return place.smallpin;
			} else
			{	GEvent.addListener(place.pin,"click", function() {map.openInfoWindowHtml(place.point, place.windowtext, WINDOW_OPTIONS);});
//				GEvent.addListener(place.pin,"mouseover", function() {map.removeOverlay(place.pin); map.addOverlay(place.altpin);});
//				GEvent.addListener(place.altpin,"mouseout", function() {map.removeOverlay(place.altpin); map.addOverlay(place.pin);});
				return place.pin;
			}
		}

//********** MAIN PROGRAM **********
		//create custom pins
		setCustomPinIcons ();
		//extract visit data from the xml file
		var get_data = GXmlHttp.create();
		get_data.open("GET", "supportfiles/tovisit.xml", true);
		//wait for parsing to complete, 4 means data successfully parsed
		get_data.onreadystatechange = function() 
		{	//everything must happen inside this IF
			if (get_data.readyState == 4)
			{	var data = get_data.responseXML;
				//data arrives as an array
				visitdata = data.documentElement.getElementsByTagName("visit");
				//extract data and store in an array of classes
				storeVisits ();
				// create and set map, dynamic TOC, and markers
				setMap (size, home);
				setPinMarkers ();
			}
		}
		get_data.send(null);
	}
	else
		alert("Sorry, the Google Maps API is not compatible with this browser");
}
//]]>

