
//Friend class
function Friend (id, link_id, name, image_url, page_url, x_pos, y_pos, z_pos, readonly)
{
  //class members
  this.id = id;
  this.link_id = link_id;
  this.name = name;
  this.image_url = image_url; 
  this.page_url = page_url;
  this.x_pos = x_pos;
  this.y_pos = y_pos;
  this.z_pos = z_pos;
  this.readonly = readonly;

  //client-side only
  this.parent = null;
  this.elem = null;
  this.in_edit_mode = false;
  this.draggableObj = null;
  
  this.view_node = null;
  this.edit_node = null;
  
  this.createDOM = function()
  {
    this.edit_node = $( Builder.node
                      ( 'div',
                        [ Builder.node
                          ( 'img',
                            { src: this.image_url, alt:this.name }
                          ),
                          Builder.node ( 'br' ),
                          Builder.node('div', { className:'friendName' }, this.name),
                           Builder.node ( 'a',{ href: ('?L=contacts.remove&chromeless=true&id='+this.link_id), onclick: ('deleteFriend('+this.link_id+',\''+this.name+'\')') },"x" )
                        ]
                      )
                    );
                    
                   
    this.view_node = $( Builder.node
                      ( 'a',
                        { href: this.page_url, onclick: ('window.location=\'' + this.page_url + '\';') },
                        [ Builder.node
                          ( 'img',
                            { src: this.image_url, alt:this.name }
                          ),
                          Builder.node ( 'br' ),
                          Builder.node('div', { className:'friendName' }, this.name)
                        ]
                      )
                   );
    
    this.elem = $( Builder.node
                    ( 'div',
                      { className: 'friend', id: ('friend_' + DOM_friends), style: 'position: absolute; left: ' + this.x_pos + '%; top: ' + this.y_pos + '%' },
                      [
                        this.edit_node,
                        this.view_node
                      ]
                    )
                  );

    this.elem.friendInstance = this;
    
    DOM_friends++;
    
    this.edit_node.hide();
    
    return this.elem;
  }
  
  this.makeTopmost = function()
  {
    this.friendInstance.parent.makeTopmost(this.friendInstance);
  }
  
  this.saveLink = function()
  {                      
    var position_data = {
                      link_id: this.link_id,
                      x_pos: this.x_pos,
                      y_pos: this.y_pos,
                      z_pos: this.z_pos
                    };
  
    new Ajax.Request(friendSystemInstallationPath + 'update_position.php',
      {
        method:'pos',
        parameters: { position_enc: Object.toJSON(position_data) }
      }
    );
  }
  
  this.setLayer = function(parent, z_pos)
  {
    this.parent = parent;
    
    var old_z = this.z_pos;
    
    this.z_pos = z_pos;
    
    this.elem.style.zIndex = 501 + this.z_pos;
    
    if(old_z != this.z_pos)
    {
      this.saveLink();
    }
  }
  
  this.dragStarting = function(draggable, mouse_event)
  {
    var friend = draggable.options.friendInstance;
        
    var windowsize = getWindowSize();
    
    //convert offsets back to pixels for dragging
    friend.elem.style.left = ((friend.x_pos / 100) * windowsize['width']) + 'px';
    friend.elem.style.top = ((friend.y_pos / 100) * windowsize['height']) + 'px';
  }

  this.dragEnding = function(draggable, mouse_event)
  {
    var friend = draggable.options.friendInstance;
    
    var cumOffset = draggable.element.cumulativeOffset();
    
    var windowsize = getWindowSize();
    
    friend.x_pos = (cumOffset['left'] / windowsize['width']) * 100;
    friend.y_pos = (cumOffset['top'] / windowsize['height']) * 100;
    
    //keep offsets in percentages, rather than pixels (as is used by dragging in scriptaculous)
    friend.elem.style.left = friend.x_pos + '%';
    friend.elem.style.top = friend.y_pos + '%';

    friend.saveLink();
  }
  
  this.makeDraggable = function(make_draggable)
  {
    if(make_draggable)
    {
      //if it is not yet draggable
      if(!this.in_edit_mode)
      {
        if(!this.readonly)
        {
          this.draggableObj = new Draggable(this.elem, {revert:false, onStart:this.dragStarting, onEnd:this.dragEnding, friendInstance:this});
        }

        this.edit_node.show();
        this.view_node.hide();

        this.elem.observe('mousedown', this.makeTopmost);
        
        this.in_edit_mode = true;
      }
    }
    else
    {
      if(this.in_edit_mode)
      {
        //if it is currently draggable
        if(this.draggableObj != null)
        {        
          this.draggableObj.destroy();
          this.draggableObj = null;
        }

        this.edit_node.hide();
        this.view_node.show();
        
        this.elem.stopObserving('mousedown', this.makeTopmost);
        
        this.in_edit_mode = false;
      }
    }
  }
}

//static function, creates a Friend object from a stdClass object
Friend.fromObject = function(link_obj)
{
  var friend = new Friend(link_obj.friend.id, link_obj.id, link_obj.friend.name, link_obj.friend.image_url, link_obj.friend.page_url, link_obj.x_pos, link_obj.y_pos, link_obj.z_pos, link_obj.readonly);
  
  return friend;
}

//counter to create a unique DOM ID for each friend
var DOM_friends = 0;


function deleteFriend(id,name){
	if(confirm('Odstranil bos frenda '+name+'. Si preprican ?')){
		//alert(id);
		window.location = "?L=contacts.remove&chromeless=true&id="+id;
		return true;
	}
	return false;
	
	
}
