<?php date_default_timezone_set("Europe/Zurich"); ?>
<section id="profile" class="message-mob content_wrapper innerPage clearfix">
             <div class="messageWraper container">
                <div class="contactHeader clearfix">
                  <h1 class="inline noborder heading"><?php echo getLang('Messages'); ?></h1>
                  <i id="backtoContact" class="fa fa-long-arrow-left"></i>                 
               </div>

               <div id="msgChat" class="popmessage-wrapper clearfix">

                <div class="inbox-left">
                  <h4><?php echo getLang('Messages'); ?></h4>
                  <div class="searchContact">
                    <input type="text" id="search" placeholder="<?php echo getLang('Search_Contacts'); ?>">
                    <i class="fa fa-search"></i>
                  </div><?php //print_r($to_friend); die;?>
                  <ul id="moblist"  class="mCustomScrollbar light" data-mcs-theme="minimal-dark">
                    <?php if(!empty($to_friend)){
                        $i = 0; 
                        foreach ($to_friend as $t_friend) {
                          
						              $last_conv = array();
                          /*if($this->session->userdata('user_id')!='11330')
                          {
                            $last_conv = conversation($this->session->userdata('user_id'),$t_friend['user_id']);
                          }*/
                         ?> 
                          <li id="user_<?php if($t_friend['user_id']!=$this->session->userdata('user_id')){ echo $t_friend['user_id'];} else { echo $t_friend['from_user_id']; } ?>" class="<?php if($i==0){ echo 'active'; } ?> listUser" data-id="<?php  if($t_friend['user_id']!=$this->session->userdata('user_id')){ echo $t_friend['user_id'];} else { echo $t_friend['from_user_id']; } ?>">
                            <a href="javascript:void(0)" class="clearfix">
                             <div class="online_wrapper">
              							 <div class="mediumbgAvatar">
              								<img src="<?php if(!empty($t_friend) && $t_friend['filename']!='' && file_exists(FCPATH.'assets/userfiles/'.$t_friend['path'].'/80/'.$t_friend['filename'].'.'.$t_friend['extension'])){ echo base_url().'assets/userfiles/'.$t_friend['path'].'/80/'.$t_friend['filename'].'.'.$t_friend['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; }?>" alt="" srcset="<?php if(!empty($t_friend['filename']) && file_exists(FCPATH.'assets/userfiles/'.$t_friend['path'].'/80/'.$t_friend['filename'].'.'.$t_friend['extension'])){ echo  base_url().'assets/userfiles/'.$t_friend['path'].'/80/'.$t_friend['filename'].'.'.$t_friend['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 1x, <?php if(!empty($t_friend['filename']) && file_exists(FCPATH.'assets/userfiles/'.$t_friend['path'].'/160/'.$t_friend['filename'].'.'.$t_friend['extension'])){ echo  base_url().'assets/userfiles/'.$t_friend['path'].'/160/'.$t_friend['filename'].'.'.$t_friend['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 2x" >
                                        
              							 </div>
                              <?php if($t_friend['online_status']==1){ echo '<i class="fa fa-circle"></i>';}?> 
                            </div>
                             <div class="conversation_text">                     
                               <h5><?php echo $t_friend['username']; ?></h5>
                               <div class="action-btns">
                                <time class="last_talktime"><?php if(!empty($t_friend['created'])){ echo date('M d',$t_friend['created']); } ?></time>                         
                                <!--span><i class="fa  fa-trash"></i></span>-->
                               </div>                      
                               <p class="last_talk"><?php echo substr(strip_tags($t_friend['text']),0,20); ?></p>
                             </div>
                            </a>
                          </li>
                    <?php $i++; }
                      }
                    ?>  
                    <li class="">
                      <div class="ajax-loader_msg" style="display:none;">
                        <img src="images/Rolling.svg" class="img-responsive" />
                      </div>
                      <div class="more_user" data-id="6" style="cursor:pointer;text-align: center;">
                        
                      </div>
                    </li>
                  </ul>
                </div>


                <div class="inbox-right-container">
                  <div class="view_header chat_user chat_new_head">
                    <a href="javascript:void(0)" class="clearfix">
                       <div class="online_wrapper">
                      <div class="mediumbgAvatar">
								<img src="<?php if(!empty($latest_userDetails) && $latest_userDetails['filename']!='' && file_exists(FCPATH.'assets/userfiles/'.$latest_userDetails['path'].'/80/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension'])){ echo base_url().'assets/userfiles/'.$latest_userDetails['path'].'/80/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; }?>" alt="" srcset="<?php if(!empty($latest_userDetails['filename']) && file_exists(FCPATH.'assets/userfiles/'.$latest_userDetails['path'].'/80/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension'])){ echo  base_url().'assets/userfiles/'.$latest_userDetails['path'].'/80/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 1x, <?php if(!empty($latest_userDetails['filename']) && file_exists(FCPATH.'assets/userfiles/'.$latest_userDetails['path'].'/160/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension'])){ echo  base_url().'assets/userfiles/'.$latest_userDetails['path'].'/160/'.$latest_userDetails['filename'].'.'.$latest_userDetails['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 2x" >
                             
							 </div>
               <?php if($latest_userDetails['online_status']==1){ echo '<i class="fa fa-circle"></i>';}?>       
              </div>
                      <div class="view_header_content">
                        <h5><?php if(!empty($latest_userDetails)){ echo $latest_userDetails['username']; }?></h5>
                        <p><?php if(!empty($latest_userDetails)){ echo $latest_userDetails['accounttype']; }?></p>
                      </div>        
                    </a>
                    <div class="deleteWholeConv"><i class="fa fa-trash deleteCommunityWholeMessage" data-id="<?php echo $latest_userDetails['user_id']; ?>" title="Delete Conversation"></i></div>
                  </div>

                  <div class="view_content">
                    <span class="chat_message">
                      <div class="ajax-loader_chat" style="display:none;">
                        <img src="images/Rolling.svg" class="img-responsive" />
                        <span>Loading messsage...</span>
                      </div>
                    <?php if(!empty($latest_messages)){ ?>
                      <div id="chatwrapper"  class="chat_wrapper " style="overflow-y:auto;">
                      <!--<div id="chatwrapper"  class="chat_wrapper mCustomScrollbar light" data-mcs-theme="minimal-dark">-->
                          <time><?php echo date('M d',$latest_messages[count($latest_messages)-1]['created']); ?></time>
                          <?php
                          $normal=0; 
                          if($membershipUser['article_id'] == 5 && $userDetials['accounttype']=='gentleman' && $latest_userDetails['accounttype'] == 'lady')
                          {
                            echo '<div class="conversation seeAllUpgrade"><h3>'.getLang('Thank_you_for_using_our_community').'</h3><a class="upgardeButt" style="text-decoration: underline;" href="'.base_url().'profile/before_settings">'.getLang('upgrade').'</a></div>';
                            $normal=1; 
                          } 
                          elseif($membershipUser['article_id'] == 5 && $userDetials['accounttype']=='couple' && $latest_userDetails['accounttype'] == 'lady')
                          {
                            echo '<div class="conversation seeAllUpgrade"><h3>'.getLang('To_continue_upgrade_or_book').'</h3><a class="upgardeButt" style="text-decoration: underline;" href="'.base_url().'profile/before_settings">'.getLang('upgrade').'</a></div>';
                            $normal=1; 
                          } 
                          else
                          { 
                            foreach ($latest_messages as $message) { ?>
                              <div class="conversation <?php if($message['from_user_id']==$this->session->userdata('user_id')){ echo 'mysite'; } else { echo 'othersite'; }?>">
                              <div class="avatar">
                              <a href="<?php echo base_url().'profil/'.$latest_userDetails['alias']; ?>" target="_blank">
                                <div class="mediumbgAvatar">
                								<img src="<?php if(!empty($message) && $message['filename']!='' && file_exists(FCPATH.'assets/userfiles/'.$message['path'].'/80/'.$message['filename'].'.'.$message['extension'])){ echo base_url().'assets/userfiles/'.$message['path'].'/80/'.$message['filename'].'.'.$message['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; }?>" alt="" srcset="<?php if(!empty($message['filename']) && file_exists(FCPATH.'assets/userfiles/'.$message['path'].'/80/'.$message['filename'].'.'.$message['extension'])){ echo  base_url().'assets/userfiles/'.$message['path'].'/80/'.$message['filename'].'.'.$message['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 1x, <?php if(!empty($message['filename']) && file_exists(FCPATH.'assets/userfiles/'.$message['path'].'/160/'.$message['filename'].'.'.$message['extension'])){ echo  base_url().'assets/userfiles/'.$message['path'].'/160/'.$message['filename'].'.'.$message['extension']; } else { echo base_url().'assets/images/default-avatar.jpg'; } ?> 2x" >
                                            
                							 </div>
                              </a>
                              </div>
                              <div class="content">
                                <p><?php echo $message['text']; ?></p>
                                <?php if($message['image_path']){ ?>
                                <p><!-- <a lass="fancybox downloadAnchor" href="#inline<?php echo $message['id']; ?>" data-fancybox-group="gallery" href="<?php echo base_url().str_replace('assets/userfiles/img/message/', 'message/', $message['image_path']); ?>" download><img src="<?php echo base_url().$message['image_path']; ?>" style="min-width: 150px;"><span class="dImageWrapper">Click to download</span></a> -->
                                  <a class="fancybox downloadAnchor" href="#inline<?php echo $message['id']; ?>" data-fancybox-group="gallery" style="background:url(<?php echo base_url().$message['image_path']; ?>) no-repeat center center / cover; min-width: 150px; min-height: 150px;" ><span class="dImageWrapper"><?php echo getLang('View');?></span></a>
                                </p>
                                <div id="inline<?php echo $message['id']; ?>" style="display:none;">
                                  <div class="hotshotWrapper">
                                    <div class="hotshotbody">
                                      <div class="border">
                                            <!-- <img src="<?php echo base_url().$message['image_path']; ?>" > -->
                                            <div style="background: url(<?php echo base_url().str_replace('message/', 'message_big/', $message['image_path']); ?>) no-repeat center center / cover; " ></div>
                                        </div>
                                      </div>
                                    </div>
                                </div>
                                <?php } ?>
                              </div>
                              <time><?php if(date('d',$message['created'])==date('d',time())){ echo date('H:i',$message['created']);} else{ echo date('M d',$message['created']); } ?> <i class="fa fa-trash deleteCommunityMessage" data-id="<?php echo $message['id']; ?>"></i></time>                        
                            </div>
                          <?php }
                            } ?>
                      </div>
                    <?php } else { ?>
                        <div id="chatwrapper"  class="chat_wrapper " style="overflow-y:auto;">
                    <!--<div id="chatwrapper"  class="chat_wrapper mCustomScrollbar light" data-mcs-theme="minimal-dark">-->
                        <time><?php echo date('M d',time()); ?></time>
                          <?php echo getLang('No_Message'); ?>
                    </div>
                    <?php } ?>
                    </span>
                    <div class="message_send">
                      <textarea id="chatArea" placeholder="<?php echo getLang('Write_a_Message'); ?>"></textarea>
                      <button class="sendChat"><img src="<?php echo base_url().'assets/images/send_mess.png'; ?>" width="30"><?php //echo getLang('Send'); ?></button>
                      
                      <div class="upload-btn-wrapper">
                        <button class="fileUploadBtn"><i class="fa fa-paperclip"></i></button>
                        <input type="file" name="messageImg" id="messageImg"/>
                      </div>
                    </div>
                  </div>

                  <input type="hidden" id="otherUser" value="<?php if(!empty($latest_messages)) { if($latest_messages[0]['from_user_id']==$this->session->userdata('user_id')){ echo $latest_messages[0]['user_id']; } elseif($latest_messages[0]['user_id']==$this->session->userdata('user_id')) { echo $latest_messages[0]['from_user_id']; } } ?>">
                  <input type="hidden" class="lasttime" value="<?php if(!empty($latest_messages)) { echo $latest_messages[0]['created']; } ?>">
                  <input type="hidden" class="finaltime" value="<?php if(!empty($latest_messages)) { echo $latest_messages[count($latest_messages)-1]['created']; } ?>">
                  <input type="hidden" class="normal" value="<?php echo $normal; ?>">
                </div>
              </div>

              <div class="helpBox">
                <span class="titleIcon"><i class="fa fa-envelope"></i></span>
                <a href="<?php echo base_url().'messages/'.base64_encode('11330');?>" ><h4><?php echo getLang('Contact_Us'); ?></h4></a>
                  <a href="<?php echo base_url().'messages/'.base64_encode('11330');?>" >
                    <div class="mediumbgAvatar" style="background:url(images/support-box-ico.png) no-repeat center center / cover;"></div>
                  </a> 
              </div>
              <div class="helpBox">
                <span class="titleIcon rotate"><i class="fa fa-mobile"></i></span> 
                <a href="javascript:void(0)" ><h4><?php echo getLang('Mobile_phone'); ?></h4></a> 
                <?php
                  if(empty($membershipUser))
                  {
                    $membershipUser['article_id'] = 4;
                  } 
                  $number = '<span style="color:#fff">+34679494448</span>';
                    if(count($userParticipation)==0 && count($userParticipationOld)==0 && $membershipUser['article_id']==5){ ?>
                    <p><?php echo getLang('Premium_Members_reach_us'); ?></p>
                    <p><?php echo getLang('Telegram_Textmessage_only'); ?></p>
                    <p><?php echo getLang('Mobile_Number').': <a style="text-decoration: underline;" href="'.base_url().'settings">'.getLang('upgrade_membership').'</a>'; ?></p>
                    <br/>
                    <p style="color: #fff; font-weight: bold;"><?php echo getLang('Emergency_number_for_participants'); ?></p>
                    <p><?php echo getLang('please_verify_your_mobile_number_account_settings'); ?></p>
                <?php } elseif((count($userParticipation)>0 || count($userParticipationOld)>0) && $membershipUser['article_id']==5){ ?>
                    <p><?php echo getLang('Premium_Members_reach_us'); ?></p>
                    <p><?php echo getLang('Telegram_Textmessage_only'); ?></p>
                    <p><?php echo getLang('Mobile_Number').': <a style="text-decoration: underline;" href="'.base_url().'settings">'.getLang('upgrade_membership').'</a>'; ?></p>
                    <br/>
                    <?php if($userDetials['mobile_number']>0){ ?>
                      <p class="mobile-contact-info">                     
                        <img class="icon whatsapp" src="<?php echo  base_url('assets/images/telegram.png'); ?>">
                        <span>
                          <?php echo getLang('Telegram').': '.$number; ?> 
                        </span>
                      </p>
                      <p><?php echo getLang('You_can_leave_us'); ?></p>
                    <?php } ?>
                <?php } elseif($membershipUser['article_id']==4 && ($userDetials['mobile_number']=='' || $userDetials['mobile_number']==0)){ ?>
                    <p><?php echo getLang('Premium_Members_reach_us'); ?></p>
                    <p><?php echo getLang('Telegram_Textmessage_only'); ?></p>
                    <p><?php echo getLang('Mobile_Number').': '.getLang('Please_verify_your_mobile_number_first'); ?></p>
                <?php } elseif($membershipUser['article_id']==4 && $userDetials['mobile_number']>0){ ?>
                    <p><?php echo getLang('Premium_Members_reach_us'); ?></p>
                    <p><?php echo getLang('Telegram_Textmessage_only'); ?></p>
                    <p class="mobile-contact-info">                     
                      <img class="icon whatsapp" src="<?php echo  base_url('assets/images/telegram.png'); ?>">
                      <span>
                        <?php echo getLang('Telegram').': '.$number; ?> 
                      </span>
                    </p>
                    <p class="mobile-contact-info">
                      <img class="icon threema" src="<?php echo  base_url('assets/images/threema.png'); ?>">
                      <span>
                        <strong>Threema ID:</strong> ZKBFZ4HP <br>
                        <strong>Nickname:</strong> CE  <br>
                        <strong>Email:</strong> info@castleevents.com 
                      </span>
                    </p>
                <?php } ?>
              </div>
                             
             </div>
</section>
<section class="all_popups">
         <!--Contact Modal -->
         <div class="modal_container" id="contact_modal">
            <span class="modal_close"><i class="fa  fa-times-circle-o"></i></span>
            
            <form id="contactFrm">
               <h2><?php echo getLang('Contact'); ?></h2>
               <!--<p>CONTACT US, WE WILL REPLY AS SOON AS POSSIBLE</p>-->
               <div class="form_group">
                  <label for="#err" class="contact_err" style="color:red;"></label>
               </div>
               <div class="form_group" <?php if($this->session->userdata('user_id')){ echo 'style="display:none;"'; } ?>>
                  <label for="emailId"><?php echo getLang('Your_E_Mail'); ?>  </label>
                  <input type="text" id="contactEmail" name="contactEmail" value="<?php if(!empty($loginDetails)){ echo $loginDetails[0]['email']; } ?>" placeholder="<?php echo getLang('EMAIL'); ?>">
               </div>
               <div class="form_group">
                  <label for="Password"><?php echo getLang('Message'); ?> </label>
                  <textarea id="message" name="message" placeholder="<?php echo getLang('Type_your_message'); ?>"></textarea>
               </div>
               <div class="contactusTeam">
                 <p class="allcaps text_gold"><?php echo getLang('One_of_our_team_will_reply_as_soon_as_possible'); ?></p>
                  <span class="avatar_profile"><img src="images/profile-Avatar.jpg" alt=""></span>
                  <span class="avatar_profile"><img src="images/profile-Avatar.jpg" alt=""></span>
                  <button type="button" class="btn_gold contactSend"><?php echo getLang('Send'); ?></button>                  
               </div>               
                
               
            </form>
            <span id="contactSuccess" style="display:none;">
               <h2><?php echo getLang('Contact_Successful'); ?></h2>
               <div class="center">
                    <p><?php echo getLang('Thanks_you_for_contacted_us'); ?>. <br/><?php echo getLang('Your_message_successfully_send_to_Castleevents'); ?>.<br/><?php echo getLang('We_will_contact_you_soon'); ?>.</p>
               </div>
            </span>
         </div>
      <div class="modalmask"></div>
</section>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
    $('#chatwrapper').scrollTop($('#chatwrapper')[0].scrollHeight);
    if($('.normal').val()==1)
    {
      $('#chatwrapper').addClass('padding0');
    }
    $('body').on('keyup','#search',function(){
      var name = $(this).val();
      $.ajax({
        url: '<?php echo base_url();?>profile/searchfriend',
        type: 'POST',
        data: 'name='+name,
        async:false,
        success: function(res)
        {
          $('#moblist .mCSB_container').html(res);
          $("#moblist").mCustomScrollbar("update");
          $("#moblist").mCustomScrollbar({
              scrollButtons:{
                  enable:true
              },
              callbacks:{
                 onTotalScroll:function(){ alert();
                    var data = $('.more_user').data('id');
                    $('.more_user').parent().remove();
                    $('.ajax-loader_msg').show();
                    $('.ajax-loader_msg').css('visibility','visible');
                    if(data>0)
                    {
                       $.ajax({
                         url: '<?php echo base_url();?>profile/getmorefriend',
                         type: 'POST',
                         data: 'data='+data,
                         async:false,
                         success: function(res)
                         {
                           $('.ajax-loader_msg').css('visibility','hidden');
                           $('.ajax-loader_msg').hide();
                           $('#moblist .mCSB_container').append(res);
                           $("#moblist").mCustomScrollbar("update");
                           $("#moblist").mCustomScrollbar({
                               scrollButtons:{
                                   enable:true
                               }
                           });
                         } 
                       })
                    }
                    
                 }
              }
            });
        } 
      })
    })
    $('#chatwrapper').bind('scroll',function(){
      if($(this).scrollTop()==0 && $('.normal').val()==0)
      {
        var user_id = "<?php echo $this->session->userdata('user_id') ?>";
        var other_user_id = $('#otherUser').val();
        var lasttime = $('.lasttime').val();
        $('.ajax-loader_chat').show();
        $('.ajax-loader_chat').css('visibility', 'visible');
          if(other_user_id!='')
          {
            $.ajax({
                url: '<?php echo base_url(); ?>profile/loadnextmessage',
                data: 'user_id='+user_id+'&other_user_id='+other_user_id+'&uptime='+lasttime,
                type: 'POST',
                success: function(res)
                {
                  if(res!='')
                  {
                    var split = res.split('|');
                    if(split[3]>0)
                    {
                      if($('.normal').val()==0)
                      {
                        $('.ajax-loader_chat').hide();
                        $('.ajax-loader_chat').css('visibility', 'none');
                        $('#chatwrapper').prepend(split[0]);
                        $('.lasttime').val(split[1]);
                        $('.finaltime').val(split[2]);
                        $('#chatwrapper').scrollTop(30);
                      }
                      else
                      {
                        $('.ajax-loader_chat').hide();
                        $('.ajax-loader_chat').css('visibility', 'none');
                      }
                    }
                    else
                    {
                      $('.ajax-loader_chat').hide();
                      $('.ajax-loader_chat').css('visibility', 'none');
                    }
                  }
                }
            })
          }
      }
    })
    setInterval(function(){
      var user_id = "<?php echo $this->session->userdata('user_id') ?>";
      var other_user_id = $('#otherUser').val();
      var finaltime = $('.finaltime').val();
        if(other_user_id!='' && $('.normal').val()==0)
        {
          $.ajax({
                url: '<?php echo base_url(); ?>profile/loadcurrentmessage',
                data: 'user_id='+user_id+'&other_user_id='+other_user_id+'&finaltime='+finaltime,
                type: 'POST',
                success: function(res)
                {
                  if(res!='')
                  {
                    var split = res.split('|');
                    if(split[2]>0)
                    {
                      if(split[3]==0)
                      {
                        $('.ajax-loader_chat').hide();
                        $('.ajax-loader_chat').css('visibility', 'none');
                        $('#chatwrapper').append(split[0]);
                        $('.finaltime').val(split[1]);
                      }
                    }
                  }
                }
            })
        }
      }, 3000);

    $('body').on('click','.listUser',function(){
      var user_id = "<?php echo $this->session->userdata('user_id') ?>";
      var other_user_id = $(this).data('id');
      $('#otherUser').val(other_user_id);
      $('.listUser').removeClass('active');
      $(this).addClass('active');
        if(other_user_id!='')
        {
          $.ajax({
              url: '<?php echo base_url(); ?>profile/latestMessage',
              data: 'user_id='+user_id+'&other_user_id='+other_user_id,
              type: 'POST',
              async:false,
              success: function(res)
              {
                if(res!='')
                {
                  var split = res.split('|');
                  $('.chat_user').html(split[0]);
                  if(split[1]!='')
                  {
                    $('.chat_message #chatwrapper').remove();
                    $('.chat_message').append(split[1]);
                    $('.lasttime').val(split[2]);
                    $('.finaltime').val(split[3]);
                    $('.normal').val(split[4]);
                    if(split[4]==1)
                    {
                      $('#chatwrapper').addClass('padding0');
                    }
                    else
                    {
                      $('#chatwrapper').removeClass('padding0');
                    }
                    $('#chatwrapper').scrollTop($('#chatwrapper')[0].scrollHeight);

                    $('#chatwrapper').bind('scroll',function(){
                        if($('#chatwrapper').scrollTop()==0  && $('.normal').val()==0)
                        {
                          var user_id = "<?php echo $this->session->userdata('user_id') ?>";
                          var other_user_id = $('#otherUser').val();
                          var lasttime = $('.lasttime').val();
                          $('.ajax-loader_chat').show();
                          $('.ajax-loader_chat').css('visibility', 'visible');
                            if(other_user_id!='')
                            {
                              $.ajax({
                                  url: '<?php echo base_url(); ?>profile/loadnextmessage',
                                  data: 'user_id='+user_id+'&other_user_id='+other_user_id+'&uptime='+lasttime,
                                  type: 'POST',
                                  success: function(res)
                                  {
                                    if(res!='')
                                    {
                                      var split = res.split('|');
                                      if(split[3]>0)
                                      {
                                        $('.ajax-loader_chat').hide();
                                        $('.ajax-loader_chat').css('visibility', 'none');
                                        $('#chatwrapper').prepend(split[0]);
                                        $('.lasttime').val(split[1]);
                                        $('.finaltime').val(split[2]);
                                        $('#chatwrapper').scrollTop(30);
                                      }
                                      else
                                      {
                                        $('.ajax-loader_chat').hide();
                                        $('.ajax-loader_chat').css('visibility', 'none');
                                      }
                                    }
                                  }
                              })
                            }
                        }
                      })
                      
                  }
                }
              }
          })
        }
    })
    $('body').on('keyup','#chatArea',function(e){
      if(e.keyCode == 13)
      {
        $('.sendChat').click();
      }
    }); 

    $('#messageImg').change(function(){
      $('.sendChat').click();
    })

    $('body').on('click','.sendChat',function(e){
        //$(this).attr('disabled',true);
        var thisbtn = $(this);
        var user_id = "<?php echo $this->session->userdata('user_id') ?>";
        var other_user_id = $('#otherUser').val();
        var text = $('#chatArea').val();
        var this1 = $('#chatArea');
        var userDiv = $('#user_'+other_user_id);
        var error_msg = "<?php echo getLang('This_feature_is_not_available_due'); ?>";
        var error_msg_block = "<?php echo getLang('You_cannot_message_that_profile'); ?>";
        var form_data = new FormData();      
        if($('#messageImg').val() !='')
        {
          var baseUrl = $('#baseUrl').val();
          var file_data = $("#messageImg").prop("files")[0];   
                     
          form_data.append("file", file_data)
        } 
        if(other_user_id!='' && ($('#messageImg').val()!='' || text!='')  && $('.normal').val()==0)
        {
          form_data.append("user_id", user_id);
          form_data.append("other_user_id", other_user_id);
          form_data.append("text", text);
          $.ajax({
              url: '<?php echo base_url(); ?>profile/sendMessage',
              data: form_data,
              type: 'POST',
              contentType: false,
              processData: false,
              success: function(res)
              {
                if(res!='')
                {
                  if(res==10)
                  {
                    $('#chatwrapper').html('<div class="conversation seeAllUpgrade"><h3>'+error_msg+'</h3></div>');
                    $('#chatwrapper').css('overflow-y','inherit');
                  }
                  else if(res==11)
                  {
                    $('#chatwrapper').html('<div class="conversation seeAllUpgrade"><h3>'+error_msg_block+'</h3></div>');
                    $('#chatwrapper').css('overflow-y','inherit');
                  }
                  else
                  {
                    $('#chatwrapper').css('overflow-y','auto');
                    var split = res.split('|');
                    this1.val('');
                    $("#messageImg").val('');
                    if(split[1]!='')
                    {
                      $('#user_'+other_user_id).find('.last_talk').html(split[2].substr(0,20));
                      $('#user_'+other_user_id).click();
                      $('#chatwrapper').scrollTop($('#chatwrapper')[0].scrollHeight);
                    }
                  }
                  $('.sendChat').prop("disabled", false);
                }
              }
          })
        }
    }); 
    $('body').on('click','.deleteCommunityMessage',function(){
        if (!confirm(Are_you_sure_you_want)) return false;
        var id = $(this).data('id');
        var this1 = $(this);
        if(id > 0)
        {
          $.ajax({
              url: '<?php echo base_url(); ?>profile/deleteCommunityMessage',
              data: 'id='+id,
              type: 'POST',
              success: function(res)
              {
                this1.parent().parent().remove();
              }
          })
        }
    }); 
    $('body').on('click','.deleteCommunityWholeMessage',function(){
        if (!confirm('Are you sure to delete?')) return false;
        var id = $(this).data('id');
        var this1 = $(this);
        if(id > 0)
        {
          $.ajax({
              url: '<?php echo base_url(); ?>profile/deleteCommunityWholeMessage',
              data: 'id='+id,
              type: 'POST',
              success: function(res)
              {
                this1.parent().parent().next().find('.chat_wrapper').html('');
                window.location.href="<?php echo base_url(); ?>messages";
              }
          })
        }
    }); 
    <?php if($msg_user_id!=''){ ?>
      var other_user_id = "<?php echo base64_decode($msg_user_id); ?>";
            setTimeout(function(){
              $('#search').val('<?php echo $msg_username; ?>').keyup();
              $('#user_<?php echo base64_decode($msg_user_id); ?>').click();
            }, 2000) 
            setTimeout(function(){
              $('#search').val('').keyup();
            }, 3000)   
            $('ul.mCustomScrollbar').find('li.active').prependTo('ul.mCustomScrollbar');
    <?php } 
          elseif(!empty($latest_messages))
          { 
            if($latest_messages[0]['user_id']==$this->session->userdata('user_id'))
            {
      ?> 
       var other_user_id = "<?php echo ($latest_messages[0]['from_user_id']); ?>";
        $('ul.mCustomScrollbar').find('li.active').prependTo('ul.mCustomScrollbar');
              
    <?php   }
            else
            {
    ?>  
      var other_user_id = "<?php echo ($latest_messages[0]['user_id']); ?>";
        $('ul.mCustomScrollbar').find('li.active').prependTo('ul.mCustomScrollbar');
    <?php   } 
          } ?> 
  })
</script>
