	var ARENA_LAT = -29.974587;
	var ARENA_LNG = -51.19321;
	var OLIMPICO_LAT = -30.060926;
	var OLIMPICO_LNG = -51.213786;
	var directionDisplay;
	var geocoder;	
	var userPositionMarker = new google.maps.Marker();
	var directionsService = new google.maps.DirectionsService();
	var map;
	var strictBounds;
		
  function initialize() {
  	
  	
  	//CONFIGURAÇAO DO MAPA
  	//CONFIGURAÇAO DO MAPA
  	//CONFIGURAÇAO DO MAPA
	directionsDisplay = new google.maps.DirectionsRenderer();
	var useDragControll;
	var useScale;
	if (/iPad|iPhone/.test(navigator.userAgent)) {
		useDragControll = false;
		useScale = false;
	}
    var myOptions = {
      zoom: 12,
      maxZoom:16,
      minZoom:11,
	  scrollwheel:false,
	  draggable:useDragControll,
	  scaleControl:useScale,
      streetViewControl:false,
      center: new google.maps.LatLng(-30.018926, -51.214786),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
	
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
								  
								  
	map.language = "pt-BR";
								  
	directionsDisplay.setMap(map);
	
	directionsDisplay.setPanel(document.getElementById("directions_panel"));
	
	geocoder = new google.maps.Geocoder();
	
	
	strictBounds = new google.maps.LatLngBounds(
     	new google.maps.LatLng(OLIMPICO_LAT-0.35, OLIMPICO_LNG-0.35), 
     	new google.maps.LatLng(OLIMPICO_LAT+0.35, OLIMPICO_LNG+0.35)
   		);

	google.maps.event.addListener(map, 'dragend', onMapMoved);
	//__
	//__
	//__
	
	
	//IMAGENS DOS PINS
	var imageArena = 'public/imgs/arena/others/logo_arena_a.png';
	var imageOlimpico = 'public/imgs/arena/others/brasao_gremio_a.png';
	
	//PIN DO OLIMPICO
    var myLatLng = new google.maps.LatLng(OLIMPICO_LAT, OLIMPICO_LNG);
    var olimpico = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: imageOlimpico
    });
	
	//PIN DA ARENA
	myLatLng = new google.maps.LatLng(ARENA_LAT, ARENA_LNG);
    var arena = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: imageArena
    });
	
	userPositionMarker = new google.maps.Marker();//{map: map,position: myLatLng}
	userPositionMarker.setMap(map);
		
		
	//PEDE PARA CALCULAR O CAMINHO PADRÃO	
	var pini = new google.maps.LatLng(OLIMPICO_LAT, OLIMPICO_LNG);
	var pend = new google.maps.LatLng(ARENA_LAT, ARENA_LNG);
	
	var request = {
        origin: pini, 
        destination: pend,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
	directionsDisplay.suppressMarkers = true;
	directionsDisplay.suppressPolylines = true;
    directionsService.route(request, onDirectionsLoaded);
  }
  
  function onMapMoved(){
  	if (strictBounds.contains(map.getCenter())) return;

     // We're out of bounds - Move the map back within the bounds

     var c = map.getCenter(),
         x = c.lng(),
         y = c.lat(),
         maxX = strictBounds.getNorthEast().lng(),
         maxY = strictBounds.getNorthEast().lat(),
         minX = strictBounds.getSouthWest().lng(),
         minY = strictBounds.getSouthWest().lat();

     if (x < minX) x = minX;
     if (x > maxX) x = maxX;
     if (y < minY) y = minY;
     if (y > maxY) y = maxY;

     map.setCenter(new google.maps.LatLng(y, x));

  }
  
  
  ////ON DIRECTIONS LOADED
  ////ON DIRECTIONS LOADED
  ////ON DIRECTIONS LOADED
  function onDirectionsLoaded(response, status){
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			var route = response.routes[0];
			
		}else{alert(status);};
		
		var legs = response.routes[0].legs;

        for (var leg = 0; leg < legs.length; leg++) {            	
			for (var step = 0; step < legs[leg].steps.length; step++) {
				if (legs[leg].steps[step].lat_lngs) {                        	
					var polylineOptions = {
                                map: map,
                                strokeColor: "#0099FF",
                                strokeOpacity: 0.9,
                                strokeWeight: 4,
                                geodesic:true,
                                zIndex:2,
                                path: legs[leg].steps[step].lat_lngs
                            }
                            
					new google.maps.Polyline(polylineOptions);
				}
			}
		}
	}
	
	function onDirectionsLoadedUser(response, status){
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			var route = response.routes[0];
			
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
			}             
		}else{alert(status);};
	}
	
	////ON ADRESS GEOCODED
	////ON ADRESS GEOCODED
	////ON ADRESS GEOCODED
	function adressGeocoded(results, status){

		if (status == google.maps.GeocoderStatus.OK) {
			var latLng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
			setMarker(latLng);
		} else {
    	    alert("Geocode was not successful for the following reason: " + status);
    	}
	}
	
	function setMarker(latLng){
		userPositionMarker.setPosition(latLng);
		
		
		//PEDE PARA CALCULAR O CAMINHO PADRÃO	
		var pini = latLng;
		var pend = new google.maps.LatLng(ARENA_LAT, ARENA_LNG);
	
		var request = {
    	    origin: pini, 
    	    destination: pend,
    	    optimizeWaypoints: true,
    	    travelMode: google.maps.DirectionsTravelMode.DRIVING
    	};
		directionsDisplay.suppressMarkers = true;
    	directionsService.route(request, onDirectionsLoadedUser);
	}
	
	function mensagem(){
		var campo = document.getElementById("txt").value;
		geocoder.geocode( { 'address': campo}, adressGeocoded);
		
	}
