function insertEmediateFallbackAds(cuList, emediateRequest, backendURL) {
    var foundEmediateFallback = false;
    for(var i=0; i < cuList.length; i++) {
        jQuery('.emediate-fallback').each(function() {
            var keepElement = false;
            jQuery(this).children().each(function() {
                var nodeName = this.nodeName.toLowerCase();
                if(nodeName != 'script' && nodeName != 'a')
                    keepElement = true;
            });

            if(!keepElement) {
                foundEmediateFallback = true;
                var id = 'EAS_fif_'+( new Date().getTime());
                jQuery(this)
                        .addClass('widget-body emediate').removeClass('content-box internetseller')
                        .html('<div class="fif_container" id="'+id+'"></div>').removeAttr("style");

                setTimeout(function() {
                    EAS_load_fif(id, backendURL + "EAS_fif.html", emediateRequest, 24, 24);
                }, 1); 
            }
        });
    }

    if(foundEmediateFallback){
        setTimeout(function() {
            emediateAdCleanup('.emediate-fallback iframe');
        },1000);
    }
}

function emediateAdCleanup(emediateClass) {
    if(typeof emediateClass == 'undefined')
        emediateClass = '.emediate iframe';
    var resize = function(frame, iframeDoc) {
        if(frame != null) {
            var frameBody =  frame.contents();
            if(frameBody != null && frameBody.find('body').eq(0).children().length == 2) {
                // No ad present, remove entire container
                var p = frame.parent();
                while(!p.hasClass('emediate')) {
                    p = p.parent();
                }
                p.remove();
            }
            else {
                var width = iframeDoc.width();
                // resize the iframe
                frame
                    .width(width)
                    .height(iframeDoc.height() - 4);

                // resize the iframe container
                frame.parent().width(width);
            }
        }
    };

    jQuery(emediateClass).each(function() {
        if(jQuery(this).contents().find('body').eq(0).children().length > 1) {
            // Already loaded
            var iframe = jQuery(this);
            var iframeDoc = jQuery(this.contentWindow.document);

            // Magic number to detect if the ad has loaded or not
            if(iframeDoc.height() == 24 && iframeDoc.width() == 24) {
                setTimeout(function() {
                    resize(iframe, iframeDoc);
                }, 1000);
            }
            else
                resize(iframe, iframeDoc);
        }
        else {
            jQuery(this).load(function() {
                resize(jQuery(this), jQuery(this.contentWindow.document));
            });
        }
    });
}

jQuery(document).ready(function() {
    emediateAdCleanup();
});
