(function($){
    var map = window.gmap = {
        init: function(o) {
            if (GBrowserIsCompatible()) {
                this.allowedBounds = new GLatLngBounds(new GLatLng(43.8109,-92.4995), new GLatLng(45.8109,-90.4995));
                this.data = o.data;

                var gmap = this.gmap = new GMap2(document.getElementById("map"));
                gmap.addControl(new GLargeMapControl());
                gmap.addControl(new GMapTypeControl());
                gmap.enableContinuousZoom();
                gmap.enableScrollWheelZoom();
                gmap.setCenter(new GLatLng(44.8109,-91.4995), o.initZoomLevel || 7);
                
                var i = o.data.length; while(i--){
                    o.data[i] = new GGeoXml(o.data[i]);
                    gmap.addOverlay(o.data[i]);
                }

                var mt = map.gmap.getMapTypes();
                for (var z=0; z<mt.length; z++) {
                    mt[z].getMinimumResolution = function() {
                        return 7;
                    };
                    mt[z].getMaximumResolution = function() {
                        return 16;
                    };
                }
                GEvent.addListener(map.gmap, "move", function() {
                    //map.checkBounds();
                    });
                GEvent.addListener(map.gmap, "zoomend", function() {
                    if(o.searchWords) {
                        $("div.gmls input.gsc-search-button").click();
                    }
                });
                GEvent.addListener(map.gmap, "dragend", function() {
                    if(o.searchWords) {
                        $("div.gmls input.gsc-search-button").click();
                    }
                });

                
                if(o.addLocal){
                    map.gmap.addControl(new google.maps.LocalSearch({
                        suppressZoomToBounds: true,
                        //resultList : google.maps.LocalSearch.RESULT_LIST_SUPPRESS,
                        suppressInitialResultSelection : true
                    }), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
                    if(o.searchWords){
                        $("#map").addClass("searchWords");
                        $("div.gmls input.gsc-input").val(o.searchWords).attr("disabled","true");
                        $("div.gmls-clear-results").attr("width","0");
                        $("div.gmls input.gsc-search-button").click();
                    }
                }
                if(o.addPhotos) {
                    this.photos = new GLayer("com.panoramio.all");
                    map.gmap.addOverlay(this.photos);
                }
                if(o.addWiki) {
                    this.wiki = new GLayer("org.wikipedia.en");
                    map.gmap.addOverlay(this.wiki);
                }
            }else {
                alert("Sorry, the Google Maps API is not compatible with this browser");
            }
        },
        checkBounds: function () {
            if (map.allowedBounds.contains(map.gmap.getCenter())) {
                return;
            }
            var C = map.gmap.getCenter(), X = C.lng(), Y = C.lat();
            var AmaxX = map.allowedBounds.getNorthEast().lng();
            var AmaxY = map.allowedBounds.getNorthEast().lat();
            var AminX = map.allowedBounds.getSouthWest().lng();
            var AminY = map.allowedBounds.getSouthWest().lat();

            if (X < AminX) {
                X = AminX;
            }
            if (X > AmaxX) {
                X = AmaxX;
            }
            if (Y < AminY)  {
                Y = AminY;
            }
            if (Y > AmaxY)  {
                Y = AmaxY;
            }
            map.gmap.setCenter(new GLatLng(Y,X));
        },
        toggle: function(el) {
            if(el.checked){
                if(this.photos) {
                    this.photos.show();
                }
                if(this.wiki) {
                    this.wiki.show();
                }
            } else {
                if(this.photos) {
                    this.photos.hide();
                }
                if(this.wiki) {
                    this.wiki.hide();
                }
            }
        }
    };
})(jQuery);