{% include "geonode/ext_header.html" %}
{% include "geonode/app_header.html" %}
{% include "geonode/geo_header.html" %}
<script type="text/javascript" src="{{ STATIC_URL}}geonode/js/utils/thumbnail.js"></script>
<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 MAPFISH_PRINT_ENABLED %}
                printService: "{{GEOSERVER_BASE_URL}}pdf/",
                {% else %}
                printService: "",
                {% endif %}
                
                portalConfig: {
                    renderTo: "preview_map",
                    height: 400 
                },

                listeners: {
                    "ready": function() {
                        var map = app.mapPanel.map;
                        var layer = app.map.layers.slice(-1)[0];

                        // FIXME(Ariel): Zoom to extent until #1795 is fixed.
                        //map.zoomToExtent(layer.maxExtent, false)
                        
                        var bbox = layer.bbox;
                        if (bbox != undefined)
                        {
                           if (!Array.isArray(bbox) && Object.keys(layer.srs) in bbox) {
                            bbox = bbox[Object.keys(layer.srs)].bbox;
                           }

                           var extent = OpenLayers.Bounds.fromArray(bbox);
                           var zoomToData = function()
                           {
                               map.zoomToExtent(extent, false);
                               app.mapPanel.center = map.center;
                               app.mapPanel.zoom = map.zoom;
                               map.events.unregister('changebaselayer', null, zoomToData);
                           };
                           map.events.register('changebaselayer',null,zoomToData);
                           if(map.baseLayer){
                            map.zoomToExtent(extent, false);
                           }
                        }
                    },
                    "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();
                    };
                }
            });
            Ext.Ajax.on('requestcomplete', function(req, cippa, opts){
                if(opts.method == 'PUT'){
                    $('#legend_icon').attr('src', $('#legend_icon').attr('src')+'&'+Math.random());
                }   
            }, this);
        });
    
    {% endautoescape %}
    </script>
