// JavaScript Document
/* Usando google maps API V3 */
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var mapa;

/* Incializa o mapa e chama funções necessárias */
function initialize() {
	mapa = criaMapa();
	var marcador = criaMarcador(mapa);
	var infowindow = criaInfoWindow();
	infowindow.open(mapa,marcador);
	/* Direção  - Como chegar */
	/* Opções dos marcadores da rota */
	directionsDisplay = new google.maps.DirectionsRenderer({
		suppressInfoWindows: true,
		suppressMarkers: true,
		preserveViewport: true
	});
	directionsDisplay.setMap(mapa);
	/* Evento click no marcador */
	google.maps.event.addListener(marcador, 'click', function() {
		infowindow.open(mapa,marcador);
	});
	calculaRota();
	/* Centraliza o mapa */
}

/* Cria e renderiza o mapa */
function criaMapa(){
	// var latlng = new google.maps.LatLng(-27.358889, -53.393889);
	var latlng = new google.maps.LatLng(-27.35670,-53.393350);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("containerMapa"), myOptions);
	return map;
}

/* Cria o balão marcador */
function criaMarcador(mapa){
	var latlng = new google.maps.LatLng(-27.355988,-53.396409);
	var marker = new google.maps.Marker({
		position: latlng,
		map: mapa,
		title:"Digifred Sistemas e Consultoria"
	});
	return marker;
}

/* Cria info window com logotipo */
function criaInfoWindow(mapa){
	var contentString = '<img src="imagens/logo.png" alt="logo" />';
	var infowindow = new google.maps.InfoWindow({
		content: contentString,
		disableAutoPan: true
	});
	return infowindow;
}

/* Exibe a rota */
function calculaRota() {
	var latlngOrigem  = new google.maps.LatLng(-27.361624,-53.389503);
	var latlngDestino = new google.maps.LatLng(-27.355834,-53.396442);
	var waypts = []
	waypts.push({
		location : new google.maps.LatLng(-27.357929,-53.398224),
		stopover: false
	});
	var request = {
		origin:latlngOrigem, 
		destination:latlngDestino,
		waypoints: waypts,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
	});
}
