/*! waitForImages jQuery Plugin - v1.5.0 - 2013-07-20
 * https://github.com/alexanderdickson/waitForImages
 * Copyright (c) 2013 Alex Dickson; Licensed MIT */
(function($) {
    var o = 'waitForImages';
    $.waitForImages = {
        hasImageProperties: ['backgroundImage', 'listStyleImage', 'borderImage', 'borderCornerImage', 'cursor']
    };
    $.expr[':'].uncached = function(a) {
        if (!$(a).is('img[src!=""]')) {
            return false
        }
        var b = new Image();
        b.src = a.src;
        return !b.complete
    };
    $.fn.waitForImages = function(j, k, l) {
        var m = 0;
        var n = 0;
        if ($.isPlainObject(arguments[0])) {
            l = arguments[0].waitForAll;
            k = arguments[0].each;
            j = arguments[0].finished
        }
        j = j || $.noop;
        k = k || $.noop;
        l = !! l;
        if (!$.isFunction(j) || !$.isFunction(k)) {
            throw new TypeError('An invalid callback was supplied.');
        }
        return this.each(function() {
            var e = $(this);
            var f = [];
            var g = $.waitForImages.hasImageProperties || [];
            var h = /url\(\s*(['"]?)(.*?)\1\s*\)/g;
            if (l) {
                e.find('*').addBack().each(function() {
                    var d = $(this);
                    if (d.is('img:uncached')) {
                        f.push({
                            src: d.attr('src'),
                            element: d[0]
                        })
                    }
                    $.each(g, function(i, a) {
                        var b = d.css(a);
                        var c;
                        if (!b) {
                            return true
                        }
                        while (c = h.exec(b)) {
                            f.push({
                                src: c[2],
                                element: d[0]
                            })
                        }
                    })
                })
            } else {
                e.find('img:uncached').each(function() {
                    f.push({
                        src: this.src,
                        element: this
                    })
                })
            }
            m = f.length;
            n = 0;
            if (m === 0) {
                j.call(e[0])
            }
            $.each(f, function(i, b) {
                var c = new Image();
                $(c).on('load.' + o + ' error.' + o, function(a) {
                    n++;
                    k.call(b.element, n, m, a.type == 'load');
                    if (n == m) {
                        j.call(e[0]);
                        return false
                    }
                });
                c.src = b.src
            })
        })
    }
}(jQuery));



