var map; //map namespace
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
OpenLayers.Util.onImageLoadErrorColor = "transparent";

(function($){
    m = window.map = {
        init: function(){
            this.gmlformat = new OpenLayers.Format.GML({
                externalProjection: new OpenLayers.Projection("EPSG:4326"),
                internalProjection: new OpenLayers.Projection("EPSG:900913")
            })
            this.gmlformat2 = new OpenLayers.Format.GML({
                externalProjection: new OpenLayers.Projection("EPSG:4326"),
                internalProjection: new OpenLayers.Projection("EPSG:900913"),
                extractAttributes: false
            })
            this.usBounds = new OpenLayers.Bounds(-14392000, 2436200, -7279500, 6594375);
            m.setupMap();
            m.setupMap2();
        },
        setupMap: function(){
            var options = {
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
                units: "m",
                maxResolution: 156543.0339,
                maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508),
                controls: [
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoom()
                ],
                numZoomLevels: 19
            }
            this.map = new OpenLayers.Map("map",options);
            var gmap = new OpenLayers.Layer.Google("Street" , {
                'sphericalMercator': true,
                type: G_PHYSICAL_MAP
            });

            m.map.addLayers([gmap]);

            m.map.zoomToExtent(this.usBounds);
            
            var smap = new OpenLayers.StyleMap({
                "default": new OpenLayers.Style({
                    externalGraphic: "/images/openlayers/orange-dot.png",
                    //graphicXOffset: -33,
                    //graphicYOffset: -18,
                    pointRadius: 13,
                    cursor: "pointer"
                }),
                "select": {
                    //externalGraphic: "/media/openlayers/pin_purple.png",
                    pointRadius: 15
                }
            });

            var l = new OpenLayers.Layer.Vector("Clients", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                projection: m.map.projection,
                protocol: new OpenLayers.Protocol.HTTP({
                    format: m.gmlformat,
                    url: "images/clients.php"
                }),
                styleMap: smap
            });

            m.map.addLayer(l);

            var currentpopup;

            this.cluster_hover = new OpenLayers.Control.SelectFeature(
                l, {
                    hover: false,
                    onSelect: function(feature,click){
                        if(currentpopup){
                            m.map.removePopup(currentpopup);
                            currentpopup.destroy();
                            currentpopup = null;
                        }
                        
                        
                        var size = feature.geometry.getBounds().getSize();
                        var popup = new OpenLayers.Popup.FramedCloud("Client_Popup",
                            feature.geometry.getBounds().getCenterLonLat(),
                            new OpenLayers.Size(10,10), m.popup_html(feature),
                            new OpenLayers.Icon(null,new OpenLayers.Size(size.w+15,size.h+15))
                            , true);
                        popup.backgroundColor = "#ddd";
                        popup.border = "1px solid #32a8a9";
                        popup.opacity = .8;
                        popup.autoSize = true;
                        currentpopup = popup;
                        m.map.addPopup(popup);
                        $("#map,#Client_Popup").unbind().click(function(){
                            if(currentpopup){
                                m.map.removePopup(currentpopup);
                                currentpopup.destroy();
                                currentpopup = null;
                            }
                        });
                    },
                    onUnselect: function(feature){
                        
                    }
                });
            this.map.addControl(this.cluster_hover);
            this.cluster_hover.activate();
        },
        popup_html: function(feature){
            var h = "", c, a = [], n = 4;
            c = feature.data;
            h += "<p><b>" + c.CUSTOMER + "</b><br/>";
            h += c.CITY + ", " + c.STATE + " | " + c.DIVISION;
            if(c.Testim1){
                h += "<br/><br/>" + c.Testim1;
                if(c.Testim2) h += c.Testim2;
                h += "";
            }
            if(c.Testim3){
                h += "<br/><br/>" + c.Testim3;
                if(c.Testim4) h += c.Testim4;
                h += "";
            }
            h += "</p>";
            return "<div class='client_popup'>" + h + "</div>";
        },
        setupMap2: function(){
            var options = {
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
                units: "m",
                maxResolution: 156543.0339,
                maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508),
                controls: [
                //new OpenLayers.Control.PanZoomBar(),
                //new OpenLayers.Control.MouseDefaults()
                ],
                numZoomLevels: 19
            }
            this.map2 = new OpenLayers.Map("map2",options);
            var gmap = new OpenLayers.Layer.Google("Street" , {
                'sphericalMercator': true,
                type: G_PHYSICAL_MAP
            });

            m.map2.addLayers([gmap]);

            var usBounds = new OpenLayers.Bounds(-14392000, 2436200, -7279500, 6594375);
            m.map2.zoomToExtent(usBounds);

            /*var geoXml = new GGeoXml("http://adc4gis.adceval.com/media/all_clients.php");

            gmap.mapObject.addOverlay(geoXml);*/
            
            var smap = new OpenLayers.StyleMap({
                "default": new OpenLayers.Style({
                    externalGraphic: "/images/openlayers/mm_20_red.png",
                    graphicXOffset: -9,
                    graphicYOffset: -18,
                    pointRadius: 10
                })
            });

            var l = new OpenLayers.Layer.Vector("Clients", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                projection: m.map.projection,
                protocol: new OpenLayers.Protocol.HTTP({
                    format: m.gmlformat2,
                    url: "images/all_clients.php"
                }),
                styleMap: smap
            });

            m.map2.addLayer(l);
        },
        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;
        }
    };
})(jQuery);

//prevent firebug errors in non-FF browsers
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
