

// this allows us to load multiple functions at window.onload without conflicts
// http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/
function addLoadEvent(func) { 
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
} 

addLoadEvent(travelInfo_Map);



function travelInfo_Map() { 
	if (GBrowserIsCompatible()) {
		
		var map = new Object();
		map.points = new Array();
		map.resources = new Array();
		
		if (document.getElementById("t_map")) {
			map.element = document.getElementById("t_map");
			map.gmap = new GMap2(map.element);
			map.gmap.addControl(new GSmallMapControl());
			map.gmap.addControl(new GMapTypeControl());
			map.xmlFile = map.element.className + '.xml';
		}
		
		GDownloadUrl(map.xmlFile, function(doc) {
			xmlDoc = GXml.parse(doc);
			markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				map.points[i] = new Object();
				map.points[i].name = GXml.value(markers[i].getElementsByTagName("name")[0]);
				map.points[i].lat = GXml.value(markers[i].getElementsByTagName("latitude")[0]);
				map.points[i].lng = GXml.value(markers[i].getElementsByTagName("longitude")[0]);
				map.points[i].street = GXml.value(markers[i].getElementsByTagName("street")[0]);
				map.points[i].city = GXml.value(markers[i].getElementsByTagName("city")[0]);
				map.points[i].state = GXml.value(markers[i].getElementsByTagName("state")[0]);
				map.points[i].zip = GXml.value(markers[i].getElementsByTagName("zip")[0]);
				map.points[i].phone = GXml.value(markers[i].getElementsByTagName("phone")[0]);
				map.points[i].web = GXml.value(markers[i].getElementsByTagName("web")[0]);
				map.points[i].desc = GXml.value(markers[i].getElementsByTagName("desc")[0]);
				map.points[i].img = GXml.value(markers[i].getElementsByTagName("image")[0]);
				map.points[i].index = i;
				map.points[i].isbase = false;
				map.points[i].nal = false;
				if (map.points[i].name == 'Northernair Lodge') {
					map.points[i].isbase = true;
					map.points[i].nal = true;
					map.points['base'] = map.points[i];
				}
				else {
					travelInfo_Desc(map.points[i]);
				}
				map.points[i].zoom = parseFloat(markers[i].getAttribute("zoom"));
				map.points[i].id = map.points[i].name.replace(/\s/gi,'_');
				map.points[i].id = map.points[i].id.replace(/[^_a-zA-Z0-9]/gi,'');
				map.points[i].id = map.points[i].id.toLowerCase();
				travelInfo_LatLong(map.points[i]);
				
			}
		});
		
		function travelInfo_LatLong(point) {
			if (point.lat == '' || point.lng == '') {
				point.geo = new GClientGeocoder();
				point.geo.address = point.street + ', ' + point.city + ', ' + point.state + ', ' + point.zip;
				point.geo.getLocations(point.geo.address,function(result) {
					point.lng = result.Placemark[0].Point.coordinates[0];
					point.lat = result.Placemark[0].Point.coordinates[1];
					point.latlng = new GLatLng(point.lat,point.lng);
					travelInfo_Marker(point);
				});
			}
			else {
				point.latlng = new GLatLng(point.lat,point.lng);
				travelInfo_Marker(point);
			}
		}
		
		function travelInfo_Marker(point) {
			if (point.nal == true) {
				point.icon = new GIcon();
				point.icon.image = '../_images/structure/pushpins/nal3.png';
				point.icon.iconSize = new GSize(20, 34);
//				point.icon.shadow = "../_images/structure/pushpins/nal-shadow.png";
//				point.icon.shadowSize = new GSize(36, 34);
				point.icon.iconAnchor = new GPoint(15, 29);
			}
			else {
				point.icon = new GIcon();
				point.icon.image = '../_images/structure/pushpins/iconb' + point.index + '.png';
				point.icon.iconSize = new GSize(20, 34);
				point.icon.iconAnchor = new GPoint(15, 29);
			}
			if (point.isbase == true)  {
				map.gmap.setCenter(point.latlng,point.zoom);
			}
			point.marker = new GMarker(point.latlng,point.icon);
			map.gmap.addOverlay(point.marker);
			
			point.marker.latlng = point.latlng;
			point.marker.name = point.name;
			point.marker.address = '';
			if (point.street != '') {
				point.marker.address += point.street + '<br />';
			}
			if (point.city != '') {
				point.marker.address += point.city;
			}
			if (point.city != '' && point.state != '') {
				point.marker.address +=  ', ';
			}
			else {
				point.marker.address +=  ' ';
			}
			if (point.state != '') {
				point.marker.address += point.state + ' ';
			}
			if (point.zip != '') {
				point.marker.address += point.zip;
			}
			GEvent.addListener(point.marker, "click", function() {
				map.gmap.openInfoWindow(this.latlng,this.name + ' <br /> ' + this.address);
			});
		}
		
		function travelInfo_Desc(point) {
			
			
			resourcesContainer = document.getElementById('t_resources');
			
			resource = document.createElement('div');
			map.resources[map.resources.length] = resource;
			resource.className = 't_resource';
			if (map.resources.length == 1) {
				resource.className += ' first';
			}
			
			if (point.img != '' && point.img != undefined) {
				photo = document.createElement('div');
				photo.className = 't_photo';
				resource.appendChild(photo);
				
				img = document.createElement('img');
				img.src = point.img;
				photo.appendChild(img);
				
				resource.className += ' t_hasImage';
			}
			
			title = document.createElement('h3');
			title.appendChild(document.createTextNode(map.resources.length + '. ' + point.name));
			title.id = point.id;
			resource.appendChild(title);
			
			if (point.desc != '' && point.desc != undefined) {
				description = document.createElement('p');
				description.innerHTML = point.desc;
				resource.appendChild(description);
			}
			
			infos = new Array();
			
			if (point.web != '' && point.web != undefined) {
				link = document.createElement('a');
				link.href = point.web;
				linkName = link.href.replace(/http.*:\/\//gi,'');
				linkName = linkName.replace(/\/$/gi,'');
				link.appendChild(document.createTextNode(linkName));
				web = document.createElement('li');
				web.appendChild(link);
				infos[infos.length] = web;
			}
			
			if (point.phone != '' && point.phone != undefined) {
				phone = document.createElement('li');
				phone.appendChild(document.createTextNode(point.phone.replace(/-/gi,'.')));
				infos[infos.length] = phone;
			}
			
			if ((point.street != '' && point.street != undefined) && (point.city != '' && point.city != undefined)) {
				dirLink = document.createElement('a');
				dirLink.appendChild(document.createTextNode('Get Directions'));
				dirAddress = point.street + ', ' + point.city;
				if (point.state != '' && point.state != undefined) {
					dirAddress += ', ' + point.state
				}
				if (point.zip != '' && point.zip != undefined) {
					dirAddress += ' ' + point.zip
				}
				dirLink.href = 'http://maps.google.com/maps?t=m&saddr=Northernair+Lodge,+Ely,+MN&daddr=' + point.name + ',+'  + dirAddress;
				directions = document.createElement('li');
				directions.appendChild(dirLink);
				infos[infos.length] = directions;
			}
			
			if (infos.length > 0) {
				info = document.createElement('ul');
				info.className = 't_info';
				for (x=0; x<infos.length; x++) {
					info.appendChild(infos[x]);
				}
				resource.appendChild(info);
			}
			
			
			resourcesContainer.appendChild(resource);
			
		}
		
	}
	
//	window.onunload = GUnload();
}
