$(document).ready(function(){
    MMF.ajax.fetch_inline = function(element, url, callback, error, data, type, replace){
        if (!type) {
            type = "GET";
        }
        
        $.ajax({
            type: type,
            data: data,
            dataType: 'html',
            url: url,
            timeout: 7500,
            beforeSend: function(XMLHttpRequest){
                complete = false
                show_loader = function(){
                    try {
                        if (complete == false) {
                                element.addClass('loading');
                        }
                    }
                    catch (err) {
                        /*FUPOSFUBARJSFML*/
                    }
                }
        	
                indicator = setTimeout('show_loader()', 1000);
            },
            complete: function(XMLHttpRequest, textStatus){
            	complete = true;
                try {
                    if (indicator) {
                        clearTimeout(indicator);
                    }
                } 
                catch (e) {
                    // There's a chance, a very small chances that between a 
                    // check for indicator and actually removing it, it will 
                    // be automatically removed because it timed out, so 
                    // we have this as a security net...
                }
                element.removeClass('loading');
            },
            success: function(data, textStatus){
                if (callback !== undefined) {
                    callback(data);
                }
                else {
                    if (replace) {
                        //console.log('replacing')
                        //element.after(data)
                        //element.remove()
                        element.replaceWith(data);
                    }
                    else {
                        element.html(data);
                    }
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                element.html(MMF.errors.ajax);
                if (error !== undefined) {
                    error();
                }
            }
        });
    };
    
    MMF.ajax.loading = function(element){
    
        original_height = element.height();
        original_width = element.width();
        
        element.prepend('<div class="float_loading"></div>');
        element.find('.float_loading').width(original_width);
        element.find('.float_loading').height(original_height);
    };
});

