
//relative path on server
var friendSystemInstallationPath = "modules/fsorter/server_pages/";

//store them in associative array
var friend_groups = new Object();
var current_group = null;

//globals
var shader;
var groupsDiv;
var closeButton;


function loadShader(parent)
{  
  shader = $(
              Builder.node
              (
                'div',
                { id: 'shader', style:'width:100%; height:100%' }
              )
           );
  
  parent.insert(shader);
  
  shader.hide();
}

function GroupsDiv(parent)
{  
  groupsDiv = $(
                  Builder.node
                  (
                    'div',
                    { id: 'groupsDiv', onclick:'closeGroup();' }
                  )
                );
              
  parent.insert(groupsDiv);
  
  groupsDiv.hide();
}

function loadCloseButton(parent)
{
  closeButton = $( Builder.node
                  ( 'div',
                    { id: 'closeButton' }, 
                    [ Builder.node
                      ( 'a',
                        { href: 'javascript:closeGroup();' },
                        [ Builder.node
                          ( 'img',
                            { src: 'modules/fsorter/pics/win_close_icon.gif' }
                          )
                        ]
                      )
                    ]
                  )
                );
                
  parent.insert(closeButton);
  
  closeButton.hide();
}

function openGroup(group_name, edit_mode)
{
  if(current_group == null)
  {
    if(friend_groups[group_name] != null)
    { 
      shader.show();
      
      friend_groups[group_name].show(edit_mode);
      
      groupsDiv.show();
      
      closeButton.show();
      
      current_group = friend_groups[group_name];
    }
  }
}

function closeGroup()
{
  if(current_group != null)
  {
    closeButton.hide();

    shader.hide();
    
    groupsDiv.hide();
    
    current_group.hide();
    
    current_group = null;
  }
}

function initFriendsSystem()
{
  var body = $$('body')[0];
  
  loadShader(body);
  
  GroupsDiv(body);
  
  loadCloseButton(body);
}

//fill this as an associative array with the names and ids of the groups you want loaded
var queuedGroupArray = new Array();

function QueuedGroup(id, name)
{
  this.id = id;
  this.name = name;
}

function queueGroup(id, name)
{
  var queuedGroup = new QueuedGroup(id, name);
  
  queuedGroupArray.push(queuedGroup);
}

function loadGroups()
{
  for(var i=0; i<queuedGroupArray.length; i++)
  {
    var queuedGroup = queuedGroupArray[i];
    
    FriendsGroup.load(queuedGroup.id, queuedGroup.name);
  }
}


Event.observe(window, 'load', initFriendsSystem);
Event.observe(window, 'load', loadGroups);



//utility method to get window size
function getWindowSize()
{
  var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
  var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);

  return { width: width, height: height };
}
