
//FriendsGroup class
function FriendsGroup(group_id, friends, group_name)
{
  //class members
  this.group_id = group_id;
  this.friends = friends;
  
  //client-side only
  this.group_name = group_name;
  this.elem = null;
  this.added = false;
  this.in_edit_mode = false;
  
  this.createDOM = function()
  {
    this.elem = $(
                  Builder.node
                  (
                    'div',
                    {
                      className: 'friends_wrapper',
                      id: 'friendGroup_' + DOM_groups
                    }
                  )
                );

    DOM_groups++;
        
    for(var i=0; i<this.friends.length; i++)
    {
      this.elem.insert( this.friends[i].createDOM(this.group_name, this.elem) );
    }
  }
  
  this.show = function(edit_mode)
  {
    if(!this.added)
    {
      var parent = $('groupsDiv');
    
      parent.insert(this.elem);
      
      //add click ignore for the divs
      for(var i=0; i<this.friends.length; i++)
      {
        this.friends[i].elem.observe('click', ignoreClick);
      }
      
      this.hide();
      
      this.added = true;
    }
    
    if(edit_mode != this.in_edit_mode)
    {
      for(var i=0; i<this.friends.length; i++)
      {
        this.friends[i].makeDraggable(edit_mode);
      }
      
      this.in_edit_mode = edit_mode;
    }
    
    this.elem.show();
  }
  
  this.hide = function()
  {
    this.elem.hide();
  }
  
  this.makeTopmost = function(friend)
  {
    for(var i=0; i<this.friends.length; i++)
    {      
      if(this.friends[i] == friend)
      {
        this.friends.splice(i, 1);
        
        this.friends.push(friend);
      }
      
      this.friends[i].setLayer(this, i);
    }
  }
}

FriendsGroup.load = function(group_id, group_name)
{ 
  new Ajax.Request(friendSystemInstallationPath + 'load_group.php',
    {
      method:'post',
      parameters: {group_id: group_id, group_name: group_name},
      onSuccess: function(transport) {
        if(transport.responseText)
        {
          var group_obj = transport.responseText.evalJSON();

          var link_objs = group_obj.links;
          
          var friends = [];
          
          for(var i=0; i<link_objs.length; i++)
          {
            var friend = Friend.fromObject(link_objs[i]);

            if(friend != null)
            {            
              friends.push(friend);
            }
          }
          
          var group = new FriendsGroup(group_id, friends, group_name);
          
          group.friends.sort(z_sort);

          group.createDOM();
                    
          for(var i=0; i<group.friends.length; i++)
          {
            group.friends[i].setLayer(group, i);
          }

          //group completed, add to list of groups    
          friend_groups[group_name] = group;

          // Next part is just for demo purposes..
          // At this point the DOM objects are created and available through the openGroup() function.
          // In this example, a button is added to the webpage which will trigger that function.
          $('testButtonForm').insert(
            Builder.node
            (
              'input',
              {
                type: 'button',
                value: 'Edit ' + group_name,
                onclick: 'openGroup(\''+group_name+'\', true);'
              }
            )
          );
          
          $('testButtonForm').insert(
            Builder.node
            (
              'input',
              {
                type: 'button',
                value: 'View ' + group_name,
                onclick: 'openGroup(\''+group_name+'\', false);'
              }
            )
          );
        }
      }
    }
  );
}

//counter to create a unique DOM ID for each group
var DOM_groups = 0;

function ignoreClick(event)
{
  event.stop();
}

function z_sort(a, b)
{
  return (a.z_pos - b.z_pos);
}