﻿function update_user_box() {
  var user_box = document.getElementById("user");
  // add in some XFBML. note that we set useyou=false so it doesn't display "you"
  user_box.innerHTML =
      "<span>"
    + "<fb:profile-pic uid='loggedinuser' facebook-logo='false' size='square' style='float:left; margin:2px;'></fb:profile-pic>"
    + "<fb:name uid='loggedinuser' useyou='false'></fb:name><br/>Úspěšně propojeno s&nbsp;Facebookem" 
    + "</span><div style='clear:both;'></div>";
    
    send_xmlhttprequest(function() {}, 'GET', 'addAppUser.php');
    

    
    //if(window.location.pathname = "/akce.html") window.location.reload();
    
    
    FB.XFBML.Host.parseDomTree();
    document.getElementById("FBconnect").innerHTML = "";
    
    //FB.Connect.showPermissionDialog("rsvp_event");
    
    }
    
  
function pageReload() {
  window.location.reload();
}
    
function eventRVSP(fbeid, state) {
  FB.Facebook.apiClient.users_hasAppPermission('rsvp_event', function(result){
                                        if(!result) {
                                          FB.Connect.showPermissionDialog("rsvp_event", function(x){
                                                      if(x == 'rsvp_event') doEventRVSP(fbeid, state);
                                                      else alert('Aby stránka Brooklyn mohla měnit tvoje RSVP statusy, potřebuje od tebe oprávnění.\nBez něj nelze tuto funkci použít a je potřeba účast potvrdit přímo na Facebooku.');
                                          });
                                        } else {
                                          doEventRVSP(fbeid, state);
                                        }
                                  } );

}
    
    
function doEventRVSP(fbeid, state) {
  if (!send_xmlhttprequest(eRVSPRet, 'GET', 'rvsp.php?fbeid=' + fbeid + '&state=' + state)) {
        alert("Chyba připojení. Zkuste to prosím později.");
    }
  if(state == 'attending') {
    document.getElementById('attending' + fbeid).style.color = "green";
    document.getElementById('unsure' + fbeid).style.color = "white";
    document.getElementById('declined' + fbeid).style.color = "white";
  }
  
  if(state == 'unsure') {
    document.getElementById('attending' + fbeid).style.color = "white";
    document.getElementById('unsure' + fbeid).style.color = "orange";
    document.getElementById('declined' + fbeid).style.color = "white";
  }

  if(state == 'declined') {
    document.getElementById('attending' + fbeid).style.color = "white";
    document.getElementById('unsure' + fbeid).style.color = "white";
    document.getElementById('declined' + fbeid).style.color = "red";
  }

}

function eRVSPRet(xmlhttp) {
  if (xmlhttp.readyState == 4) {
    //alert("aaa" + xmlhttp.responseText);
  }
}
    
function send_xmlhttprequest(obsluha, method, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

