function friends_finder() {
    
    var self = this;
    this.proceeding = false;
    
    this.emails = new Array();
    this.names = new Array();    
    this.add_friend_to_array = function ()
    {
        jQuery("#friends_list").show();
        var _mail = jQuery("#friends_email").attr('value');
        var _name = jQuery("#friends_name").attr('value');                
        if (_mail.length>0)
        {
            this.emails.push(_mail+' ');
            this.names.push(_name+' ');
            jQuery("#friends_list>ul").append('<li class="friends-list-item">'+ this.emails.length + ') ' + _mail + ' (' + _name + ')</li>');                    
        }
    }
    
    this.add_friend_ = function ()
    {
        //jQuery(".data:first").clone().appendTo(".data:last").toggle();
        //jQuery(".data:last").clone();
        jQuery('.data:last').children().clone().insertAfter(jQuery('.friend-email:last').parent());
    }
    
    this.remove_friend_ = function ()
    {
        //console.log('test');
        if (jQuery(".friend-email").size() > 2)
        {
            jQuery(".friend-email:last").parent().remove();
            jQuery(".friend-name:last").parent().remove();            
        }
            //jQuery(".data:last").remove();
    }
    
    this.send_invites_ = function ()
    {        
        if (this.proceeding == false)
        {
            this.proceeding = true;
            this.cleararrays();
            jQuery(".friend-name").each(function(index)
                {
                    if (index > 0)
                    {                    
                        if (jQuery(this).val().length > 0)
                        {
                            self.names.push(jQuery(this).val());
                            //console.log(jQuery(this).val());                        
                        }                        
                    }
                            
                }
            );
            jQuery(".friend-email").each(function(index)
                {
                    if (index > 0)
                    {                    
                        if (jQuery(this).val().length > 0)
                        {
                            self.emails.push(jQuery(this).val());
                            //console.log(jQuery(this).val());     
                        }                        
                    }                        
                }
            );                
            this.send_invites();            
        } else {
            alert ('Please wait until the current invite request is finished');
        }

    }
    
    this.cleararrays = function ()
    {
        this.emails = new Array();
        this.names = new Array();
    }
    
    this.remove_friend_from_array = function ()
    {
        var _size = this.emails.size();
        if (_size > 0)
        {
            jQuery(".friends-list-item:last").remove();        
            this.emails.pop();
            this.names.pop();    
        }        
    }
    
    this.push = function(_email, _name)
    {
        this.emails.push(_email);
        this.names.push(_name);
    }
    
    this.send_invites = function(action)
    {
                
        var _email = jQuery("#friends_email").attr("value");        
        var _name = jQuery("#friends_name").attr("value");        
        var _size = this.emails.size();        
        if (_size > 0)
        {
            jQuery.post("ajax/friends_finder/send_invites.php", {'emails[]': this.emails, 'names[]': this.names}, function(data){
                if (data.length > 0)
                {
                	jQuery('#friend_finder_results_data').html(data);
                	jQuery('#friend_finder_results').show();
                    //jconfirm.jmessage(data, 'jconfirm.popup_hide()', 1);
                }
                if (typeof(action) != 'undefined' && action.length > 0) eval(action);                    
                //set_load_params('updated=true&is_manual=1');
                set_load_params('');                
                this.proceeding = false;

            });
        } else
        {
            if (_email && _email.length > 0)
            {
                _name += ' '; 
                jQuery.post("ajax/friends_finder/send_invites.php", {'emails[]': _email, 'names[]': _name}, function(data) {
                    if(data.length > 0)
                        jconfirm.jmessage(data);    
                    if (typeof(action) != 'undefined' && action.length > 0) eval(action);                         
                    //set_load_params('updated=true&is_manual=1');                                   
                    click_inboxTabs('my_contacts', 'friend_content');
                    set_load_params('');
                    this.proceeding = false;
                    
                });                                                                     
            }
            else
            {
            	this.proceeding = false;
            }
        }            
            
    }
    
    this.fetch_contacts = function(pointer)
    {
        var a=jQuery('#email').val(); 
        var b=jQuery('#password').val();
        if (a.length > 0 && b.length > 0)
        {
            setCookie('ff_username', a); 
            setCookie('ff_password', b); 
            tb_show('Friend Finder', 'inc/contacts/friend_finder_tb.php?height=500&width=720&TB_iframe=true', false); 
            pointer.blur();             
        } 
        return false;
    }
    
    this.show_list_empty = function()
    {
        jconfirm.jmessage('Your contact list is empty or your email address/password is incorrect. Try again or try another email account. Alternatively you can enter your friends email adresses manually.');
        tb_remove();
    }
    
}

var ff = new friends_finder();
