<?php

require_once '../incfiles/core.php';
require_once '../incfiles/auth.php';
require_once '../autoload/navigation.php';
require_once '../autoload/user.php';
require_once '../autoload/smiles.php';
require_once '../incfiles/user.php';

if (empty($user_id)) go(URL);
if ($_GET['p']==1) go(URL.'/chat/');

$rand = rand(0000,9999);

require_once 'incfiles/func.php';
require_once 'handler/msg.php';

$title = lang('Мини-Чат','Міні-Чат');
require_once '../incfiles/head.php';

	?>
	<script type="text/javascript">
	$(function(){
			$('#form').on('submit', function(e){
		    e.preventDefault();
		    var $that = $(this),
		        formData = new FormData($that.get(0));
		    $.ajax({
		      url: $that.attr('action'),
   		     type: $that.attr('method'),
		      contentType: false,
		      processData: false,
		      data: formData,
		      success: function(response){
 		       if(response){
					$('#content_chat').load($that.attr('action')+ '#content_chat .chat');
					$('#textarea').val('');
					$("#form_content").html('');
					$("#form_remsg").html('');
 		      }
		    }
		    });
		  });
		});
	</script>
	<?

echo '<div class="title">'.lang('Мини-Чат','Міні-Чат').'</div>
	<div class="block2">
	<form id="form" action="'.URL.'/chat/" method="post">
	<input id="textarea" class="input" name="msg" value=""/>
	<div id="form_remsg"></div>
	<input class="button2" type="submit" value="'.lang('Написать','Написати').'">
	<a class="button3" href="'.URL.'/ajax/smiles.php" onclick="FormContent.Smiles(this.href); return false"><img src="'.URL.'/design/img/smile.png"/></a>
	<a class="for_mobile button3" href="'.URL.'/chat/?r='.$rand.'" onclick="Page.Go(this.href); return false"><img src="'.URL.'/design/img/refresh.png"/></a>
	</form>
	</div><div id="form_content"></div>';
	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `chat`"),0);
	if ($all > 0)
	{
		$cp = ceil($all/15);
		$p = (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p']>2 and $_GET['p']<=$cp) ? $_GET['p'] : 2;
		$start = $p*15-15;
		$result = mysql_query("SELECT * FROM `chat` ORDER by `time` DESC LIMIT $start,15");
		while ($arr = mysql_fetch_array($result)) echo '<div class="block"><a href="" onclick="Chat.reMsg('.$arr['user_id'].',\''.user_name3($arr['user_id']).'\'); return false"><b>'.user_name3($arr['user_id']).'</b></a> '.vtime($arr['time']).' '.(!empty($arr['for_id']) && $arr['for_id']==$user_id?'<font color="#E66916">'.links(smiles($arr['msg'])).'</font>':links(smiles($arr['msg']))).'</div>';
		navi($p,$cp,'/chat/history.php?');
	}

require_once '../incfiles/foot.php';


?>