(function($) {

    function lateInit() {
        $('#scrollableArea').ready(function() {
            $('#scrollableArea a').lightBox('/page');
        });
        $(window).trigger("resize");
    };
    
    $(document).ready(function(){
        var fbQueries = [];
        $.getScript("http://connect.facebook.net/en_US/all.js",                             
            function(){
                FB.init({
                    status : true, 
                    cookie : true,
                    oauth : true, 
                    xfbml : true 
                });
                // Load facebook images
                $("div.thumbgallery.fb").each(function(num) {
                    fbQueries.push(FB.api("/" + $(this).attr("id") + "/photos",function(response){
                        if (response) {
                            for(var i=0; i < response["data"].length;i++) {
                                $("#scrollableArea").append($('<a href="' + response["data"][i]['source'] + '"><img src="' + response["data"][i]["images"][2]['source'] + '" alt=""/></a>'));
                            };
                            lateInit();
                        } 
                    }));
                });
                $("div.thumbgallery span.fb").each(function(num) {
                    fbQueries.push(FB.api("/" + $(this).attr("id"),function(response){
                        $(this).remove();
                        if (response) {
                            $("#scrollableArea").append($('<a href="' + response['source'] + '"><img src="' + response['images'][2]['source'] + '" alt=""/></a>'));
                            lateInit();
                        }
                    }));
                });
            }
            
        );

        // General init
        lateInit();
        $("div.thumbgallery").smoothDivScroll({scrollingSpeed: 12, mouseDownSpeedBooster: 3, autoScroll: "onstart", autoScrollDirection: "right", visibleHotSpots: "always", hotSpotsVisibleTime: 9});    
    });
})(jQuery);

