/*
  Supinfo Around the World
  Supinfo à travers le monde
  http://mart.e-supinfo.net/supinfo.a.travers.le.monde/
  
  This file is offered under the terms of the Attribution-ShareAlike Creative Commons License.
  See http://creativecommons.org/licenses/by-sa/2.5
  
  You can create your own map by using our map creation tool
  "Strike Up Your Google Maps"
  http://www.martwebstudio.net/strike.up.your.gmaps

  @author Martin Yung
  @version 0.6 2006-04-18
*/

    var iconSmall = new GIcon();
    iconSmall.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
    iconSmall.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    iconSmall.iconSize = new GSize(12, 20);
    iconSmall.shadowSize = new GSize(22, 20);
    iconSmall.iconAnchor = new GPoint(6, 20);
    iconSmall.infoWindowAnchor = new GPoint(9, 2);
    iconSmall.infoShadowAnchor = new GPoint(18, 25);
    
    var marker = new Array();
    
    // "map" is the ID of the DIV in which the map contains
    var map = new GMap2(document.getElementById("map"));
    
	loadFile(georssFile);
		
    map.addControl(new GLargeMapControl());
    //map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
    map.setCenter(new GLatLng(40.713955826286046,-63.6328125),2);
    //map.setMapType(G_NORMAL_MAP);
    //map.setMapType(G_SATELLITE_MAP);
    map.setMapType(G_HYBRID_MAP);

	
	
	function loadFile(fileName) {
	    
		// Your map data imported using XML external source
	    var request = GXmlHttp.create();
		request.open('GET', fileName, true);
		request.onreadystatechange = function() {
			
			if (request.readyState == 4) {
				
				var xmlDoc = request.responseXML;
				var items = xmlDoc.getElementsByTagName("item");
				
				/* Namespace 'geo' for non IE browsers */
				var xmlns_geo = 'http://www.w3.org/2003/01/geo/wgs84_pos#';
				
				//document.getElementById("sidebar").innerHTML = '';
				
				for (var i = 0; i < items.length; i++) {
					
					var item = items[i];
					
					/* Create a point object */
					if (navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
					    var lng = item.getElementsByTagName('geo:long')[0].firstChild.nodeValue;
				        var lat = item.getElementsByTagName('geo:lat')[0].firstChild.nodeValue;
					}
					else {
				        var lng = item.getElementsByTagNameNS(xmlns_geo,'long')[0].firstChild.nodeValue;    // for Mozilla
					    var lat = item.getElementsByTagNameNS(xmlns_geo,'lat')[0].firstChild.nodeValue;     // for Mozilla
				    }
				    
					var point = new GLatLng(lat, lng);
					
					/* Create a marker object for the point we have just created */
					var title = item.getElementsByTagName("title")[0].firstChild.nodeValue;

						var link = 'http://';
						var content = '<strong>'+title+'</strong><br/><br/>';

					var description = item.getElementsByTagName("description")[0].firstChild.nodeValue;
					content += description;
					//document.getElementById("sidebar").innerHTML += '<p><strong><a href="javascript:goToThisPoint('+i+')">'+title+'</a></strong><br/>'+description+'</p>';
					addMarker(point, content, iconSmall);
				}
			}
		}
		
		request.send(null);	
	
	}

    function goToThisPoint(id) {
	    marker[id].openInfoWindowHtml(marker[id].html);
    }
	
    function addMarker(point, htmlContent, icon) {
	    
	    marker.push(new GMarker(point, icon));
    	
    	var id = marker.length-1;
	    // Show this marker's index in the info window when it is clicked.
	    marker[id].html = '<div class="infowindow">' + htmlContent + '</div>';

	    //GEvent.addListener(marker[id], 'mouseover', function() {
	    GEvent.addListener(marker[id], 'click', function() {
		    marker[id].openInfoWindowHtml(marker[id].html);
	    });
	    try{
	    map.addOverlay(marker[id]);}catch(e){}
	    //window.setTimeout(plotPoint,timeOut);
	    return id;
    }
    
    
    function resizeMap() {

	    /* Again, IE and Firefox have different ways to get the window's height */
	    if (document.documentElement &&	document.documentElement.clientHeight) {
		    var winHeight = document.documentElement.clientHeight;
	    }
	    else if (window.self && self.innerHeight) {
		    var winHeight = self.innerHeight;   // This is for Mozilla
	    }
    	
	    var e = document.getElementById("map");
	    var e2 = document.getElementById("sidebar");
	    var offset = e.offsetTop;
	    
	    var height = winHeight - offset - 70;
    	
	    e.style.height = height + "px";
	    e2.style.height = height + "px";
    }
	
