Check if Fancybox is initialized

by Marventus

HTML

<script src="http://thegeekinvasion.com/wp-content/plugins/tgi-fancybox/fancybox/jquery.fancybox.pack.js"></script>
<link rel="stylesheet" href="http://thegeekinvasion.com/wp-content/plugins/tgi-fancybox/fancybox/jquery.fancybox.pack.css">
<div class="wrap">
    <p class="desc">Fancybox is randomly loaded on Window load. However, <em>typeof $.fancybox == 'function'</em> returns always <strong>true</strong>, whereas <em>$.each( $._data(document, "events")</em> only does if Fancybox is actually initialized (Example: <em>$(".fancybox").fancybox();</em>).</p>
    <div class="fb-container">
        <a class="fancybox" rel="gallery1" href="http://farm4.staticflickr.com/3824/9041440555_2175b32078_b.jpg" title="Calm Before The Storm (One Shoe Photography Ltd.)">
            <img src="http://farm4.staticflickr.com/3824/9041440555_2175b32078_m.jpg" alt="" />
        </a>
        <a class="fancybox" rel="gallery1" href="http://farm3.staticflickr.com/2870/8985207189_01ea27882d_b.jpg" title="Lambs Valley (JMImagesonline.com)">
            <img src="http://farm3.staticflickr.com/2870/8985207189_01ea27882d_m.jpg" alt="" />
        </a>
        <a class="fancybox" rel="gallery1" href="http://farm4.staticflickr.com/3677/8962691008_7f489395c9_b.jpg" title="Grasmere Lake (Phil 'the link' Whittaker (gizto29))">
            <img src="http://farm4.staticflickr.com/3677/8962691008_7f489395c9_m.jpg" alt="" />
        </a>
        <br/>
        <div class="fb-buttons">
            <input id="check-fb" type="button" value="Check FB" />
        </div><!-- .fb-buttons -->
    </div><!-- .fb-container -->
    <div class="clear"></div>
    
    <hr>
    <div class="fb-status">
        <p class="heading">Fancybox Status:</p>
        <p class="status">Fancybox is <span class="check"></span>.</p>
        <br/>
        <p class="heading">Tests:</p>
        <p>typeof $.fancybox == 'function': <span class="check typeof"></span></p>
        <p>$(document).data("events"): <span class="check...

CSS

html {
    color: #777;
    font-size: 0.9em;
}
a {
    float:left;
    margin-right:5px;
}
a:last-child {
    margin-right:0;
}
em {
    background:#eee;
    font-style:normal;
}
hr, div, p {
    margin:30px 0 0;
}
img {
    max-height:137px;
}
input {
    font-size:12px;
    margin-top:10px;
    padding:10px 20px;
}
label, .legend p {
    color:#555;
    font:0.8em sans-serif;
}
p {
    font-family:sans-serif;
}
strong {
    font-style:inherit;
    font-weight:bold;
}
sup {
    color:#c00;
    font:bold 10px sans-serif;
    margin:-5px 7px 0 2px;
}
.check {
    font-weight:bold;
}
.clear {
    clear:both;
}
.desc {
    font-size: 1.2em;
    font-style:italic;
    text-align:justify;
}
.fb-buttons {
    margin-top:10px;
    width:100%;
}
.fb-container {
    margin-left:auto;
    margin-right:auto;
}
.fb-status {
    background:#ddd;
    padding:10px 20px;
}
.fb-status p {
    margin-top:10px;
}
.fb-status p:last-child {
    margin-bottom:10px;
}
.heading {
    font-size: 1.5em;
}
.wrap {
    margin-left:auto;
    margin-right:auto;
    width:90%;
}

JavaScript

jQuery(function ($) {
    function fancyboxIsInit() {
        var fbInit = false;
        if (typeof $._data(document, "events") !== "undefined") {
            $.each($._data(document, "events").click, function (i, v) {
                if (v.namespace === 'fb-start') fbInit = true;
            });
        }
        return fbInit;
    }
    function fbCheck() {
        $(".check").each(function () {
            if ($(this).text() === "false" || $(this).text().search(/not/i) > -1) $(this).css("color", "#c00");
            else $(this).css("color", "green");
        });
    }
    $(window).load(function () {
        var load = Math.random() < 0.5,
            typeOf, clicks;
        if (load) $(".fancybox").fancybox();
        $(".status .check").text(load ? "loaded" : "not loaded");
        fbCheck();
        $("#check-fb").on("click.checkFb", function () {
            typeOf = typeof $.fancybox == 'function' ? "true" : "false";
            clicks = fancyboxIsInit() ? "true" : "false";
            $("span.typeof").html(typeOf);
            $("span.events").html(clicks);
            fbCheck();
        });
        var w = 0;
        $(".fb-container .fancybox").each(function() {
            w += $(this).outerWidth(true);
        });
        $(".fb-container").width(w);
    });
});