<?
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', `guest` = '0', `files` = '0', `photo` = '0', `notes` = '0', `time_active` = '0' WHERE `status` != '1' OR `photo` != '5' OR `notes` != '2' OR `time_active` < '180'");
		mysql_query("UPDATE `user_job` SET `days` = `days` + '1', `status` = '0', `guest` = '0', `files` = '0', `photo` = '0', `notes` = '0', `time_active` = '0' WHERE `status` = '1' AND `photo` = '5' AND `notes` = '2' AND `time_active` > '179'");
		mysql_query("UPDATE `cron` SET `time` = '" . $ftime . "' WHERE `id` = 'job'");
	}
	
	// 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охраняем guest
	if ($job['guest'] != '1' && mysql_result(mysql_query("SELECT COUNT(*) FROM `guest` WHERE `time` > '$ftime' AND `id_user` = '" . $user['id'] . "' LIMIT 1"), 0) >= 1)
	{
		mysql_query("UPDATE `user_job` SET `guest` = '1' 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'] . "'");	
	}
	
}
?>