<?
    
    include './system/common.php';
    
 include './system/functions.php';
        
      include './system/user.php';
    
if(!$user) {

  header('location: /');
    
exit;

}


switch($_GET['action']) {

default:

    $title = 'Рейтинг кланов';    

include './system/h.php';  

?>

<div class='title'><?=$title?></div>
 <div class='line'></div>

<div class='menu'>
<?

    $max = 10;
  $count = mysql_result(mysql_query('SELECT COUNT(*) FROM `clans`'),0);
  $pages = ceil($count/$max);
   $page = _string(_num($_GET['page']));

    if($page > $pages) {
    
   $page = $pages;
    
    }
  
    if($page < 1) {
    
   $page = 1;
    
    }
    
  $start = $page * $max - $max;


  if($page == 1) {
  
    $i = $page - 1;
  
  }
  elseif($page == 2) {
    
    $i = ($page + 9);
  
  }
  else
  {
  
    $i = ($page * 10) - 9;
  
  }

if($count > 0) {

$q = mysql_query('SELECT * FROM `clans` ORDER BY `level` DESC,`exp` DESC LIMIT '.$start.', '.$max.'');
  while($row = mysql_fetch_array($q)) {
  
  $i++;

if($i < 4) {

?>

<div class='content'>
<?=$i?> место<br/>

<table cellpadding='0' cellspacing='0'>
<tr>
<td><img src='/images/icon/clan/gerb/<?=$row['gerb']?>.png' alt='*'/></td><td valign='top' style='padding-left: 5px;'><img src='/images/icon/clan/<?=$row['r']?>.png' alt=''*/> <a href='/clan.php?id=<?=$row['id']?>'><?=$row['name']?></a><br/>
<img src='/images/icon/level.png'/> Уровень: <b><?=$row['level']?></b><br/>
<img src='/images/icon/exp.png' alt='*'/> Опыт: <?=n_f($row['exp'])?></td>
</tr></table>
</div><div class='line'></div>

<?

  }
  else
  {

?>

<li><img src='/images/icon/clan/<?=$row['r']?>.png' alt=''*/> <a href='/clan.php?id=<?=$row['id']?>'><?=$row['name']?></a>, <img src='/images/icon/level.png'/> <?=$row['level']?></li>

<?

  }


  }

?>

<li><?=pages('?')?></li>

<?

  }
  else
  {

?>


<?

  }
  
?>

<li><a href='?action=search'><img src='/images/icon/clan.png' alt='*'/> Поиск клана</a></li>

<li class='no_b'><a href='/clans/create/'><img src='/images/icon/clan.png' alt='*'/> Создать клан</a></li>


</div>

<?

include './system/f.php';

  break;

  case 'create':

    $title = 'Создать клан';    

include './system/h.php';  

$cost = 2000;

?>

<div class='title'><?=$title?></div>
 <div class='line'></div>

<?

  if($clan) {

?>

<div class='content'><font color='#999'>Для создания клана необходимо выйти из уже существующего</font></div>

<?

  }
  else
  {
  
$name = _string($_POST['name']);
$name = strToLower($name);

  if($name && $user['g'] >= $cost) {
    $clans = mysql_query('SELECT * FROM `clans` WHERE `name` = "'.$name.'"');
    $clans = mysql_fetch_array($clans);
  
  if(!$clans) {
    mysql_query('UPDATE `users` SET `g` = "'.($user['g'] - $cost).'" WHERE `id` = "'.$user['id'].'"');
  
  mysql_query('INSERT INTO `clans` (`name`,`r`) VALUES ("'.$name.'", "'.$user['r'].'")');

  $clan_id = mysql_insert_id();
  
  mysql_query('INSERT INTO `clan_memb` (`clan`,`user`,`rank`, `time`,`last_update`) VALUES ("'.$clan_id.'", "'.$user['id'].'", "4", "'.time().'","'.(time() + ((60 * 60) * 24)).'")');
  
  header('location: /clan.php');
  
  }
  
  }

?>

<div class='content' align='center'>
  <form action='' method='post'>
  Название клана:<br/>
  <input name='name'/><br/>
  <input type='submit' value='Создать клан'/><br/>
  <font color='#999'>Цена: <img src='/images/icon/gold.png' alt='*'/> <?=$cost?> золота</font>
  </form>
</div>

<?
  
  }

include './system/f.php';

  break;
  
}

?>