(function($) {
    $.extend($.fn, {
        dattr: function(attribute) {
            return $(this).attr('data-' + attribute);
        },
        numbersOnly: function() {
            return $(this).bind('keypress', function(e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
                {
                    if ($(this).next('.msg').length == 0) {
                        $(this).after('<span/>').next().addClass('msg').text('Digits Only').show().fadeOut(1200, function() { $(this).remove(); });
                    }
                    return false;
                }
            });
        },
        multibind: function() {
            var namespace = '';
            var hash = arguments[0];
            if (typeof(hash) == 'string') {
                namespace = hash;
                hash = arguments[1];
            }

            var eventName = function(name) {
                return (namespace == '') ? name : [name, namespace].join('.');
            };

            return this.each(function() {
                for (method in hash) {
                    $(this).bind(eventName(method), hash[method]);
                }
            });
        },
        
        loading: function(mode) {
            var mode = mode || 'show';

            return this.each(function() {
                switch(mode) {
                    case 'show':
                    $(this).children().hide();
                    $(this).find('div.loading').remove();
                    $(this).append('<div class="loading"/>');
                    break;

                    case 'hide':
                    $(this).children().show();
                    $(this).find('div.loading').remove();
                    break;
                }
            });
        },
        
        unwrap: function(expr) {
            return this.each(function() {
                $(this).parents(expr).eq(0).after(this).remove();
            });
        }
    });
    
    $.extend($, {
        extractParam: function(url, name) {
            var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
            return results[1] || 0;
        }
    });
})(jQuery);
