window.console = window.console ? window.console : {log: function(m) { alert(m); }};

(function($) {
    var LocationPicker = function($input) {
        this.$input = $input;

        this.lat = 41.89485825537461;
        this.lng = -87.63638377189636;
        
        var raw_value = this.$input.val()
        if(raw_value) {
            this.lat = parseFloat(raw_value.split(',')[0]);
            this.lng = parseFloat(raw_value.split(',')[1]);
        } else {
            this.$input.val(this.lat + ',' + this.lng);
        }
        
        this.rewriteMarkup();
        this.setupMapWidget();
        this.setupAddressEntry();
    }

    LocationPicker.prototype.rewriteMarkup = function() {
        this.$map = $('<div></div>');
        this.$map.addClass('google-map');
        this.$map.attr('id', this.$input.attr('id') + '-map');
        this.$map.css('width', '303px');
        this.$map.css('height', '200px');        
        this.map_id = this.$map.attr('id');
        this.$input.before(this.$map);
        this.$input.hide();
    }

    LocationPicker.prototype.setupMapWidget = function() {
        var lat_lng = new google.maps.LatLng(this.lat, this.lng);
        var map_options = {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            zoomControl: true,
            center: lat_lng
        };
        
        this.map = new google.maps.Map(document.getElementById(this.map_id), map_options);
        this.geocoder = new google.maps.Geocoder();

        this.marker = new google.maps.Marker({
            position: lat_lng,
            map: this.map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            title: "Here!"
        });

        var self = this;
        
        google.maps.event.addListener(this.marker, 'mouseup', function() {
            self.updateLocationValue();
        });
    }

    LocationPicker.prototype.updateLocationValue = function() {
        var new_lat_lng_val = this.marker.position.lat() + "," + this.marker.position.lng();
        this.$input.val(new_lat_lng_val);
    }

    LocationPicker.prototype.setupAddressEntry = function() {
        var $address_entry_container = $('<div></div>');
        var $address_entry_input = $('<input type="text" class="address-entry-input" />');
        var $address_entry_button = $('<button class="address-entry-go">Search</button>');
        $address_entry_container.append($address_entry_input);
        $address_entry_container.append($address_entry_button);
        this.$map.before($address_entry_container);

        var self = this;
        
        $address_entry_button.click(function() {
            self.geocodeAddress($address_entry_input.val());
            return false;
        });

        $address_entry_input.keyup(function(e) {
            if(e.keyCode == 13) {
                self.geocodeAddress($address_entry_input.val());
                return false;
            }
        });
    }

    LocationPicker.prototype.geocodeAddress = function(address) {
        var self = this;
        
        this.geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                self.map.setCenter(results[0].geometry.location);
                self.marker.position = results[0].geometry.location;
                self.updateLocationValue();
            } else {
                console.log("Geocode was not successful for the following reason: " + status);
            }
        });
    }
    
    // Init
    $(function() {
        $('.location_picker').each(function() {
            new LocationPicker($(this));
        });

    });
})(jQuery)
