
Type.registerNamespace('SfUI');

SfUI.ButtonType = function() { };
SfUI.ButtonType.prototype = {
    Push: 0, 
    Check: 1
}
SfUI.ButtonType.registerEnum('SfUI.ButtonType', false);

SfUI.Button = function SfUI_Button(id) {
    this._buttonType = SfUI.ButtonType.Push;
    this._id = id;
    this._link = $(this._id + 'Link');
    this._img = $(this._id + 'Img');
    this._link.observe('mouseover', Function.createDelegate(this, function(sender, args) {
        this._isHilited = true;
        this._paint();
    }));
    this._link.observe('mouseout', Function.createDelegate(this, function(sender, args) {
        this._isHilited = false;
        this._paint();
    }));
    this._link.observe('mouseup', Function.createDelegate(this, function(sender, args) {
        this._isPressed = true;
        this._paint();
    }));
    this._link.observe('mousedown', Function.createDelegate(this, function(sender, args) {
        this._isPressed = false;
        this._paint();
    }));
    this._link.observe('click', Function.createDelegate(this, function(sender, args) {
        this._isPressed = false;
        if (this._enabled) {
            if (this._click) {
                this._click(this, Sys.EventArgs.Empty);
            }
        }
        this._paint();
    }));
    this._link.observe('dblclick', Function.createDelegate(this, function(sender, args) {
        this._isPressed = false;
        if (this._enabled) {
            if (this._dblClick) {
                this._dblClick(this, Sys.EventArgs.Empty);
            }
        }
        this._paint();
    }));
    this._images = new Array(2);
    this._images[0] = new SfUI.ButtonImage();
    this._images[1] = new SfUI.ButtonImage();
}
SfUI.Button.prototype = {
    _link: null,
    _img: null,
    _id: null,
    _enabled: true,
    _isHilited: false,
    _isPressed: false,
    _isChecked: false,
    _images: null,
    _click: null,
    _dblClick: null,
    
    SetButtonType: function Buttons_Button$SetButtonType(type) {
        this._buttonType = type;
    },
    
    SetChecked: function Buttons_Button$SetChecked(isChecked) {
        if (this._buttonType !== SfUI.ButtonType.Check) {
            throw Error.invalidOperation('Button must be check type');
        }
        this._isChecked = isChecked;
        this._paint();
    },
    
    GetChecked: function Buttons_Button$GetChecked() {
        return this._isChecked;
    },
    
    SetNormalImage: function Buttons_Button$SetNormalImage(index, val) {
        this._images[index].set_normal(val);
    },
    
    SetOverImage: function Buttons_Button$SetOverImage(index, val) {
        this._images[index].set_over(val);
    },
    
    SetPressedImage: function Buttons_Button$SetPressedImage(index, val) {
        this._images[index].set_pressed(val);
    },
    
    SetDisabledImage: function Buttons_Button$SetDisabledImage(index, val) {
        this._images[index].set_disabled(val);
    },
    
    Enable: function Buttons_Button$Enable(enabled) {
        this._enabled = enabled;
        this._paint();
    },
    
    Initialize: function Buttons_Button$Initialize() {
        this._paint();
    },
    
    SetTooltip: function Buttons_Button$SetTooltip(val) {
        this._link.setAttribute('title', val);
        this._img.setAttribute('alt', val);
    },
    
    SetClickHandler: function Buttons_Button$SetClickHandler(handler) {
        this._click = handler;
    },
    
    SetDblClickHandler: function Buttons_Button$SetDblClickHandler(handler) {
        this._dblClick = handler;
    },
    
    _paint: function Buttons_Button$_paint() {    
        var imageSrc = this._getCurrentImageSrcFromButtonImage(this._getCurrentButtonImage());    
        if(imageSrc != null)
        {
            this._img.setAttribute('src', imageSrc);
        }
    },
    
    _getCurrentButtonImage: function Buttons_Button$_getCurrentButtonImage() {
        if (this._buttonType === SfUI.ButtonType.Push) {
            return this._images[0];
        }
        else {
            if (this._isChecked) {
                return this._images[1];
            }
            else {
                return this._images[0];
            }
        }
    },
    
    _getCurrentImageSrcFromButtonImage: function Buttons_Button$_getCurrentImageSrcFromButtonImage(currentImage) {
        if (!this._enabled) {
            if(this._link){this._link.setStyle({ cursor: 'default' })};
            return currentImage.get_disabled();
        }
        if (this._isPressed) {
            if (currentImage.get_pressed()) {
                return currentImage.get_pressed();
            }
            else {
                return currentImage.get_normal();
            }
        }
        if (this._isHilited) {
            return currentImage.get_over();
        }
        if(this._link){this._link.setStyle({ cursor: 'pointer' })};
        return currentImage.get_normal();
    }
}

SfUI.Button.registerClass('SfUI.Button');


SfUI.ButtonImage = function SfUI_ButtonImage() {}
SfUI.ButtonImage.prototype = {
    _normal: null,
    
    get_normal: function Buttons_ButtonImage$get_normal() {
        return this._normal;
    },
    set_normal: function Buttons_ButtonImage$set_normal(value) {
        this._normal = value;
        return value;
    },
    
    _over: null,
    
    get_over: function Buttons_ButtonImage$get_over() {
        return this._over;
    },
    set_over: function Buttons_ButtonImage$set_over(value) {
        this._over = value;
        return value;
    },
    
    _pressed: null,
    
    get_pressed: function Buttons_ButtonImage$get_pressed() {
        return this._pressed;
    },
    set_pressed: function Buttons_ButtonImage$set_pressed(value) {
        this._pressed = value;
        return value;
    },
    
    _disabled: null,
    
    get_disabled: function Buttons_ButtonImage$get_disabled() {
        return this._disabled;
    },
    set_disabled: function Buttons_ButtonImage$set_disabled(value) {
        this._disabled = value;
        return value;
    }
}

SfUI.ButtonImage.registerClass('SfUI.ButtonImage');

