/**
 * @author Matthew Foster
 * @date   February 5th 2007
 * @purpose Allow a good base class to create a table with fundamental event handlers.
 */
 var GridBase = Class.create();
	
	Object.extend(GridBase.prototype,
    {
    
        initialize : function(options){
            
            
            this.options =  {
                                rows       : 5,
                                cols       : 5,
                                mouseDown  : false,
                                onCellOver : this.cellOver.bindAsEventListener(this),
                                onCellOut  : this.cellOut.bindAsEventListener(this),
                                onCellDown : this.cellDown.bindAsEventListener(this),
                                onCellUp   : this.cellUp.bindAsEventListener(this)
                                
                            }
            
            Object.extend(this.options, options || {});
            
            if(!this.options.mouseDown)
                this.monitorDocument();
            
            
                            
            this.buildGrid();
            
            this.disableSelect();
        },

        disableSelect : function(){
        
            this.table.onselectstart = function(e){
                                            Event.stop(e);
                                            return false;
                                        }
        
        },
        monitorDocument : function(){
        
            Event.observe(document, "mousedown", this.documentDown.bindAsEventListener(this));
            Event.observe(document, "mouseup", this.documentUp.bindAsEventListener(this));
                                    
        },
        documentDown : function(e){
            
            this.options.mouseDown = true;
        
        },
        documentUp : function(e){
        
            this.options.mouseDown = false;
        
        },
        buildGrid : function(){
            this.table = $C("table");
            this.tbody = $C("tbody");
            
            this.table.appendChild(this.tbody);
                                    
            (this.options.rows).times(this.buildRow.bind(this));
        
        
        
        },
        buildRow : function(){
            var row = $C("tr");
        
            
            (this.options.cols).times(this.buildCell.bind(this, row));
            
            this.tbody.appendChild(row);
        },
        buildCell : function(row){
            var cell = $C("td");
            
            cell.unselectable = "on";
            cell.style.MozUserSelect = "none";
            
            cell.innerHTML = "&nbsp;";
            row.appendChild(cell);
            
            this.attachCellListeners(cell);
            
        },                     
        attachCellListeners : function(cell){
            //over and out
            Event.observe(cell, "mouseover", this.options.onCellOver);
            Event.observe(cell, "mouseout", this.options.onCellOut);
            
            //down and up
            Event.observe(cell, "mousedown", this.options.onCellDown);
            Event.observe(cell, "mouseup", this.options.onCellUp);
            
        },
        
        cellOver : function(e){
            var cell = Event.element(e);
            
            
            Element.addClassName(cell, "over");
            
            if(this.options.mouseDown)
                this.cellDown(e);
            
        
        },
        cellOut : function(e){
            var cell = Event.element(e);
            
            Element.removeClassName(cell, "over");
            
            if(this.options.mouseDown)
                this.cellUp(e);
        
        },
        cellDown : function(e){
                                        
            var cell = Event.element(e);
            
            Element.addClassName(cell, "down");
        },
        cellUp : function(e){
            var cell = Event.element(e);
            
            Element.removeClassName(cell, "down");
            
        }
    
    
    
    }
);
