<?
    
    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-top">Кланы</div><div class="separ2"></div><div class="menu_link2" style="text-align:center"><a href="/clans/create/" class="mybutt_att" style="width:42%"><img src="http://213.239.195.28/xaos/16x16/tower.png" alt=""/> Создать клан</a> <a href="" class="mybutt_att" style="width:42%"><img src="http://213.239.195.28/xaos/16x16/user_knight.png" alt=""/> Я хочу в клан!</a></div>

<?

    $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="separ"></div><div class=""><div class="menu_link3"><img src="http://213.239.195.28/xaos/16x16/tower.png" alt=""/> <a href="/clan.php?id=<?=$row['id']?>" style="color:skyblue"><?=$row['name']?></a><span style="color:#a5a5a5"> <img src="http://213.239.195.28/xaos/16x16/arrow_up.png" alt=""/> <?=$row['level']?> ур. <img src="http://213.239.195.28/xaos/icons_old/exp2.png" alt=""/> <?=n_f($row['exp'])?> </span></div>



<?

  }
  else
  {

?>

<div class="menu_link3"><img src="http://213.239.195.28/xaos/16x16/tower.png" alt=""/> <a href="/clan.php?id=<?=$row['id']?>" style="color:skyblue"><?=$row['name']?></a><span style="color:#a5a5a5"> <img src="http://213.239.195.28/xaos/16x16/arrow_up.png" alt=""/> <?=$row['level']?> ур. <img src="http://213.239.195.28/xaos/icons_old/exp2.png" alt=""/> <?=n_f($row['exp'])?> </span></div>

<?

  }


  }

?>


</div><div class="separ"></div><div class="menu_link2"><div class="div_hr2"></div><center><div class="navi">  <span class="mybutt_no" style="color:white;width:25px">1</span>  <a href="<?=pages('?')?>" class="mybutt_att" style="width:25px">2</a>  <a href="<?=pages('?')?>" class="mybutt_att" style="width:25px">3</a>  <a href="<?=pages('?')?>" class="mybutt_att" style="width:25px">></a>  <a href="<?=pages('?')?>" class="mybutt_att" style="width:35px">>></a> <?=pages('?')?></div></center></div><div class="separ"></div><a href="" class="menu_link"><img src="http://213.239.195.28/xaos/icons_old/balloon-ellipsis.png" alt=""/> Заявка на вступление</a><a href="?action=search" class="menu_link"><img src="http://213.239.195.28/xaos/icons/zoom.png" alt=""/> Поиск клана</a><div class="separ3"></div>
<?

  }
  else
  {

?>


<?

  }
  
?>








<?

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'].'", "9", "'.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;
  
}

?>