var ButtonMaker = {
    start: function() {
        var buttons = document.getElementsByClassName('tr_button');
        for (var i = 0; i < buttons.length; i++) {
            var index = -1;
            if (buttons[i].src) {
                this.addprops(buttons[i]);

                buttons[i].stopObserving('mouseout', buttons[i]._mouseout);
                buttons[i].stopObserving('mouseover', buttons[i]._mouseover);
                buttons[i].stopObserving('mousedown', buttons[i]._mousedown);

                buttons[i].observe('mouseout', buttons[i]._mouseout);
                buttons[i].observe('mouseover', buttons[i]._mouseover);
                buttons[i].observe('mousedown', buttons[i]._mousedown);
            }
        }
    },

    mouseout: function() {
        ButtonMaker.addprops(this);
        if (this._src) {
            this.src = this._src;
        }
    },

    mouseover: function() {
        ButtonMaker.addprops(this);
        if (this._mouseoverSrc) {
            this.src = this._mouseoverSrc;
        }
    },

    mousedown: function() {
        ButtonMaker.addprops(this);
        if (this._mousedownSrc) {
            this.src = this._mousedownSrc;
        }
    },

    addprops: function(el) {
        var index;
        if (el.src && !el._src && (-1 != (index = el.src.lastIndexOf('.')))) {
            var filename = el.src.substr(0, index);
            var ext = el.src.substr(index);

            /*
             fix buttons paths taking into account the version control
             information.
            */
            var newIndex;
            if (-1 != (newIndex = filename.lastIndexOf('.')) && 'v' == filename.substr(newIndex + 1, 1)) {
                ext = filename.substr(newIndex) + ext;
                filename = filename.substr(0, newIndex);
            }

            el._src = el.src;
            el._mouseoverSrc = filename + '_mouseover' + ext;
            el._mousedownSrc = filename + '_mousedown' + ext;

            el._mouseout = this.mouseout.bindAsEventListener(el);
            el._mouseover = this.mouseover.bindAsEventListener(el);
            el._mousedown = this.mousedown.bindAsEventListener(el);
        }
    }
}

Event.observe(window, 'load', ButtonMaker.start.bindAsEventListener(ButtonMaker));