google.load("maps", "2.x");
var xmlhttp;
var map;
var centerLatitude = 45;
var centerLongitude = -90; 
var startZoom = 3;
var deselectCurrent = function() {};
var allcity = {};

 

/* [listing 6-20] */
function initializePoint(pointData) {
	
	var point = new GLatLng(parseFloat(pointData.latitude), parseFloat(pointData.longitude));
	var marker = new GMarker(point,{icon:icon,title:pointData.tooltip}); 
	
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;
	
	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.name + ' </strong><br/>Tel: ' + pointData.tel + '<br/>Fax: ' + pointData.fax + '<br/>' + pointData.toll;
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; } 
		map.setCenter(new GLatLng(45, -90), 15);
		
        marker.openInfoWindowHtml("<div style='height:110px;'><h2>Neo-Traffic</h2><div class='infobulle'>" + pointData.address + '<br/>' + pointData.zipcode +' , ' + pointData.city +'<br/>' + pointData.type +"</div></div>");

		
	GEvent.addListener(marker, "infowindowclose", function() { 
			//map.panTo(point); 
			map.setCenter(new GLatLng(46.5, -97), 4);
				});		
	
		map.panTo(point);
		return false;
	}
	

	GEvent.addListener(marker, 'click', focusPoint);
	listItemLink.onclick = focusPoint;
	
	pointData.show = function() {
		if (!visible) {
			
			document.getElementById('sidebar-list').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hide = function() {
		if (visible) {
			
			document.getElementById('sidebar-list').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}

	pointData.show();
}

function changeBodyClass(from, to) {
	document.body.className = document.body.className.replace(from, to);
	
	return false;
}

function setAlertText(str) {
document.getElementById('alert').innerHTML = '<p>' + str + '</p>';
}

function initData() {
	
	
	 this.map = new google.maps.Map2(document.getElementById("map"));
	 this.map.addControl(new GSmallMapControl());
	 this.map.addControl(new GMapTypeControl());
	 this.map.enableScrollWheelZoom(); 

	 this.map.addMapType(G_PHYSICAL_MAP);
	 this.map.addControl(new GOverviewMapControl());
	 this.map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	  
	  this.icon = new GIcon(G_DEFAULT_ICON);
	  this.icon.image = "_img/map_pointer.png";
	  this.icon.shadow = "_img/map_shadow.png";
	  this.icon.iconSize = new GSize(48, 37);
	  this.icon.shadowSize = new GSize(48, 37);
	  this.icon.iconAnchor = new GPoint(5, 37);
	  this.icon.infoWindowAnchor = new GPoint(5, 2);
	  this.icon.infoShadowAnchor = new GPoint(5, 2);
	
	var bounds = new GLatLngBounds();
	
	/* [listing 6-21] */
	
	for (id =0; id<markers.length; id++) {
	
		allcity[markers[id].city] = true;
		
		
		initializePoint(markers[id]);
		
		bounds.extend(new GLatLng(markers[id].latitude, markers[id].longitude));
		 				
	}

	/* [listing 6-21 end] */
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	changeBodyClass('loading', 'standby');
}

function init() {
	
	
	xmlhttp = GXmlHttp.create();
xmlhttp.open('GET', '_js/map_contact_data_fr.js', true);
xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8')

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status != 200)
setAlertText('Could not access map data.');
else
{
var responseText = xmlhttp.responseText;
markers = eval(responseText);

if (!markers)
setAlertText('Map data error.');
else
initData();

}
}
}
xmlhttp.send(null);
	 
}


google.setOnLoadCallback(init);

