<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: /page/chat.php
*
*/


if(!isset($_SESSION['id']))
{
	Header("Location: /");
	exit;
}

if(isset($_GET['dell']))
{
	$dell = intval($_GET['dell']);
	$q = $mysql->prepare("DELETE FROM db_chat WHERE Uid = ?");
	$q->execute(array($dell));
	Header("Location: /chat");
	exit;
}
?>


<!-- OPEN CENTER -->
<tr>
    <td align="left" valign="top">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td width="284" align="left" valign="top">
                        <?php require('theme/users_block.php');?>
                    </td><!-- MENU USER -->
                    <td align="left" valign="top">
                    <?php require('theme/menu.php'); ?>
                    <div style="height:15px"></div>
                    <table width="672" cellspacing="0" cellpadding="0" style="padding-left:10px;padding-right:10px;">
                        <tbody>
                            <tr></tr>
                            <tr>
                                <td align="left" valign="top">
                                    <div style="background:url('/images/title.jpg') no-repeat center;text-align:center"><span style="background-color:#F0E8D5;padding-right:15px;padding-left:15px;font-size:18px">Добро пожаловать</span></div>
                                    <br />
                                    <!-- Описание контента -->


									<?php

									if(isset($_GET['ban']))
									{
										if(isset($_POST['ban']))
										{
											$srok = intval($_POST['srok']);
											$lg = clean($_POST['loginban']);
											if($srok == 1)
											{
												$timeBan = time() + (60*60*24);
												$t = 'на 24 часа';
											}
											elseif($srok == 2)
											{
												$timeBan = time() + (60*60*24*2);
												$t = 'на 48 часов';
											}
											elseif($srok == 3)
											{
												$timeBan = time() + (60*60*24*100500);
												$t = 'навсегда';
											}
											if($lg != $_SESSION['login'])
											{
											$q = $mysql->prepare("UPDATE db_users SET ChatBan = ?, DateBanChat = ? WHERE Login = ?");
											$q->execute(array('1', $timeBan, $lg));
											
											$text = 'Пользователь <b>'.$lg.'</b> был забанен '.$t.' модератором <b>'.$_SESSION['login'].'</b>';
											$w = $mysql->prepare("INSERT INTO db_chat SET Login = ?, Text = ?, DateAdd = ?");
											$w->execute(array('Robot', $text, time()));
											}
											header("Location: /chat");
											exit;
											
										}
										$login = clean($_GET['ban']);
									?>

									<div class="title">Забанить пользователя</div>
									<div class="text" align="center">
									<form method="post" action="">
									<input type="hidden" name="loginban" value="<?=$login; ?>">
									Логин: <input type="text" name="login" value="<?=$login; ?>">
									<br>
									Срок: <select name="srok">
									<option value="1"> 24 часа
									<option value="2"> 48 часов
									<option value="3"> Навсегда
									</select>
									<br>
									<input type="submit" value="Забанить" name="ban">
									</form>
									</div>

									<?php return; } ?>

									<script type="text/javascript" src="/js/jquery.cookie.js"></script>
									<script type="text/javascript">
									$(document).ready(function(){
										$("#chat #messege").animate({height:(document.body.clientHeight-250)+"px"},1000);
										$("#chat #online").animate({height:(document.body.clientHeight-250)+"px"},1000);
										process('','/process/chat.php?action=load','messege','messege','messege'); 
										process('','/process/chat_online.php','online','online','online'); 
										setInterval("refresh()",5000);
										setInterval("refreshOnline()",6155);
									});

									function refresh() {
										process('','/process/chat.php?action=refresh&allmessege='+$("#allmessege").html()+'','','messege');
									}

									function refreshOnline() {
										process('','/process/chat_online.php?allonline='+$("#allonline").html()+'','','online');
									}

									function send(e) {
										if(e.keyCode == 13) {
											process('messegeForm','/process/chat.php?action=send','messegeForm .button','messege','send','',76);
										}
									} 

									function treatment(login) {
										if(login == '<?=$_SESSION['login']; ?>') {
											if (typeof clear_id !=="undefined"){clearTimeout(clear_id);} 
											$("#messege").append('<div class="error left">Нельзя себе отправить сообщение!</div>'); 
											clear_id = setTimeout('$("#messege .error").slideUp(500)',4000); 
											with (document.getElementById ('messege')) scrollTop = scrollHeight;
											return;
										}
										$("#messegeForm input[name=messege]").val("[to]:"+login+"[to]: "+$("#messegeForm input[name=messege]").val());
										$("#messegeForm input[name=messege]").focus();
									}
									function volumes() {
										if($("#volume").attr("src") == "/images/volume.png") {
											$("#volume").attr("src","/images/volumem.png");
											$.cookie('volume', 'off');
										}
										else {
											$("#volume").attr("src","/images/volume.png");
											$.cookie('volume', 'on');
										}
									}
									jQuery.fn.extend({
										insertAtCaret: function(myValue){
											return this.each(function(i) {
												if (document.selection) {
													this.focus();
													var sel = document.selection.createRange();
													sel.text = myValue;
													this.focus();
												}
												else if (this.selectionStart || this.selectionStart == '0') {
													var startPos = this.selectionStart;
													var endPos = this.selectionEnd;
													var scrollTop = this.scrollTop;
													this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
													this.focus();
													this.selectionStart = startPos + myValue.length;
													this.selectionEnd = startPos + myValue.length;
													this.scrollTop = scrollTop;
												} else {
													this.value += myValue;
													this.focus();
												}
											})
										}
									});
									$(function(){
										$("#smile").hover(function() {
											$("#smileBlock").stop().fadeIn(500);
										}, function() {
											$("#smileBlock").fadeOut(500);
										});
										$("#smileBlock").hover(function() {
											$("#smileBlock").stop().fadeIn(500);
										}, function() {
											$("#smileBlock").fadeOut(500);
										});
										$("#smileBlock img").click(function() {
											$('#messegeForm input[name=messege]').insertAtCaret($(this).attr("alt"));
										});
									});
									function controlpanel(action) {
										switch(action) {
											case "plus":
												var h = $("#chat #messege").height();
												$("#chat #messege").animate({height:(h + 50)+"px"},200);
												$("#chat #online").animate({height:(h + 50)+"px"},200);
											break;
											case "minus":
												var h = $("#chat #messege").height();
												$("#chat #messege").animate({height:(h - 50)+"px"},200);
												$("#chat #online").animate({height:(h - 50)+"px"},200);
											break;
											case "test":
												var audiom = $("#audiom")[0]; 
												audiom.play();
											break;
											case "down":
												with (document.getElementById ('messege')) scrollTop = scrollHeight;
												with (document.getElementById ('online')) scrollTop = scrollHeight;
											break;
										}
									}
									</script>

									<div id="chatContainerTable">
										<audio id="audiom" preload="auto">
											<source src="/sound/messege.mp3">
											<source src="/sound/messege.ogg">
										</audio>

										<div id="chat">
											<div id="processing"></div>
											<div id="allmessege"></div>
											<div id="allonline"></div>
											<div id="messege"><i class="grey">Загрузка сообщений...</i></div>
											<div id="online"><i class="grey">Загрузка пользователей...</i></div>
											<div class="clear"></div>
											<div id="controlpanel">
												<img src="/images/plus.png" onclick="controlpanel('plus')"/>
												<img src="/images/minus.png" onclick="controlpanel('minus')"/>
												<img src="/images/test.png" onclick="controlpanel('test')"/>
												<img src="/images/down.png" onclick="controlpanel('down')"/>
											</div>

											<?php if(GetUser("ChatBan", $_SESSION['id']) != 1) { ?>

											<div id="frm">
											<form action="" method="post" id="messegeForm" onsubmit="return false;">
												<input type="text" name="messege" maxlength="255" placeholder="Текст сообщения" onkeyup="send(event)">
												<div id="smileBlock">
												
												<?php for($i = 1; $i <= 100; $i++) { ?>

													<img src="/images/smile/<?=$i; ?>.gif" alt="smile:<?=$i; ?>;"/>

												<?php } ?>
											</div>
												<img src="/images/smile.png" id="smile"/>
												<img src="/images/volume.png" id="volume" onclick="volumes()"/>
												<span class="button" onclick="process('messegeForm','/process/chat.php?action=send','messegeForm .button','messege','send','',76);">Отправить</span>
											</form>
											</div>

											<?php } else echo '<center>Вы забанены в чате</center>';?>
										</div>
										
									</div>
									<div id="chatFooter"><img src="/images/chat/bottom.jpg" width="655" height="27"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>                                
                    </td><!-- CONTENT USER -->
                </tr>
            </tbody>
        </table>
    </td>
</tr>
<!-- CLOSE CENTER -->








