/* Extracted from original login to be used as a general method for triggering the facebook login box
 * on the WiseStats site, accepts the url to redirect to after the user has allowed wisestats permission
 */
function triggerFacebookLogin()
{
		FB.login(handleSessionResponse, {perms: 'email'});
		function handleSessionResponse(response) {
				if(response.session) {
						// we should ask for permission to their email, if we can't get it then Fbook is not an option
						FB.api({
										method: 'fql.query',
										query:  'SELECT uid, name, email FROM user WHERE uid=' + FB.getSession().uid
								},
								function(response) {
										var user = response[0];
										var perms = response[1];
										var remem = $('#remember-me-box:checked').val();
										$.ajax({
												type: 'POST',
												url:  pronto.url('/user/auth/login/facebook'), 
												data:  { user: user, permissions: perms, facebook: 'true', remember_me: remem },
												dataType: 'json',
												success: function(data){
														if(data.failure) {
															// fail elegently :)
															window.location.replace("/");
														}if(data.url) {
															window.location.replace(data.url);
														} else {
															alert(data.url);
														}
												} 
										});
								}
					);
				}
		}
}

function FbLoginAndPostTo(url, jsonData)
{
	//Assigning the the local scope for access in the sessionResponse
	this.url = url;	
	this.jsonData = jsonData;

	FB.login(handleSessionResponse, {perms: 'email'});
	function handleSessionResponse(response) {
                if(response.session) {
                        // we should ask for permission to their email, if we can't get it then Fbook is not an option
                        FB.api({
                                        method: 'fql.query',
                                        query:  'SELECT uid, name, email FROM user WHERE uid=' + FB.getSession().uid
                                },
                                function(response) {
                                        var user = response[0];
                                        var perms = response[1];
                                        $.ajax({
                                                type: 'POST',
                                                url:  pronto.url(url),
                                                data:  { user: user, permissions: perms, facebook: 'true', login: false},
                                                dataType: 'json',
                                                success: function(data){
                                                        if(data.failure) {
                                                            // fail elegently :)
                                                            window.location.replace("/");
                                                        }
                                                }
										});

                                }
                    );
					
				}
        }
}


