(function($){
    var m = window.map = {
        init: function(){
            OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
            OpenLayers.Util.onImageLoadErrorColor = "transparent";
            OpenLayers._getScriptLocation = function(){
                return "http://rs3.adc4gis.com/js/openlayers/2.8/"
            }
            var b = new OpenLayers.Bounds();
            b.extend(new OpenLayers.LonLat(-93.15980851073404,43.755965665039774));
            b.extend(new OpenLayers.LonLat(-90.04198526784404,46.3858687564057));
            b = m.transform(b);
            this.map = new OpenLayers.Map("map",{
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
                units: "m",
                maxExtent: b,
                eventListeners: {
                    "zoomend": m.resetPopups
                },
                maxResolution: 156543.0339,
                theme: base.context+"/openlayers/theme/default/style.css"
            });
            var goptions = {
                'sphericalMercator': true,
                minZoomLevel: 7,
                maxZoomLevel: 17
            };
            var gmap = new OpenLayers.Layer.Google("Street" , goptions);
            goptions.type = G_PHYSICAL_MAP;
            var gphy = new OpenLayers.Layer.Google("Terrain", goptions);
            goptions.type = G_HYBRID_MAP;
            var ghyb = new OpenLayers.Layer.Google("Hybrid", goptions);
            goptions.type = G_SATELLITE_MAP;
            var gsat = new OpenLayers.Layer.Google("Aerial", goptions);
            m.map.addLayers([gmap,gphy,ghyb,gsat]);
            m.map.zoomToExtent(b);

            this.gmlformat = new OpenLayers.Format.GML({
                externalProjection: new OpenLayers.Projection("EPSG:4326"),
                internalProjection: new OpenLayers.Projection("EPSG:900913")
            });
            this.navigation = new OpenLayers.Control.MouseDefaults();
            m.map.addControl(this.navigation);
            this.layers();
        },
        styleMap:  new OpenLayers.StyleMap({
            "default": new OpenLayers.Style({
                fillColor: "#ffff00",
                strokeColor: "#ff0000",
                strokeWidth: 2,
                fillOpacity: 0.4,
                cursor: "pointer"
            }),
            "select": new OpenLayers.Style({
                fillColor: "#66ccff",
                strokeColor: "#3399ff"
            })
        }),
        layers: function(){
            //http://w16.adceval.com/geoserver/wms?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&SRS=EPSG%3A4326&typename=smartt%3AMunic_World
            var cty = this.counties = new OpenLayers.Layer.Vector("GML", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: base.context+"/openlayers/data/counties.jsp",
                    format: m.gmlformat
                }),
                styleMap: m.styleMap,
                maxScale: 0.5e6
            });
            var mun = this.munic = new OpenLayers.Layer.Vector("GML", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: base.context+"/openlayers/data/munic.jsp",
                    format: m.gmlformat
                }),
                styleMap: m.styleMap,
                minScale: 0.5e6
            });

            m.map.addLayers([cty,mun]);
            var hover = this.county_hover = new OpenLayers.Control.SelectFeature(cty, m.select.options);
            m.map.addControl(hover);
            hover.activate();
            
            hover = this.munic_hover = new OpenLayers.Control.SelectFeature(mun, m.select.options);
            m.map.addControl(hover);
            hover.activate();
        },
        resetPopups: function(){
            if(m.feature_timeout) {
                clearTimeout(m.feature_timeout);
            }
            if(m.county_hover) {
                m.county_hover.unselectAll();
            }
            if(m.munic_hover) {
                m.munic_hover.unselectAll();
            }
        },
        select: {
            options: {
                hover: false,
                onSelect: function(feature){
                    var popup = new OpenLayers.Popup.FramedCloud("chicken",
                        feature.geometry.getBounds().getCenterLonLat(),
                        null, m.select.popupHTML(feature),
                        null, true);
                    feature.popup = popup;
                    m.map.addPopup(popup);
                },
                onUnselect: function(feature){
                    m.map.removePopup(feature.popup);
                    feature.popup.destroy();
                    feature.popup = null;
                }
            },
            popupHTML: function(feature){
                var h = "<div><p>";
                h += "<strong>"+feature.data.name+"</strong><br/><br/>";
                h += "2008 Population Projection: <strong>"+feature.data.N3+"</strong><br/>";
                h += "2000 Census: <strong>"+feature.data.N4+"</strong><br/>";
                h += "Growth from 2000 to 2008: <strong>"+feature.data.N8+"%</strong><br/>";
                if(feature.data.N61) {
                    h += "2008 Est. Median Household Income: <strong>$"+this.formatCurrency(feature.data.N61)+"</strong><br/>";
                }
                if(feature.data.N98) {
                    h += "2008 Est. Median Housing Value: <strong>$"+this.formatCurrency(feature.data.N98)+"</strong><br/>";
                }
                if(feature.data.chamber){
                    h += "<a class='a_blue ieblue link' href='"+feature.data.chamber+"' target='_blank'>"+feature.data.chamber+"</a><br/>";
                }
                h += "<br/></p></div>";
                return h;
            },
            formatCurrency: function(str){
                str = str.substring(0, str.length - 3) + "," + str.substring(str.length - 3, str.length);
                return str;
            }
        },
        transform: function(obj){
            obj.transform(new OpenLayers.Projection("EPSG:4269"), new OpenLayers.Projection("EPSG:900913"));
            return obj;
        },
        transformz: function(obj, clone){
            if(obj instanceof OpenLayers.Bounds && clone){
                obj = new OpenLayers.Bounds(obj.left, obj.bottom, obj.right, obj.top);
            }
            obj.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4269"));
            return obj;
        },
        setBase: function(type){
            var l = null;
            var i = m.map.layers.length; while (i--) {
                if(m.map.layers[i].name.toUpperCase() == type.toUpperCase()){
                    l = m.map.layers[i];
                    break;
                }
            }
            if(l) {
                m.map.setBaseLayer(l);
            }
        },
        pan: function(dir){
            switch(dir){
                case "west": m.map.pan(-80,0); break;
                case "east": m.map.pan(80,0); break;
                case "north": m.map.pan(0,-80); break;
                case "south": m.map.pan(0,80); break;
                default: break;
            }
        }
    };
})(jQuery);