<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs  title="Invitations"
  author="Frederic DROUET"
  author_email="fdrouet@exoplatform.com"
  thumbnail="images/gadget-social-inbox-icon_96x96.png">
    <Require feature="settitle" />
    <Require feature="dynamic-height" />
    <Require feature="opensocial-0.8" />
    <Require feature="views" />
    <Locale messages="locales/default.xml" />
    <Locale lang="fr" messages="locales/fr.xml" />
  </ModulePrefs>
  <Content type="html">
    <![CDATA[     
    <head>
      <title>__MSG_title__</title>
      <link rel="stylesheet" type="text/css" href="/exo-gadget-resources/skin/exo-gadget/gadget-common.css" />
      <style type="text/css">
        
        .requests_container {
          display: none;
        }
        .requests_container, .requests_title {
          width: 100%;
        }
        .requests_title {
          font-weight: bold;
          font-size: 14px;
          text-decoration: underline;
          padding-bottom: 10px;
        }
        .nb_container {
          /*float: right;*/
          min-width: 20px;
          margin-right: 20px;
          -moz-border-radius: 10px;
          border-radius: 10px;
          text-align: center;
          padding-top: 1px;
          padding-bottom: 1px;
          padding-right: 3px;
          padding-left: 3px;
        }
        .nb_no, .nb_yes {
          border-style: solid;
          border-width: 1px;
          font-weight: bold;
        }
        .nb_no {
          color: transparent;
          border-color: transparent;
          background-color: transparent;
        }
        .nb_yes, .nb_yes a {
          border-color: red;
          background-color: red;
          color: white;
        }
        
        .light_message {
          color: lightgray;
        }
        
        .customLink {
           text-decoration:underline;
           color: #226ab4;
           cursor:pointer;
         }
		 
		 .UIgadgetthemes .Gadcont span {
			font-weight: bold;
			color: #1A1A1A;
		 }
        
      </style>
      <script language="javascript" type="text/javascript" src="scripts/jquery/1.5.2/jquery.min.js"></script>
      <script language="javascript" type="text/javascript" src="scripts/jquery/plugins/jquery.timers-1.2.js"></script>
      <script type="text/javascript">
        function loadProfile() {
          // Adding eXo Platform container information
          var opts = {};
          opts[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [
            opensocial.Person.Field.PROFILE_URL,
            "portalName",
            "restContext",
            "host"];        
          var req = opensocial.newDataRequest();
          req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER, opts), 'viewer');
          req.send(onLoadProfile);
        }
        
        function onLoadProfile(data) {
          var viewer = data.get('viewer').getData();
          var viewerIdentityId = viewer.getId();
          var viewerProfileUrl = viewer.getField("profileUrl");
          var eXoUserID = viewerProfileUrl.substr(viewerProfileUrl.lastIndexOf('/') + 1);
          var hostName = viewer.getField('hostName');
          var portalName = viewer.getField('portalName');
          var restContext = viewer.getField('restContextName');
          var address = window.top.location.href;
          var baseContext = hostName + "/" + portalName + "/";
          var extensionContext = address.replace(baseContext, "");
          var extensionParts = extensionContext.split("/");
          var context = baseContext + extensionParts[0] + "/" + extensionParts[1];
          var url_Spaces = context + '/spaces';
          var url_InvitationsSpaces = context + '/invitationSpace';
          var url_InvitationsPeople = context + '/connections/myInvitations/' + eXoUserID;
          // for debuging purpose
          /*
          var config_items = [];
          config_items.push('<div>viewerIdentityId:' + viewerIdentityId + '</div>');
          config_items.push('<div>viewerProfileUrl:' + viewerProfileUrl + '</div>');
          config_items.push('<div>eXoUserID:' + eXoUserID + '</div>');
          config_items.push('<div>hostName:' + hostName + '</div>');
          config_items.push('<div>portalName:' + portalName + '</div>');
          config_items.push('<div>restContext:' + restContext + '</div>');
          config_items.push('<div>baseContext:' + baseContext + '</div>');
          config_items.push('<div>address:' + address + '</div>');
          config_items.push('<div>extensionContext:' + extensionContext + '</div>');
          config_items.push('<div>extensionParts:' + extensionParts + '</div>');
          config_items.push('<div>context:' + context + '</div>');
          config_items.push('<div>url_Spaces:' + url_Spaces + '</div>');
          config_items.push('<div>url_InvitationsSpaces:' + url_InvitationsSpaces + '</div>');
          config_items.push('<div>url_InvitationsPeople:' + url_InvitationsPeople + '</div>');
          $('<div/>', {'class': 'users',  html: config_items.join('')}).appendTo('#pending-requests-gadget');
          config_items.push('<div>:' + + '</div>');
          */
          // debuging end

          
          $("#connections_requests_nb").addClass("nb_no");
          $("#connections_requests_handler").html("<div class='light_message'>__MSG_no_new_contact__</div>");
          $("#space_requests_nb").addClass("nb_no");
          $("#space_requests_handler").html("<div class='light_message'>__MSG_no_new_space__</div>");
          showPeople();
                    
          //
          // fetch all received contact requests
          //
          var contacts_items = [];
          $.getJSON('/rest/gadgets/social-inbox/v1/contacts/'+ eXoUserID +'/pending', function(data) {
            $.each(data, function(i, relationRequests) {
              if( relationRequests.length > 0 ) {
                $('#connections_requests_nb').empty();
                $('#connections_requests_nb').removeClass();
                $('#connections_requests_nb').addClass("nb_container nb_yes");
                $('#connections_requests_nb').append(relationRequests.length);
                $('#connections_requests_handler').empty();
                $.each(relationRequests, function(key, relationRequest) {
                  var contactAvatar = "<a href='/portal/intranet/profile/" + relationRequest.relationshipId + "' target='_parent'>" + "<img style='width: 24px; height: 24px; border-width: 0px;' src='" + (relationRequest.avatarUrl == null ? "/social-resources/skin/ShareImages/Avatar.gif" : relationRequest.avatarUrl) + "'>" + "</img></a>";
                  var contactInfo = "<a href='/portal/intranet/profile/" + relationRequest.relationshipId + "' target='_parent'>" + "<b>" + relationRequest.requesterName + "</b>" + "</a>" +
                                       (relationRequest.postion == null ? "" : " (" + "<span style='font-size: smaller;'>" + relationRequest.postion + "</span>" + ")") + "<br/>";
                  var contactRow = "<tr>" +
                                      "<td style='width: 20px;'>" + contactAvatar + "</td>" +
                                      "<td style='padding-left: 3px;'>" + contactInfo + "</td>" +
                                      //"<td>" + "<a href='/portal/intranet/connections/myInvitations' target='_parent' style='float: right;'>Add</a>" + "</td>" +
                                   "</tr>";
                  
                  contacts_items.push(contactRow);
                });
              }
            });
            $("#connections_requests_handler").append(contacts_items.join(""));
            // adjust the gadget size
            gadgets.window.adjustHeight($("#social-inbox-gadget").get(0).offsetHeight);
          });

          //
          // fetch all received invitations to joins a Space
          //
          var items_spaces_requests = [];
          $.getJSON('/rest/gadgets/social-inbox/v1/spaces/'+eXoUserID+'/pending', function(data) {
            $.each(data, function(i, invitedSpaces) {
              if (invitedSpaces.length > 0) {
                $('#space_requests_nb').empty();
                $('#space_requests_nb').removeClass();
                $('#space_requests_nb').addClass("nb_container nb_yes");
                $('#space_requests_nb').append(invitedSpaces.length);
                $('#space_requests_handler').empty();
                $.each(invitedSpaces, function(key, invitedSpace) {
                  var spaceAvatar = "<img style='width: 24px; height: 24px; border-width: 0px;' src='" + (invitedSpace.avatarUrl == null ? "/social-resources/skin/ShareImages/SpaceImages/SpaceLogoDefault_61x61.gif" : invitedSpace.avatarUrl) + "' title='" + invitedSpace.description + "'>" + "</img>";
                  var spaceInfo = "<b>" + invitedSpace.prettyName + "</b>" + " (__MSG_members__: " + invitedSpace.invitedUsers.length + ")";
                                
                  var spaceRow = "<tr>" +
                                      "<td style='width: 20px;'>" + spaceAvatar + "</td>" +
                                      "<td style='padding-left: 3px;'>" + spaceInfo + "</td>" +
                                      //"<td>" + "<a href='/portal/intranet/invitationSpace' target='_blank' style='float: right;'>Add</a>" + "</td>" +
                                   "</tr>";
                  
                  items_spaces_requests.push(spaceRow);
                });
              }
            });
            $("#space_requests_handler").append(items_spaces_requests.join(""));
            // adjust the gadget size
            gadgets.window.adjustHeight($("#social-inbox-gadget").get(0).offsetHeight);
          });
        }
        
        function showPeople(){
          $("#space_requests").hide();
          $("#peopleLink").removeClass();
          $("#spaceLink").addClass("ThemeColor customLink");
          $("#connections_requests").show();
        }
        
        function showSpace(){
          $("#connections_requests").hide();
          $("#spaceLink").removeClass();
          $("#peopleLink").addClass("ThemeColor customLink");
          $("#space_requests").show();
        }

        function init() {
          $("#peopleLink").click(function () {
             showPeople();
             gadgets.window.adjustHeight($("#social-inbox-gadget").get(0).offsetHeight);
          });

          $("#spaceLink").click(function () {
             showSpace();
             gadgets.window.adjustHeight($("#social-inbox-gadget").get(0).offsetHeight);
          });
          
          loadProfile();
          $(document).everyTime("120s", "reloadPage", function() {
            loadProfile();
          }, 0);
        }
        
        gadgets.util.registerOnLoadHandler(init);
        
      </script>
    </head>
    <body>
      <div id="social-inbox-gadget" class="UIGadgetThemes UIYellowThemes">
        <div class="TitGad ClearFix">
          <div class="ContTit">__MSG_title__</div>
        </div>
        <div id="GadgetContent" class="GadCont">
            <div align="left"><div id="userIdDiv" class='ThemeColor customLink' style ="font-size:15px;"></div></div>
            <div align="left" style="padding-bottom: 12px;"><span id='peopleLink' class='ThemeColor customLink'>__MSG_people__</span> <span id="connections_requests_nb" class="nb_container"></span> | <span id='spaceLink' class='ThemeColor customLink'>__MSG_space__ </span> <span id="space_requests_nb" class="nb_container"></span> </div>
        
            <div id="connections_requests" class="templates requests_container">
              <table id="connections_requests_handler" cellpadding="2" style="margin-left: 5px; width:90%">
              </table>
              <div style="text-align: center; padding-top: 5px;"><a href='/portal/intranet/connections/receivedInvitations' target='_parent'>__MSG_find_more__</a></div>
            </div>
          
            <div id="space_requests" class="templates requests_container" style="display:none;">
              <table id="space_requests_handler" cellpadding="2" style="margin-left: 5px; width:90%">
              </table>
              <div style="text-align: center; padding-top: 5px;"><a href='/portal/intranet/invitationSpace' target='_parent'>__MSG_find_more__</a></div>
            </div>
        </div>
      </div>
    </body>
    ]]>
  </Content>
</Module>
