var ID_ADDRESS_TXT = 'txt_address';
var ID_SEARCH_BTN = 'btn_search';
var ID_MAP_PH = 'm0';

var _SHOW_QUICK_REPORT_LINKS = false;

var EvalRetailLocation = function(mapid, lat, lon, zoom, locations) {
    this.locations = locations;
    this.popup_html = null;
    EvalRetailLocation._baseClass.call(this, mapid, null, new GLatLng(lat, lon), zoom);
} .inheritFrom(MarketMap).newClass(function(_baseClass) {
    this.initMap = function() {
        this.map = new GMap2($(this.mapid));
        this.map.enableInfoWindow();
        this.map.disableDoubleClickZoom();
        this.map.enableContinuousZoom();
        this.map.disableScrollWheelZoom();
        this.map.addControl(new GLargeMapControl());
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new CellsControl());
        this.map.setCenter(this.start_point, this.start_zoom);

        GEvent.addListener(this.map, 'click', function(ov, p) { MAP_INSTANCE.onMapClick(ov, p); });
        GEvent.addListener(this.map, 'infowindowbeforeclose', function() { MAP_INSTANCE.freeze = false; });
        GEvent.addListener(this.map, 'infowindowopen', function() { MAP_INSTANCE.freeze = true; });

        if (!this.parseURL()) {
            this.mode = MODE_MAP;
            this.showMainMap(true);
        }

        var btn_search = $(ID_SEARCH_BTN);
        if (btn_search) Event.observe(btn_search, 'click', function() { MAP_INSTANCE.onSearch(); });

        textbox_button(ID_ADDRESS_TXT, ID_SEARCH_BTN);
    }

    this.onShowObjects = function(data) {
        _baseClass.onShowObjects.call(this, data);

        if (this.locations) {
            for (var i = 0; i < this.locations.length; i++) {
                var l = this.locations[i];
                var m = new GMarker(new GLatLng(l.Lat, l.Lon));


                var loc = trim(l.Name.replace(/[^a-zA-Z0-9,\-]+/g, ' ').replace(/\s+/g, ' '));
                var url = '/retail-location-details/' + l.Lat + '/' + l.Lon + '/' + escape(loc) + '/' + l.ID + '.aspx?back_url=' + escape(document.location);
                var html = '<h4>' + l.Name + '</h4><ul class="list">'
                    + '<li><a href="' + url + '">see details</a></li>'
                    + '<li><a href="' + url + '#reports">see reports</a></li>';

                if (_SHOW_QUICK_REPORT_LINKS) html += '<li><a href="/brands-gaps/retail-location/' + l.Lat + '/' + l.Lon + '/' + escape(loc) + '/' + l.ID + '.aspx">See Top Brand Report</a></li>'

                html += '</ul>';

                m.bindInfoWindowHtml(html);

                this.map.addOverlay(m);
            }
        }

        if (this.popup_html) {
            this.map.openInfoWindowHtml(this.map.getCenter(), this.popup_html);
            this.popup_html = null;
        }

    }

    this.onMapClick = function(ov, p) {
        if (p) this.showLocation(p, false);
        _baseClass.onMapClick.call(this, ov);
    }

    this.onSearch = function() {
        var txt = $(ID_ADDRESS_TXT);
        if (txt) {
            var g = new GClientGeocoder();
            g.getLatLng(txt.value, function(res) {
                if (res == null) {
                    showModalDialog('Unknown address <strong>"' + txt.value + '"</strong>', MD_OK);
                } else {
                    MAP_INSTANCE.showLocation(res, true);
                }
            });
        }
    }

    this.showLocation = function(pos, zoomin) {
        var g = new GClientGeocoder();
        g.getLocations(pos, function(res) {
            if (res.Status.code == 200) {
                var addr = res.Placemark[0].address;
                var z = MAP_INSTANCE.map.getZoom();
                if (z < 14 && zoomin) z = 14;
                var loc = trim(addr.replace(/[^a-zA-Z0-9,\-]+/g, ' ').replace(/\s+/g, ' '));
                var url = '/retail-location-details/' + pos.lat() + '/' + pos.lng() + '/' + escape(loc) + '.aspx?back_url=' + escape(document.location);
                var html = '<div class="popup_addr"><h4>' + addr + '</h4><ul class="list">'
                    + '<li><a href="' + url + '">see details</a></li>'
                    + '<li><a href="' + url + '#reports">see reports</a></li>';

                if (_SHOW_QUICK_REPORT_LINKS) html += '<li><a href="/brands-gaps/retail-location/' + pos.lat() + '/' + pos.lng() + '/' + escape(loc) + '/0.aspx">See Top Brand Report</a></li>'

                html += '</ul></div>';
                
                MAP_INSTANCE.popup_html = html;
                MAP_INSTANCE.map.setCenter(pos, z);
            }
        });
    }



});



