print_r = function(a, dTab) {
	//initiate the return variable
	var ret = "";
	
	//the depth tabbing variable helps in indentation
	if(!dTab) dTab = "\t";
	
	//If the input variable is a collection object then iterate
	if(typeof(a) == 'object'){
		
		//foreach implementation in javascript
		for(var sub in a) {
			var val = a[sub];
			ret += "'" + sub + "' =>";
			
			//incase the value obtained is again a collection
			if(typeof(val) == 'object') {
			
				//drill it down by calling the print_r function recurrsively
				ret += "\n" + dTab + "[" + print_r(val, dTab + "\t") + "]\n" + (dTab.substring(0, (dTab.length-1)));
			} else {
				ret += " \"" + val + "\"";
			}
		}
	} else {
		//Not a collection
		ret = "'" + a + "' is of type '" + typeof(a) + "', not array/object.";
	}
	return ret;
}

function bindFacebookEvents() {
	
	FB.Event.subscribe('auth.login', function(response) {
		if (response.session) {
			if (response.perms) {
			} else {
			}
			location.reload(true);
		} else {
		}
	});

	FB.Event.subscribe('auth.logout', function(response) {
		$.ajax({
			url: "/solo/member/logout",
			success: function(){
				location.reload(true);
			}
		});
	});
	
	FB.Event.subscribe('auth.sessionChange', function(response) {
	});
	
	FB.Event.subscribe('auth.statusChange', function(response) {
		if (response.status == 'notConnected') {
			// Do something to log them out.
		}
	}); 
	
	$('.facebook_login_button').facebookLogin();
	$('.facebook_logout_button').facebookLogout();
};



// Plugin for creating login buttons
(function($) {
	$.fn.facebookLogin = function(options) {

		var defaults = {
			'perms':''
		};
		
		return this.each(function() {
			var relOptions = getRelOptions(this);
			var opts = $.extend(defaults, relOptions, options);
			$(this).click(function() {
				FB.login(function(response) {}, opts);
			});
		});
	};
	
	$.fn.facebookLogout = function(options) {

		var defaults = {
		};
		var opts = $.extend(defaults, options);
		
		return this.each(function() {
			$(this).click(function() {
				FB.logout();
			});
		});
	};
	
	function getRelOptions (object) {
		var raw = $(object).attr('rel');
		return eval('('+raw+')');
	}
	
})(jQuery);
