/*
 * Copyright © 2008  Neogeo Technologies, Toulouse, France

 * This file is part of Opencarto web map publishing system project
 *
 * Opencarto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Opencarto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Opencarto.  If not, see <http://www.gnu.org/licenses/>.
 */

Ext.namespace('opencarto.selection');
opencarto.displayDataGrid = true;
// initialisation des variables globales du module
opencarto.selection.mode = 'ST_Intersects';
opencarto.selection.behaviour = 'new';
opencarto.selection.tool = 'circle';
opencarto.selection.wkt = null;


// défintion des barres d'outils et boutons 

opencarto.selection.toolbar = new Ext.Toolbar.MenuButton({
        id: 99,
        iconCls: 'selectionPanel',
        tooltip: oMessages.sel_tools,
        tooltipType:'title',
        //text: 'Sélection',
        enableToggle: true,
        pressed: false,
        disabled:true,
        handler: opencarto.toolbarHandler,
        menu:{
            items:[
                '<b class="menu-title">Outils</b>',
                new Ext.menu.CheckItem({
                    id: oMessages.circle,
                    text: 'Cercle',
                    group:'tool',
                    checked: opencarto.selection.tool == 'circle' ? true:false,
                    checkHandler: function(){ opencarto.selection.onSmCheck(this); }
                }),new Ext.menu.CheckItem({
                    id:'rectangle',
                    text: oMessages.rectangle,
                    group:'tool',
                    checked: opencarto.selection.tool == 'rectangle' ? true:false,
                    checkHandler: function(){ opencarto.selection.onSmCheck(this); }
                }),new Ext.menu.CheckItem({
                    id:'polygon',
                    text: oMessages.polygon,
                    group:'tool',
                    checked: opencarto.selection.tool == 'polygon' ? true:false,
                    checkHandler: function(){ opencarto.selection.onSmCheck(this); }
                }),'-',
                '<b class="menu-title">Mode de sélection</b>',
                new Ext.menu.CheckItem({
                    id:'ST_Intersects',
                    text: oMessages.intersects,
                    group:'mode',
                    checked: true,
                    checkHandler: function(){ opencarto.selection.onSmCheck(this); }
                }),new Ext.menu.CheckItem({
                    id:'ST_Contains',
                    text: oMessages.contains,
                    group:'mode',
                    checked: false,
                    checkHandler : function(){ opencarto.selection.onSmCheck(this); }
                })
                /*,'-',
                '<b class="menu-title">Comportement</b>',
                {
                    id:'new',
                    text: 'Remplace',
                    tooltip: 'Remplace une sélection existante par la nouvelle.',
                    tooltipType:'title',
                    group:'behaviour',
                    checked: true,
                    checkHandler: opencarto.selection.onSmCheck
                },
                {
                    id:'add',
                    text: 'Ajoute',
                    tooltip: 'Ajoute la nouvelle sélection aux éléments déjà sélectionnés.',
                    tooltipType:'title',
                    group:'behaviour',
                    checked: false,
                    checkHandler: opencarto.selection.onSmCheck
                }  */
            ]
        }
    });
 
opencarto.selection.unselectButton =   new Ext.Toolbar.Button({
                id: 17,
                iconCls: 'deleteSelected',
                tooltip: oMessages.del_sel,
                tooltipType:'title',
                //text: 'Effacer',
                enableToggle: false,
                pressed: false,
                handler: opencarto.toolbarHandler
            });
    
     
     
     

// méthodes et fonctions spécifiques au module
 

   
opencarto.selection.onSmCheck = function(item){
    if (item.checked){
        eval('opencarto.selection.'+item.group + '="' + item.id+'"');
        opencarto.selection.toolbar.toggle();
        opencarto.toolbarHandler(opencarto.selection.toolbar);
    }
}
opencarto.selection.resetToolbar = function(){
    if (opencarto.selection.tool)
    {
        target = opencarto.selection.toolbar.menu.items.get(opencarto.selection.tool);
        target.setChecked(false);
        
    }   
}

opencarto.selection.activateControl = function(){
    if (opencarto.selection.addFeatureControl){
        opencarto.map.removeControl(opencarto.selection.addFeatureControl);
        opencarto.selection.addFeatureControl.destroy();
    }
    
	
    switch(opencarto.selection.tool){
        case 'rectangle':
            var options = {handlerOptions: {sides: 4,irregular:true,angle:0}};
            opencarto.selection.addFeatureControl = new OpenLayers.Control.DrawFeature(opencarto.vectorLayer,OpenLayers.Handler.RegularPolygon,options);
            break;
        case 'circle':
            var options = {handlerOptions: {sides: 36}};
            opencarto.selection.addFeatureControl = new OpenLayers.Control.DrawFeature(opencarto.vectorLayer,OpenLayers.Handler.RegularPolygon,options);
            break;
        case 'polygon':
            opencarto.selection.addFeatureControl = new OpenLayers.Control.DrawFeature(opencarto.vectorLayer,OpenLayers.Handler.Polygon);
            break;
    }
    
    opencarto.map.addControl(opencarto.selection.addFeatureControl);
    opencarto.selection.addFeatureControl.featureAdded=function(feature){
        // construction d'un WKT correct:
        opencarto.selection.wkt = feature.geometry.toString();
        opencarto.vectorLayer.destroyFeatures(opencarto.vectorLayer.features);
        if (opencarto.datagrid)
        	opencarto.datagrid.displayData(opencarto.layerGrid.getSelectionModel().getSelected().get('options.layerid'));
    }
    opencarto.selection.addFeatureControl.activate();
   
}

opencarto.selection.selectAll = function(ds,recs,opts){
    //var txt = oMessages.layer;
    var txt = "Couche";    

    if (opencarto.selection.wkt){
        if (opencarto.datagrid){
        	opencarto.datagrid.grid.getSelectionModel().selectAll();
        }
        txt = oMessages.selection_lc;
        opencarto.selection.wkt= null;

    }
    else if (opencarto.search && (opencarto.search.geom != '' || opencarto.search.queryID))
    {
    	if (opencarto.datagrid){
    	opencarto.datagrid.grid.getSelectionModel().selectAll();
    	}
        txt = oMessages.selection_lc;
        opencarto.search.queryID = null;
    	
    	
    }
    // complétion du titre du panel avec le nom de la couche
    if (opencarto.datagrid){
    	txt+=' ' + opencarto.getLayerLabel(opencarto.datagrid.layer_id);
    }
   
    target = Ext.getCmp('dataLayer');
    ds.getTotalCount() > 1 ? s = 's' : s='';
    target.setTitle(ds.getTotalCount() + ' ' + oMessages.record + s + ' '+oMessages.in_the+' '+txt);
    target.add(opencarto.datagrid.grid);
    opencarto.Mask.hide();
    target.doLayout();
    target.expand();
}
opencarto.selection.unSelectAll = function() {
    if (opencarto.datagrid){
    	opencarto.datagrid.grid.getSelectionModel().clearSelections();
    }
    opencarto.selection.wkt = null;
    opencarto.vectorLayer.destroyFeatures(opencarto.vectorLayer.features);
}




   

