//Switch off Facebook JDK loading
Flux.loadFacebookJDK = false;

// Set up Facebook Library Call /////
var holdReportingTitle = new Array(); // reporting eVar33 holder
var currentReportingTitleIndex = 0; // index of currently playing video in playlist - Will always be 0 if this is a single video
var indexOfLikeSubscribe = null;
var facebookInitDone = false;

$(document).ready(function() {
	// Test to make sure facebook div does not already exist & create if it does not
	if ( !$('#fb-root').length ) {
		var fbDiv = document.createElement('div');
		fbDiv.id='fb-root';

		document.body.appendChild(fbDiv);
	} else {
		//fb-root already exists
	}
	
	$('#fb-root').css({
		border : '0px none',
		margin : '0px',
		padding : '0px',
		display: 'none'
	});
	
	
	facebookInitDone = facebookInit();
});

function facebookInit() {
	// exit if this has already been defined
	if ( facebookInitDone == true || typeof(FB) !='undefined' ) return true;

	window.fbAsyncInit = function() { 
		FB.init({
			appId: fbAppId,
			status: true,
			cookie: true,
			xfbml: true
		});
	};
	(function() {
		var e = document.createElement('script');
		e.async = true;
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		document.getElementById('fb-root').appendChild(e);
	}());
	
	return true;
	
	/*
	if (typeof(FB) == 'undefined') {
		// Flux can jump in and kill the FB object that we loaded
		// Stay right here if that happens until we make the FB library
		setTimeout(function() { facebookInit(); }, 50);
	} else {
		return true;
	}*/
}

// Ensures function that is passed to it waits for facebook to load
function runAfterFacebookInit(callback) {
	
    if (typeof(window.fbAsyncInit) == 'undefined' || typeof(window.fbAsyncInit.hasRun) == 'undefined' || !window.fbAsyncInit.hasRun){
        setTimeout(function() { runAfterFacebookInit(callback); }, 200);
    } else {
        if (callback) { callback(); }
    };
}
