
var map = (function () {
    var mapEvent = {
        recenterOrPanToLatLng : function(mmap) {
            MEvent.addListener(mmap, "click", function(e1, e2) {
                mmap.recenterOrPanToLatLng(e2);
            });
            return mmap;
        },
        recenterToRestaurant : function(mmap, props) {
            MEvent.addListener(mmap, "rclick", function() {
                mmap.recenterOrPanToLatLng(new MPoint(props.lon, props.lat));
                // mmap.centerAtLatLng(new MPoint(props.lon, props.lat));
            });
            return mmap;
        },
        storeZoomLevel : function(mmap) {
            MEvent.addListener(mmap, "zoom", function(n) {
                var mainMap = document.getElementById("div-mainmap");
                var zm = mmap.getZoomLevel();
                if (mainMap.mZM < zm) {
                    mainMap.cZM = mainMap.mZM;
                    mmap.zoomTo(mainMap.mZM);
                } else {
                    mainMap.cZM = zm;
                }
            });
            return mmap;
        },
        wheelZoomTo : function(mmap) {
            MEvent.addListener(mmap, "wheel", function(n) {
                var mainMap = document.getElementById("div-mainmap");
                var zm = mmap.getZoomLevel();
                var _zm = zm + n;
                if (_zm <= mainMap.mZM && _zm >= 0) {
                    mmap.zoomTo(_zm);
                    mainMap.cZM = _zm;
                }
            });
            return mmap;
        },
        moveIng : function(mmap, ctrlObj) {
            MEvent.addListener(mmap, "move", function() {
                mmap.removeControl(ctrlObj);
            });
            return mmap;
        },
        moveEnd : function(mmap, ctrlObj) {
            MEvent.addListener(mmap, "moveend", function() {
                mmap.addControl(ctrlObj);
            });
            return mmap;
        },
        autoUpdate : function(mmap, ctrlObj) {
            MEvent.addListener(mmap, "moveend", function () {
                 ctrlObj.update(mmap.getCenterLatLng());
            });
            ctrlObj.update(mmap.getCenterLatLng());
            return mmap;
        }
    };
    var markerEvent = {
        openInfoWindowHtml : function(mmap, marker, html) {
            MEvent.addListener(marker, "click", function(e) {
                // adjust popup.
                mmap.openInfoWindowHtml(e.rpos, html, new MSize(0,18), null, null);
                //e.openInfoWindowHtml(html);
                setTimeout(redrawFuckinInfoWindow, 50);
                function redrawFuckinInfoWindow() {
                    mmap.infowindow.draw();
                }
            });
            return marker;
        }
    };
    var createInfoHtml = function (item) {
        var html = [];
        if (item.image) html.push('<p class="image"><img src="', item.image, '" width="75" height="75" /></p>');
        html.push( '<p class="name"><a href="', item.url, '">', item.name, '</a></p>');
        if (item.stars) html.push('<p class="name"><img src="', item.stars.image, '" width="64" height="11" /> ', item.stars.pt, '</p>');
        html.push(
            '<p class="tel">', item.tel, '</p>',
            '<p class="address">', item.address, '</p>'
        );
        return ['<div class="infoHtml">', html.join(""), '</div>'].join("");
    };
    var createIcon = function (item) {
        var ic = new MIcon();
        ic.width  = 30;
        ic.height = 36;
        ic.iconAnchor = new MPoint(10,30); // visual judgement...
        ic.image = item.icon;
        return ic;
    };

    return {
        init : function (args) {
            var mainMap = document.getElementById('div-mainmap');
            this.mmap = new MMap( mainMap );
            this.lon = args['lon'];
            this.lat = args['lat'];
            this.setZoom = args['zoom'];
            this.maxZoom = args['max_zoom'];
            this.minZoom = args['min_zoom'];
            mainMap.cZM = this.setZoom;
            mainMap.mZM = this.maxZoom;
            this.mmap.autoScroll = true;
        },
        addItem : function (items) {
            this.mmap.clearOverlays();
            for (var i = 0; i < items.length; i++) {
                var ic = createIcon(items[i]);
                var mk = new MMarker( new MPoint(items[i].lon_deg, items[i].lat_deg), ic );
                markerEvent.openInfoWindowHtml(this.mmap, mk, createInfoHtml(items[i]));
                this.mmap.addOverlay(mk);
            }
        },
        drawMap : function () {
            // controll
            this.mmap.centerAndZoom( new MPoint(this.lon, this.lat), this.setZoom );
            var ctrl = new MSliderMapionControl();
            ctrl.f_closed = true;
            this.mmap.addControl(ctrl);
            mapEvent.autoUpdate(this.mmap, ctrl);
            var scl = new MScaleControl();
            this.mmap.addControl(scl, new MControlPosition(M_ANCHOR_BOTTOM_RIGHT, "5", "5"));

            // event
            mapEvent.recenterOrPanToLatLng(this.mmap);
            mapEvent.recenterToRestaurant(this.mmap, this);
            mapEvent.storeZoomLevel(this.mmap);
            // mapEvent.wheelZoomTo(this.mmap);
            // mapEvent.moveIng(this.mmap, ctrl);
            // mapEvent.moveEnd(this.mmap, ctrl);
        }
    }
})();

