function MyFacebookLib() {	
	var me = this;
	
	/*
	FB is defined in init_facebook.js on DOM load
	*/
	
	var options_func = "";
	var options_func_params = false;
	var options_func_oncomplete = false;
	var options_func_onerror = false;
	var options_func_onlogout = false;
	var options_func_onloginok = false;
	var options_func_onlogincancel = false;
	var options_func_onpermsallow = false;
	var options_func_onpermscancel = false;
	var options_func_perms = false;//"read_stream,publish_stream,offline_access,photo_upload"
	
	me.set = function(options) {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.set()");
		
		if(options) {
			options_func = options.func;
			options_func_params = options.params;
			options_func_oncomplete = options.oncomplete;
			options_func_onerror = options.onerror;
			options_func_onlogout = options.onlogout;
			options_func_onloginok = options.onloginok;
			options_func_onlogincancel = options.onlogincancel;
			options_func_onpermsallow = options.onpermsallow;
			options_func_onpermscancel = options.onpermscancel;
			options_func_perms = options.perms;
		}
	};
	
	me.init = function() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.init()");
		
		executeAction();
	};
	
	function executeAction() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.executeAction()");
		
		try {
			var result = MyLib.FunctionHandler.callFunction(options_func, options_func_params);
	
			if(isActionResultValid(result)) {
				var func_oncomplete = options_func_oncomplete;
			
				reset();
			
				MyLib.FunctionHandler.callFunction(func_oncomplete, result);
			}
			else {
				switch(result) {
					case "LOGIN REQUIRED": 
						logoutAndLogin(); 
						break;
					case "NOT ENOUGH PERMISSION": 
						handlePermissionsPrompt(); 
						break;
			
					//case "LIMIT REACHED": //call default
					//case "INTERNAL ERROR": //call default
					default:
						var func_onerror = options_func_onerror;
			
						reset();
			
						MyLib.FunctionHandler.callFunction(func_onerror, result);
				}
			}
		}
		catch(e) {
			onerror(e);
		}
	};
	
	function handleConnectToFacebook() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.handleConnectToFacebook()");
		
		try {
			//this shows the connect prompt. on connect, calls the executeAction method.
			FB.ensureInit(function() {
				FB.Connect.requireSession(
					function() {
						MyLib.FunctionHandler.callFunction(options_func_onloginok, false);
						
						executeAction();
					},
					function() {
						var func_onlogincancel = options_func_onlogincancel;
						
						reset();
						
						MyLib.FunctionHandler.callFunction(func_onlogincancel, false);
					},
					true
					/* 
					 * isUserHint:
					 *   true if is user action or false if is automatic. 
					 *   If false, the FB JS shows an extra popup so the user can click it and the FB JS opens the login window. 
					 *   If true the FB JS opens directly the login window. 
					*/
				);
			});
		}
		catch(e) {
			onerror(e);
		}
	};
	
	function logoutAndLogin() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.logoutAndLogin()");
		
		try {
			//force user to logout and reconnect with facebook. clears out the current fb session key as well.
			FB.ensureInit(function() {
				FB.Connect.logout(
					function() {
						MyLib.FunctionHandler.callFunction(options_func_onlogout, false);
						
						handleConnectToFacebook();
					}
				);
				
				$("#RES_ID_fb_pop_dialog_table #RES_ID_fb_logout_info").each(function(index, elm) {
					$("#RES_ID_fb_pop_dialog_table").css("display", "none");
				});
			});
		}
		catch(e) {
			onerror(e);
		}
	};
	
	function handlePermissionsPrompt() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.handlePermissionsPrompt()");
		
		try {
			var removed_elms = MyLib.FlashHandler.removeAllFlashObjects();
		
			if(options_func_perms) {
				//this shows the permissions prompt. on permissions granted, calls the publish method.
				FB.ensureInit(function() {
					FB.Connect.showPermissionDialog(
						options_func_perms,
						function(perms) {
							MyLib.FlashHandler.addRemovedFlashObjects(removed_elms);
	
							if(!perms) {
								var func_onpermscancel = options_func_onpermscancel;
				
								reset();
					
								MyLib.FunctionHandler.callFunction(func_onpermscancel, false);
							}
							else if(perms && perms.toString().indexOf(options_func_perms) >= 0) {
								MyLib.FunctionHandler.callFunction(options_func_onpermsallow, false);
				
								executeAction();
							}
						},
						false
					);
					
					$("#RES_ID_fb_pop_dialog_table .fb_connect_dialog_iframe").each(function(index, elm) {
						if(elm) {
							$("#RES_ID_fb_pop_dialog_table").height(400);
							$(elm).height(390);
						}
					});
					
					$("#RES_ID_fb_pop_dialog_table").css("z-index", 500);
				});
			}
			else {
				onerror("MyFacebookLib.options_func_perms cannot be undefined!");
			}
		}
		catch(e) {
			onerror(e);
		}
	};
	
	function isActionResultValid(result) {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.isActionResultValid()");
		
		if(result == "INTERNAL ERROR" || result == "LOGIN REQUIRED" || result == "NOT ENOUGH PERMISSION" || result == "LIMIT REACHED" || result.toString().substring(0,5) == "*****") {
			return false;
		}
		return true;
	};
	
	function reset() {
		MyLib.DebugHandler.showMessage("call MyFacebookLib.reset()");
		
		options_func = "";
		options_func_params = false;
		options_func_oncomplete = false;
		options_func_onerror = false;
		options_func_onlogout = false;
		options_func_onloginok = false;
		options_func_onlogincancel = false;
		options_func_onpermsallow = false;
		options_func_onpermscancel = false;
		options_func_perms = false;
	};
	
	function onerror(err) {
		var func_onerror = options_func_onerror;
		
		reset();
		
		MyLib.FunctionHandler.callFunction(func_onerror, false);
		
		if(err) {
			if(typeof err == "object") {
				MyLib.DebugHandler.showException(err);
			}
			else {
				MyLib.DebugHandler.showMessage(err);
			}
		}
	};
}
