var map;
 


// game = 1 global hunt, 2=US hunt,  3=Gaea
  
//alert('game = ' + game);
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
//var baseIcon0 = new GIcon();
//baseIcon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
//baseIcon0.iconSize = new GSize(19, 17);
//baseIcon0.shadowSize = new GSize(19, 17);
//baseIcon0.iconAnchor = new GPoint(5, 17);
//baseIcon0.infoWindowAnchor = new GPoint(5, 1);
//baseIcon0.infoShadowAnchor = new GPoint(9, 12);
  

// var baseIcon1 = new GIcon();
//baseIcon1.shadow = "http://www.google.com/mapfiles/shadow50.png";
//baseIcon1.iconSize = new GSize(30	, 30);
//baseIcon1.shadowSize = new GSize(30,30);
//baseIcon1.iconAnchor = new GPoint(5, 17);
//baseIcon1.infoWindowAnchor = new GPoint(5, 1);
//baseIcon1.infoShadowAnchor = new GPoint(9, 12);
	 
function init() { 					//   init 	
 	 // alert(' center @ ' + centerLatitude + ' ' + centerLongitude ); 
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GOverviewMapControl());     
 
  map.enableDoubleClickZoom();
  map.enableScrollWheelZoom();
  map.removeMapType(G_NORMAL_MAP);
  map.addMapType(G_PHYSICAL_MAP);      
  
   //alert('passed lat lng: ' +       centerLatitude + ' ' + centerLongitude); 
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
  map.setMapType(G_PHYSICAL_MAP);
    //alert(' retrieve');
  retrieveMarkers();
  
}
 

 window.onload = init; 
 
function createMarker(latlng, html, iconImage, icType) {	//  createmarker
// ictype = 1 for type 0, country icon
// ictype = 2 for type 0, treasure

	
//if (iconImage !='') {	
//	if (icType == 0) {	 
//		var icon = new GIcon(baseIcon0);
//	} else {
//		var icon = new GIcon(baseIcon1);
//	}
//	icon.image = iconImage;	 
  //  var marker = new GMarker(latlng, icon );
//} else {
 	var marker = new GMarker(latlng);
//}

GEvent.addListener(marker, 'click', function() {
var markerHTML = html;
marker.openInfoWindowHtml(markerHTML);
});


return marker;
}								//  end createmarker
 
function retrieveMarkers() {
  
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
   
  function loadXML(xmlFile)
{
  xmlDoc.async="false";
  xmlDoc.onreadystatechange=verify;
  xmlDoc.load(xmlFile);
  xmlObj=xmlDoc.documentElement;
}

 function verify()
{
  // 0 Object is not initialized
  // 1 Loading object is loading data
  // 2 Loaded object has loaded data
  // 3 Data from object can be worked with
  // 4 Object completely initialized
  if (xmlDoc.readyState != 4)
  {
      return false;
  }
}
    //  alert('b4 loadxml gametype= '+game);
 if (game == 1) {
 		loadXML('http://cascoly.com/games/hunt/country.xml');
 }
 if (game == 2) {
  loadXML('http://cascoly.com/games/hunt/state.xml');
 }
if (game == 3) {
    // alert(' starting gaea gametype= '+game  );
 loadXML('http://cascoly.com/games/food/country.xml');
 }

 var markers = xmlDoc.documentElement.getElementsByTagName("marker");
   //alert(' n markers: ' + markers.length);
 
for (var i = 0; i < markers.length; i++) {
  	//var lng2 = markers[1].getAttribute("lng");
	//var lat2 = markers[1].getAttribute("lat");


  	var lng = markers[i].getAttribute("lng");
	var lat = markers[i].getAttribute("lat");
//check for lng and lat so MSIE does not error
//on parseFloat of a null value
if(lng && lat) {					//  ----begin  if(lng && lat)
var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
 
//marker info shown:
// contains popup or link to details
if (game == 1 ) {
var popurl = "showcountry.asp?cname="   + markers[i].getAttribute("country"); 
var html = '<div><b>Country</b> '+ markers[i].getAttribute("country") + '</div>'
 //+ '<div><b>Abbr</b> '+ markers[i].getAttribute("abbr") + '</div>'
} 
if (game == 2) {
var popurl = "showstate.asp?cname="   + markers[i].getAttribute("state"); 
var html = '<div><b>Country</b> '+ markers[i].getAttribute("state") + '</div>'
 
}              
 if (game == 3 ) {
var popurl = "http://cascoly.com/games/food/showcountry.asp?cabbr="   + markers[i].getAttribute("abbr"); 
var html = '<div><b>Country</b> '+ markers[i].getAttribute("country") + '</div>'
 //+ '<div><b>Abbr</b> '+ markers[i].getAttribute("abbr") + '</div>'
}

if (nmoves < 0) {
   lastdist = 0;
} else {
   lastdist = Math.round(jcalcdist(lat, lng, lat2, lng2)/1000);  
}   

if (game > 0 ) {
 var tdist1 = Math.round(jcalcdist( lat, lng, tlat1, tlng1)/1000);
  var tdist2 = Math.round(jcalcdist( lat, lng, tlat2, tlng2)/1000);
   var tdist3 = Math.round(jcalcdist( lat, lng, tlat3, tlng3)/1000);
  var tdist4 = Math.round(jcalcdist( lat, lng,   tlat4, tlng4)/1000);
  var tdist5 = Math.round(jcalcdist( lat, lng, tlat5, tlng5)/1000);
//alert('tdist1 ' + tdist1);
switch (game)
{
case 2:
	html = '<div><a href=hunt.asp?p=2&c='  + markers[i].getAttribute("abbr")  + '&dist='  
	//var caption = 'Search here';
	break;
case 3:
	html = '<div><a href=http://cascoly.com/games/food/showcountry.asp?p=2&cabbr='  + markers[i].getAttribute("abbr")  + '&dist='  
	//var caption = 'Explore';
	break;

default:
	html = '<div><a href=hunt.asp?p=2&c='  + markers[i].getAttribute("country")  + '&dist='  
} 
html = html    + lastdist + '&clat=' + lat + '&clng=' + lng  + '&tdist1=' + tdist1
html = html   + '&tdist2=' + tdist2 + '&tdist3=' + tdist3 + '&tdist4=' + tdist4 + '&tdist5=' + tdist5
html = html+ '>Explore</a></div>'    

 //alert( html)
}
if (lastdist > 0) {
	html = html 	+ '<div>Distance from last stop: ' + lastdist + ' km</div>'
} else {			// if not in game, just show link
    if (game == 1 || game == 3 ) { 
	 html = html  + '<div> <a href=http://cascoly.com/wfb/data/' +  markers[i].getAttribute("abbr")   + '.asp target= "_blank">CIA World Factbook data</a></div>';
	}
}
   

var icType = 0;	
var iconImage = "http://cascoly.com/games/cw/iconb.png";
var marker = createMarker(latlng, html, iconImage, 1); 

map.addOverlay(marker );

 //print markers[i].getAttribute("cName");

}																			//  ----end   if(lng && lat)
} //for 
}					// function


 var newwindow;
function poptastic(url)
{
	newwindow=window.open(url,'name','height=500,width=600,resizeable=1,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}

