function initDockableWindows (mapID, dws)
{
	var dockableWindows = [];
	
	for (var i = 0; i < dws.length; i++)
	{
	    var dwin = dws[i];
	    var startOpen              = (typeof (dwin.openStart)               != "undefined"  && dwin.openStart              == "yes");
	    var requiresAuthentication = (typeof (dwin.requiresAuthentication)  != "undefined"  && dwin.requiresAuthentication == "yes");
        var hidden = (requiresAuthentication && (!isSU || !loginInfo.isLogged || !loginInfo.userName)); 


        if (!hidden)
        {
            var config = { xtype: 'panel' };
                            	                         
    	    if (dwin.MENU && dwin.MENU.length > 0)
    	    {
    			var menuItems = [];
    			
    	        for (var j = 0; j < dwin.MENU.length; j++)
    	        {
    	            var mnu = dwin.MENU[j];
    				menuItems.push ({
                                	    xtype   : 'menutextitem',
                                	    cls     : 'menuItem',
                                	    overCls : 'x-menu-item-hover',
    							        taskID  : mnu.taskID, 
    									url     : mnu.url,
    									text    : mnu.text_node,
    								    handler : function (item) 
    									{ 
    									    var params = item.url ? { url : item.url } : {};
    									    executeTask (item.taskID, params ); 
    									}
                    				});
    	        }                            
    			config.items = menuItems;
    	    }
    	    else if (dwin.TREEVIEW)
            {
                switch (dwin.id)
                {
    	                case "Risultati":
    	                    config.layout = "fit";
    	                	config.items =  [ buildTreeRisultati (mapID,  dwin.TOOLBAR) ];
                            config.bbar  =
                            {
                    			height: 26,
                    			defaults: { style: 'padding : 0px 5px;' },
                    		    items: 
                    		    [
                    		        buildToolbarRisultati (mapID, dwin.TOOLBAR)
                    		    ]
                    		};
   	                        break;
    	                    
    	                case "Legenda":
    	                    config.layout = "fit";
    	                	config.items =  [ buildTreeLegenda (mapID, XML2JSON (dwin.TREEVIEW.src, "")) ];
    	                    break;
    	        }
    	    }                    

            dockableWindows.push({
                                    id: "dw_" + mapID + "_" + dwin.id, 
                                    title: dwin.caption, 
                                    collapsed : !startOpen,
                                    open: startOpen,
                                    layout: 'fit',
                                    items: [ config ]
                                 });
		}
	}
	
    return dockableWindows;
}


function cambioScala (scala)
{
    ctrlMainMap.clear();
    ctrlMainMap.set_geo_centre_scale (ctrlMainMap.get_geo_center(),  scala);
}


function initToolbar (mapID, XMLbuttons, XMLScales)
{
	var toolbarItems = initTools (mapID, XMLbuttons);

    toolbarItems.push ('->');
    if (isSU)
    {
        toolbarItems = addLoginTools (mapID, toolbarItems);
    }
    
    var scalesMenu = [];
    Ext.each (XMLScales, function (val)
    {
        scalesMenu.push ({ text: '1:' + val, handler: cambioScala });
    });

    toolbarItems.push (
    { 
        xtype: 'tbtext', 
        text: "Scala  : <input id='" + mapID + "_scaleText' value='" + appContext.startScale + "' size='5'>",
        menu: scalesMenu 
    });
    
    toolbarItems.push (
    { 
        xtype: 'tbtext', 
        style:  'margin-left : 20px; ',    // background: transparent; 
        text: "Consulta mappa al: "
    });

    toolbarItems.push (
    { 
        text:   "<span id='_linkDate'> " + Ext.util.Format.date(stringToDate(appContext._date), "j F Y")  + "</span>", 
        cls:    'calendar', 
        style:  'margin-right: 10px; padding: 2px 5px;',
        tooltip: "Imposta la data di riferimento per la consultazione della mappa",
        menu: new Ext.menu.DateMenu({ handler :  function (datePicker, changedDate) { onConfirmDate (changedDate) } })
    });                                                                    


	return new Ext.Toolbar({ id: mapID+ "_Tools", style: "width: 100%", items: toolbarItems});
}


// Impostare sticky buttons ? 	

function setState (s, mapControl)
{
    var s1  = document.getElementById('PRG_state_1');
    var s3  = document.getElementById('PRG_state_3');
    var s13 = document.getElementById('PRG_state_13');

    removeClassName(s1,  "selected");
    removeClassName(s13, "selected");
    removeClassName(s3,  "selected");

    switch (s)
    {
        case "1":
            addClassName (s1,  "selected");
            break;
            
        case "13":
            addClassName (s13, "selected");
            break;
        
        case "3":
            addClassName (s3,  "selected");
            break;
    }        
    
    appContext._state = s;
    appContext.save();

    var geoMap = geoMaps[MappaCorrente.mapID];
    geoMap.refreshMapState (true);
}




function onConfirmDate (changedDate)
{                       
    var today = new Date();  
    var span = document.getElementById ('_linkDate');
    
    if (changedDate > today)
    {          
        Ext.Msg.show({
           title:'Data non valida',
           msg: "Non \u00E8 consentito impostare una data successiva a quella di oggi!",
           buttons: Ext.Msg.OK, 
           icon: Ext.MessageBox.ERROR
        });                         

        span.innerHTML = Ext.util.Format.date(stringToDate(appContext._date), "j F Y")
    }                                                                        
    else if (changedDate.toString() != appContext._date)
    {       
        var formattedDate = Ext.util.Format.date(changedDate, "j F Y");
        Ext.Msg.confirm ("Cambio data", "Confermi il cambio di data al " + formattedDate + " ? ", function (buttonId)
        {
            if (buttonId == "yes")
            {
                appContext._date  = dateToString (changedDate);
                appContext.save();
                span.innerHTML = formattedDate;

                var geoMap = geoMaps[MappaCorrente.mapID];
                geoMap.refreshMapState (true);
            }
            else
                span.innerHTML = Ext.util.Format.date(stringToDate(appContext._date), "j F Y")                                    
        });  
    }                                      
    else
        span.innerHTML = Ext.util.Format.date(stringToDate(appContext._date), "j F Y")
}


function getMapObject (mapID)
{
    for (var i = 0; i < Application.MAPS.length; i++)
    {
        if (Application.MAPS[i].mapID == mapID)
            return Application.MAPS[i];
    }
    return null;
}
	
	
function setInactiveMap (mapID)
{
    MappaCorrente = getMapObject (mapID);
/*    
    mapLegend  =  Ext.getCmp (mapID + "_Legend");  
    mapLegend.removeAll (true);
    resultTree =  Ext.getCmp (mapID + "_Results");  
    resultTree.removeAll(true);
*/
    mapLegend = null;
    resultTree = null;
    ToolBox = null;
    ctrlMainMap = null;

    MappaCorrente = null;
    appContext.theMapID = "";    
    appContext.save();    
}
	

function setActiveMap (mapID)
{
    var campoScala = document.getElementById(mapID + "_scaleText");
    
    campoScala.onchange = function ()
    {
        var nuovaScala = parseInt(this.value);
        this.value = nuovaScala;
        cambioScala (nuovaScala);
    }
    
    // Imposta le variabili globali relative al contesto di mappa corrente
    MappaCorrente = getMapObject (mapID);
    appContext.theMapID = mapID;    
    appContext.save();

    var mapLegend  =  Ext.getCmp (MappaCorrente.mapID + "_Legend");  
    mapLegend.forceLoad ();
    
    // Imposta le window fisse
    Ext.each (MappaCorrente.DOCKWINDOWS, function (item)
    {
		var dw = Ext.getCmp ("dw_" + mapID + "_" + item.id);	
		if (dw && dw.pinned)	
			dw.pin();
    });              


    var geoMap = geoMaps[mapID];
    
    ToolBox = geoMap.activate (MappaCorrente, mapLegend);
    resultTree =  Ext.getCmp (MappaCorrente.mapID + "_Results");  
    resultTree.forceLoad ();        

    ctrlMainMap = MappaCorrente.ctrl;

    geoMap.initCanvasDrawers(mapID);

    // resize controlli di mappa e refresh mappa
    geoMap.resizeControls ();          
/*
    if (treeViews ["Legenda"])
        treeViews ["Legenda"].activateAllChecks();
*/

    if (MappaCorrente.defaultState) 
    {
        setState (MappaCorrente.defaultState, geoMap);
    }                  
    geoMap.refreshMapState (false);
      
    geoMap.setMapExtent();
    ctrlMainMap.redraw();    
}



function createMapItems (maps)
{
	var mapItems = [];
    var blockResize = true;	
	
	Ext.each (maps, function (map)
	{
        if (map.SU == "no" || isSU)
        {
			mapItems.push ({
	                            title: 		map.caption,
	                            layout: 	'border',
	                            tabTip:		map.tooltip,
	                            id:         map.mapID + "_Tab",
	                            mapID:      map.mapID,
								listeners:
								{
									activate: function (tab)
									{ 
									    if (MappaCorrente)      // Se onload pagina, salta
									    {
									        if (MappaCorrente.mapID != tab.mapID)
									        {
        									    setInactiveMap (MappaCorrente.mapID);
									            setActiveMap (tab.mapID);
									        }
									    }
									}
								},
								
                            	items: [
		                                	{
			                                    id: map.mapID + 'Toolbar',
			                                    region:'north',
			                                    items: initToolbar 		(map.mapID, map.TOOLBARBUTTONS, map.SCALES)
											},
			                                {
                                                xtype : 'dockableContainer',
                                                region:'west',
                                                margins:'5 0 5 5',
                                                split: !Ext.isIE6,
                                                width: '25%',
                                                id: map.mapID + '_Docker',
                                                homeTo: map.mapID + '_Panel',
                                                minWidth: 100,
                                                items  : initDockableWindows (map.mapID, map.DOCKWINDOWS)
											},
			                                {
			                                    id: map.mapID + '_Panel',
			                                    
			                                    region:'center',
			                                    margins:'5 5 5 0',
			                                    cls:'empty',
			                                  // IL PADDING CAUSA ERRORI DI RICALCOLO NELL'EVENTO CLICK / NEL GEOCONVERTER
			                                  // bodyStyle:'background:white; padding: 10px; font-size: 12pt; line-height: 20pt;',
			                                    bodyStyle:'background:white;                font-size: 12pt; line-height: 20pt;',
			                                    html: "<div id='" + map.mapID + "' style='width: 100%; height: 100%'></div>",
    			                                listeners:
    			                                {
    			                                    bodyResize: function ()
    			                                    {
    			                                        if (MappaCorrente && geoMaps && geoMaps[MappaCorrente.mapID])
                                                            geoMaps[MappaCorrente.mapID].resizeControls ();          
    			                                    },
    			                                    
    			                                    afterRender: function ()        // Crea la mappa dopo che hai renderizzato il nodo contenitore
    			                                    {
                                                        var geoMap = new GeoMap ();
                                                        map.ctrl = geoMap.init (map.mapID);
                                                        geoMaps[map.mapID] = geoMap;
    			                                    }
    			                                }
			                                }
			                                
														                                    
									   ]
						}
			);
						
		}
	});
	
	return mapItems;
}										



function buildViewPort ()
{
    var viewport = new Ext.Viewport(
    {
        layout:'border',
        id: 'mainViewport',
        items:
        [
            {
                collapsible: false,
                region:'center',
                margins: '0 0 0 0',
                xtype: 'tabpanel',
                id:    'mainTabPanel',
                activeItem: (appContext.theMapID) + "_Tab",
                items:  createMapItems (Application.MAPS),
                headerCfg: 
                {
                    id: 'headerComune',
                    html: '<div id="headerText">Comune di Firenze - Piano Regolatore Generale</div><div id="copyright"><a href="http://www.geoin.it" target="_blank" >Sviluppato da Geoin srl</a></div>'
                }
            }
        ], 
        listeners:
        {
            beforerender: function ()
            {
                // Rimuovi messaggio di loading
                var l = document.getElementById ('loading');
                if (l)
                    l.parentNode.removeChild (l);
            }
            
        }
        
    });
}


function main() 
{
    Ext.QuickTips.init();
    
    buildViewPort ();
    
    // Imposta la scala di default per l'ampiezza del browser corrente
    setStartScale (appContext.theMapID);
    setActiveMap(appContext.theMapID);            // Oppure espressione per calcolare il valore di MapID del pannello attivo
}


function setStartScale (mapID)    
{
    var mapBox   = Ext.getCmp(mapID + '_Panel').getBox();
    
    if (appContext.center_x == null && appContext.center_y == null)
    {
        var MBR      = appContext.startMBR;
        
        var di = new display_info();
        di.display = display_type.GEORECT;
        di.device = new device_info ();
        di.device.dev_rect = new rect (0, 0, mapBox.width, mapBox.height, true);
        di.geo_rect = new rect (MBR.l, MBR.t, (MBR.r - MBR.l),  (MBR.t - MBR.b), false);
        di.compute_display();
        var computedScale = di.scale;
    
        if (computedScale < 100)
            computedScale = (parseInt (computedScale / 10) + 1) * 10;
        else if (computedScale < 1000)
            computedScale = (parseInt (computedScale / 100) + 1) * 100;
        else if (computedScale < 10000)
            computedScale = (parseInt (computedScale / 1000) + 1) * 1000;
        else
            computedScale = (parseInt (computedScale / 10000) + 1) * 10000;
            
        appContext.startScale = computedScale ;
        appContext.scale     = computedScale ;
        appContext.center_x  = appContext.startX      ;
        appContext.center_y  = appContext.startY      ;
        appContext.save();
    }
    
    document.getElementById(mapID + "_scaleText").value = appContext.scale;
}


Ext.BLANK_IMAGE_URL = 'img/s.gif';
Ext.onReady(main);