<?php

require '../system/start.php';
require '../system/functions.php';
reg();
switch($act)
{
default:
$str['mesto'] = 'Голосования';
$str['block'] = $set['site'].' / Голосования';
include '../style/'.$style.'/head.php';
echo '<div class="on"><img class="ico" src="../style/'.$style.'/ico/vote.gif" alt="" /> <a href="index.php">Голосования</a></div>';
$vot = mysql_result(mysql_query("select count(*) from `users` where `where` = 'Голосования' and time > ".(time()-300).""),0);
$query = mysql_query("select * from `votes`	order by `id` desc limit ".$page.",".$set['str']."");
$total = mysql_result(mysql_query("select count(*) from `votes`"),0);
while($row = mysql_fetch_assoc($query))
{
	$i++;
	if($i%2)
	{
		echo '<div class="forum1">';
	}
	else
	{
		echo '<div class="forum2">';
	}
	echo '<a href="index.php?act=view&amp;id='.$row['id'].'">'.$row['title'].'</a> ('.$row['count'].') <br/><a href="index.php?act=results&amp;id='.$row['id'].'">[Просмотреть результаты]</a></div>';
}
if($total > $set['str'])
{
	pages($page, $total, 'topic.php?id='.$query['id'].'&amp;', $set['str']);
}
echo '<div class="where">В голосовании '.$vot.' человек</div>';
break;
case 'view':
$q = mysql_fetch_assoc(mysql_query("select * from `votes` where `id` = ".int($_GET['id']).""));
$str['mesto'] = $q['title'];
$str['block'] = $set['site'].' / Голосования ';
include '../style/'.$style.'/head.php';
echo '<div class="where">"'.$q['title'].'"</div>';
if(empty($q['title']))
{
	echo '<div class="on">Голосование не найдено!</div>';
	break;
}
$query = mysql_query("select * from `votes_answers` where `id_vote` = ".$q['id']."");
echo '<div class="on">';
if(mysql_num_rows($query) == 0)
{
	echo 'Вариантов нет!</div>';
	break;
}
while($row = mysql_fetch_assoc($query))
{
	echo '<a href="index.php?act=vote&amp;id='.$row['id'].'">'.$row['title'].'</a><br />';
}
echo '</div>';
echo '<div class="where"><img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="index.php">назад</a></div>';
break;
case 'vote':
reg();
$q = mysql_fetch_assoc(mysql_query("select * from `votes_answers` where `id` = ".int($_GET['id']).""));
$str['mesto'] = $q['title'];
$str['block'] = $set['site'].' / Голосования ';
include '../style/'.$style.'/head.php';
echo '<div class="where">"'.$q['title'].'"</div>';
if(empty($q['title']))
{
	echo '<div class="on">Вариант не найден!</div>';
	break;
}
$result = mysql_result(mysql_query("select count(*) from `votes_answers_users` where `id_user` = ".$user['id']." and `id_vote` = ".$q['id_vote'].""),0);
if($result != 0)
{
    echo '<div class="forum1">Вы уже голосовали на данную запись!</div>';
}
else
{
   	mysql_query("UPDATE `votes` SET `count` = `count`+1 WHERE `id` = ".$q['id_vote']."");
   	mysql_query("UPDATE `votes_answers` SET `count` = `count`+1 WHERE `id` = ".$q['id']."");
	mysql_query("INSERT INTO `votes_answers_users` (id_vote,id_user) VALUES (".$q['id_vote'].",".$user['id'].")");
	echo '<div class="forum1">Ваш голос успешно отдан!</div>';
}
echo '<div class="where"><img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="index.php">назад</a></div>';
break;
case 'results':
$q = mysql_fetch_assoc(mysql_query("select * from `votes` where `id` = ".int($_GET['id']).""));
$str['mesto'] = $q['title'];
$str['block'] = $set['site'].' / Голосования  ';
include '../style/'.$style.'/head.php';
echo '<div class="where">"'.$q['title'].'"</div>';
if(empty($q['title']))
{
	echo '<div class="on">Голосование не найдено!</div>';
	break;
}
$query = mysql_query("select * from `votes_answers` where `id_vote` = ".$q['id']."");
echo '<div class="where">';
if(mysql_num_rows($query) == 0)
{
	echo 'Вариантов нет!</div>';
	break;
}
while($row = mysql_fetch_assoc($query))
{
	if($q['count'] == 0)
	{
		$num = 0;
	}
	else
	{
		$num = round($row['count']/$q['count']*100, 2);
	}
	echo $row['title'].' <img src="grafic.php?num='.$num.'" alt=""><br />';
}
echo '</div>';
echo '<div class="where"><img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="index.php">назад</a></div>';
break;
}

include '../style/'.$style.'/foot.php';
?>