function Wall(sid, user_id, user_info, date_format, tr_disc, template, tr_popup) {
    var oThis = this;
    
    this.popup_window = null;
    
    this.templates = {
        vote_panel: template
    }
    this.tr = {
        WALL_USER_LIKE_THIS: '<a href="/{$user_usr}">{$user_name}</a> харесва това!'
    }
    
    this.last_del_btn = null;
    this.is_control_pressed = false;
    this.smiles = {
        ':)': '<img src="http://i.kefche.net/ic/smiles/smile.gif" alt=":)" />',
        ':-)': '<img src="http://i.kefche.net/ic/smiles/smile.gif" alt=":-)" />',
        ':(': '<img src="http://i.kefche.net/ic/smiles/sadsmile.gif" alt=":(" />',
        ':-(': '<img src="http://i.kefche.net/ic/smiles/sadsmile.gif" alt=":-(" />',
        ':D': '<img src="http://i.kefche.net/ic/smiles/bigsmile.gif" alt=":D" />',
        ':d': '<img src="http://i.kefche.net/ic/smiles/bigsmile.gif" alt=":D" />',
        '8)': '<img src="http://i.kefche.net/ic/smiles/cool.gif" alt="8)" />',
        ':O)': '<img src="http://i.kefche.net/ic/smiles/surprised.gif" alt=":O)" />',
        ':o)': '<img src="http://i.kefche.net/ic/smiles/surprised.gif" alt=":O)" />',
        ';)': '<img src="http://i.kefche.net/ic/smiles/wink.gif" alt=";)" />',
        ';-)': '<img src="http://i.kefche.net/ic/smiles/wink.gif" alt=";-)" />',
        ';(': '<img src="http://i.kefche.net/ic/smiles/crying.gif" alt=";(" />',
        ';-(': '<img src="http://i.kefche.net/ic/smiles/crying.gif" alt=";-(" />',
        ':*': '<img src="http://i.kefche.net/ic/smiles/kiss.gif" alt=":*" />',
        ':P': '<img src="http://i.kefche.net/ic/smiles/tongueout.gif" alt=":P" />',
        ':p': '<img src="http://i.kefche.net/ic/smiles/tongueout.gif" alt=":P" />',
        ':-P': '<img src="http://i.kefche.net/ic/smiles/tongueout.gif" alt=":-P" />',
        ':-p': '<img src="http://i.kefche.net/ic/smiles/tongueout.gif" alt=":-P" />',
        '|-)': '<img src="http://i.kefche.net/ic/smiles/sleepy.gif" alt="|-)" />', 
        'x=(': '<img src="http://i.kefche.net/ic/smiles/angry.gif" alt="x=(" />',
        'X=(': '<img src="http://i.kefche.net/ic/smiles/angry.gif" alt="x=(" />',
        ':-S': '<img src="http://i.kefche.net/ic/smiles/worried.gif" alt=":-S" />',
        ':-s': '<img src="http://i.kefche.net/ic/smiles/worried.gif" alt=":-S" />',
        '8-|': '<img src="http://i.kefche.net/ic/smiles/nerd.gif" alt="8-|" />'
    };
    
    this.editComment = function (msg_id, link, reply) {
        var msg = link.parentNode.getElementsByTagName('div')[0];
        
        if(!link.taEdit) {
            var br = document.createElement('br');
            link.formEdit = document.createElement('div');
            link.taEdit = document.createElement('textarea');
            link.saveBtn = document.createElement('input');
            
            link.saveBtn.type = 'button';
            link.saveBtn.value = 'Save';
            link.saveBtn.onclick = function () {
                oThis.save_comment(msg_id, link, reply);
            };
            
            link.taEdit.className = 'pr-wall-inp-textarea';
            
            msg.parentNode.appendChild(link.formEdit);
            link.formEdit.appendChild(link.taEdit);
            link.formEdit.appendChild(br);
            link.formEdit.appendChild(link.saveBtn);
            
            show(msg);
            
            var comm = msg.innerHTML;
            comm = comm.replace(/<img[^>]+alt="([^"><]+?)".*?>/gi, '$1');
            link.taEdit.value = br2nl(comm);
        }
        
        toggle(link.formEdit);
        toggle(msg);
    }
        this.save_comment = function (msg_id, link, reply) {
            var request = {
                url: '/modules/wall/ajax_handle.php?action=save_comment',
                data: {
                    user_id: user_id,
                    msg_id: msg_id,
                    comment: link.taEdit.value,
                    reply: reply
                },
                onSuccess: function (res) {
                    var comm = link.taEdit.value,
                        msg = link.parentNode.getElementsByTagName('div')[0];
                    
                    hide(link.formEdit);
                    show(msg);
                    
                    for(var smilie in oThis.smiles) {
                        var regex = new RegExp(RegExp.escape(smilie), "g");
                        comm = comm.replace(regex, oThis.smiles[smilie]);
                    }
                    
                    msg.innerHTML = nl2br(comm);
                }
                
            };
            myAJAX(request);
        }
    
    this.move_panel_down = function (module_id, link) {
        var list = document.getElementsByName('module_position[]'),
            to = 0;
        
        // Намираме елемента след него
        
        for(var i=0; i<list.length; i++) {
            if (list[i].value == module_id)
                to = i;
        }
        
        if (list[to+1]) {
            
            var request = {
                url: '/users/ajax_handle.php?action=move_module',
                data: {
                    module_id: module_id,
                    move: 'down'
                },
                onSuccess: function (res) {
                    
                    if (to < list.length) {
                        var panel = list[to].parentNode.parentNode.parentNode,
                            to_panel = list[to+1].parentNode.parentNode.parentNode;
                        
                        fadeIn(panel, 30);
                        fadeIn(to_panel, 30);
                        to_panel.parentNode.insertBefore(to_panel, panel);
                    }
                }
                
            };
            myAJAX(request);
            
        }
    }
    
    this.move_panel_up = function (module_id, link) {
        var list = document.getElementsByName('module_position[]'),
            to = 0;
        
        // Намираме елемента преди него
        
        for(var i=0; i<list.length; i++) {
            if (list[i].value == module_id) {
                to = i;
            }
        }
        
        if (to-1 >= 0 && list[to-1]) {
            var request = {
                url: '/users/ajax_handle.php?action=move_module',
                data: {
                    module_id: module_id,
                    move: 'up'
                },
                onSuccess: function (res) {
                    
                    if (to > 0) {
                        var panel = list[to].parentNode.parentNode.parentNode,
                            to_panel = list[to-1].parentNode.parentNode.parentNode;
                        
                        fadeIn(panel, 30);
                        fadeIn(to_panel, 30);
                        panel.parentNode.insertBefore(panel, to_panel);
                    }
                }
                
            };
            myAJAX(request);
        }
    }
    
    
    this.vote = function (wall_id, obj) {
        var request = {
            url: '/modules/wall/ajax_handle.php?action=add_vote',
            data: {
                wall_id: wall_id
            },
            onSuccess: function (res) {
                oThis.add_vote(wall_id);
            }
            
        };
        myAJAX(request);
    }
    
    this.add_vote = function (wall_id) {
        var cont_id = this.templates.vote_panel.panel.id.replace('{$wall_id}', wall_id);
        
        if (!$(cont_id)) {
            var cont = document.createElement('div');
            
            cont.id = cont_id;
        } else {
            var cont = $(cont_id);
        }
        
        var vote = document.createElement('div');
        var transl = this.templates.vote_panel.panel.tr;
        
        vote.className = this.templates.vote_panel.panel.row_css;
        if (user_id == user_info.id) {
            vote.onmouseover = function () {
                oThis.showRemoveBtn(this);
            }
            vote.onmouseout = function () {
                oThis.hideRemoveBtn();
            }
        }
        
        vote.innerHTML = this.templates.vote_panel.panel.row_html.replace('{$usrid}', user_info.id).replace('{$wall_id}', wall_id).replace('DISCUSSIONS_DELETE', tr_disc['DISCUSSIONS_DELETE']).replace(transl, this.tr[transl]).replace('{$user_name}', user_info.name).replace(/\{\$user_usr\}/g, user_info.usr);
        cont.appendChild(vote);
        fadeIn(vote, 10);
    }
    
    this.add_event = function (obj, msg_div) {
        var comment_obj = obj.parentNode.getElementsByTagName('textarea')[0],
            comment = comment_obj.value;
        
        obj.disabled = true;
        var request = {
            url: '/modules/wall/ajax_handle.php?action=add_event',
            data: {
                comment: comment,
                user_id: user_id
            },
            onSuccess: function (res) {
                obj.disabled = false;
                comment_obj.value = '';
                
                if (res && res.length > 0) {
                    var div = document.createElement('div');
                    
                    div.innerHTML = res;
                    $('recent_activities').parentNode.insertBefore(div, $('recent_activities').nextSibling);
                    fadeIn(div, 10);
                }
            },
            onFailure: function (res) {
                obj.disabled = false;
                
                show(msg_div);
                setOpacity(msg_div, 100);
                msg_div.innerHTML = res;
                setTimeout(function () {fadeOut(msg_div, 70)}, 3000);
            }
            
        };
        myAJAX(request);
    }
    
    this.hide_event = function (wall_id, obj) {
        var request = {
            url: '/modules/wall/ajax_handle.php?action=remove_event',
            data: {
                wall_id: wall_id
            },
            onSuccess: function (res) {
                oThis.remove_event(obj.parentNode.parentNode.parentNode);
            }
            
        };
        myAJAX(request);
    }
    
    this.remove_event = function (obj) {
        fadeOut (obj, 70, function () { obj.parentNode.removeChild(obj); });
    }
    
    this.toggleComment = function (e, wall_id, obj) {
        if (!e) e = window.event;
        
        var comm_panel = $('comment_panel_'+wall_id);
        
        toggle(comm_panel);
        
        if (comm_panel.getElementsByTagName('textarea').length > 0)
            comm_panel.getElementsByTagName('textarea')[0].focus();
        
        this.handle_onclick(e, comm_panel);
    }
    
    this.comment = function (wall_id, obj) {
        var comment_obj = obj.parentNode.getElementsByTagName('textarea')[0],
            comment = comment_obj.value;
        
        obj.disabled = true;
        var request = {
            url: '/modules/wall/ajax_handle.php?action=add_comment',
            data: {
                wall_id: wall_id,
                comment: comment
            },
            onFailure: function (res) {
                obj.disabled = false;
            },
            onSuccess: function (res) {
                comment_obj.value = '';
                hide(obj.parentNode);
                
                var comm_id = eval('('+res+')');
                oThis.add_comment(wall_id, comment, comm_id);
            }
            
        };
        myAJAX(request);
    }
    
    this.add_comment = function (wall_id, comment, comm_id) {
        var cont = $('comments_panel_' + wall_id),
            comm = document.createElement('div');
        
        comm.className = this.templates.vote_panel.comment.css;
        comm.innerHTML = this.templates.vote_panel.comment.row_html
            .replace('DISCUSSIONS_DELETE', tr_disc['DISCUSSIONS_DELETE'])
            .replace(/\{\$comm_id\}/g, comm_id).replace('{$post_before}', tr_disc['DISCUSSION_POST_NOW'])
            .replace(/\{\$comment\}/g, comment)
            .replace('{$user_avatar}', user_info.avatar)
            .replace('{$user_name}', user_info.name)
            .replace(/\{\$user_usr\}/g, user_info.usr);
        for(var smilie in this.smiles) comm.innerHTML = comm.innerHTML.replace(smilie, this.smiles[smilie]);
        
        if (user_id == user_info.id) {
            comm.onmouseover = function () {
                oThis.showRemoveBtn(this);
            }
            comm.onmouseout = function () {
                oThis.hideRemoveBtn();
            }
        }
        
        if (cont.childNodes.length > 0) {
            cont.insertBefore(comm, cont.childNodes[0]);
        } else {
            cont.appendChild(comm);
        }
        
        fadeIn(comm, 5);
    }
    
    this.remove_comment = function(comm_id, obj) {
        var request = {
            url: '/modules/wall/ajax_handle.php?action=remove_comment',
            data: {
                comm_id: comm_id
            },
            onSuccess: function (res) {
                fadeOut(obj.parentNode, 100, function () {
                    obj.parentNode.parentNode.removeChild(obj.parentNode);
                });
            }
        };
        myAJAX(request);
    }
    
    this.remove_vote = function (wall_id, usrid, obj) {
        var request = {
            url: '/modules/wall/ajax_handle.php?action=remove_vote',
            data: {
                wall_id: wall_id,
                usrid: usrid
            },
            onSuccess: function (res) {
                fadeOut(obj.parentNode, 50, function () {
                    if (obj.parentNode)
                        obj.parentNode.parentNode.removeChild(obj.parentNode);
                });
            }
        };
        myAJAX(request);
    }
    
    this.showRemoveAll = function () {
        if (this.popup_window) {
            this.popup_window.hide();
            delete this.popup_window;
        }
        
        this.popup_window = new PopupWindow(tr_popup['WALL_DELETE_ALL_MESSAGES_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['WALL_DELETE_ALL_MESSAGES_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.wall_del_all();
                    }),
                    new Button(tr_popup['WALL_CLOSE_BTN'], 'input-button', function () {oThis.popup_window.hide(); delete oThis.popup_window;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr_popup['WALL_DELETE_ALL_MESSAGES_DESC']));
            
            return cont;
        }
        
        this.popup_window.display($('main_wraper'), content, buttons, 'center');
    }
        this.wall_del_all = function (btn) {
            this.popup_window.showWait();
            var request = {
                url: '/modules/wall/ajax_handle.php?action=delete_all',
                data: {},
                onSuccess: function (res) {
                    location.reload();
                }
            };
            myAJAX(request);
        }
    
    this.showRemoveBtn = function(obj) {
        this.hideRemoveBtn();
        
        var list = obj.getElementsByTagName('div'),
            btn = list[list.length-1];
        
        show(btn);
        
        this.last_del_btn = btn;
    }
    
    this.hideRemoveBtn = function (obj) {
        if (this.last_del_btn) {
            hide(this.last_del_btn);
        }
        if(obj) {
            this.hideSettings(obj);
        }
    }
    
    this.showSettings = function (obj) {
        var n = obj;
        
        do n = n.previousSibling;
        while (n.nodeType != 1);
        
        show(n);
        hide(obj);
    }
    
    this.hideSettings = function (obj) {
        var n = obj;
        
        do n = n.nextSibling;
        while (n.nodeType != 1);
        
        show(n);
        hide(obj);
    }
    
    this.selectPage = function (page, link) {
        var cont = link.parentNode;
        
        showWait(cont);
        var request = {
            url: '/modules/wall/ajax_handle.php?action=wall_page',
            data: {
                page: page,
                user_id : user_id
            },
            onSuccess: function (res) {
                location.href = '#profile-wall';
                hideWait(cont);
                $('wall_list').innerHTML = res;
                
                var list = cont.getElementsByTagName('a');
                for(var i=0; i<list.length; i++) list[i].className ='';
                link.className = 'sel';
            },
            onFailure: function (res) {
                alert(res);
            }
        };
        myAJAX(request);
    }
    
    this.handle_onclick = function (e, obj) {
        if (!e) e = window.event;
        
        if (e.stopPropagation) e.stopPropagation();
        e.cancelBubble = true;
        
        obj.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;
            
            hide(obj);
            document.onclick = null;
        }
        return false;
    }
    
    this.get_key = function(e) {
        if (!e) e = window.event;
        
        var asc = !e.keyCode ? (!e.which ? e.charCode : e.which) : e.keyCode;
        
        if (asc == 83 && this.is_control_pressed) {
            if (e.stopPropagation) e.stopPropagation();
            if (e.preventDefault) e.preventDefault();
            e.cancelBubble = true;
        }
        
        if (asc == 17) {
            this.is_control_pressed = 1;
        }
    }
    
    this.check_key = function(e, obj) {
        if (!e) e = window.event;
        
        var asc = !e.keyCode ? (!e.which ? e.charCode : e.which) : e.keyCode;
        
        if (asc != 17) {
            // натиснат е клавиш
            if (this.is_control_pressed) {
                
                if (asc == 13 || asc == 83) {
                    // CTRL + ENTER или CTRL+S
                    this.is_control_pressed = false;
                    obj.parentNode.getElementsByTagName('input')[0].onclick();
                }
            }
        } else {
            this.is_control_pressed = false;
        }
    }
    
    
    this.show_all = function (obj) {
        var list = obj.parentNode.getElementsByTagName('div');
        
        for(var i=0; i<list.length; i++) {
            if (list[i].className.indexOf('hide') >= 0 && list[i].className.indexOf('del_btn') < 0){
                list[i].className = list[i].className.replace(' hide', '');
                fadeIn(list[i], 30);
            }
        }
        
        obj.parentNode.removeChild(obj);
    }
}