/* FBProxy Object */
var FBProxy = new Object();

/*
 * Load and init
 */
FBProxy.loadAndInit = function(facebookLocale, initParams, logParams, callBackFunction) {
	try {
		FBProxy.initParams = initParams;
		FBProxy.initCallBackFunction = callBackFunction;
		FBProxy.Logger.feature = logParams.feature;
		FBProxy.Logger.userId = logParams.userId;
		FBProxy.Logger.isFundraiser = logParams.isFundraiser;
		FBProxy.Logger.fundraisingPageUrl = logParams.fundraisingPageUrl;
	
		FBProxy.Logger.log("docReady",true);
	
		(function(d, s, id) {
				  var js, fjs = d.getElementsByTagName(s)[0];
				  if (d.getElementById(id)) {return;}
				  js = d.createElement(s); js.id = id;
				  js.src = "//connect.facebook.net/"+facebookLocale+"/all.js#xfbml=1";
				  js.async=true;
				  fjs.parentNode.insertBefore(js, fjs);
				}(document, "script", "facebook-jssdk"));
	} catch(err) {
		FBProxy.Logger.error("FBProxy.loadAndInit("+FBProxy.initParams+", "+initParams+", "+logParams+", "+callBackFunction+")",err);
		return;
	}
}

window.fbAsyncInit = function() {
	try {
		FB.init(FBProxy.initParams);
	} catch(err) {
		FBProxy.Logger.error("FB.init("+FBProxy.initParams+")",err);
		return;
	}
	
	// IE facebook bugs fix
	if ($.browser.msie && ($.browser.version.substr(0,1)=="6" || $.browser.version.substr(0,1)=="7")) {
		// bug with fbml and asynchronous loading
		$("fb\\:like").each(function() {
			$(this).parent().html($(this).parent().html());
			// bug with like button on ie with obsolete flash
			FB.XD._transport = "postmessage";
		});
		$("fb\\:login-button").each(function() {
			$(this).parent().html($(this).parent().html());
		});
	}
	// END
	
		
	// IE facebook bugs log
	if (document.location.href.indexOf("fb_xd_fragment") !=-1) {
		FBProxy.Logger.error("BUG_fb_xd_fragment",document.location.href);
		document.getElementsByTagName("html")[0].style.display="block";
	}
	
	FBProxy.Logger.log("fbInit",true);
	
	/* unknown effect */
	try {
		FB.Event.subscribe("fb.log", function(response) {
			if (response) {
				FBProxy.Logger.error("fb.log",response);
			}
		});
	} catch(err) {
		FBProxy.Logger.error("FB.Event.subscribe(fb.log)",err);
	}
	
	if (FBProxy.initCallBackFunction) {
		FBProxy.initCallBackFunction();
	}
}

/*
 * Logger
 */
FBProxy.Logger = new Object();
FBProxy.Logger.log = function(logPoint, success) {
	var logUrl = "/static/facebooklog"
		+ "?feature=" + (FBProxy.Logger.feature?encodeURIComponent(FBProxy.Logger.feature):"")
		+ "&logPoint=" + (logPoint?encodeURIComponent(logPoint):"")
		+ "&userId=" + (FBProxy.Logger.userId?encodeURIComponent(FBProxy.Logger.userId):"")
		+ "&isFundraiser=" + (FBProxy.Logger.isFundraiser?"1":"0")
		+ "&fundraisingPageUrl=" + (FBProxy.Logger.fundraisingPageUrl?encodeURIComponent(FBProxy.Logger.fundraisingPageUrl):"")
		+ "&success=" + (success?"1":"0");
	$.get(logUrl);
};
FBProxy.Logger.error = function(source, error) {
	var logUrl = "/static/facebookerror"
		+ "?feature=" + (FBProxy.Logger.feature?encodeURIComponent(FBProxy.Logger.feature):"")
		+ "&userId=" + (FBProxy.Logger.userId?encodeURIComponent(FBProxy.Logger.userId):"")
		+ "&isFundraiser=" + (FBProxy.Logger.isFundraiser?"1":"0")
		+ "&fundraisingPageUrl=" + (FBProxy.Logger.fundraisingPageUrl?encodeURIComponent(FBProxy.Logger.fundraisingPageUrl):"")
		+ "&source=" + (source?encodeURIComponent(source):"")
		+ "&error=" + (error?encodeURIComponent(error):"");
	$.get(logUrl);
};

/*
 * Proxified methods
 */
FBProxy.Event = new Object();
FBProxy.Event.subscribe = function(eventName, callBackFunction) {
	try {
		FB.Event.subscribe(eventName, function(response) {
			if (response && (response.error || response.error_msg)) {
				FBProxy.Logger.error("FB.Event.subscribe("+eventName+")",response.error || response.error_msg);
			}
			callBackFunction(response);
		});
	} catch(err) {
		FBProxy.Logger.error("FB.Event.subscribe("+eventName+")",err);
	}
}

FBProxy.getLoginStatus  = function(callBackFunction) {
	try {
		FB.getLoginStatus(function(response) {
			if (response && (response.error || response.error_msg)) {
				FBProxy.Logger.error("FB.getLoginStatus()",response.error || response.error_msg);
			}
			callBackFunction(response);
		});
	} catch(err) {
		FBProxy.Logger.error("FB.getLoginStatus()",err);
	}
}

FBProxy.api = function(path, method, params, callBackFunction) {
	try {
		FB.api(path, method, params, function(response) {
			if (response && (response.error || response.error_msg)) {
				FBProxy.Logger.error("FB.api("+path+", "+method+", "+params+")",response.error || response.error_msg);
			}
			callBackFunction(response);
		});
	} catch(err) {
		FBProxy.Logger.error("FB.api("+path+", "+method+", "+params+")",err);
	}
}

FBProxy.Data = new Object();
FBProxy.Data.query = function(requestTemplate, data) {
	var query;
	try {
		query = FB.Data.query(requestTemplate, data);
	} catch(err) {
		FBProxy.Logger.error("FB.Data.query("+requestTemplate+", "+data+")",err);
		return query;
	}
	try {
		query.wait(function(response) {
			if (response && (response.error || response.error_msg)) {
				FBProxy.Logger.error("query.wait()",response.error || response.error_msg);
			}
		});
	} catch(err) {
		FBProxy.Logger.error("query.wait()",err);
	}
	return query;
}

FBProxy.ui = function(params, callBackFunction) {
	try {
		FB.ui(params, function(response) {
			if (response && (response.error || response.error_msg)) {
				FBProxy.Logger.error("FB.ui("+params+")",response.error || response.error_msg);
			}
			callBackFunction(response);
		});
	} catch(err) {
		FBProxy.Logger.error("FB.ui("+params+")",err);
		return query;
	}
}
