if (typeof(google) != 'undefined') {
google.load('maps' , '2');
google.load('search' , '1');
}
(function($) {
       
    $(document).ready(function() {

        /* Maps */
        if (typeof(google) != 'undefined') {
        
        $('.location-map').each(function() {
            var container = $(this);
            var address = container.find(':input.address').val();
            var key = container.find(':input.key').val();
            var geocoder = new GClientGeocoder();
            geocoder.getLocations(
                address,
                function(response) {
                    var place = response.Placemark[0];
                    var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                    var url = 'http://maps.google.com/staticmap?center=' + place.Point.coordinates[1] + ',' + place.Point.coordinates[0] + 
                        '&markers=' + place.Point.coordinates[1] + ',' + place.Point.coordinates[0] + ',blue&zoom=13&size=500x300&key=' + key;
                    
                    container.find('img')
                        .attr('src', url);
                    
                }
            );
            
        });
       $('.map-container').each(function() {
            var container = $(this);
            var query = container.attr('title');
            var mapElement = container.find('.map')[0];
            
            var map = new GMap2(mapElement);
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
           // map.setUIToDefault();
            
            var directionsPanel = container.find('.directions');
            var geocoder = new GClientGeocoder();
            geocoder.getLocations(
              query,
              function(response) {
                if (response.Placemark.length == 0) {
      
                    return;
                }
                var place = response.Placemark[0];
                var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                
                map.setCenter(point, 13);
                var blueIcon = new GIcon(G_DEFAULT_ICON);
                blueIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';
                blueIcon.iconSize = new GSize(32,32);
                var marker = new GMarker(point, {
                    icon: blueIcon 
                });
                map.addOverlay(marker);
                marker.openInfoWindowHtml(place.address);
              });
            
            if (directionsPanel.length == 1) {
                var directions;
                
                var directionsContainer = container.find('.directions-container form').submit(function() {
                    if (directions) {
                        directions.clear();
                    }
                    map.clearOverlays();
                    directions = new GDirections(map, directionsPanel[0]);
                    directions.load('from: ' + $(this).find(':input[name=from]').val() + ' to: ' + query);
                   return false; 
                });
                
                container.find('.directions-container form :input[name=from]').each(function() {
                    if ($(this).val().length > 0) {
                        $(this).closest('form').submit();
                    }
                });
            } else if (container.find('.results').length == 1) {
                  // ...and add a couple of controls.
                map.addControl(new google.maps.SmallMapControl()); // Add a small map control
                map.addControl(new google.maps.MapTypeControl()); // Add the map type control
                
                var searchControl = new google.search.SearchControl();
                
                var searcher = new google.search.LocalSearch();
                searcher.setCenterPoint(query);
         
                var options = new google.search.SearcherOptions();
                options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
                
                searchControl.addSearcher(searcher, options);
                
                // And second, we need is a search complete callback!
                 searchControl.setSearchCompleteCallback(searcher , function() {
                   map.clearOverlays();
                   var results = searcher.results; // Grab the results array
                   // We loop through to get the points
                   for (var i = 0; i < results.length; i++) {
                     var result = results[i]; // Get the specific result
                     var markerLatLng = new google.maps.LatLng(parseFloat(result.lat),
                                                               parseFloat(result.lng));
                     var marker = new google.maps.Marker(markerLatLng); // Create the marker
               
                     // Bind information for the infoWindow aka the map marker popup
                     marker.bindInfoWindow(result.html.cloneNode(true));
                     result.marker = marker; // bind the marker to the result
                     map.addOverlay(marker); // add the marker to the map
                   }
               
                   // Store where the map should be centered
                   var center = searcher.resultViewport.center;
               
                   // Calculate what the zoom level should be
                   var ne = new google.maps.LatLng(searcher.resultViewport.ne.lat,
                                                   searcher.resultViewport.ne.lng);
                   var sw = new google.maps.LatLng(searcher.resultViewport.sw.lat,
                                                   searcher.resultViewport.sw.lng);
                   var bounds = new google.maps.LatLngBounds(sw, ne);
                   var zoom = map.getBoundsZoomLevel(bounds, new google.maps.Size(350, 350));
               
                   // Set the new center of the map
                   // parseFloat converts the lat/lng from a string to a float, which is what
                   // the LatLng constructor takes.
                   map.setCenter(new google.maps.LatLng(parseFloat(center.lat),
                                                        parseFloat(center.lng)),
                                                        zoom);
                 });
               
                 // Draw the control
                 searchControl.draw(container.find('.results')[0]);
               
               /*
                $('form.gsc-search-box').hide();
                $('.gsc-resultsHeader tr:first').hide();
                */
                

                 // Set the map's center point and finish!
                 map.setCenter(new google.maps.LatLng(37.443915 , -122.163610), 11);
               
                // Execute an initial search
                 searchControl.execute(container.find(':input.keywords').val());
              
                
            }
       });
        }
    });
})(jQuery);