   /***********************************************************************/
   /*                                                                     */
   /*  SocialContacts: all about a user's friends                         */
   /*                                                                     */
   /*  Methods:                                                           */
   /*                                                                     */
   /*  createFriendship(user_id)        -  creates social connection      */
   /*  cancelFriendship(user_id)        -  removes social connection      */
   /*  getSummary(targetcontainer)      -  renders list of friends        */
   /*  getManageList(targetcontainer)   -  renders managelist of friends  */
   /*                                                                     */
   /***********************************************************************/

   var SocialContacts = Class.create();
   
   SocialContacts.prototype = {
   
      // constructor
      initialize: function(user_id) {
         this.user_id = user_id;
      },
      
      
      
      // create social connection between this.user_id and given user_id
      createFriendship: function(user_id) {
         
         var url = 'ajax/socialcontacts-createfriendship.php';
         var pars = {
            user_id1: this.user_id,
            user_id2: user_id
         };
         
         new Ajax.Request(url, {
                  method: 'post',
                  parameters: pars, 
                  onComplete: function(transport) {
                        var response = transport.responseJSON;
                        if (response) { 
                           gui.handleResponse(response); 
                           
                           // refresh list, if called when managelist is visible
                           try {
                              // todo: remember sortorder of list
                              this.getManageList('myfriends_managelist', {sortorder:'nachname'});
                           } catch(e) { }
                           
                        }
                        
                  }.bind(this)
         });
         
         
      },
      
      // remove the waiting status in table social_contacts
      clearWaitingFriendship: function(user_id) {
         
        var url = 'ajax/socialcontacts-cancelfriendship.php';
            var pars = {
               user_id1: this.user_id,
               user_id2: user_id
            };
          
            new Ajax.Request(url, {
                     method: 'post',
                     parameters: pars, 
                     onComplete: function(transport) {
                           // this.workingindicator.switchOff();
                           var response = transport.responseJSON;
                           if (response) { 
                              gui.handleResponse(response); 
                           }
                           
                     }.bind(this)
             });
            
      },
      
      

      // unlink social connection between this.user_id and given user_id
      cancelFriendship: function(user_id) {
         
         var message="Wirklich aus Kontaktliste entfernen?";
         if (confirm(message)) {
            
            var url = 'ajax/socialcontacts-cancelfriendship.php';
            var pars = {
               user_id1: this.user_id,
               user_id2: user_id
            };
          
            new Ajax.Request(url, {
                     method: 'post',
                     parameters: pars, 
                     onComplete: function(transport) {
                           // this.workingindicator.switchOff();
                           var response = transport.responseJSON;
                           if (response) { 
                              gui.handleResponse(response); 
                              
                              // refresh list, if called from managelist
                              try {
                                 // todo: remember sortorder of list
                                 this.getManageList('myfriends_managelist', {sortorder:'nachname'});
                              } catch(e) { }
                              
                              // remove link from functionlist box 
                              try {
                                $('functionlink_cancelFriendship').remove();
                              } catch(e) { }
                              
                              // remove link from userlistitem
                              try {
                                $('user'+user_id+'_cancelFriendship').remove();
                              } catch(e) { }
                              
                           }
                           
                     }.bind(this)
             });
           
            alert('Kontakt wurde aus deiner Kontakliste entfernt!');
            var messagesbox = new MessageBox(this.user_id);
            messagesbox.notify_user_who_is_removed(user_id);        
         }
         
      },
      
     
      // render a compact icon list of friends (as small boxes)
      getSummary: function(targetcontainer) {
         
         // this.workingindicator.switchOn();
         
         var url = 'ajax/socialcontacts-getsummary.php';
         var pars = {
            user_id: this.user_id
         };
         
         new Ajax.Request(url, {
                  method: 'post',
                  parameters: pars, 
                  onComplete: function(transport) {
                        // this.workingindicator.switchOff();
                        var response = transport.responseJSON;
                        if (response) { 
                           gui.handleResponse(response);
                           $(targetcontainer).update(response.html);
                        }
                        
                  }.bind(this)
          });
         
      },
      
      
      // render a list of friends, each with manage/function buttons
      getManageList: function(targetcontainer, params) {
         
         // this.workingindicator.switchOn();
         
         var sortorder = 'nachname';
         if (params.sortorder) {
            sortorder = params.sortorder;
         }
         
         var url = 'ajax/socialcontacts-getmanagelist.php';
         var pars = {
            user_id: this.user_id,
            sortorder: sortorder
         };
         
         new Ajax.Request(url, {
                  method: 'post',
                  parameters: $H(pars).toQueryString(), 
                  onComplete: function(transport) {
                     
                        // this.workingindicator.switchOff();
                        var response = transport.responseJSON;
                        
                        if (response) { 
                           gui.handleResponse(response); 
                           $(targetcontainer).update(response.html);
                           
                           try {
                              $('friendsintro').update(response.intro);
                           } catch(e) { }
                        }
                        
                  }.bind(this)
          });
         
      }
      
      
   }
   
