<?php
include("includes/site_info.php");
include('session_handler.inc');
require 'includes/facebook.php';

$WUserID = $_SESSION['Sess_UserId'];
$WUserType = $_SESSION['Sess_UserType'];
$WUserLastVisit = $_SESSION['Sess_LastVisit'];

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '132880206758943',
  'secret' => '16d42b51af2920ff3c03f9dbf7c0d5e2',
  'cookie' => true,
));

$user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $me = null;
  }
}
//print_r($me);
// Names and links
$app_name = "Wauhaha";
$app_url = $CONST_LINK_ROOT."/".$_SESSION['Sess_UserName']."&ref_id=".$_SESSION['Sess_referCode']; // Assumes application is at http://apps.facebook.com/app-url/
$inbox_url = $CONST_LINK_ROOT."/home.php"; 

?>	
	<div align="center" class="facebook "> <img src="images/facebook_large.jpg" alt="Facebook" width="100" height="100" border="0" /><br />
		<br />
		Start expanding your network today <br />
	<?php	
if ($me) {
?>

		<br />
		<input type="button" onclick="sendRequest(); return false;" value="Send Request to your network Now" class="button"/>
		<br /><br />
		<div id="resRequest"></div>
	
	<?php
} else {
?>
		<br />
		<fb:login-button autologoutlink="false" scope="email,offline_access,user_birthday">Invite Your Friends Now</fb:login-button>
	<?php
}
?>
	</div>
		<div id="fb-root-invite"></div>
<script type="text/javascript">
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '132880206758943',
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true, // parse XFBML
          oauth: true
        });
      };

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root-invite').appendChild(e);
		  
      }());
		
		function sendRequest() {
  			FB.ui({method: 'apprequests',
			 message: '<?=$me[name]?> has started using <?=$app_name?> and thought it\'s so cool even you should try it out!',
			 max_recipients: 25,
			 show_error: true,
			 title: 'Suggest <? echo $app_name; ?> to your friends - it\'s free!',
			 data: '<?=$_SESSION['Sess_referCode']?>'
  			}, requestCallback);
		}
		
		function requestCallback(response) {
        // Handle callback here
		  if (response && response.request_ids) {
		  		var reqCount = response.request_ids.length;
				
		  		document.getElementById("resRequest").innerHTML = 'Thank you for inviting ' + reqCount + ' of your friends to join <b><?=$app_name?></b>.<br><br><h2><a href="<?=$inbox_url?>">Click here to continue surfing <?=$app_name?></a>.</h2>';
			}
      }

</script>