/**
 * Extending jQuery to pad numbers.
 *
 * @since 0.2
 * @author Cheyne Rood <cheyner@xonatek.com>
 * @param string pad_str
 * @param integer pad_limit
 * @param string pad_with
 */
(function($) {
	$.pad = function(str, limit, pad_with){

		var diff = limit - str.length;

		for(i = 0; i < diff; i++) {
			str = pad_with + str;
		}

		return str.toString();

	}
})(jQuery);

$.extend({
	hpAjax: function( origSettings ) {

		var ctx;
		var s = jQuery.extend(true, {

			dataType:	'json',
			success:	function(data, status, xhr) {

				if(!data.success) {

					if(jQuery.isFunction(s.hpError)) {

						s.hpError.call(ctx, data, status, xhr);

					}

				} else {

					if(data.created && jQuery.isFunction(s.hpCreated)) {

						s.hpCreated.call(ctx, data, status, xhr);

					} else if(jQuery.isFunction(s.hpUpdated)) {

						s.hpUpdated.call(ctx, data, status, xhr);

					}

					if(jQuery.isFunction(s.hpSuccess)) {

						s.hpSuccess.call(ctx, data, status, xhr);

					}

				}

			}

		}, jQuery.ajaxSettings, origSettings);

		ctx = origSettings && origSettings.context || s;

		return jQuery.ajax(s);
	}
});

/**
 * Note (ryan): Added April 21, 2010. Can *likely* be removed with next version of jquery. We're on 1.4.2 now.
 *
 * Patch (plugin) for jQuery bug 6359: "live('submit') does nothing in IE if
 * live('click') was called before. same with delegate."
 *
 * The workaround is to ensure that live('click') calls happen *after*
 * live('submit') calls. Fixing live() fixes delegate(), which calls live().
 *
 * This plugin uses setTimeout(..., 0) to effect the workaround. That is, it
 * defers live('click') calls to a future execution context. It should work
 * around the issue in most cases.
 *
 * @author Jonathan Aquino
 * @see http://dev.jquery.com/ticket/6359
 * @see TEZLA-538
 */
(function($) {
    var originalLive = jQuery.fn.live;
    jQuery.fn.live = function(types) {
        var self = this;
        var args = arguments;
        if (types == 'click') {
            setTimeout(function() {
                originalLive.apply(self, args);
            }, 0);
        } else {
            originalLive.apply(self, args);
        }
    };
})(jQuery);
