var map;

var baseIcon				= new GIcon();
baseIcon.shadow				= "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize			= new GSize(30, 34);
baseIcon.shadowSize			= new GSize(37, 34);
baseIcon.iconAnchor			= new GPoint(15, 34);
baseIcon.infoWindowAnchor	= new GPoint(9, 2);
baseIcon.infoShadowAnchor	= new GPoint(18, 25);

// Creates a marker whose info window displays the letter corresponding to
// the given index
function createMarker(point, index, $frase, $icone) {
	// Create a lettered icon for this point using our icon class from above
	var letter = String.fromCharCode("A".charCodeAt(0) + index);
	var icon = new GIcon(baseIcon);

	if($icone.length > 0){
		icon.image = "icons/" + $icone + ".png";
	}else{
		icon.image = "icons/ponto.png";
	}

	var marker = new GMarker(point, icon);

	// Show this marker's index in the info window when it is clicked
	//var html = "Letra <strong>" + letter + "</strong>";
	GEvent.addListener(marker, "click", function() {
		//marker.openInfoWindowHtml(html);
		marker.openInfoWindowHtml($frase);
	});

	return marker;
}



function oMapa(){

	//
	map = new GMap(document.getElementById("map"), [G_SATELLITE_TYPE]);

	//map.addControl(new GSmallMapControl());	//Adiciona o controle de mapa Pequeno
	//map.addControl(new GSmallZoomControl());	//Adiciona o controle de zoom Pequeno, só de ZOOM
	map.addControl(new GMapTypeControl());	//Adiciona o controle de tipo
	map.addControl(new GLargeMapControl());

	GEvent.addListener(map, "moveend", function() {
	  var center = map.getCenterLatLng();

	  //var latLngStr = '(' + center.x + ', ' + center.y + ')';
	  //document.getElementById("message").innerHTML = latLngStr;

	  document.getElementById("centerX").value = center.x;
	  document.getElementById("centerY").value = center.y;
	});

	$csa  = new GPoint(-46.639355421066284, -23.56836327603561);

	map.centerAndZoom($csa, 1);

	var $Item = new Array();
	$Item[0] = document.getElementById("pColegio");
	$Item[1] = document.getElementById("pMetroVergueiro");
	$Item[2] = document.getElementById("pCentroCultural");
	$Item[3] = document.getElementById("pav23demaio");
	$Item[4] = document.getElementById("pHospitalBeneficencia");
	$Item[5] = document.getElementById("pMetroParaiso");
	$Item[6] = document.getElementById("pPonteDoParaiso");
	$Item[7] = document.getElementById("pMetroAnaRosa");
	$Item[8] = document.getElementById("pShoppingPaulista");
	$Item[9] = document.getElementById("pMercadoExtra");


	for($i=0; $i<$Item.length; $i++){

		// Criando o Ponto
		$x = parseFloat($Item[$i].childNodes[0].value);
		$y = parseFloat($Item[$i].childNodes[1].value);
		$Ponto = new GPoint($x, $y);

		//Nome da imagem fica no 3o nó
		$icone = $Item[$i].childNodes[2].value;

		//Criando um marcador
		$marcador = createMarker($Ponto, $i, $Item[$i].innerHTML, $icone);

		map.addOverlay($marcador);
		
	}

}

function Posicionar(){
	try{
		$x = parseFloat(document.getElementById("centerX").value);
		$y = parseFloat(document.getElementById("centerY").value);

		$csa  = new GPoint($x, $y);
		map.centerAndZoom($csa, 0);

	}catch(e){
		alert('coloque apenas números válidos');
	}
}

function Posicionar2($Elemento){
	try{
		$x = parseFloat(document.getElementById($Elemento).childNodes[0].value);
		$y = parseFloat(document.getElementById($Elemento).childNodes[1].value);

		$csa  = new GPoint($x, $y);
		map.centerAndZoom($csa, 0);

	}catch(e){
		alert('coloque apenas números válidos');
	}
}

function CriarRota1(){
	
	try{

		$points  = [];

		$points.push(new GPoint(-46.6389799118042, -23.56888447041363));
		$points.push(new GPoint(-46.63870096206665, -23.568894304249927));
		$points.push(new GPoint(-46.63876533508301, -23.568441947017654));
		$points.push(new GPoint(-46.63801431655884, -23.567547061816374));
		$points.push(new GPoint(-46.63650155067444, -23.569002476400737));
		$points.push(new GPoint(-46.63896918296814, -23.570841389333086));
		$points.push(new GPoint(-46.639055013656616, -23.571205235364612));

		$polyline = new GPolyline($points, "#ff0000", 10);

		map.addOverlay($polyline);

	}catch(e){
		alert(e.message);
	}


}



