<?
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/start.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/sess.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/home.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/settings.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/db_connect.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/ipua.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/sys/inc/fnc.php';

$id = (int) $_GET['id'];

$foto = mysql_fetch_assoc(query("SELECT * FROM `gallery_foto` WHERE `id` = '$id'  LIMIT 1"));
$gallery = mysql_fetch_assoc(query("SELECT * FROM `gallery` WHERE `id` = '$foto[id_gallery]' LIMIT 1")); 
$ank = get_user($gallery['id_user']);
$user = get_user($_SESSION['id_user']);

if (!$foto['id']) 
{
	die('Фотография не найдена');
	exit;
}

if (isset($user) && $user['id'] != $ank['id'] && isset($_GET['rating']))
{
	$rate = (int) $_GET['rating'];
	
	if (mysql_result(query("SELECT COUNT(*) FROM `gallery_rating` WHERE `id_user` = '$user[id]' AND `id_foto` = '$foto[id]'"), 0) == 0)
	{	
		if ($rate > 0 && $rate < 7)
		{
			$c = mysql_result(query("SELECT COUNT(*) FROM `user_set` WHERE `id_user` = '$user[id]' AND `ocenka` > '$time'"), 0);
			
			if ($c == 0 && $rate == 6)
			{
				?>
				<script language="JavaScript"> 
				  window.location.href = "/user/money/plus5.php"
				</script>
				<?
				exit;
			}
			
			query("INSERT INTO `gallery_rating` (`id_user`, `id_foto`, `like`, `time`, `avtor`) values('$user[id]', '$foto[id]', '" . $rate . "', '$time', $foto[id_user])",$db);
			query("UPDATE `gallery_foto` SET `rating` = '" . ($foto['rating'] + $rate) . "' WHERE `id` = '$foto[id]' LIMIT 1",$db);
		}
	}
	
	if (isset($_GET['update']))
	{
		$rate = mysql_fetch_assoc(query("SELECT * FROM `gallery_rating` WHERE `id_foto` = $foto[id] AND `id_user` = '$user[id]' LIMIT 1"));
		$c = mysql_result(query("SELECT COUNT(*) FROM `user_set` WHERE `id_user` = '$user[id]' AND `ocenka` > '$time'"), 0);
		
		if ($c == 0)
		{
			?>
			<script language="JavaScript"> 
			  window.location.href = "/user/money/plus5.php"
			</script>
			<?
			exit;
		}
		query("UPDATE `gallery_foto` SET `rating` = '6' WHERE `id` = '$foto[id]' LIMIT 1", $db);
		$rate = 6;
	}
	$link = "actionAjax('func=photo&id=" . $foto['id'] . "&rating=6&update', 'rate_". $foto['id']."');";
	?><?= __('Ваша оценка')?> <span class="rtr_icon rtr_<?= $rate?>"></span> <?= ($rate < 6 ? __('повысить до') . ' <a onclick="' . $link . '" title="5+"><span class="rtr_icon rtr_6"></span></a>' : '')?>
    
    <?	
}
exit;