//<![CDATA[
var map = null;
var geocoder = null;
var addressMarker;

function load() {
	setupMap();
	
	//call the XML file and push the results into an array
	$.ajax({
		type: "GET",
		cache: false,
		dataType: "xml",
		url: "/include/markers.php",
		data: "",
		success: function(xml){
			//run a foreach loop on each "marker" node within the file
			$(xml).find('marker').each(function(){
				var city = $(this).attr('city');
				var state = $(this).attr('state');
				var country = $(this).attr('country');
				//alert(city);
				var headerArray = Array();
				var storyArray = Array();
				
				//inner foreach on each story
				$(this).children().each(function(){
					var storyCopy = '';
					
					var name = $(this).find('firstname').text() + " " + $(this).find('lastname').text();
					var company = $(this).find('company').text();
					var storyText = $(this).find('storyText').text();
					var imgName = $(this).find('image').text();
					
					if(imgName != "" && imgName != null)
					{
						storyCopy += '<img src="/image_upload/'+imgName+'" style="float:right;" />';
					}
					storyCopy += '<h2>' + name + '</h2><p><strong>' + company + '<br />' + city + ', ' + state + '<br /></strong></p><p>' + storyText + '</p>';
					storyArray.push(storyCopy);
					headerArray.push(company);
				});
				
				var marker = createMarker(city + ', ' + state, country, storyArray, headerArray);
			});
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
		  alert("Error loading map markers. Click OK to continue.");
		}
	});
}

function setupMap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
		map.enableDoubleClickZoom();
 		map.enableScrollWheelZoom();
   		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(28.30,0.00), 2);
		map.setMapType(G_HYBRID_MAP);
		//map.setMapType(G_NORMAL_MAP)
		G_HYBRID_MAP.getMinimumResolution = function () { return 4 };
		G_HYBRID_MAP.getMaximumResolution = function () { return 12 }; 
		//G_NORMAL_MAP.getMinimumResolution = function () { return 4 };
		//G_NORMAL_MAP.getMaximumResolution = function () { return 10 };
		geocoder = new GClientGeocoder();
		
		var redIcon = new GIcon(G_DEFAULT_ICON);
    	redIcon.image = "/images/blank.png";
		markerOptions = { icon:redIcon };
  }
}

function createMarker(address, countryCode, htmls, labels) {
  if (geocoder) {
    geocoder.setBaseCountryCode(countryCode);
    geocoder.getLatLng(address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
        } else {
					var marker = new GMarker(point, markerOptions);
					GEvent.addListener(marker, "click", function() {
						// adjust the width so that the info window is large enough for this many tabs
						if (htmls.length < 4) {
							for (var i=0; i<htmls.length; i++) {
								htmls[i] = '<div style="width:350px; height:240px">' + htmls[i] + '</div>';
							}
						}
						var tabs = [];
						for (var i=0; i<htmls.length; i++) {
							tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
						}
						marker.openInfoWindowTabsHtml(tabs);
					});
					
					map.addOverlay(marker);
					return marker;
        }
      }
    );
  }
}
//]]>