 
 //Refresh Status
 setInterval(function(){
   data_user();
   update_aktifitas_terakhir();
   update_otomatis();
   refresh_notif_chat();
   update_chat_otomatis_guest();
},5000);

function muat_ulang_chat_guest(username_guest,email_guest){
   $.ajax({
      url:"chat/refresh_dialog_guest.php",
      method:"POST",
      data:{email_guest:email_guest},
      success:function(data){
      $('#chat_history_guest'+username_guest).html(data);
      }
     })
 }

 function update_chat_otomatis_guest(){
   $('.chat_history_guest').each(function(){
     var username_guest = $(this).data('touserid');
     var email_guest = $(this).attr('email_guest');

     muat_ulang_chat_guest(username_guest,email_guest);
   });
}


function refresh_notif_chat(){
   $.ajax({
      url: 'chat/refresh_notification_chat.php',
      method : "GET",
   }).done(function(data){
   $('#chat').html(data);
   });
}

function data_user(){
    $.ajax({
           url: 'chat/user_data.php',
           method : "GET",
     }).done(function(data){
       $('.user_data').html(data);
    });
 }

function update_aktifitas_terakhir(){
   $.ajax({
      url: 'chat/update_aktifitas_terakhir.php',
      method : "GET",
   }).done(function(data){
   });
}

function update_status(to_user_id,from_user_id){
   $.ajax({
      url: 'chat/update_status_chat.php',
      method : "POST",
      data :{to_user_id:to_user_id, from_user_id:from_user_id}
   }).done(function(data){
   });
 } 


$(document).on('click', '.start_chat', function(){
   var to_user_id = $(this).data('touserid');
   var to_user_name = $(this).data('tousername');
   var from_user_id = $(this).data('fromuserid');
  
   make_chat_dialog_box(to_user_id, to_user_name);
  
   $("#user_dialog_"+to_user_id).dialog({
    autoOpen:false,
    width:400,
    show: {
      effect: "drop",
      duration: 1000
    },
    hide: {
      effect: "drop",
      duration: 1000
    }
   });
   
   $('#user_dialog_'+to_user_id).dialog('open');

   //untuk mengupdate status saat memulai chat
   update_status(to_user_id,from_user_id);

});  
 
function autoscroll(to_user_id){

   window.setTimeout(function() {
   var elem = document.getElementById('chat_history_'+to_user_id);
   elem.scrollTop = elem.scrollHeight;
   }, 1);

}

function autoscroll3(to_user_id){

   window.setTimeout(function() {
   var elem = document.getElementById('chat_history_guest'+to_user_id);
   elem.scrollTop = elem.scrollHeight;
   }, 1);

}

function muat_ulang_chat(to_user_id){
   $.ajax({
      url:"chat/muat_ulang_chat.php",
      method:"POST",
      data:{to_user_id:to_user_id},
      success:function(data){
      $('#chat_history_'+to_user_id).html(data);
      }
     })
 }


function update_otomatis(){
    $('.chat_history').each(function(){
      var to_user_id = $(this).data('touserid');
      muat_ulang_chat(to_user_id);
    });
}


$(document).on('click', '.send_chat', function(){

  var to_user_id   = $(this).attr('id');
  var chat_message = $('#chat_message_'+to_user_id).val();
  //mengambil value/isi pesan di chatbox susuai user

  if(chat_message == ''){
   // alert('Mohon Isi Pesan... ');
  }else{

   $.ajax({
         url: 'chat/insert_chat.php',
         method : "POST",
         data :{to_user_id:to_user_id, chat_message:chat_message}
      }).done(function(data){

         autoscroll(to_user_id);
         //mengosongkan input setelah mengirim pesan
         $('#chat_message_'+to_user_id).val('');
         //Hasil Chat akan di kirim ke chatstory 
         $('#chat_history_'+to_user_id).html(data);
      });
   }
   
 exit; 

});

function make_chat_dialog_box(to_user_id, to_user_name){

   var modal_content = '<div id="user_dialog_'+to_user_id+'" class="user_dialog" title="'+to_user_name+'">';
       modal_content += '<div style="" class="chat_history" data-touserid="'+to_user_id+'" id="chat_history_'+to_user_id+'">';
       modal_content += muat_ulang_chat(to_user_id);
       modal_content += '</div>';
       modal_content += '<div class="form-group">';
       modal_content += '<textarea rows="2" placeholder="Write a message..." style="border-radius: 10px; border-color:#c7f5c0;  background-color: #f8f8f8; border-width: 3px;" cols="46" user_id="'+to_user_id+'" name="chat_message_'+to_user_id+'" id="chat_message_'+to_user_id+'" class="form-control txtarea"></textarea>';
       modal_content += '</div><div class="form-group" align="right">';
       modal_content += '<button type="button" style="display:none;" name="send_chat" id="'+to_user_id+'" class="btn btn-success send_chat">Send</button></div></div>';
      $('#user_model_details').html(modal_content);
 
      autoscroll(to_user_id);
 
 } 


//send chat enter
$(document).on('keydown', '.txtarea', function(){

var to_user_id = $(this).attr('user_id');
var chat_message = $('#chat_message_'+to_user_id).val();

if (event.keyCode == 13) {

   if( $.trim(chat_message) == ''){
      // alert('Mohon Isi Pesan... ');
      }else{
      
      $.ajax({
            url: 'chat/insert_chat.php',
            method : "POST",
            data :{to_user_id:to_user_id, chat_message:chat_message}
   
         }).done(function(data){
   
            //mengosongkan input setelah mengirim pesan
            $('#chat_message_'+to_user_id).val('');
            //Hasil Chat akan di kirim ke chatstory 
            $('#chat_history_'+to_user_id).html(data);
            
            autoscroll(to_user_id);
         });

      }
}
exit; 
});


//send guest, val java script nya di guest js

$(document).on('click', '.send_guest', function(){

   var username = $(this).attr('id');

   var email_guest = $(this).attr('email');

   var chat_message = $('#chat_message_'+username).val(); 

   if(chat_message == ''){
   //  alert('Mohon Isi Pesan... ');
   }else{
       $.ajax({
           url: 'chat/reply_chat_guest.php',
           method : "POST",
           data :{chat_message:chat_message, email_guest:email_guest}
      
         }).done(function(data){         
           //mengosongkan input setelah mengirim pesan
           $('#chat_message_'+username).val('');
           //Hasil Chat akan di kirim ke chatstory 
           $('#chat_history_guest'+username).html(data);
           
           autoscroll3(username);
        });
    }
   
 });



 $(document).on('keydown', '.txtarea_toguest', function(){
   
   if (event.keyCode == 13) {

         var username = $(this).attr('username_guest');
         var email_guest = $(this).attr('email_guest');
         var chat_message = $('#chat_message_'+username).val();
      
         if( $.trim(chat_message) == ''){
         
            }else{
               
               $.ajax({
                  url: 'chat/reply_chat_guest.php',
                  method : "POST",
                  data :{chat_message:chat_message, email_guest:email_guest}
            
                }).done(function(data){
                  
                  //mengosongkan input setelah mengirim pesan
                  $('#chat_message_'+username).val('');
                  //Hasil Chat akan di kirim ke chatstory 
                  $('#chat_history_guest'+username).html(data);
                  
                  autoscroll3(username);
               });
   
         }
     
   }

   });