/*
 * 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/>.
 */


opencarto.selection.mode = 'ST_Intersects';
opencarto.selection.behaviour = 'new';
opencarto.selection.tool = 'circle';
opencarto.selection.wkt = null;

opencarto.selection.onSmCheck = function(item){
    if (item.checked){
        eval('opencarto.selection.'+item.group + '="' + item.id+'"');
        if (!opencarto.selection.toolbar.pressed)
            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);
        opencarto.displayData(opencarto.layerGrid.getSelectionModel().getSelected().get('options.layerid'));

       
    }
    opencarto.selection.addFeatureControl.activate();
   
}

opencarto.selection.selectAll = function(ds,recs,opts){
    var txt = 'couche';
    if (opencarto.selection.wkt || opencarto.search.geom != '' || opencarto.search.queryID){
        opencarto.datagrid.grid.getSelectionModel().selectAll();
        txt = 'sélection';
        opencarto.selection.wkt= null;
        opencarto.search.queryID = null;
    }
    else{
        if(ds.getTotalCount() == 1){
            opencarto.datagrid.grid.getSelectionModel().selectAll();
        }
    }
    target = Ext.getCmp('dataLayer');
    ds.getTotalCount() > 1 ? s = 's' : s='';
    target.setTitle(ds.getTotalCount() + ' enregistrement' + s + ' dans la '+txt);
    //target.add(opencarto.datagrid.grid);
    opencarto.Mask.hide();
    //target.doLayout();
    target.expand();
}
opencarto.selection.unSelectAll = function() {
    opencarto.datagrid.grid.getSelectionModel().clearSelections();
    opencarto.selection.wkt = null;
    opencarto.vectorLayer.destroyFeatures(opencarto.vectorLayer.features);
}
