var maps ={
    handleNoGeolocation : function(errorFlag) {
        if (errorFlag == true) {
            alert("Geolocation service failed.");
            initialLocation = nunoa;
        } else {
            alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
            initialLocation = suecia;
        }
        maps.map.setCenter(initialLocation);
    },
    
    showSteps : function(directionResult) {
        // For each step, place a marker, and add the text to the marker's
        // info window. Also attach the marker to an array so we
        // can keep track of it and remove it when calculating new
        // routes.

        var myRoute = directionResult.routes[0].legs[0];
        for (var i = 0; i < myRoute.steps.length; i++) {
            var marker = new google.maps.Marker({
                position: myRoute.steps[i].start_point, 
                map: maps.map
            });
            maps.attachInstructionText(marker, myRoute.steps[i].instructions);
            markerArray[i] = marker;
        }
        
    },
    attachInstructionText : function(marker, text) {
        stepDisplay = new google.maps.InfoWindow();
        google.maps.event.addListener(marker, 'click', function() {
            stepDisplay.setContent(text);
            stepDisplay.open(maps.map, marker);
        });
    },
    gmaps: function(e,i){
        maps.map="";
        var directionDisplay;
        var directionsService;
        var stepDisplay;
        var markerArray = [];
        var initialLocation;
        var browserSupportFlag =  new Boolean();

        directionsService = new google.maps.DirectionsService();                    

        var nunoa = new google.maps.LatLng(-33.447541,-70.60644);
        var suecia = new google.maps.LatLng(-33.421483,-70.607582);
       
        var mapsOptions = {
            zoom: 12,
            center: nunoa,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        maps.map = new google.maps.Map(document.getElementById("gmapshtml5"), mapsOptions);   
        
        var rendererOptions = {
            map: maps.map
        }
        directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

        // Instantiate an info window to hold step text.


        if(navigator.geolocation) {
            browserSupportFlag = true;
            navigator.geolocation.getCurrentPosition(
                function(position) {
        
                    // First, clear out any existing markerArray
                    // from previous calculations.
                    for (i = 0; i < markerArray.length; i++) {
                        markerArray[i].setMap(null);
                    }
                    initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                    
                    var start = initialLocation;
                    var end = "Avenida suecia, 84. Providencia";
                    var request = {
                        origin: start,
                        destination: end,
                        travelMode: google.maps.DirectionsTravelMode.DRIVING
                    };

                    // Route the directions and pass the response to a
                    // function to create markers for each step.
                    directionsService.route(request, function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                            //maps.showSteps(response);                            
                            var myRoute = response.routes[0].legs[0];
                            for (var i = 0; i < myRoute.steps.length; i++) {
                                var marker = new google.maps.Marker({
                                    position: myRoute.steps[i].start_point, 
                                    map: maps.map
                                });
                                maps.attachInstructionText(marker, myRoute.steps[i].instructions);
                                markerArray[i] = marker;
                            }
                        }
                    });
                    
                    maps.map.setCenter(initialLocation);
                }, function() {
                    maps.handleNoGeolocation(browserSupportFlag);
                });
        // Try Google Gears Geolocation
        }else {
            browserSupportFlag = false;
            maps.handleNoGeolocation(browserSupportFlag);
        }
    }
}
