var DD_ACTION_BASIC = 'basic';
var DD_ACTION_ACTIONITEM = 'actionitem';
var DD_ACTION_PROJECT = 'project';

function AvatarDropdown(params)
{
    var self = this;
    this.params = params;
    this.avatars = {};
    this.htmlids = {
        avatar_dropdown: 'avatar_dropdown',
        opt_profile: 'av_dd_opt_profile',
        opt_communicate: 'av_dd_opt_communicate'
    };
    this.show_for_item = null;
    this.item_data = null;
    
    this.init = function()
    {
        this.dropdown = $('#' + this.htmlids.avatar_dropdown);
        this.opt_profile = $('#' + this.htmlids.opt_profile);
        this.opt_communicate = $('#' + this.htmlids.opt_communicate);
        
        this.opt_profile.click(function(){ self.triggerOptProfile(); });
        this.opt_communicate.click(function(){ self.triggerOptCommunicate(); });
        
        $(document).click(function(e){ if ( !e.isPropagationStopped() ) self.hideDropdown(); });
    }
    
    /**
     * Register dropdown action for an avatar.
     * @param av_class string, css class of the avatar
     * @param uid int, id of the user that has this avatar
     * @param action string, type of the action that should be performed when user is selecting 'communicate' option
     * @param action_item_id string id of the connected item
     */
    this.registerAvatar = function(av_class, uid, action, action_item_id)
    {
        if ( !this.avatars[av_class] )
        {    
            this.avatars[av_class] = {uid: uid, action: action, action_item_id: action_item_id};
            
            // init live click
            $('.' + av_class).live('click', function(e){
                self.triggerDropdown(self.avatars[av_class], this, e);
            });
        }
    }
    
    this.triggerDropdown = function(data, item, e)
    {
        e.stopPropagation();
  
        if ( this.show_for_item == item )
        {
            this.hideDropdown();
            return true;
        }
        this.show_for_item = item;
        this.item_data = data;
        
        // don't show communicate option if user is clicking on own avatar
        if ( current_user_id == this.item_data.uid )
            this.opt_communicate.hide();
        else
            this.opt_communicate.show();
        
        this.dropdown.css('left', (e.pageX + 5));
        this.dropdown.css('top', (e.pageY + 5));
        this.dropdown.show();
        
        return true;
    }
    
    this.hideDropdown = function()
    {        
        this.dropdown.hide();
        this.show_for_item = null;
        this.item_data = null;
    }
    
    this.triggerOptProfile = function()
    {
        // show user's profile page
        window.open(this.params.profileurl + this.item_data.uid);
    }
    
    this.triggerOptCommunicate = function()
    {      
        if ( !confirm('Are you sure you want to Communicate with this person?') )
            return false;

        // create popup communicate, and communicate should be assigned to the proper item if needed
        CPopupController.getInstance().createCPopup({
            uid: this.item_data.uid,
            connect_type: this.item_data.action,
            connect_id: this.item_data.action_item_id
        });
    }
}
