/* 
 * Скрипт для отправки сообщений через AJAX
 * 
 */

$(document).ready(function() {
    $('.writeMsg').click(function() {
        $('#overflow').show();
        idTo = $(this).attr('idTo');
        nameSurname = $(this).attr('nameto');
        $('#msgNameSurname').val(nameSurname)
            
    });
        
    $('#overflow .delete').click(function() {
        $('#overflow').hide();
            
    });
        
    $('#sendMsg').click(function() {
        $('.hoverpanel').remove();
        noticeOk = "<div class='hoverpanel green'>Сообщение отправленно</div>";
        noticeErr = "<div class='hoverpanel red'>Сообщение не отправленно!</div>";
        textMsg = $('#textMsg').val();
        headerMsg = $('#headerMsg').val();
        $.post('/ajax/send-message', {
            idTo:idTo, 
            textMsg:textMsg, 
            headerMsg:headerMsg
        }, function(resp) {
            $('#overflow').hide();
            if(resp)
                $('body').append(noticeOk);
            else
                 $('body').append(noticeErr);
            $('.hoverpanel').delay(3000).fadeOut(1500, function(){
                $('.hoverpanel').remove();
            });
                    
    });
    return false;
});


 $('#replaySendMsg').click(function() {
        $('.hoverpanel').remove();
        noticeOk = "<div class='hoverpanel green'>Сообщение отправленно</div>";
        noticeErr = "<div class='hoverpanel red'>Сообщение не отправленно!</div>";
        idTo = $('#RidTo').val();
        textMsg = $('#RtextMsg').val();
        headerMsg = $('#RheaderMsg').val();
        $.post('/ajax/send-message', {
            idTo:idTo, 
            textMsg:textMsg, 
            headerMsg:headerMsg
        }, function(resp) {
            $('#overflow').hide();
            if(resp)
                $('body').append(noticeOk);
            else
                 $('body').append(noticeErr);
            $('.hoverpanel').delay(3000).fadeOut(1500, function(){
                $('.hoverpanel').remove();
            });
            window.location = '/communications/message/sent';
    });
    return false;
});





//Событие - выделить все сообщения.
$('#selectAllMsg').click(function(){
    selectAllMessages();
    return false;

})
        
//Событие - выделить не прочитанные
$('#selectUnread').click(function(){
    unceckAllMessages();
    selectUnread();
    return false;

})
        
//Событие - выделить все прочитанные
$('#selectReaded').click(function(){
    unceckAllMessages();
    selectReaded();
    return false;

})
        
        
        
//Обработчик удаления
$('#batchDeletion').click(function(){
    var box = new Array();
    i = 0;
    
    $("input:checkbox:checked").each(function(){
        box[i] = $(this).val()
        i +=1
    })
    $('#deletionData').val(box);

})

//Обработчик для to Active
$('#batchActive').click(function(){
    var box = new Array();
    i = 0;
    
    $("input:checkbox:checked").each(function(){
        box[i] = $(this).val()
        i +=1
    })
    $('#activeData').val(box);
    
})

//Обработчик для to Unactive
$('#batchUnactive').click(function(){
    var box = new Array();
    i = 0;
    
    $("input:checkbox:checked").each(function(){
        box[i] = $(this).val()
        i +=1
    })
    $('#unactiveData').val(box);
    
})



});



/*
$(document).ready(function() {
        $('.delMsg').click(function() {

            var idMsg = $(this).parent().get(0).firstChild.innerHTML;
            $.post('/ajax/delete-message', {id:idMsg}, function() {
                location.reload(true);
            });
        });

    });

*/

function selectAllMessages(){
    $(".docs input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
}

function selectUnread(){
    $(".docs .unread input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
}

function selectReaded(){
    $(".docs .readed input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
}

function unceckAllMessages(){
    $(".docs input[type='checkbox']:not([disabled='disabled'])").attr('checked', false);
}

