function truncate(text, length, ellipsis) {       
  
    // Set length and ellipsis to defaults if not defined   
    if (typeof length == 'undefined') var length = 100;   
    if (typeof ellipsis == 'undefined') var ellipsis = '...';   
  
    // Return if the text is already lower than the cutoff   
    if (text.length < length) return text;   
  
    // Otherwise, check if the last character is a space.   
    // If not, keep counting down from the last character   
    // until we find a character that is a space   
    for (var i = length-1; text.charAt(i) != ' '; i--) {   
        length--;   
    }   
  
    // The for() loop ends when it finds a space, and the length var   
    // has been updated so it doesn't cut in the middle of a word.   
    return text.substr(0, length) + ellipsis + text.substr(length, length + 100);   
}  



var markerindex;
//var xmlfileused = "location.php"; //The xml file currently loaded this one at start of page.
var point;
//var xmlfilename = "location.php";

//Arrays to hold copies of the markers and html used by the side_bar because the function closure trick doesnt work there.
//Global Variables Declared here. 
var gmarkers = [];
var htmls = [];
var i = 0;
var map;
var tooltip = document.createElement("div");
var letter ="";
var jlabels = [];

function load(){

	if (GBrowserIsCompatible()) { //Main Code
		map = new GMap2(document.getElementById("map"));//Create Map
		//Create Zoom control and set it on right hand side of map. 80px from top
		var mapTypeControl = new GLargeMapControl();
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,80));

		//Create Custom Map Control Input Box and Button
		//map.addControl(new mapsearchControl());
		//map.addControl(new switchControl());
		//map.addControl(mapTypeControl,topRight);
		map.addControl(new GSmallMapControl()); // zoom tool
		map.addControl(new GMapTypeControl());
		new GKeyboardHandler(map);
		// MAP CENTER is set to Madrid: 40.420300 -3.705774
		// By default, the Google Maps API provides three map types: G_NORMAL_MAP, G_SATELLITE_MAP, and G_HYBRID_MAP. 
		map.setCenter(new GLatLng(40.420300, -3.705774), 1, G_NORMAL_MAP);
		//map.addControl(new GOverviewMapControl(new GSize(100,100)));

		//setTimeout("positionOverview(558,254)",1);

		//Open Message Box for Showing text.  // ====== set up marker mouseover tooltip div ======
		document.getElementById("map").appendChild(tooltip);
		tooltip.style.visibility="hidden";

		var request = GXmlHttp.create();
		request.open("GET", "http://www.pbs.org/newshour/world/scripts/map/" + xmlfilename , true);   
		request.onreadystatechange = function() {
			if (request.readyState == 4) { //Removed for IE documentElement error.//var xmlDoc = request.responseXML;
				var xmlDoc = GXml.parse(request.responseText); 
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");

				for (var i = 0; i < markers.length; i++) { // obtain the array of markers and loop through it
					 // obtain the attribues of each marker
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var lng = parseFloat(markers[i].getAttribute("lng"));
					var point = new GLatLng(lat,lng);
					var label = markers[i].getAttribute("label");   
					var html = markers[i].getAttribute("html");

					var locations = xmlDoc.documentElement.getElementsByTagName("location");
					var html_locations = "";
					for (var j = 0; j < locations.length; j++)  { // obtain the array of markers and loop through it
						var url = locations[j].getAttribute("url");
						var label = locations[j].getAttribute("label");
						//if (label.length > 60) {
						//	label = truncate(label, 60, "<BR>");
						//}
						
						var id = locations[j].getAttribute("id");
						var date = locations[j].getAttribute("date");
						if (id == i) {
							html_locations = html_locations;
							if (date) {
								html_locations = html_locations + date;
							}
							var str_target = "";
							if (t == "blank") {
								str_target = " target='_blank'";
							} else {
								str_target = " target='_top'";
							}
							html_locations = html_locations + "<BR><a href='" + url + "'" + str_target + ">" + label + "</a><BR>";
						}	
					}
            
					// icon info
					var iconObj = new GIcon();
					iconObj.image = markers[i].getAttribute("icon");
					iconObj.iconSize = new GSize(13,13);
					iconObj.iconAnchor = new GPoint(5,13);
					iconObj.infoWindowAnchor = new GPoint(9, 2);
					 
					var htmlx = "";
					htmlx = html_locations;           
				   
					// create the marker
					var marker = createMarker(lat,lng,point,label,htmlx,iconObj);            
				 
					map.addOverlay(marker);
				}		 
			}						
		}
		request.send(null);    
					 
	} else  {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}   
	markerindex = i;
}

     

         
//Switch box fucntions to move on different sections of the map      
              
function createMarker(lat,lng,point,name,html,iconObj) {

	var marker = new GMarker(point,iconObj); 
	// === store the name so that the tooltip function can use it ===

	marker.tooltip = name;
	//GEvent.addListener(marker, "click", function() 
	GEvent.addListener(marker, "mouseover", function() {
		//marker.openInfoWindowTabsHtml([new GInfoWindowTab("Overview",html), new GInfoWindowTab("Video",youtubevideoformat)],{suppressMapPan:true});
		marker.openInfoWindowTabsHtml([new GInfoWindowTab("Overview",html)],{suppressMapPan:true});

		map.savePosition();
		function subGPoints(a,b) { //returns the distance in pixels between point a and b 
			return new GPoint(a.x-b.x, a.y-b.y); 
		} 
		//Pixel distance to the center of the map 
		var CDivPixel = map.fromLatLngToDivPixel(map.getCenter()); 
		//Pixel distance from the marker point    
		var pointDivPixel = map.fromLatLngToDivPixel(point); 
		//Difference between the above mentioned distances 
		var fromCenter = subGPoints(pointDivPixel, CDivPixel); 
		//Pan to the corrected location (the -40 and +215 sizes are the distances from my marker to the center of the infowindow 
		//map.panBy(new GSize(-fromCenter.x+6,-fromCenter.y+225)) 
		map.panBy(new GSize(-fromCenter.x-30,-fromCenter.y+90)) 
	}
	);
             
	GEvent.addListener(map, "infowindowclose", function() { 
		//map.returnToSavedPosition()
	}); 
	// save the info we need to use later for the side_bar
	jlabels[i] = name;
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	//  ======  The new marker "mouseover" and "mouseout" listeners  ======
	//GEvent.addListener(marker,"mouseover", function() {showTooltip(marker);});        
	//GEvent.addListener(marker,"mouseout", function() {tooltip.style.visibility="hidden"});

	//GEvent.addListener(marker,"mouseover", function() {showTooltip(marker);});        
	GEvent.addListener(marker,"mouseout", function() {tooltip.style.visibility="hidden"});
	return marker;		   
}
