function Friends(sid, user, friend_cats, tr) {
    var oThis = this;
    
    this.friends_popup = null;
    
    this.is_control_pressed = false;
    
    this.showIgnoreRemove = function(user_id, link) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        this.friends_popup = new PopupWindow(tr['MODULES_IGNORED_REMOVE_TITLE'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr['MODULES_IGNORED_REMOVE_BTN'], 'input-submit', function () {this.disabled=true; oThis.ignore_remove(user_id, this, link)} ),
                    new Button(tr['MODULES_IGNORED_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr['MODULES_IGNORED_REMOVE_DESC']));
            
            return cont;
        }
        
        this.friends_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
    this.ignore_remove = function (user_id, btn, link) {
        var request = {
            url: '/users/ajax_handle.php?action=remove_ignored',
            data: {
                user_id: user_id
            },
            onSuccess: function (res) {
                oThis.friends_popup.hide();
                delete oThis.friends_popup;
                
                fadeOut(link.parentNode, 70, function () {link.parentNode.parentNode.removeChild(link.parentNode);});
            },
            onFailure: function (res) {
                oThis.friends_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 1000);
            }
        }
        
        myAJAX(request);
    }
    
    
    
    this.checkAll = function (name) {
        var list = document.getElementsByName(name);
        
        for(var i=0; i<list.length; i++) {
            list[i].checked=true;
        }
    }
    
    this.uncheckAll = function (name) {
        var list = document.getElementsByName(name);
        
        for(var i=0; i<list.length; i++) {
            list[i].checked = false;
        }
    }
    
    this.showApproveFriend = function (user_id, link, name) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        if (!user_id) {
            user_id = new Object();
            var list = document.getElementsByName(name);
            for(var i=0; i<list.length; i++) {
                if (list[i].checked == true)
                    user_id[list[i].value] = list[i].value;
            }
        }
        
        var select = new DropDown(tr['PROFILE_SELECT_FRIEND_GROUP'], 'dropdown-std', null);
        
        this.friends_popup = new PopupWindow(tr['PROFILE_ADD_FRIEND'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr['PROFILE_ADD_FRIEND_BTN'], 'input-submit', function () {this.disabled=true; oThis.approve_friend(user_id, select.getValue(), this, link, name)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array(),
                add_cat = document.createElement('input');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_ADD_FRIEND_DESC']));
            
            add_cat.className = 'input-std';
            add_cat.value = tr['PROFILE_CREATE_FRIEND_GROUP'];
            add_cat.onfocus = function () {if (this.value == tr['PROFILE_CREATE_FRIEND_GROUP']) this.value = '';};
            add_cat.onblur = function () {if (this.value == '') this.value = tr['PROFILE_CREATE_FRIEND_GROUP'];};
            add_cat.onkeydown = function (event) {var value = this.value;oThis.get_key(event, function () {oThis.add_friends_group(value, select)});};
            
            add_elements.push(add_cat);
            
            var options = new Array();
            for(var i in friend_cats) {
                var opt = new DropDownOption(friend_cats[i], i, function (event) {select.selectOption(event, this.v, this.t);});
                options.push(opt);
            }
            
            select.setOptions(options);
            select.init();
            select.addElements(add_elements);
            cont.appendChild(select.get());
            
            return cont;
        }
        
        this.friends_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
    
    this.approve_friend = function (user_id, group_id, btn, link, name) {
        var request = {
            url: '/users/ajax_handle.php?action=approve_friend',
            data: {
                friend_id: user_id,
                group_id: group_id
            },
            onSuccess: function (res) {
                oThis.friends_popup.hide();
                delete oThis.friends_popup;
                
                if (typeof(user_id) == 'object') {
                    var list = document.getElementsByName(name),
                        is_all = true;
                    
                    for(var i=0; i<list.length; i++) {
                        if(user_id[list[i].value]) {
                            list[i].checked = false;
                            fadeOut(list[i].parentNode, 50);
                        } else {
                            is_all = false;
                        }
                    }
                    if (is_all) location.reload();
                } else {
                    fadeOut(link, 50, function () {link.parentNode.removeChild(link);});
                }
            },
            onFailure: function (res) {
                oThis.friends_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 1000);
            }
        }
        
        myAJAX(request);
    }
    
    
    
    
    this.showRemoveInvite = function (user_id, link, name) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        if (!user_id) {
            user_id = new Object();
            var list = document.getElementsByName(name);
            for(var i=0; i<list.length; i++) {
                if (list[i].checked == true)
                    user_id[list[i].value] = list[i].value;
            }
        }
        
        this.friends_popup = new PopupWindow(tr['PROFILE_REMOVE_INVITE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr['PROFILE_REMOVE_INVITE_BTN'], 'input-submit', function () {this.disabled=true; oThis.remove_invite(user_id, this, link, name)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_REMOVE_INVITE_DESC']));
            
            return cont;
        }
        
        this.friends_popup.setContentFunction ( content );
        this.friends_popup.setButtonFunction ( buttons );
        
        this.friends_popup.init();
        this.friends_popup.show();
        $('main_wraper').appendChild(this.friends_popup.get());
        this.friends_popup.position('center');
    }
    
    
    this.remove_invite = function (user_id, btn, link, name) {
        var request = {
            url: '/users/ajax_handle.php?action=remove_invite',
            data: {
                friend_id: user_id
            },
            onSuccess: function (res) {
                oThis.friends_popup.hide(); delete oThis.friends_popup;
                
                if (typeof(user_id) == 'object') {
                    var list = document.getElementsByName(name),
                        is_all = true;
                    
                    for(var i=0; i<list.length; i++) {
                        if(user_id[list[i].value]) {
                            list[i].checked = false;
                            fadeOut(list[i].parentNode, 30);
                        } else {
                            is_all = false;
                        }
                    }
                    if (is_all) location.reload();
                } else {
                    fadeOut(link, 30, function () {link.parentNode.removeChild(link);});
                }
                
                
            },
            onFailure: function (res) {
                oThis.friends_popup.hide(); delete oThis.friends_popup;
                
                if (typeof(user_id) == 'object') {
                } else {
                    fadeOut(link, 30, function () {link.parentNode.removeChild(link);});
                }
            }
        };
        myAJAX(request);
    }
    
    
    
    
    
    this.showAddFriend = function (user_id) {
        if (this.friends_popup) return;
        
        var select = new DropDown(tr['PROFILE_SELECT_FRIEND_GROUP'], 'dropdown-std', null),
            msg = document.createElement('textarea');
        
        this.friends_popup = new PopupWindow('Add Friend', 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr['PROFILE_INVITE_FRIEND_BTN'], 'input-submit', function () {this.disabled=true; oThis.add_friend(user_id, select.getValue(), msg, this)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array(),
                add_cat = document.createElement('input'),
                add_msg_cont = document.createElement('div'),
                add_msg_a = document.createElement('a');
            
            msg.className = 'textarea-std';
            msg.style.display = 'none';
            msg.onblur = function () {if (this.value == '') hide(this);};
            
            add_msg_a.href = '#';
            add_msg_a.appendChild(document.createTextNode(tr['PROFILE_WRITE_MESSAGE']));
            add_msg_a.onclick = function (event) {
                toggle(msg);
                
                if (msg.style.display == 'block') msg.focus();
                
                if (!event) event = window.event;
                
                if (event.stopPropagation) event.stopPropagation();
                event.cancelBubble = true;
                
                msg.onclick = function (e) {
                    if (!e) e = window.event;
                    
                    if (e.stopPropagation) e.stopPropagation();
                    e.cancelBubble = true;
                }
                
                document.onclick = function (e) {
                    if (!e) e = window.event;
                    
                    var rightclick = false;
                    
                    if (e.which) rightclick = (e.which == 3);
                    else if (e.button) rightclick = (e.button == 2);
                    
                    if (rightclick) return;
                    
                    if (msg.value == '') {
                        hide(msg);
                    }
                    
                    document.onclick = function (){};
                }
                
                return false;
            };
            
            add_msg_cont.style.zoom = 1;
            add_msg_cont.style.margin = '0 0 10px 0';
            add_msg_cont.appendChild(add_msg_a);
            add_msg_cont.appendChild(msg);
            cont.appendChild(add_msg_cont);
            
            
            
            //cont.appendChild(document.createTextNode());
            
            add_cat.className = 'input-std';
            add_cat.value = tr['PROFILE_CREATE_FRIEND_GROUP'];
            add_cat.onfocus = function () {if (this.value == tr['PROFILE_CREATE_FRIEND_GROUP']) this.value = '';};
            add_cat.onblur = function () {if (this.value == '') this.value = tr['PROFILE_CREATE_FRIEND_GROUP'];};
            add_cat.onkeydown = function (event) {var value = this.value;oThis.get_key(event, function () {oThis.add_friends_group(value, select)});};
            
            add_elements.push(add_cat);
            
            var options = new Array();
            for(var i in friend_cats) {
                var opt = new DropDownOption(friend_cats[i], i, function (event) {select.selectOption(event, this.v, this.t);});
                options.push(opt);
            }
            
            select.setOptions(options);
            select.init();
            select.addElements(add_elements);
            cont.appendChild(select.get());
            
            return cont;
        }
        
        this.friends_popup.setContentFunction ( content );
        this.friends_popup.setButtonFunction ( buttons );
        
        this.friends_popup.init();
        this.friends_popup.show();
        $('main_wraper').appendChild(this.friends_popup.get());
        this.friends_popup.position('center');
    }
    
    this.add_friend = function (user_id, group_id, msg, btn) {
        var message = '';
        
        if (msg.style.display != 'none') message = msg.value;
        
        var request = {
            url: '/users/ajax_handle.php?action=add_friend',
            data: {
                friend_id: user_id,
                group_id: group_id,
                msg: message
            },
            onSuccess: function (res) {
                oThis.friends_popup.removeButtons();
                oThis.friends_popup.addButton(new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ));
                oThis.friends_popup.showMessage(res);
            },
            onFailure: function (res) {
                oThis.friends_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 1000);
            }
        };
        myAJAX(request);
    }
    
    this.add_friends_group = function (title, select) {
        var request = {
            url: '/users/ajax_handle.php?action=add_friend_group',
            data: {
                title: title
            },
            onSuccess: function (res) {
                var cat_id = eval('('+res+')');
                
                if (select) {
                    select.addOption(title, cat_id, function (event) {select.selectOption(event, this.v, this.t);});
                    select.selectOption (null, cat_id, title);
                }
                
                friend_cats[cat_id] = title;
            }
        };
        myAJAX(request);
    }
    
    
    
    this.showRemoveAllFriends = function () {
        this.friends_popup && this.friends_popup.hide() && (delete this.friends_popup);
        
        this.friends_popup = new PopupWindow(tr['PROFILE_REMOVE_ALL_FRINEDS_BTN'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr['PROFILE_REMOVE_ALL_FRINEDS_BTN'], 'input-submit', function () {this.disabled = true; oThis.remove_all_friends(this)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_REMOVE_ALL_FRINEDS_CONFIRM']));
            
            return cont;
        }
        
        this.friends_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
        this.remove_all_friends = function (btn) {
            var request = {
                url: '/users/ajax_handle.php?action=remove_all_friends',
                data: {},
                onSuccess: function (res) { location.href = location.href; },
                onFailure: function (res) { oThis.friends_popup.showError(res); btn.disabled = false; }
            };
            myAJAX(request);
        }
    
    this.showAddGroup = function (link) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        var add_cat = document.createElement('input');
        
        this.friends_popup = new PopupWindow(tr['PROFILE_CREATE_FRIEND_GROUP_TITLE'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr['PROFILE_ADD_BTN'], 'input-submit', function () {this.disabled=true; oThis.add_friends_group_(add_cat.value, link)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                br = document.createElement('br');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_ADD_FRIEND_GROUP_DESC']));
            cont.appendChild(br);
            
            add_cat.className = 'input-std';
            cont.appendChild(add_cat);
            
            return cont;
        }
        
        this.friends_popup.setContentFunction ( content );
        this.friends_popup.setButtonFunction ( buttons );
        
        this.friends_popup.init();
        this.friends_popup.show();
        $('main_wraper').appendChild(this.friends_popup.get());
        this.friends_popup.position('center');
    }
    
    this.add_friends_group_ = function (title, link) {
        var request = {
            url: '/users/ajax_handle.php?action=add_friend_group',
            data: {
                title: title
            },
            onSuccess: function (res) {
                if (oThis.friends_popup) {
                    oThis.friends_popup.hide();
                    delete oThis.friends_popup;
                }
                
                var cat_id = eval('('+res+')'),
                    a = document.createElement('a'),
                    span = document.createElement('span');
                
                span.onclick = function () {
                    oThis.showRemoveGroup(cat_id, this.parentNode);
                    return false;
                }
                
                a.href = '/'+ user['usr'] +'/friends/'+ cat_id +'/';
                a.appendChild(span);
                a.appendChild(document.createTextNode(title));
                
                link.parentNode.insertBefore(a, link);
                
                friend_cats[cat_id] = title;
            }
        };
        myAJAX(request);
    }
    
    this.showRemoveGroup = function (group_id, link) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        this.friends_popup = new PopupWindow(tr['PROFILE_REMOVE_FRIEND_GROUP_TITLE'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr['PROFILE_REMOVE_BTN'], 'input-submit', function () {this.disabled=true; oThis.remove_friends_group(group_id, link)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_REMOVE_FRIEND_GROUP_DESC']));
            
            return cont;
        }
        
        this.friends_popup.setContentFunction ( content );
        this.friends_popup.setButtonFunction ( buttons );
        
        this.friends_popup.init();
        this.friends_popup.show();
        $('main_wraper').appendChild(this.friends_popup.get());
        this.friends_popup.position('center');
    }
    
    
    this.remove_friends_group = function (group_id, link) {
        var request = {
            url: '/users/ajax_handle.php?action=remove_friend_group',
            data: {
                group_id: group_id
            },
            onSuccess: function (res) {
                if (oThis.friends_popup) {
                    oThis.friends_popup.hide();
                    delete oThis.friends_popup;
                }
                
                delete friend_cats[group_id];
                
                fadeOut(link, 50, function () {link.parentNode.removeChild(link)});
            }
        };
        myAJAX(request);
    }
    
    this.renameGroup = function (e, group_id, title, obj) {
        if (!e) {
            e = window.event;
        }
        if (e.keyCode == 32 || e.keyCode > 40 || e.keyCode == 8) {
            if (obj.timeOut) {
                clearTimeout(obj.timeOut);
            }
            
            if (!$("group_save_aj")) {
               var save = document.createElement("div");
               save.className = "aj-save";
               save.id = "group_save_aj";
               save.style.display = "none";
               save.innerHTML = "saving..";
               obj.parentNode.appendChild(save);
            } else {
                var save = $("group_save_aj");
                hide(save);
            }
            
            if ($('friends_group_'+group_id)) {
                $('friends_group_'+group_id).innerHTML = title;
            }
            
            obj.timeOut = setTimeout(
                function () {
                    show(save);
                    var request = {
                        url: '/users/ajax_handle.php?action=rename_friend_group',
                        data: {
                            title: title,
                            group_id: group_id
                        },
                        onSuccess: function (res) {
                            hide(save);
                        }
                    };
                    myAJAX(request);
                }, 700);
        }
    }
    
    this.showGroupsDropDown = function (event, user_id, link, sel_cat) {
        select = new DropDown(tr['MODULE_FRIENDS_MOVE_TO'], 'dropdown-std', null);
        
        var options = new Array();
        for(var i in friend_cats) {
            if (i != sel_cat) {
                var opt = new DropDownOption(friend_cats[i], i, function () {oThis.move_friend(user_id, this.v, link.parentNode.parentNode.parentNode.parentNode)});
                options.push(opt);
            }
        }
        
        select.setOptions(options);
        select.init();
        select.toggleMenu(event);
        
        link.parentNode.parentNode.insertBefore(select.get(), link.parentNode);
        hide(link.parentNode);
    }
    
    this.move_friend = function (user_id, group_id, link) {
        var request = {
            url: '/users/ajax_handle.php?action=move_friend',
            data: {
                group_id: group_id,
                friend_id: user_id
            },
            onSuccess: function (res) {
                fadeOut(link, 50, function () {link.parentNode.removeChild(link)});
            }
        };
        myAJAX(request);
    }
    
    this.showRemoveFriend = function (user_id, link) {
        if (this.friends_popup) {
            this.friends_popup.hide();
            delete this.friends_popup;
        }
        
        this.friends_popup = new PopupWindow(tr['PROFILE_REMOVE_FRIEND_TITLE'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr['PROFILE_REMOVE_FRIEND_BTN'], 'input-submit', function () {this.disabled=true; oThis.remove_friend(user_id, link, this)} ),
                    new Button(tr['PROFILE_CLOSE_BTN'], 'input-button', function () {oThis.friends_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr['PROFILE_REMOVE_FRIEND_DESC']));
            
            return cont;
        }
        
        this.friends_popup.setContentFunction ( content );
        this.friends_popup.setButtonFunction ( buttons );
        
        this.friends_popup.init();
        this.friends_popup.show();
        $('main_wraper').appendChild(this.friends_popup.get());
        this.friends_popup.position('center');
    }
    
    
    this.remove_friend = function (user_id, link, btn) {
        var request = {
            url: '/users/ajax_handle.php?action=remove_friend',
            data: {
                friend_id: user_id
            },
            onSuccess: function (res) {
                if (oThis.friends_popup) {
                    oThis.friends_popup.hide();
                    delete oThis.friends_popup;
                }
                
                if(link) {
                    fadeOut(link, 50, function () {link.parentNode.removeChild(link)});
                } else {
                    location.reload();
                }
            },
            onFailure: function (res) {
                oThis.friends_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 1000);
            }
        };
        myAJAX(request);
    }
    
    
    
    this.get_key = function(e, okFunc) {
        if (!e) e = window.event;
        
        var asc = !e.keyCode ? (!e.which ? e.charCode : e.which) : e.keyCode;
        
        if (asc != 17) {
            // натиснат е клавиш
            if (asc == 13 || (this.is_control_pressed && asc == 83)) {
                // CTRL + ENTER или CTRL+S
                this.is_control_pressed = false;
                if(okFunc) okFunc();
            }
        } else if (asc == 17) {
            this.is_control_pressed = 1;
        } else {
            this.is_control_pressed = false;
        }
        
        
    }
    
    this.check_key = function(e, okFunc) {
        if (!e) e = window.event;
        
        var asc = !e.keyCode ? (!e.which ? e.charCode : e.which) : e.keyCode;
        
        
    }
}