{% include "geonode/ext_header.html" %}
{% include "geonode/app_header.html" %}
{% include "geonode/geo_header.html" %}

<script type="text/javascript">
{% autoescape off %}
        var app;
        Ext.onReady(function() {
            GeoExt.Lang.set("{{ LANGUAGE_CODE }}");
            var config = {
                tools: [{
                    ptype: "gxp_wmsgetfeatureinfo",
                    format: "grid",
                    actionTarget: "main.tbar",
                    outputConfig: {width: 400, height: 200, panIn: false}
                }],
                {% if PROXY_URL %}
                proxy: '{{ PROXY_URL }}',
                {% endif %}
                localGeoServerBaseUrl: "{{GEOSERVER_BASE_URL}}",
                authorizedRoles: "{{ user.is_authenticated|yesno:"ROLE_ADMINISTRATOR,ROLE_ANONYMOUS" }}",

                /* The URL to a REST map configuration service.  This service 
                 * provides listing and, with an authenticated user, saving of 
                 * maps on the server for sharing and editing.
                 */
                rest: "/maps/",
                {% if MF_PRINT_ENABLED %}
                printService: "{{GEOSERVER_BASE_URL}}pdf/",
                {% else %}
                printService: "",
                {% endif %}
                
                portalConfig: {
                    renderTo: "preview_map",
                    height: 400 
                },

                listeners: {
                    "ready": function() {
                        map = app.mapPanel.map;
                        map.zoomToExtent(map.layers.slice(-1)[0].maxExtent);
                    },
                    "beforeunload": function() {
                        if (modified) {
                            styleEditor.show();
                            return false;
                        }
                    }
                }
            };

            config = Ext.apply(config, {{ viewer|safe }});
            app = new GeoExplorer.Viewer(config);

            for (var key in app.tools) {
                var tool = app.tools[key];
                if (tool.ptype == 'gxp_styler') {
                    tool.rasterStyling = true;
                };
            };

            // change style displayed in map
            Ext.get(Ext.DomQuery.select("input[@name='style']")).on("click", function(evt, elem) {
                app.selectedLayer.getLayer().mergeNewParams({
                    "STYLES": elem.id,
                    "_dc": Math.random()
                }); 
            });
            
            Ext.get(Ext.DomQuery.select(".style-edit")).on("click", function(evt, elem) {
                for (var key in app.tools) {
                    var tool = app.tools[key];
                    if (tool.ptype == 'gxp_styler') {
                        tool.actions[0].execute();
                    };
                }
            });

        });
    
    {% endautoescape %}
    </script>
