//<![CDATA[
var map = null;
var marker = null;

//*****************************************************************************
// Initialize Google Map
//*****************************************************************************
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));		
    map.setCenter(new GLatLng(40.0, -8.0), 1);
	
	var customUI = map.getDefaultUI();
	customUI.controls.scalecontrol = true;
	customUI.maptypes.satellite = true;
	customUI.maptypes.hybrid = true;
	customUI.maptypes.physical = true;
	customUI.zoom.scrollwheel = true;
	map.setUI(customUI); 
								
  }
}

//*****************************************************************************
// Initialize Google Map
//*****************************************************************************
function initializeSimple() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));		
    //map.setCenter(new GLatLng(40.0, -8.0), 1);
	
	var customUI = map.getDefaultUI();
	customUI.controls.scalecontrol = false;
	customUI.controls.maptypecontrol = false;
	customUI.maptypes.satellite = false;
	customUI.maptypes.hybrid = false;
	customUI.maptypes.physical = false;
	customUI.zoom.scrollwheel = true;
	//map.setUI(customUI); 
								
  }
}

//*****************************************************************************
// Criar marker
//*****************************************************************************
function createMarker(point, candrag) {
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.image = "/themes/img/site/balloonmarkercyan_si_p.png";
	baseIcon.shadow = "/themes/img/site/balloonmarkershadow_si.png";
	baseIcon.iconSize = new GSize(22, 27);
	baseIcon.shadowSize = new GSize(38, 27);
	baseIcon.iconAnchor = new GPoint(19, 26); //GPoint(13, 18);
	baseIcon.infoWindowAnchor = new GPoint(13, 18);
	markerOptions = {icon:baseIcon, draggable:candrag};	
	var marker = new GMarker(point, markerOptions);	
	return marker;
}

//*****************************************************************************
// Adicionar listener - baloon
//*****************************************************************************
function addListenerBaloon(mk, text){
	GEvent.addListener(mk, "click", function() {
		mk.openInfoWindowHtml(text);
	});
}

//*****************************************************************************
// Adicionar listener - centrar mapa
//*****************************************************************************
function addListenerDragendCenterMap(mk, mp){
		GEvent.addListener(mk, "dragend", function() {
		var point = mk.getLatLng();
		var zm = mp.getZoom();		
		if (zm < 6){
			mp.setCenter(point,6); 
		}
		else{
			mp.setCenter(point);
		}
	});
}


//*****************************************************************************
// Adicionar listener - escreve coords
//*****************************************************************************
function addListenerDragendGetCoords(mk){
		GEvent.addListener(mk, "dragend", function() {
		var point = mk.getLatLng();
		computepos(point);
	});
}


//*****************************************************************************
// function computepos (point)
//*****************************************************************************
function computepos (point){
	var latA = Math.abs(Math.round(value=point.y * 1000000.));
	var lonA = Math.abs(Math.round(value=point.x * 1000000.));
	if(value=point.y < 0){
		var ls = '-' + Math.floor((latA / 1000000));
	}
	else{
		var ls = Math.floor((latA / 1000000));
	}

	var lm = Math.floor(((latA/1000000) - Math.floor(latA/1000000)) * 60);
	var ld = ( Math.floor(((((latA/1000000) - Math.floor(latA/1000000)) * 60) - Math.floor(((latA/1000000) - Math.floor(latA/1000000)) * 60)) * 100000) *60/100000 );

	if(value=point.x < 0){
  	var lgs = '-' + Math.floor((lonA / 1000000));
	}
	else{
		var lgs = Math.floor((lonA / 1000000));
	}

	var lgm = Math.floor(((lonA/1000000) - Math.floor(lonA/1000000)) * 60);
	var lgd = ( Math.floor(((((lonA/1000000) - Math.floor(lonA/1000000)) * 60) - Math.floor(((lonA/1000000) - Math.floor(lonA/1000000)) * 60)) * 100000) *60/100000 );

	document.getElementById("latbox").value=point.y;
	//document.getElementById("latboxm").value=ls;
	//document.getElementById("latboxmd").value=lm;
	//document.getElementById("latboxms").value=ld;

	document.getElementById("lonbox").value=point.x;
	//document.getElementById("lonboxm").value=lgs;
	//document.getElementById("lonboxmd").value=lgm;
	//document.getElementById("lonboxms").value=lgd;
} //function computepos (point)


//*****************************************************************************
// function showAddress()
//*****************************************************************************
// ====== Geocoding ======
function showAddress() {
  	var search = document.getElementById("CoutrySearch").value;
		var latValue = document.getElementById("lat").value;
		var lonValue = document.getElementById("lon").value;
		
//		if (latValue=="" || lonValue==""){
		// ====== Perform the Geocoding ======        
    geo.getLocations(search, function (result)
		{ 
			// If that was successful
      if (result.Status.code == G_GEO_SUCCESS) {
				// How many resuts were found
        // Loop through the results, placing markers
        //for (var i=0; i<result.Placemark.length; i++) {
        	var p = result.Placemark[0].Point.coordinates;
          //marker = new GMarker(new GLatLng(p[1],p[0]));
					marker = new GMarker(new GLatLng(p[1],p[0]), {draggable: true});
          map.addOverlay(marker);
					
					GEvent.addListener(marker, "dragend", function() {
						var point = marker.getLatLng();
						posset = 1;
						if (zm == 0){
							map.setCenter(point,5); zm = 5;}
						else{
							map.setCenter(point);
						}
						computepos(point);
					});
				//}fOR
				
        // centre the map on the first result
        var p = result.Placemark[0].Point.coordinates;
        map.setCenter(new GLatLng(p[1],p[0]),4);
			}
      // ====== Decode the error status ======
      else {
      	var reason="Code "+result.Status.code;
      	if (reasons[result.Status.code]) {
        	reason = reasons[result.Status.code]
				} 
        alert('Could not find "'+search+ '" ' + reason);
			}			
		});//geo.getLocations(search, function (result)			
/*
		}
		else{
		
		}
*/
		
}//function showAddress()


//*****************************************************************************
// function moveMarker()
//*****************************************************************************
function moveMarker(){
		lt = document.getElementById("latbox").value;
		lg = document.getElementById("lonbox").value;
		if (lt == "" || lg == "") {
			//alert("Sorry, you must enter both values for Latitude and Longitude");	
		}
		else {
			marker.setPoint(new GLatLng(lt,lg));
			map.setCenter(new GLatLng(lt,lg));
		}
}


//*****************************************************************************
// function moveMarker(lt,lg,zm)
//*****************************************************************************
function moveMarker(lt,lg,zm){
		if (lt == "" || lg == "") {
			//alert("Sorry, you must enter both values for Latitude and Longitude");	
		}
		else {
			marker.setPoint(new GLatLng(lt,lg));
			map.setCenter(new GLatLng(lt,lg),zm);
		}
}


//*****************************************************************************
// function moveMarker(lt,lg,zm)
//*****************************************************************************
function addMarker(lt,lg){
		if (lt != "" && lg != "") {
			marker = new GMarker(new GLatLng(lt,lg), {draggable: false});
      map.addOverlay(marker);
		}
}//function addMarker(lt,lg)


//*****************************************************************************
// function centrar mapa
//*****************************************************************************
function mapSetCenter(lt,lg,zm){
	map.setCenter(new GLatLng(lt,lg),zm);
}
//]]>
