/**
 * Ext.ux.plugins.rowSelectionModel plugin for Ext.form.TextField
 *
 * @autor  Isaac Peraza
 * @fecha    20 de junio del 2009
 *
 * @class Ext.grid.CheckboxSelectionModelNotColumn
 * @extends Ext.grid.RowSelectionModel
 * Para evitar que se seleccione la fila cuando damos un click en alguna de las columas.
 */

Ext.grid.CheckboxSelectionModelNotColumn = Ext.extend(Ext.grid.RowSelectionModel, {
    
    header: '<div class="x-grid3-hd-checker">&#160;</div>',
    
    width: 20,
    
    sortable: false,

    
    menuDisabled:true,
    fixed:true,
    dataIndex: '',
    id: 'checker',

    
    initEvents : function(){
        Ext.grid.CheckboxSelectionModel.superclass.initEvents.call(this);
        this.grid.on('render', function(){
            var view = this.grid.getView();
            view.mainBody.on('mousedown', this.onMouseDown, this);
            Ext.fly(view.innerHd).on('mousedown', this.onHdMouseDown, this);

        }, this);
    },

    
    onMouseDown : function(e, t){
        if(e.button === 0 && t.className == 'x-grid3-row-checker'){ 
            e.stopEvent();
            var row = e.getTarget('.x-grid3-row');
            if(row){
                var index = row.rowIndex;
                if(this.isSelected(index)){
                    this.deselectRow(index);
                }else{
                    this.selectRow(index, true);
                }
            }
        }
    },

    
    onHdMouseDown : function(e, t){
        if(t.className == 'x-grid3-hd-checker'){
            e.stopEvent();
            var hd = Ext.fly(t.parentNode);
            var isChecked = hd.hasClass('x-grid3-hd-checker-on');
            if(isChecked){
                hd.removeClass('x-grid3-hd-checker-on');
                this.clearSelections();
            }else{
                hd.addClass('x-grid3-hd-checker-on');
                this.selectAll();
            }
        }
    },
	
	handleMouseDown : function(g, rowIndex, e){
	    if(e.button !== 0 || this.isLocked()){
	        return;
	    };
	    var view = this.grid.getView();
	    if(e.shiftKey && this.last !== false){
	        var last = this.last;
	        this.selectRange(last, rowIndex, e.ctrlKey);
	        this.last = last;             
			view.focusRow(rowIndex);
	    }else{
	        var isSelected = this.isSelected(rowIndex);
	        if(e.ctrlKey && isSelected){
	            this.deselectRow(rowIndex);
	        }else if(!isSelected || this.getCount() > 1){
	            //this.selectRow(rowIndex, e.ctrlKey || e.shiftKey);
	            view.focusRow(rowIndex);
	        }
	    }
	},
	
    renderer : function(v, p, record){
        return '<div class="x-grid3-row-checker">&#160;</div>';
    }
});
 
