﻿var map;

function load() {
	var map = new google.maps.Map(document.getElementById("map"), {
		center: new google.maps.LatLng(27.9, 86.8),
        	zoom: 10,
        	mapTypeId: 'roadmap'
      	});

	var infoWindow = new google.maps.InfoWindow;

	var url = 'topmarkersXML.php?listID=0&year=' + year;

	downloadUrl(url, function(data) {
        	var xml = data.responseXML;
        	var markers = xml.documentElement.getElementsByTagName("marker");
		var bounds = new google.maps.LatLngBounds();

	        for (var i = 0; i < markers.length; i++) {
          
			var id = 'c' + markers[i].getAttribute("topid");
			var country = markers[i].getAttribute("country");
			var countryid = markers[i].getAttribute("countryid");
			var visible = markers[i].getAttribute("visible");
			var continent = markers[i].getAttribute("continent");
			var list = markers[i].getAttribute("toplist");
			var rank = markers[i].getAttribute("rank");
			var text = markers[i].getAttribute("text");
			var imagepath = markers[i].getAttribute("picturepath");
			var countryurl = markers[i].getAttribute("countryurl");

			if(visible == 1){
				var html = '<table><tr><td valign="top" width="105"><h3 class="countryinfobox"><span style="color: #ccc;font-size: 20px;font-weight: bold;margin: 0px 0px 0px 0px;">' + rank + '.</span> ' + country + '</h3><br/><a class="maplink" href="places.php?country=' + countryurl + '">> Find places</a></td><td valign="top"><a class="maplink" href="#' + id + '"><img width="105" height="70" src="pictures/' + imagepath + '" alt="" /></a></td></tr></table>';
			} else {
				var html = '<table><tr><td valign="top" width="105"><h3 class="countryinfobox"><span style="color: #ccc;font-size: 20px;font-weight: bold;margin: 0px 0px 0px 0px;">' + rank + '.</span> ' + country + '</h3></td><td valign="top"><a class="maplink" href="#' + id + '"><img width="105" height="70" src="pictures/' + imagepath + '" alt="" /></a></td></tr></table>';
			}

			var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));

			if(list == 1){
				image = 'graphics/marker' + rank + 'green.png';
			}else if(list == 2){
				image = 'graphics/marker' + rank + 'yellow.png';
			} else {
				image = 'graphics/marker' + rank + 'red.png';
			}

	          	var marker = new google.maps.Marker({
        	    		map: map,
            			position: point,
				title: country,
				icon: image
            		});

          		bindInfoWindow(id, marker, map, infoWindow, html);
			bounds.extend(point);
    			map.fitBounds(bounds);
        	}
     });
}

function bindInfoWindow(id, marker, map, infoWindow, html) {  
	google.maps.event.addListener(marker, 'click', function() {
        	infoWindow.setContent(html);
        	infoWindow.open(map, marker);
     	});

	if(document.getElementById(id)){
		google.maps.event.addDomListener(document.getElementById(id),'click', function() {
			infoWindow.setContent(html);
   			infoWindow.open(map,marker);
			scroll(0,0);
  		});
	}
}

function downloadUrl(url, callback) {
	var request = window.ActiveXObject ?
		new ActiveXObject('Microsoft.XMLHTTP') :
		new XMLHttpRequest;

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
          		request.onreadystatechange = doNothing;
          		callback(request, request.status);
        	}
      	};

	request.open('GET', url, true);
      	request.send(null);
}

function doNothing() {}
