
var objmap, mapwidth, mapheight, maplng, maplat, zoomlevel;
var markerobj = new Array();
var markerfunc = new Array();
var infotoggle = false;

function LoadMapFromXML(url) {
	document.getElementById('googlemap').style.display = 'block';
	document.getElementById('googlemap').innerHTML = 'Loading...';
	var request = GXmlHttp.create();
	request.open('GET', url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4)
		{
			window.scrollTo(0, 0);
			var xmlDoc = request.responseXML;
			var maps = xmlDoc.getElementsByTagName('map');
			if (maps.length > 0)
			{
				map = maps[0];
				zoomlevel = (17 - parseFloat(map.getElementsByTagName('zoom')[0].firstChild.nodeValue));
				maplng = parseFloat(map.getElementsByTagName('lng')[0].firstChild.nodeValue);
				maplat = parseFloat(map.getElementsByTagName('lat')[0].firstChild.nodeValue);

				if (objmap)
				{
					objmap = null;
				}

				objmap = new GMap2(document.getElementById('googlemap'));
				objmap.addControl(new GSmallMapControl());	

				objmap.setCenter(new GLatLng(maplat, maplng), zoomlevel);
				
				objloc = document.getElementById('locations');
				if (objloc) objloc.innerHTML = '<h3>Locations</h3>\r\n<ul>\r\n';
				
				var locations = map.getElementsByTagName('locations-item');
				for (var i = 0; i < locations.length; i++)
				{
					if (locations[i].getElementsByTagName('lng')[0].firstChild && locations[i].getElementsByTagName('lat')[0].firstChild)
					{
						var lng = parseFloat(locations[i].getElementsByTagName('lng')[0].firstChild.nodeValue);
						var lat = parseFloat(locations[i].getElementsByTagName('lat')[0].firstChild.nodeValue);
						var address = "", city = "", province = "", country = "", postal_code = "", phone = "", tollfree = "", fax = "";
						
						if (locations[i].getElementsByTagName('address')[0].firstChild)
						{
							address = locations[i].getElementsByTagName('address')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('city')[0].firstChild)
						{
							city = locations[i].getElementsByTagName('city')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('province')[0].firstChild)
						{
							province = locations[i].getElementsByTagName('province')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('country')[0].firstChild)
						{
							country = locations[i].getElementsByTagName('country')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('postal_code')[0].firstChild)
						{
							postal_code = locations[i].getElementsByTagName('postal_code')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('phone')[0].firstChild)
						{
							phone = locations[i].getElementsByTagName('phone')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('tollfree')[0].firstChild)
						{
							tollfree = locations[i].getElementsByTagName('tollfree')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('fax')[0].firstChild)
						{
							fax = locations[i].getElementsByTagName('fax')[0].firstChild.nodeValue;
						}
						if (locations[i].getElementsByTagName('company_id')[0].firstChild)
						{
							company = locations[i].getElementsByTagName('company_id')[0].getElementsByTagName('name')[0].firstChild.nodeValue;
						}
	
						if (objloc)
						{
							html  = "<li>\r\n";
							if (city && !isNaN(lng) && !isNaN(lat))
							{
								html += "<a href=\"#content\" onclick=\"LocationList(" + i + ")\"><b>" + city + "</a></b><br />\r\n";
							}
							else
							{
								html += "<b>" + city + "</b><br />\r\n";
							}
							if (company) html += company + "<br />\r\n";
							html += address + "<br />\r\n";
							if (city) html += city;
							if (city && province) html += ", ";
							if (province) html += province;
							if (province && country) html += ", ";
							if (country) html += country;
							if (postal_code) html += " " + postal_code;
							html += "<br />\r\n";
							if (phone) html += "<b>Phone:</b> " + phone + "<br />\r\n";
							if (tollfree) html += "<b>Toll-free:</b> " + tollfree + "<br />\r\n";
							if (fax) html += "<b>Fax:</b> " + fax + "<br /></li>\r\n";
							
							objloc.innerHTML += html;
						}
						
						if (!isNaN(lng) && !isNaN(lat))
						{
							objmap.addOverlay(Marker(i, address, city, province, country, postal_code, phone, tollfree, fax, company, lng, lat));
						}
					}
				}
				
				objloc.innerHTML += '</ul>\r\n';
			}
			else
			{
				alert('ERROR: Could not load XML source file for map.');
			}
		}
	}
	request.send(null);
}

function LocationList(markerIndex)
{
	objmap.closeInfoWindow();
	objmap.setCenter(new GLatLng(maplat, maplng), zoomlevel);
	markerfunc[markerIndex]();
}

function Marker(markerIndex, address, city, province, country, postal_code, phone, tollfree, fax, company, lng, lat)
{
	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point);
	markerobj[markerIndex] = marker;
	
	eval("var func = function() { InfoWindow(" + markerIndex + ", '" + address + "', '" + city + "', '" + province + "', '" + country + "', '" + postal_code + "', '" + phone + "', '" + tollfree + "', '" + fax + "', '" + company + "', " + lng + ", " + lat + "); }");
	markerfunc[markerIndex] = func;
	
	GEvent.addListener(marker, 'click', func);
	GEvent.addListener(marker, 'infowindowclose', function() {
		objmap.setCenter(new GLatLng(maplat, maplng), zoomlevel);
		infotoggle = false;
	});
	
	return marker;
}

function ToggleMarker(markerIndex, address, city, province, country, postal_code, phone, tollfree, fax, company, lng, lat)
{
	if (markerobj[markerIndex])
	{
		objmap.removeOverlay(markerobj[markerIndex]);
		markerobj[markerIndex] = null;

		var d = document.getElementById('location_' + markerIndex);
		if (d)
		{
			d.className = "";
		}
	}
	else
	{
		objmap.addOverlay(Marker(markerIndex, address, city, province, country, postal_code, phone, tollfree, fax, company, lng, lat));

		var d = document.getElementById('location_' + markerIndex);
		if (d)
		{
			d.className = "active";
		}
	}
}

function InitMap() {
	objmap = new GMap2(document.getElementById('googlemap'));
	objmap.setCenter(new GLatLng(55, -100), 2);
	objmap.addControl(new GSmallMapControl());	
}

function InfoWindow(markerIndex, address, city, province, country, postal_code, phone, tollfree, fax, company, lng, lat)
{
	objmap.closeInfoWindow();
	objmap.setCenter(new GLatLng(maplat, maplng), zoomlevel);
	html  = "<div class=\"locationtext\">\r\n";
	if (company) html += "<b>" + company + "</b><br />\r\n";
	html += "<b>Address:</b><br />\r\n";
	html += address + "<br />\r\n";
	if (city) html += city;
	if (city && province) html += ", ";
	if (province) html += province;
	if (province && country) html += ", ";
	if (country) html += country;
	if (postal_code) html += " " + postal_code;
	html += "<br />\r\n";
	if (phone) html += "<b>Phone:</b> " + phone + "<br />\r\n";
	if (tollfree) html += "<b>Toll-free:</b> " + tollfree + "<br />\r\n";
	if (fax) html += "<b>Fax:</b> " + fax + "<br /></li>\r\n";
	objmap.setCenter(new GLatLng(maplat, maplng), 14)
	markerobj[markerIndex].openInfoWindowHtml(html);
}