
	// When our document is ready to be manipulated load up the FB JS Class Work
	$(document).ready(function() {
		FB.init("40155179f507fdcd1e187281bc2ebeab", "/xd_receiver.htm");
	});

	/* 
	 * This is a callback function for the FB:login button which we display on our
	 * page and it gets called when the user is trying to login via the javascript method of our site
	*/
	function connectToFB() {
		/*
		 * Now that the user has logged into facebook we can use the FB API to get some info about 
		 * the user and we can use the FB sessions to get there user id so lets get that info now
		 */
		var api = FB.Facebook.apiClient; 
		var uid = api.get_session().uid;

		var fields = new Array();
		fields[0] = 'name';
		fields[1] = 'pic';
		fields[2] = 'uid';
      
		/*
		 * This function call will get the users first name, profile picture, and user id from the API
		 */
		api.users_getInfo(uid, fields, function userStatus(result) {
    	  	var name = result[0]['name'];
	      	var pic = result[0]['pic'];
	      	var uid = result[0]['uid'];
	      	var refer = jQuery("input#refer").val();
	      	
	      	/*
	      	 * Send an AJAX request to the login script to do whatever with the info
	      	 */
	        $.ajax({
	        	 type: "POST",
	        	 url: "http://www.superadder.com/login.php",
	        	 data: "fid="+urlencode(uid)+"&name="+urlencode(name)+"&pic="+urlencode(pic)+"&submit=Login&new="+refer,
	        	 success: function(msg) {
              window.location = 'http://www.superadder.com/home.php';
             },
	        });
          
	        return true;
		});
      
		return true;
    }
    
    /*
     * This function is used so that we don't get any errors when sending our AJAX responce out
     */          
    function urlencode( str ) {                          
        var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
        var ret = str.toString();
        
        var replacer = function(search, replace, str) {
            var tmp_arr = [];
            tmp_arr = str.split(search);
            return tmp_arr.join(replace);
        };
        
        // The histogram is identical to the one in urldecode.
        histogram['!']   = '%21';
        histogram['%20'] = '+';
        
        // Begin with encodeURIComponent, which most resembles PHP's encoding functions
        ret = encodeURIComponent(ret);
        
        for (search in histogram) {
            replace = histogram[search];
            ret = replacer(search, replace, ret) // Custom replace. No regexing
        }
        
        // Uppercase for full PHP compatibility
        return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
            return "%"+m2.toUpperCase();
        });
        
        return ret;
    }