

(function() {
    
    $.fn.onlinechat = function() {
        $(this).each(function(){
            
            var roomid = 0;
            var type = 0;
            var msgbox = $('#message',$(this));
            var messages = $('.messages',$(this));
            var status = $('#refresh',$(this));
            var formula = $('.formula',$(this));
            var captcha;
            var question;
            
            var counter = null;
            
            $(':input',$(this)).each(function(){
                if ($(this).attr('name')) {
                    var name = $(this).attr('name').match('([a-z]+)$')[1];
                    
                    if (name == 'rid') {
                        roomid = $(this).val();
                    } else if (name == 'type') {
                        type = $(this).val();
                    } else if (name == 'question') {
                        question = $(this);
                    } else if (name == 'captcha') {
                        captcha = $(this);
                    }
                }
            });
            
            function refresh() {
                status.html('<strong>stahuji data</strong>');
                
                $.ajax({
                    url: '/ajax/onlinechat/messages/',
                    data: {rid: roomid, sort: 3, view: 'XSLT'},
                    dataType: 'html',
                    async: true,
                    success: function(data) {
                        var obj = $(data);
                        
                        // definovani chovani tlacitek pro odpoved a smazani
                        if (type > 1) {
                            $('.message',obj).each(function(){
                                // definice klicovych prvku zpravy
                                var _msg = $(this);
                                var _reply = $('.reply',_msg);
                                var _erase = $('.erase',_msg);
                                var _info = $('.info',_msg);
                                
                                var id = parseInt(_reply.attr('href').match('([0-9]+)$')[1]);
                                
                                // tlacitko odpovedet
                                _reply.click(function(){
                                    if (parseInt(question.val()) == id) {
                                        question.val('');
                                    } else {
                                        if (parseInt(question.val()) != 0) {
                                            $('.message.reply').removeClass('reply');
                                            $('.info.reply').removeClass('reply');
                                        }
                                        question.val(id);
                                    }
                                    
                                    _msg.toggleClass('reply');
                                    _info.toggleClass('reply');
                                }).css('visibility','visible');
                                
                                // tlacitko smazat
                                if (type > 2) {
                                    _erase.click(function(){
                                        _msg.addClass('erase');
                                        _info.toggleClass('erase');
                                        _reply.css('visibility','hidden');
                                        
                                        if (parseInt(question.val()) == id) {
                                            _reply.click();
                                        }
                                        
                                        $.ajax({
                                            url: '/ajax/onlinechat/erase/',
                                            data: {mid: id},
                                            dataType: 'html',
                                            async: false,
                                            success: function() {
                                                new Tooltip(new TooltipRequest({
                                                    url: '/ajax/messenger/'
                                                }));
                                            },
                                            error: function() {
                                                new Tooltip(new TooltipRequest({
                                                    type: 'error',
                                                    failure: 'Nepodařilo se spojit se serverem'
                                                }))
                                            }
                                        });
                                    }).css('visibility','visible');
                                }
                                
                                // po byl refresh a pritom oznacene pole?
                                // oznacim znova
                                if (parseInt(question.val()) == id) {
                                    question.val('');
                                    _reply.click();
                                }
                            });
                        }
                        
                        messages.replaceWith(obj);
                        messages = obj;
                        
                        status.html(15);
                        countdown();
                    },
                    error: function() {
                        new Tooltip(new TooltipRequest({
                            type: 'error',
                            failure: 'Chyba připojení: ztracen kontakt se serverem'
                        }));
                        
                        status.html('<strong>chyba připojení</strong>');
                    }
                });
            }
            
            function countdown() {
                if (!counter) {
                    counter = setInterval(countdown,1*1000);
                } else if (!parseInt(status.html())) {
                    if (counter) {
                        clearInterval(counter);
                        counter = null;
                    }
                    
                    refresh();
                } else {
                    status.html(parseInt(status.html())-1);
                }
            }
            
            $('form',$(this)).submit(function(){
                var elements = {
                };
            
                $(':input',$(this)).each(function(){
                    if ($(this).attr('name')) {
                        elements[$(this).attr('name').match('([-_a-z]+)$')[1]] = $(this).val();
                    }
                });
                
                if (msgbox.attr('disabled')) {
                    return false;
                }
                
                msgbox.attr('disabled','disabled');
                
                $.ajax({
                    url: '/ajax/onlinechat/send/',
                    data: elements,
                    dataType: 'html',
                    async: true,
                    success: function() {
                        new Tooltip(new TooltipRequest({
                            url: '/ajax/messenger/'
                        }));
                        
                        if (formula.length > 0) {
                            captcha.val('');
                            captcha.focus();
                            
                            $.ajax({
                                type: 'GET',
                                url: '/ajax/captcha/',
                                data: {sess: 'onlinechatuser'},
                                dataType: 'json',
                                success: function(data) {
                                    formula.html(data.formula + ' = ');
                                }
                            });
                        }
                        
                        msgbox.val('');
                        question.val('');
                        refresh();
                    },
                    error: function() {
                        new Tooltip(new TooltipRequest({
                            type: 'error',
                            failure: 'Nastala chyba při odesílání zprávy'
                        }));
                    },
                    complete: function() {
                        msgbox.removeAttr('disabled');
                    }
                });
                
                return false;
            });
            
            refresh();
        });
    }
    
}) (jQuery);


