/**
 * Библиотека за правене на бутони, чрез картинки
 *
 * Изисква mootools 1.11 минимум
 *
 * @author Красимир Йосифов
 * @version 1.0  14.04.2008 г.
 */

var mooButtons = new Class({
    version : '1.0',
    options : {
        debug : false,              // debug = ( true | false )
        buttonsClass : 'button'
    },

    initialize : function( options ) {
        // инициализиране на променливи
        this.setOptions( options );
        this.buttons = $$( '.' + this.options.buttonsClass );
        this.attachEvents();
    },
    
    attachEvents : function( ) {
        this.buttons.each(function(button, index){
            button.addEvent('mouseenter', function( e ){
                var buttonSrc = this.getImageSrc( button );
                button.src = buttonSrc.replace(/_normal/g, '_hover');
            }.bind(this));
            button.addEvent('mousedown', function( e ){
                var buttonSrc = this.getImageSrc( button );
                button.src = buttonSrc.replace(/_hover/g, '_click');
            }.bind(this));
            button.addEvent('mouseup', function( e ){
                var buttonSrc = this.getImageSrc( button );
                button.src = buttonSrc.replace(/_click/g, '_hover');
            }.bind(this));
            button.addEvent('mouseleave', function( e ){
                var buttonSrc = this.getImageSrc( button );
                button.src = buttonSrc.replace(/_hover/g, '_normal');
            }.bind(this));
        }, this);
    },
    
    isPngFixed : function( obj ) {
        if (obj.style.filter) {
            return true;
        } else {
            return false;
        }
    },
    
    getImageSrc : function( button ) {
        if (this.isPngFixed(button)) {
            buttonSrc = button.getStyle('filter');
            pattern = /src='([^']*)'/g;
            results = pattern.exec(buttonSrc);
            if (results.length == 2) {
                buttonSrc = results[1];
            } else {
                buttonSrc = '';
            }
        } else {
            buttonSrc = button.src;
        }
        return buttonSrc;
    }
});
mooButtons.implement(new Options, new Events);