var map ;
var geocoder ;
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
var continent2;

function convert_to_DMS(degrees, type){

var cp;
if(degrees < 0){
  if(type=="Lat"){
      cp="S";
  } else {
      cp="W";
  }
  degrees = degrees * (-1);
} else {
  if(type=="Lat"){
      cp="N";
  } else {
      cp="E";
  }
}
var m;
var d = Math.floor(degrees);
if(d==0) m=0;
m = Math.floor((degrees%d)*60); 
var s = Math.floor((((degrees%d)*60)%m)*60); 
if(m==0) s=0;

var my_string = type+": "+d.toString() +"º "+ m.toString() + "' "+ s.toString() +"'' "+cp+"\n";

return my_string;
}		

function load() {
	if (GBrowserIsCompatible()) {
		 
		map = new GMap2(document.getElementById("map"));
		geocoder = new GClientGeocoder();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(59.293241,11.106491), 11);

     }
}

//recebe o pais, o ponto, o numero e a lista de regioes
/*
function createMarker(address,point, number,lista) {
var index = number -1 ;
if (number < 9) {
var letter = String.fromCharCode("A".charCodeAt(0) + index);
var icon = new GIcon(baseIcon);
  icon.image = "http://www.google.com/mapfiles/marker.png";
var marker = new GMarker(point,icon);
} else {
var marker = new GMarker(point);
}

			
			GEvent.addListener(marker, "click", function() {

if ( lista != null){	

		changeIndex('produce_countries.php','nav','pais='+address);			fillCountry(lista[address]);
}


marker.openInfoWindowHtml(address+"<br><br>"+convert_to_DMS(point.lat(), "Lat")+"<br>"+convert_to_DMS(point.lng(), "Lng"));
			}
			);
			return marker;
}	*/   

//recebe o pais especifico e a lista continental
function showAddress(address, lista) {

	geocoder.getLatLng(lista[address][lista[address].length - 1],function(point) {
	 
			 	 if (!point) {
	   				//alert(address + " not found");
	   			} else {
				    	map.addOverlay(createMarker(address,point,1,lista));
	    		}	     
	    	}	     
	 	 	);
}

        
   
function getContinent(continent) {
		
if(continent == "Moskva,Russia" )
   continent2="Eurasia";
else if( continent == "Morocco" )
  continent2="North Africa";
else if( continent == "Angola" )
  continent2="Sub-saharan Africa";
else if( continent == "Tokyo, Japan" )
  continent2="East Asia";
else if( continent == "South Carolina, USA" )
  continent2="North America";
else if( continent == "Cali, Colombia" )
  continent2="Central and South America";
else if( continent == "Sydney, Australia" )
  continent2="Australasia";
else if( continent == "Tehran, Iran" )
continent2="Near East";
else if( continent == "Goa, India" )
  continent2="South Asia";
else if( continent == "Tampines,Singapore")
  continent2="Southeast Asia";

		geocoder.getLatLng(continent,function(point) {
	 
	  	if (!point) {
	   	//	alert(continent + " not found");
	   	} else {
	   	    map.setCenter(point, 3);
		}
	    });
	    
}
   
function createMarker2(point, pais1, reg) {

  var marker = new GMarker(point);
  GEvent.addListener(marker, 'click', function() {

  changeIndex('produce_countries.php','nav','pais='+pais1);
  fillCountry3(point);

  });
  return marker;
}


//primeiro é o vector dps o local no mapa
function fillMap(paises,cont) {

load(); 
getContinent(cont);
GDownloadUrl("continent.xml", function(data, responseCode) {
  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");
  for (var i = 0; i < markers.length; i++) {
     if(markers[i].getAttribute("cont")==continent2){
      var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));
      
     //map.addOverlay(new GMarker(point));
     map.addOverlay(createMarker2(point, markers[i].getAttribute("pais"), markers[i].getAttribute("regiao")));
   }

  }
});

}//end fillMap
//-----------

function createMarker4(point, reg, pais) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, 'click', function() {

marker.openInfoWindowHtml(reg+", "+pais+"<br><br>"+convert_to_DMS(point.lat(), 
"Lat")+"<br>"+convert_to_DMS(point.lng(), "Lng"));


  });
  return marker;
}

//------------
function fillCountry3(point){

load();
map.setCenter(point,3);

GDownloadUrl("pais.xml?nocache="+new Date().getTime(), function(data, responseCode) {

  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");

  for (var i = 0; i < markers.length; i++) {

      var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));

      map.addOverlay( createMarker4(point, markers[i].getAttribute("regiao"),
markers[i].getAttribute("pais") ));

  }
});


}

function fillCountry4(){


load();

GDownloadUrl("pais.xml?nocache="+new Date().getTime(), function(data, responseCode) {

  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");

  for (var i = 0; i < markers.length; i++) {
      
      var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));

      if(i==0) { map.setCenter(point,3); }

      map.addOverlay( createMarker4(point, markers[i].getAttribute("regiao"),
markers[i].getAttribute("pais") ));

  }
});

}

function fillCountry2(paises, pais){
   for (var i in paises){
   if(i==pais)    
       fillCountry(paises[i]);
   }

}

function fillCountry(pais){
//imprime(pais);
load();
getContinent(pais[0]);
	for(var i=0;i<pais.length ;i++) {
                  //  alert (pais[i]);
			showRegion(pais[i]);	
		}
}

function showRegion(address) {
	geocoder.getLatLng(address,function(point) {
	 
			 	 if (!point) {
	   				//alert(address + " not found");
	   			} else {
	   	 			
				    map.addOverlay(createMarker(address,point,1,null));
	    		}	     
	    	}	     
	 );
}