  //lme test
  //api_key = "4bb3578ade6597c05a18d03de4c58843";  
  
  //live
  api_key = "cc01cce147e064bb93fff224dd6a0a0b";

  //local
  //tbcode = 106019826690;
  //test server
  //tbcode = 105170746690;
  //live
  tbcode = 115605875876;
  
  //FF error
  //window.attachEvent("onload", fbInit);
  fbInit();
  
  //initialise facebook connection
  function fbInit()
  {
    try
    {   
      FB.init(api_key,"xd_receiver.html");
      
      FB.Connect.ifUserConnected(fbUserIsConnected);
      
    }
    catch(e)
    {
        setTimeout("fbInit()", 400);
    }
  }
  
  //send template data to facebook
  function fbConnect()
  {
    
    try
    {
      FB.Connect.showFeedDialog(tbcode, templateData)
    }
    catch(e)
    {
      setTimeout("fbConnect()", 400);
    }
  }
  
  //if user is connected
  function fbUserIsConnected()
  {
    //update the connect button
    fbUpdateConnectButton();
    //hide the custom connect link
    fbHideConnectLink();
    
    //log user in to LME if associated 
    //account exists
    //set/check auto login cookie so
    //this doesnt run on logout
    //fbConnectAccount();
    
    //get some user profile data
    var viewer  = FB.Facebook.apiClient.fql_query(    
      'SELECT name FROM user WHERE uid='+FB.Facebook.apiClient.get_session().uid,   
      function(results) {
        //alert( results[0].name, results[0].pic_square_with_logo, results[0].profile_url)
      }
    )
    
  }
  
  //onconnect 
  function fbJustConnected()
  {
      //update the connect button
      fbUpdateConnectButton();
      //check if facebook account is linked with LME account
      fbConnectAccount();
  }
  
  //update the connect button  to logout button 
  function fbUpdateConnectButton()
  {
    document.getElementById("fb_login").innerHTML = '<a href="javascript: void(0);" onclick="FB.Connect.logoutAndRedirect(\'user_logout.php\');return false;" ><img id="fb_logout_image" src="http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif" alt="Connect"/></a>';
  }

  //hide the custom connect link  
  function fbHideConnectLink()
  {
    try
    {
      document.getElementById("fb_connect_link").innerHTML = '';
    }
    catch(e)
    {}
  }
  
  //Ajax
  function GetXmlHttpObject()
  {
    var xmlHttp=null;
  
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    return xmlHttp;
  }

  //check if facebook account is linked with LME account  
  //using Ajax and fbconnect.php
  function fbConnectAccount()
  {
    
    xmlHttp=GetXmlHttpObject();
    
    xmlHttp.onreadystatechange=function()
    {
          
      if(xmlHttp.readyState==4)
      {
        
        if(xmlHttp.responseText)
        {
          signuppage = "fblogin.php?"
          
          response = xmlHttp.responseText;
          //alert(response)
          switch(response)
          {
            case "signup":
              location.href=signuppage;
            break;
            case "refresh":
              location.reload();
            break;
 
          }
        }           
      }
          
    }
    
    url="fbconnect.php?";
    
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
      
  }

  //callback function for facebook logout 
  //to also logout of main account
  function fbLmeLogout(delay){
    setTimeout("function(){location.href='user_logout.php}", delay);
  }

