var ActionButton = Class.create();

ActionButton.prototype = {
    click: function() {
    },
    styleChanged: function() {
    },

    initialize: function(id) {
        this._id = id;

        this._handlers = new Hash();

        this._rootControl = this._getControl("root");

        this._initHandler(this._rootControl, "click", this._onClick);
        this._initHandler(this._rootControl, "mouseover", this._onMouseOver);
        this._initHandler(this._rootControl, "mouseout", this._onMouseOut);
        this._initHandler(this._rootControl, "mouseup", this._onMouseUp);
        this._initHandler(this._rootControl, "mousedown", this._onMouseDown);
        this._disabled = false;
    },

    dispose: function() {
        this._handlers.keys().each(function(event) {
            var handler = this._handlers[event];
            this._rootControl.stopObserving(event, handler);
            this._handlers[event] = null;
        }.bind(this));
        this._rootControl = null;
    },

    enable : function() {
        this._disabled = false;
        this._setStatus("iActionsRoot");
    },

    disable : function() {
        this._setStatus("dActionsRoot");
        this._disabled = true;
    },

    _onClick: function() {
        if(this._disabled) return;
        this.click();
    },

    _onMouseOver: function() {
        this._setStatus("aActionsRoot");
    },

    _onMouseOut: function() {
        this._setStatus("iActionsRoot");
    },

    _onMouseUp: function() {
        this._setStatus("aActionsRoot");
    },

    _onMouseDown: function() {
        this._setStatus("pActionsRoot");
    },

    _setStatus: function(style) {
        if(this._disabled) return;
        this._rootControl.className = "actionsRoot " + style;
        this.styleChanged(style);
    },

    _getControl: function(location, suffix) {
        if (typeof(suffix) == 'undefined') suffix = "";
        return $(this._id + "_" + location + suffix);
    },

    _initHandler: function(control, event, handler) {
        var h = handler.bind(this);
        control.observe(event, h);
        this._handlers[event] = h;
    }
}

var TrackingType = {    
    Single : 1,
    Multiple : 2
}

var SelectionTracker = Class.create();

SelectionTracker.prototype = {    
    initialize : function(grid, trackingType) {
        this._grid = grid;
        this._trackingType = trackingType;
        this.actions = new Array();
        
        grid.attachChangeHandler(function() {
            this.track();
        }.bind(this));

        grid.attachDisposedHandler(function() {
            this.track();
        }.bind(this))
    },

    track : function() {        
        var currentSelection = this._grid.getSelectedItems().length;
        var disabling = true;

        if(this._trackingType == TrackingType.Single && currentSelection == 1) {
            disabling = false;
        } else if(this._trackingType == TrackingType.Multiple && currentSelection >= 1) {
            disabling = false;
        }

        this.actions.each(function(action) {
            if(disabling) action.disable();
            else action.enable();
        }.bind(this));
    }
}