<?
if (isset($user))
{
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `user_job` WHERE `type` = 'job' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) == 0)
	{
		mysql_query("INSERT INTO `user_job` (`id_user`, `time`) values('$user[id]', '" . time() . "')"); 
	}
	
	$job = mysql_fetch_assoc(mysql_query("SELECT * FROM `user_job` WHERE `id_user` = '" . $user['id'] . "' LIMIT 1"));
	
	
	// Крон
	$cron_job = mysql_query("SELECT * FROM `cron` WHERE `id` = 'job'");
	if (mysql_num_rows($cron_job) == 0)mysql_query("INSERT INTO `cron` (`id`, `time`) VALUES ('job', '$ftime')");

	$l_job = mysql_fetch_assoc($cron_job);

	if ($l_job['time'] < $ftime)
	{
		mysql_query("UPDATE `user_job` SET `days` = '0', `status` = '0', `files` = '0', `photo` = '0', `notes` = '0', `time_active` = '0' WHERE `status` != '1' OR `files` != '5' OR `photo` != '5' OR `notes` != '2' OR `time_active` < '180'");
		mysql_query("UPDATE `user_job` SET `days` = `days` + '1', `status` = '0', `files` = '0', `photo` = '0', `notes` = '0', `time_active` = '0' WHERE `status` = '1' AND `files` = '5' AND `photo` = '5' AND `notes` = '2' AND `time_active` > '179'");
		mysql_query("UPDATE `cron` SET `time` = '" . $ftime . "' WHERE `id` = 'job'");
	}
	
	// Cохраняем файлы
	if ($job['files'] != '5' && mysql_result(mysql_query("SELECT COUNT(*) FROM `obmennik_files` WHERE `time` > '$ftime' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) >= 5)
	{
		mysql_query("UPDATE `user_job` SET `files` = '5' WHERE `id_user` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user` SET `balls` = '" . ($user['balls'] + 100) . "' WHERE `id` = '" . $user['id'] . "'");
		$_SESSION['job_success'] = 1;
	} 
	
	// Cохраняем фото
	if ($job['photo'] != '5' && mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `time` > '$ftime' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) >= 5)
	{
		mysql_query("UPDATE `user_job` SET `photo` = '5' WHERE `id_user` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user` SET `balls` = '" . ($user['balls'] + 100) . "' WHERE `id` = '" . $user['id'] . "'");
		$_SESSION['job_success'] = 1;
	} 
	
	// Cохраняем дневники
	if ($job['notes'] != '2' && mysql_result(mysql_query("SELECT COUNT(*) FROM `notes` WHERE `time` > '$ftime' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) >= 2)
	{
		mysql_query("UPDATE `user_job` SET `notes` = '2' WHERE `id_user` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user` SET `balls` = '" . ($user['balls'] + 100) . "' WHERE `id` = '" . $user['id'] . "'");
		$_SESSION['job_success'] = 1;
	} 
	
	// Cохраняем статусы
	if ($job['status'] != '1' && mysql_result(mysql_query("SELECT COUNT(*) FROM `status` WHERE `time` > '$ftime' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) >= 1)
	{
		mysql_query("UPDATE `user_job` SET `status` = '1' WHERE `id_user` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user` SET `balls` = '" . ($user['balls'] + 100) . "' WHERE `id` = '" . $user['id'] . "'");
		$_SESSION['job_success'] = 1;
	} 
	
	// Фиксируем время проведенное на сайте
	if ($job['time_active'] >= '180' && $job['time_active'] != '1000')
	{
		mysql_query("UPDATE `user_job` SET `time_active` = '1000' WHERE `id_user` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user` SET `rating` = '" . ($user['rating'] + 1) . "', `balls` = '" . ($user['balls'] + 100) . "' WHERE `id` = '" . $user['id'] . "'");
		$_SESSION['job_success'] = 1;
	} 
	
	// Обновляем время
	if ($job['time'] < time() && $job['time_active'] != '1000')
	{
		mysql_query("UPDATE `user_job` SET `time` = '" . (time() + 180) . "', `time_active` = '" . ($job['time_active'] + 3) . "' WHERE `id_user` = '" . $user['id'] . "'");	
	}
	
	if ($job['days'] == 10)
	{
		mysql_query("UPDATE `user` SET `rating` = '" . ($user['rating'] + 30) . "', `balls` = '" . ($user['balls'] + 3000) . "', `money` = '" . ($user['money'] + 5) . "', `my_job` = '0' WHERE `id` = '" . $user['id'] . "'");
		mysql_query("UPDATE `user_job` SET `days` = '0' WHERE `id_user` = '" . $user['id'] . "'");
		
		// Сообщение о выплатах
		$msg = 'Уважаемый [b]' . user::nick($user['id'], 0) . '[/b] вы выполнили (СУПЕР ЗАДАНИЕ) Тимона, и в награду получаете [red]5[/red] [blue]монет[/blue], [red]3000[/red] [blue]баллов[/blue] и [red]30%[/red] [blue]рейтинга[/blue] .ор. Выполняйте задания еще 10 дней подряд, и снова получите награду за (СУПЕР ЗАДАНИЕ) :) ';
		
		// Отправляем
		mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$user[id]', '".my_esc($msg)."', '$time')");
	}
}
?>