<?php
$tab_style = 'block';
if (isset($_SESSION['open_chat'])) {
  if($_SESSION['open_chat'] == 0) {
    $tab_style = 'none';
  }
}
$chat_status_style = 'right:0px; left:auto;';
if ($wo['language_type'] == 'rtl') {
   $chat_status_style = 'right:auto; left:0;';
}
?>
<div class="chat-all-container">
   <div class="chat-tab"></div>
   <div class="chat-container">
      <div class="online-toggle pointer" onclick="Wo_OpenChatUsersTab();">
         <i class="fa fa-users"></i>  <?php echo $wo['lang']['online_chat'];?> (<span class="count-online-users"><?php echo Wo_CountOnlineUsers();?></span>)
      </div>
      <div class="chat-status">
       <div class="dropdown">
            <i class="fa fa-cog" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"></i>
            <ul class="dropdown-menu" role="menu" style="min-width: 80px; font-size: 12px; <?php echo $chat_status_style; ?>">
                <li><a href="#" onclick="Wo_UpdateStatus('online',event);"><i class="fa fa-eye"></i> <?php echo $wo['lang']['online'];?></a></li>
                <li><a href="#" onclick="Wo_UpdateStatus('offline',event);"><i class="fa fa-eye-slash"></i> <?php echo $wo['lang']['offline'];?></a></li>
            </ul>
         </div>
         </div>
      <div class="online-content-toggler" style="display:<?php echo $tab_style; ?>">
         <div class="chat-opacity <?php echo ($wo['user']['status'] == 1) ? ' active': '';?>">
            <div class="text-center">
               <?php echo $wo['lang']['you_are_currently_offline'];?>
               <h2><i class="fa fa-eye-slash"></i></h2>
            </div>
         </div>
         <div class="offline-users">
            <?php 
            $Offlineusers = Wo_GetChatUsers('offline');
            if (count($Offlineusers) == 0) {
              echo '<span class="no-online-users center-text">' . $wo['lang']['no_offline_users'] . '</span>';
            } else {
              foreach ($Offlineusers as $wo['chatList']) {
                echo Wo_LoadPage('chat/offline-user');
              }
            }
            ?>
         </div>
         <div class="clear"></div>
         <div style="height: 1px; background-color: #ededed; text-align: center; margin-top:25px;">
            <span style="background-color: white; position: relative; top: -0.8em; color:#888; font-size:12px;">
            <?php echo $wo['lang']['online'];?> 
            </span>
         </div>
         <div class="online-users">
            <?php 
            $OnlineUsers = Wo_GetChatUsers('online');
            if (count($OnlineUsers) == 0) {
                echo '<span class="no-online-users center-text">' . $wo['lang']['no_online_users'] . '</span>';
            } else {  
                foreach ($OnlineUsers as $wo['chatList']) {
                  echo Wo_LoadPage('chat/online-user');
                }
            } 
            ?>
         </div>
         <div class="search">
            <input type="text" class="form-control search-users-chat" placeholder="<?php echo $wo['lang']['search_for_users'];?>" onkeyup="Wo_ChatSearchUsers(this.value);">
         </div>
      </div>
   </div>
</div>
<script type="text/javascript">
$(function() {
    <?php if(isset($_SESSION['chat_id']) && !empty($_SESSION['chat_id'])) { ?>
        Wo_OpenChatTab(<?php echo $_SESSION['chat_id'];?>);
    <?php } ?>
    setInterval(function() {
        Wo_CountOnlineUsers();
    }, 20000);
    setInterval(function() {
        Wo_UpdateOnlineUsers();
    }, 15000);
    setInterval(function() {
        Wo_UpdateOfflineUsers();
    }, 20000);
    setInterval(function() {
        Wo_getNewChatMessages();
    }, 5000);
    setInterval(function() {
        Wo_UpdateUserChatStatus($('form.chat-sending-form').find('#user-id').val());
    }, 40000);
    
    <?php if ($wo['config']['message_seen'] == 1) { ?>
    setInterval(function() {
        Wo_getChatMessageSeen();
    }, 6000);
    <?php } ?>

    <?php if ($wo['config']['message_typing'] == 1) { ?>
    setInterval(function() {
        Wo_isTyping();
    }, 4000);
    <?php } ?>
});
function Wo_CountOnlineUsers() {
    var count_span = $('.count-online-users');
    $.get(Wo_Ajax_Requests_File(), {f:'chat', s:'count_online_users'}, function(data) {
        if (data.status == 200) {
            count_span.html(data.html);
        }
    });
}

function Wo_UpdateOnlineUsers() {
    var online_users_container = $('.online-users');
    if ($('.search-users-chat').val().length > 0) {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {f:'chat', s:'update_online_recipients'}, function(data) {
        if (data.status == 200) {
            if (data.html.length == 0) {
                online_users_container.html('<span class="no-online-users center-text"><?php echo $wo["lang"]["no_online_users"];?></span>');
            } else {
                online_users_container.html(data.html);
            }
        }
    });
}
function Wo_UpdateOfflineUsers() {
    var offline_users_container = $('.offline-users');
    $.get(Wo_Ajax_Requests_File(), {f:'chat', s:'update_offline_recipients'}, function(data) {
        if (data.status == 200) {
            if (data.html.length == 0) {
                offline_users_container.html('<span class="no-online-users center-text"><?php echo $wo["lang"]["no_offline_users"];?></span>');
            } else {
                offline_users_container.html(data.html);
            }
        }
    });
}

function Wo_ChatSearchUsers(search_query) {
    var offline_users_container = $('.online-users');
    var input = $($('.search-users-chat'));
    if (input.val() == "") {
        Wo_UpdateOnlineUsers();
    }
    $.post(Wo_Ajax_Requests_File() + '?f=chat&s=search_for_recipients', {
        search_query: search_query
    }, function(data) {
        if (data.status == 200) {
            if (data.html.length == 0) {
                offline_users_container.html('<span class="no-online-users center-text"><?php echo $wo["lang"]["no_users_found"];?></span>');
            } else {
                offline_users_container.html(data.html);
            }
            $('.chat-container').scrollTop($('.chat-container')[0].scrollHeight);
        }
    });
}

function Wo_UpdateStatus(status,event) {
    event.preventDefault();
    var status_container = $('.online-content-toggler');
    var offline_status = status_container.find('.chat-opacity');
    $.post(Wo_Ajax_Requests_File() + '?f=chat&s=update_chat_status', {
        status: status
    }, function(data) {
        if (data.status == 0) {
            offline_status.removeClass('active').fadeOut(200);

        } else {
            offline_status.addClass('active').fadeIn(200);
        }
    });
}

function Wo_RegisterTyping() {
    chat_container = $('.chat-tab');
    recipient_id = chat_container.find('#user-id').val();
    if (typeof recipient_id === 'undefined') {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'recipient_is_typing',
        recipient_id: recipient_id
    });
}

function Wo_DeleteTyping() {
    chat_container = $('.chat-tab');
    recipient_id = chat_container.find('#user-id').val();
    if (typeof recipient_id === 'undefined') {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'remove_typing',
        recipient_id: recipient_id
    });
}

function Wo_CloseChat() {
    <?php if ($wo['config']['message_typing'] == 1) { ?>
        Wo_DeleteTyping();
    <?php } ?>
    $('.chat-wrapper').remove();
    $(document.body).removeAttr('data-chat-recipient');
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'close'
    });
}

function Wo_getNewChatMessages() {
    chat_container = $('.chat-tab');
    user_id = chat_container.find('#user-id').val();
    var message_id = chat_container.find('.chat-messages-wrapper').find('.messages-wrapper:last').attr('data-message-id');
    if (typeof user_id === 'undefined') {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'get_new_messages',
        user_id: user_id,
        message_id:message_id
    }, function(data) {
        if (data.status == 200) {
            //Wo_DeleteTyping();
            $(".chat-messages-wrapper").find("div[class*='message-seen']").empty();
            $(".chat-messages-wrapper").find("div[class*='message-typing']").empty();
            $(".chat-messages-wrapper").append(data.html);
            $('.chat-messages-wrapper').scrollTop($('.chat-messages-wrapper')[0].scrollHeight);
            if (data.sender == data.receiver) {
               document.getElementById('message-sound').play();
            }
            if (!$('#sendMessage').is(':focus')) {
                document.title = 'New Message | ' + document_title;
            }
        }
    });
}

function Wo_getChatMessageSeen() {
    chat_container = $('.chat-wrapper');
    var last_id = chat_container.find('.messages-text:last').attr('data-message-id');
    if (!$('.messages-text:last').find('.message-seen').is(':empty')) {
      return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'messages',
        s: 'get_last_message_seen_status',
        last_id: last_id
    }, function(data) {
        if (data.status == 200) {
            chat_container.find('.messages-text:last').find('.message-seen').hide().html('<i class="fa fa-check"></i> <?php echo $wo["lang"]["seen"];?> (<span class="ajax-time" title="' + data.time + '">' + data.seen + '</span>)').fadeIn(300);
            $('.chat-messages-wrapper').scrollTop($('.chat-messages-wrapper')[0].scrollHeight);
        }
    });
}   
function Wo_ShareChatFile() {
    document.title = document_title;
    $("#sendMessage").focus();
    $("form.chat-sending-form").submit();
}
function Wo_AddEmoToChat(code) {
    inputTag = $('.chat-sending-form textarea');
    inputVal = inputTag.val();
    $('.emo-container').hide();
    if (typeof(inputTag.attr('placeholder')) != "undefined") {
        inputPlaceholder = inputTag.attr('placeholder');
        if (inputPlaceholder == inputVal) {
            inputTag.val('');
            inputVal = inputTag.val();
        }
    }
    if (inputVal.length == 0) {
        inputTag.val(code + ' ');
    } else {
        inputTag.val(inputVal + ' ' + code);
    }
    inputTag.keyup().focus();
}

function Wo_SubmitChatForm(e) {
    // document.title = document_title;
    //$('.charsLeft-message').fadeIn(200);
    <?php if ($wo['config']['message_typing'] == 1) { ?>
    if ($('.chat-tab #sendMessage').val()) {
        if (typeof ($('.chat-tab').attr('data-typing')) == "undefined") {
            $('.chat-tab').attr('data-typing', 'true');
            Wo_RegisterTyping();
        } 
    } else {
        $('.chat-tab').removeAttr('data-typing');
        Wo_DeleteTyping();
    }
    <?php } ?>
    chat_tab_container = $('.chat-tab');
    if (e.keyCode == 13 && e.shiftKey == 0) {
        e.preventDefault();
        $('form.chat-sending-form').submit();
        chat_tab_container.find('.messages-text:last').find('.message-seen').empty();
    }
}

function Wo_UpdateUserChatStatus(user_id) {
    chat_user_tab = $('.chat-wrapper').find('.chat-status');
    if (typeof user_id === 'undefined') {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'update_tab_status',
        user_id: user_id
    }, function(data) {
        if (data.status == 200) {
            chat_user_tab.addClass('active');
        } else {
            chat_user_tab.removeClass('active');
        }
    });
}


function Wo_isTyping() {
    chat_tab_container = $('.chat-tab');
    recipient_id = chat_tab_container.find('#user-id').val();
    if (typeof recipient_id === 'undefined') {
       return false;
    }
    $.get(Wo_Ajax_Requests_File(), {
        f: 'chat',
        s: 'is_recipient_typing',
        recipient_id: recipient_id
    }, function(data) {
        if (data.status == 200) {
            chat_tab_container.find('.messages-text:last').find('.message-typing').html('<img class="user-avatar-left" src="' + data.img + '" alt="Profile Picture"><div id="loading"> <img class="typing" src="' + data.typing + '"></div>').fadeIn(300);

            $('.chat-messages-wrapper').scrollTop($('.chat-messages-wrapper')[0].scrollHeight);
            
        } else {
            chat_tab_container.find('.messages-text:last').find('.message-typing').empty();
        }
    });
} 

</script>