try {
  if(IALoid) {
$(document).ready(function(){
	$('.mail-load-block').css('display', "block");
  $('#button-mail').attr('type','button');
  var timer;

$('#message-textarea').on('keyup',function(e){
    var elm=$(this);
    var time=(new Date()).getTime();
    var delay=1000;
 
    elm.attr({'keyup':time});
    elm.off('keypress');
    elm.on('keydown',function(e){$(this).attr({'keyup':time});});
    elm.on('keypress',function(e){$(this).attr({'keyup':time});});
 
    setTimeout(function(){
        oldtime=parseFloat(elm.attr('keyup'));
        if(oldtime<=(new Date()).getTime()-delay & oldtime>0 & elm.attr('keyup')!='' & typeof elm.attr('keyup')!=='undefined'){
            IALoid.typing = false;
            elm.removeAttr('keyup');
        }
    },delay);
});
  $('#message-textarea').on('keyup', function(){IALoid.typing = true;});
  Intervals.push(setInterval(function () {
        if(IALoid.typing == true) {
          $.ajax({
               'url' : '/mail?act=typing',
               'type' : 'GET',
               'data' : {'id': IALoid.id, 't': 0},
               'beforeSend' : function(){
               },
               'success' : function(data){
               }
            });
        }
  }, 2000));
  Intervals.push(setInterval(function () {
    $.ajax({
               'url' : '/mail?act=typing',
               'type' : 'GET',
               'data' : {'id': IALoid.id, 't': 1},
               'beforeSend' : function(){
               },
               'success' : function(data){
                  data = parseInt(data);
                  if(parseInt(((new Date()).getTime()) / 1000) < data + 3) {
                    $('#message-typing').css('display','inline');
                  } else {
                     $('#message-typing').css('display','none');
                  }
               }
            });
  }, 1000));
});
Intervals.push(setInterval(function(){
message_load_data();
}, 1500));
$(window).scroll(function(){
   if (IALoid.scroll_start < IALoid.posts_count && ! IALoid.load && on_screen('.mail-load'))
    {     
       message_load_scrolling();
    }
});
function on_screen(elem){

var docViewTop = $(window).scrollTop(),
    docViewBottom = docViewTop + window.innerHeight,
    elemTop = $(elem).offset().top;
    var r = (( docViewBottom >= elemTop));
   
   return r;
}


function message_load_scrolling()
{
    if ( IALoid.scroll_start < IALoid.posts_count && ! IALoid.load )
    {
        $.ajax({
           'url' : '?act=posts_scrolling',
           'data' : {'start' : IALoid.scroll_start, 'id' : IALoid.id},
           'beforeSend' : function()
           {
               IALoid.load = true;
           },
           'success' : function(data)
           {
               IALoid.load = false;
               var j = $.parseJSON(data);
               var count = j.data.length;
               if ($('.mail').children(':last').attr('class') == '_line')
                  $('.mail ._line:last').remove();
               for(var i = 0; i < count; i++)
               {
                   var q = j.data[i];
                   var last = $('.mail .list:last');
                   var n = (q.id_user != IALoid.last_user);
                   IALoid.scroll_start++;
                   $('.mail').append((n ? '<div class="_line"></div>' : '') + '<div data-user="' 
+ q.id_user + '" class="list mail-message' + (q.new != 0 ? ' mail-message-new' : '' ) + '"> ' + (n ? '<div class="post-l mail-message-l"><a href="/id' +
q.id_user + '"><img src="' + q.ava + '" style="border-radius: 50%;width: 45px;height: 45px;"></a></div>' : '' ) + ' <div class="post-c mail-message-r"><span class="_fl-r _time _gray">' + q.time + '</span> ' + (n ? '<div dell><a href="/id'+q.id_user+'"><b>' + q.login +'</b></a></div>' : '' ) + '<span style="vertical-align:sub">' + q.message + 
'</span></div></div>');
                   IALoid.last_user = q.id_user;
                }
               if (IALoid.scroll_start >= IALoid.posts_count)
              		$('.mail-load').html('Все сообщения загружены');

            }
        });
    }
}

function message_load_data()
{
    $.ajax({
    'url': '?act=posts',
    'data': {'id': IALoid.id, 'start': IALoid.start,'active': Linesoc.activeWindow},
    'success' : function(data){
        //$('#data').html(data);
        var j = $.parseJSON(data);
        if(Linesoc.activeWindow) {
        if (j.read == "0")
        {
           $('div[data-user="' + IALoid.user + '"]').removeClass('mail-message-new');
        }
        $('div[data-user != "' + IALoid.user + '"]').removeClass('mail-message-new');
        }
        var count = j.data.length;
        if (count > 0)
           IALoid.start = j.data[0].id;
        for(var i = 0; i < count; i++)
        {
            
            var q = j.data[i];
            IALoid.scroll_start++;
            IALoid.posts_count++;
            var first = $('.mail .list:first');
            if (q.id_user == first.attr('data-user'))
            {
               first.find('div[dell]').remove();
            }
            $('.mail').prepend('<div data-user="' 
+ q.id_user + '" class="list mail-message mail-message-new"><div dell class="post-l mail-message-l"><a href="/id' +
q.id_user + '"><img src="' + q.ava + 
'" style="border-radius: 50%;width: 45px;height: 45px;"></a></div> <div class="post-c mail-message-r"><span class="_fl-r _time _gray">' + q.time + '</span> <div dell><a href="/id'+q.id_user+'"><span class="_name">' 
+ q.login +'</span></a></div><span style="vertical-align:sub">' + q.message + 
'</span></div></div>' + ( q.id_user != first.attr('data-user') ? '<div class="_line"></div>' : '' ));
        }
    }
    });
}
function message_create_post()
{
   if (!IALoid.load)
   {
      var msg = $('#message-textarea').val();
      $.ajax({
         'url' : '/mail?act=new_post',
         'type' : 'POST',
         'data' : {'id': IALoid.id, 'message': msg},
         'beforeSend' : function(){
            IALoid.load = true;
         },
         'success' : function(data){
         IALoid.load = false;
            
            var j = $.parseJSON(data);
            if(j.error != 0) {
            	Linesoc.addNote(j.error_string, 1);
            	//console.log(data);
            } else {
            	$('#message-textarea').val(null);
            	message_load_data();
            	Linesoc.delNote();
            }
         }

      });
   }
}}
} catch(e) {}