DRHorton.components = DRHorton.components || {};

DRHorton.components.Button = function(params){
    
    var Event = DRHorton.events.Event;
    var EventDispatcher = DRHorton.events.EventDispatcher;
    var Button = DRHorton.components.Button;
    
    var
        _label = (typeof params.label == "undefined") ? "" : params.label,
        _imageUrl = (typeof params.imageUrl == "undefined") ? "" : params.imageUrl,
        _this = this,
        _element = document.createElement("button"),
        _buttonImg,
        
        _onCreationComplete = function(){
            $(_element).css({
                "background": "none",
                "background-image": "url(" + _buttonImg.src + ")",
                border: "none",
                width: _buttonImg.width + "px",
                height: _buttonImg.height + "px"
            });  
            _this.loaded = true;
            _this.dispatchEvent(new Event(Button.CREATION_COMPLETE)); 
        }    
    ;
    
    this.loaded = false;
    
    this.element = function(){
        return _element;
    };
    
    this.load = function(){
        $(_element).text(_label);
        if(_imageUrl.length){
            _buttonImg = new Image();
            _buttonImg.onload = _onCreationComplete;
            _buttonImg.src = _imageUrl;
            
        }
        else _onCreationComplete();
    };
  
    this.element = function(){return _element};
    
    $(_element).bind("click", function(){
        _this.dispatchEvent(new Event(Button.CLICK));
        return false;
    });
    
};

DRHorton.components.Button.inherits(DRHorton.events.EventDispatcher);

DRHorton.components.Button.CREATION_COMPLETE = "DRHorton.components.Button.CREATION_COMPLETE";
DRHorton.components.Button.CLICK = "DRHorton.components.Button.CLICK";
DRHorton.components.Button.DOWN = "DRHorton.components.Button.DOWN";
DRHorton.components.Button.UP = "DRHorton.components.Button.UP";

