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

  header('location: /');
    
exit;

}

$id = _string(_num($_GET['id']));

if(!$id && $clan) {
    $id = $clan['id'];
}

  $i = mysql_query('SELECT * FROM `clans` WHERE `id` = "'.$id.'"');
  $i = mysql_fetch_array($i);

  if(!$i) {
  
      header('location: /clans/');
  
  exit;
  
  }

switch($_GET['action']) {
  default:

    $title = 'Клан "'.$i['name'].'"';    

include './system/h.php';

?>

<div class="title-top"><?=$title?></div>
 <div class='line'></div>

<?

if($clan && $clan['id'] == $i['id'] && $clan_memb['rank'] == 6 && $_GET['adm'] == true) {
  $text = _string($_POST['text']);
  if($text) {
    mysql_query('INSERT INTO `clan_msg` (`clan`,
                                             `user`,
                                             `text`,
                                             `time`) VALUES ("'.$clan['id'].'",
                                                             "'.$user['id'].'",
                                                                   "'.$text.'",
                                                                   "'.time().'")');
    header('location: /clan/');  
  }

?>

<div class='content'>
<form action='/clan/?adm=true' method='post'>
  Новое обьявление:<br/> <input name='text'/> <input type='submit' value='Отправить'/>
</form>

</div>
 <div class='line'></div>

<?

  }

?>


<?

  $_exp = round(100 / (clan_exp($i['level']) / $i['exp']));

  if($_exp > 100) {
  
     $_exp = 100;
  
  }

?>


<?

  if($clan && $clan['id'] == $i['id'] && $clan_memb['rank'] == 4) {

?>


<?

  }

?>


<?

  if($clan && $clan['id'] == $i['id'] && $clan_memb['rank'] == 4) {

?>


<?

  }

?>





<div class="separ2"></div><div class="menu_link2"><div style="padding:0px"><table><tr><td style="width:56px;padding-top:6px"><img src="/images/icon/clan/gerb/<?=$i['gerb']?>.png" alt="" style="border:1px solid #131313;border-radius:2px;height:50px;width:50px"/></td><td style="padding:2px;"><img src="http://213.239.195.28/xaos/16x16/tower.png" alt=""/> <span style="color:khaki;font-size:16px"><?=$i['name']?></span><br/><div style="color:#a5a5a5;font-size:small"><img src="http://213.239.195.28/xaos/16x16/arrow_up.png" alt=""/> Уровень: <?=$i['level']?><br/><img src="http://213.239.195.28/xaos/icons_old/exp2.png" alt=""/> Опыт: <?=numi($i['exp'])?> / <?=numi(clan_exp($i['level']))?></div></td></tr></table></div>




<?

if($clan['id'] == $i['id']) {

?>

<div style="color:#a5a5a5;font-size:small;padding-top:6px"><i>"Путь!!!"</i></div></div><div class="separ"></div><div class="menu_link2"><img src='/xaos/style2/<?=$i['r']?>.png' alt=''/> Звание: <span style="color:khaki">Верховный Магистр</span><br/>
<img src="http://213.239.195.28/xaos/icons_old/exp2.png" alt=""/> <span style="color:khaki">2.41k</span> (0%)
<img src="http://213.239.195.28/xaos/16x16/purse.png" alt=""/> <span style="color:khaki">402</span> (0%)
<img src="http://213.239.195.28/xaos/style2/money.png" alt=""/> <span style="color:khaki">0</span> (0%)
<img src="http://213.239.195.28/xaos/16x16/diamond.png" alt=""/> <span style="color:khaki">0</span> (0%) 
</div><div class="separ"></div><div class="menu_link2"><div style="color:#a5a5a5;font-size:small"><img src="http://213.239.195.28/xaos/16x16/chess_tower.png" alt=""/> Основатель: <a href="">1</a><br/><img src="http://213.239.195.28/xaos/style2/dsword.png" alt="" style="height:16px"/> Сторона: <span style="color:khaki"> <?=($i_clan['r'] == 0 ? 'Орда':'Альянс')?></span>, 0 место в Лиге Кланов.<br/></div></div>
<div class="separ"></div><div class="menu_link2"><div style="color:#a5a5a5;font-size:small"><a href="/clan/money/" class="mybutt_off" style="color:white"><img src="http://213.239.195.28/xaos/style2/money.png" alt=""/> Золото: <?=numi($clan['g'])?></a><a href="/clan/money/" class="mybutt_off" style="color:white"><img src="http://213.239.195.28/xaos/16x16/purse.png" alt=""/> Монеты: <?=numi($clan['s'])?></a><a href="/clan/money/" class="mybutt_off" style="color:white"><img src="http://213.239.195.28/xaos/16x16/diamond.png" alt=""/> Кристаллы: 95.73k</a></div></div>

<div class="separ"></div><a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/mail_yellow.png" alt=""/> Рассылка уведомлений<span style="color:#a5a5a5">, всем за <img src="http://213.239.195.28/xaos/16x16/purse.png" alt=""/> 1k</span></a><div class="menu_link"><img src="http://213.239.195.28/xaos/16x16/helmet_mine.png" alt=""/> <span style="color:#a5a5a5">Золотой Рудник (5 д 7 ч)</span></div>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/style2/dsword.png" alt=""/> Тренировочный Лагерь <span style="color:#a5a5a5">(8 / 1750)</span></a>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/chess_tower.png" alt=""/> Алтари Клана <span style="color:#a5a5a5">(1 / 110)</span></a>
<a href="/clan/built/" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/tower.png" alt=""/> Постройки Клана <span style="color:#a5a5a5">(<?=$i['built_1']?> / 360)</span></a>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/chart_bar.png" alt=""/> Рейтинг клана</a>

<?

}

    $max = 100;
  $count = mysql_result(mysql_query('SELECT COUNT(*) FROM `clan_memb` WHERE `clan` = "'.$i['id'].'"'),0);
  $pages = ceil($count/$max);
   $page = _string(_num($_GET['page']));

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

?>


<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/user_gladiator.png" alt=""/> Состав клана<span style="color:#a5a5a5"> (<?=$count?> / 100)</span></a>

<?

if($count > 0) {
?>

<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/books.png" alt=""/> Боевая Летопись</a>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/document_signature.png" alt=""/>  Дипломатия</a><a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/document_index.png" alt=""/>   История <span style="color:#a5a5a5"> *</span></a>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/icons_old/mails-stack.png" alt=""/>   Рассылки клана <span style="color:#a5a5a5"> (518 д 3 ч)</span></a><a href="/chat/clan/" class="menu_link"><img src="http://213.239.195.28/xaos/icons_old/balloon-ellipsis.png" alt=""/> Чат<?=($_chat > 0 ? '<font color=\'\'>*</font>':'')?></a>
<a href="" class="menu_link"><img src="http://213.239.195.28/xaos/16x16/newspaper.png" alt=""/> Форум <span style="color:#a5a5a5">*</span></a>


<?

  $_chat = mysql_query('SELECT COUNT(*) FROM `chat` WHERE `clan` = "'.$clan['id'].'" AND `to` = "'.$user['id'].'" AND `read` = "0"');
  $_chat = mysql_result($_chat,0);

?>



<div class='menu'>
  <li><b>Состав клана: (<?=$count?>)</b></li>



<?


$q = mysql_query('SELECT * FROM `clan_memb` WHERE `clan` = "'.$i['id'].'" ORDER BY `rank` DESC,`exp` DESC LIMIT '.$start.', '.$max.'');
  while($row = mysql_fetch_array($q)) {

  $memb = mysql_query('SELECT * FROM `users` WHERE `id` = "'.$row['user'].'"');
  $memb = mysql_fetch_array($memb);

  switch($row['rank']) {
  
    case 0:
    $rank = 'Новобранец';
     break;
    case 1:
    $rank = 'Послушник';
     break;
    case 2:
    $rank = 'Солдат';
     break;
    case 3:
    $rank = 'Воин';
     break;
    case 4:
    $rank = 'Рыцарь';
     break;
case 5:
$rank = 'Лорд';
break;
case 6:
$rank = 'Привратник';
break;
case 7:
$rank = 'Казначей';
break;
case 8:
$rank = 'Верховный Магистр';
break;
case 9:
$rank = 'Верховный Магистр';
break;
    
  }

?>

<li>
<?
  
if($clan && $clan['id'] == $i['id'] && $row['user'] != $user['id'] && $clan_memb['rank'] == 9 && $_GET['adm'] == true) {

?>

<span style='float: right;'><a href='/clan/memb/<?=$row['id']?>/'><img src='/images/icon/settings.png' alt='*'/></a></span>

<?

}

?> 

<img src='/xaos/style2/<?=$memb['r']?>.png' alt=''/> <a href='/user/<?=$memb['user']?>/'><?=$memb['login']?></a>, <?=n_f($row['exp'])?> - <?=$rank?>

<?

  }

?>

<li class='no_b'><?=pages('/clan/'.$i['id'].'/'.($_GET['adm'] == true ? '?adm=true&':'?'))?></li>

<?
  
  }
  else
  {
 
?>


<?

  }

?>

<
</div>

<?

if($clan && $clan['id'] == $i['id']) {

  if(isSet($_GET['exit']) && $clan_memb['rank'] != 9) {
  
  mysql_query('DELETE FROM `clan_memb` WHERE `clan` = "'.$clan['id'].'" AND `user` = "'.$user['id'].'"');
    
    header('location: /clans/');

  exit;

  }

?>


<div class='line'></div>
<div class='list'>

<?

if($clan['id'] == $i['id'] && $clan_memb['rank'] == 9) {

?>

  <li><a href='/clan/<?=($_GET['adm'] == true ? '':'?adm=true')?>' <?=($_GET['adm'] == true ? 'style="color:#999;"':'')?>><img src='/images/icon/arrow.png' alt='*'/> <?=($_GET['adm'] == true ? 'Скрыть управление':'Управление кланом')?></a></li>

<?

  }

?>

  <li class='no_b'><a href='/clan/?exit'><img src='/images/icon/arrow.png' alt='*'/> Покинуть клан</a></li>

</div>

<?

}

?>

</div>

<?

if($clan['id'] == $i['id'] && $clan_memb['rank'] == 9) {

?>

 <div class='line'></div>
<div class='content'>

<?

  if($_POST['change_rank_for_invite']) {
  
    $rank = _string(_num($_POST['rank']));
    
    mysql_query('UPDATE `clans` SET `rank_for_invite` = "'.$rank.'" WHERE `id` = "'.$clan['id'].'"');
  
  
    header('location: /clan/');
  
  }

?>

  Приглашать в клан может:<br/>
   <form action='/clan/' method='post'>
   <select name='rank'>
  <option value='6'>Привратник</option>
 <option value='7'>Казначей</option>
 <option value='8'>Верховный Магистр</option>
 <option value='9'>Верховный Магистр</option>
   </select><br/>
   
    <input type='submit' value='Сохранить' name='change_rank_for_invite'/>
   </form>
</div>
 <div class='line'></div>
<div class='content'>

<?

  if($_POST['change_rank_for_delete']) {
  
    $rank = _string(_num($_POST['rank']));
    
    mysql_query('UPDATE `clans` SET `rank_for_delete` = "'.$rank.'" WHERE `id` = "'.$clan['id'].'"');
  
  
    header('location: /clan/');
  
  }

?>

   Удалять из клана может:<br/>
   <form action='/clan/' method='post'>
   <select name='rank'>
  <option value='7','8','9'>все</option>
   <option value='8'>Верховный Магистр</option>
   </select><br/>
   
   <input type='submit' value='Сохранить' name='change_rank_for_delete'/>
   </form>
</div>
 <div class='line'></div>
<div class='content'>

<?

  if($_POST['change_name']) {
  
    if($clan['g'] < 500) {
    
      header('location: /clan/');
     
    exit;
    
    }
    
    $name = _string($_POST['name']);
    
    if($name) {
    
    mysql_query('UPDATE `clans` SET `g` = `g` - 500,
                                 `name` = "'.$name.'" WHERE `id` = "'.$clan['id'].'"');
  
    }
  
    header('location: /clan/');
  
  }

?>

  <form action='/clan/?change_name=true' method='post'>
  Новое название:<br/>
  <input name='name'/> <input type='submit' value='Сохранить' name='change_name'/>
  </form>
</div>

<?

}
  
include './system/f.php';
  break;



  case 'money':
    $title = 'Казна клана';    

include './system/h.php';  

if(!$clan['id'] OR $clan['id'] != $i['id']) {

  header('location: /clan/');

exit;

}

$g = _string(_num($_POST['g']));

$s = _string(_num($_POST['s']));

  if($g OR $s) {
    if($g && $user['g'] >= $g) {
    
      mysql_query('UPDATE `clans` SET `g` = `g` + '.$g.' WHERE `id` = "'.$clan['id'].'"');
      mysql_query('UPDATE `users` SET `g` = `g` - '.$g.' WHERE `id` = "'.$user['id'].'"');
    
    }
    
    if($s && $user['s'] >= $s) {
      
      mysql_query('UPDATE `clans` SET `s` = `s` + '.$s.' WHERE `id` = "'.$clan['id'].'"');
      mysql_query('UPDATE `users` SET `s` = `s` - '.$s.' WHERE `id` = "'.$user['id'].'"');
    
    }
  
  header('location: /clan/money/');
  
  }

?>
<div class='title'><?=$title?></div>
 <div class='line'></div>
<div class='content'>
   Казна клана: <img src='/images/icon/silver.png' alt='*'/> <?=n_f($i['s'])?> <img src='/images/icon/gold.png' alt='*'/> <?=n_f($i['g'])?><br/>
У вас на счету: <img src='/images/icon/silver.png' alt='*'/> <?=n_f($user['s'])?> <img src='/images/icon/gold.png' alt='*'/> <?=n_f($user['g'])?>
</div>
 <div class='line'></div>
<div class='content'>

<form action='/clan/money/' method='post'>
  <img src='/images/icon/gold.png' alt='*'/>   <input name='g' value='0'/><br/>
  <img src='/images/icon/silver.png' alt='*'/> <input name='s' value='0'/><br/>
  <input type='submit' value='Пополнить'/>
</form>

</div>

<?
include './system/f.php';
  break;

  case 'memb':

if(!$clan['id'] OR $clan['id'] == $i['id'] && $clan_memb['rank'] < 9) {

  header('location: /clan/');

exit;

}

$memb = _string(_num($_GET['memb']));

  $memb = mysql_query('SELECT * FROM `clan_memb` WHERE `clan` = "'.$clan['id'].'" AND `id` = "'.$memb.'"');
  $memb = mysql_fetch_array($memb);

  if(!$memb) {

  header('location: /clan/');

exit;
  
  }
  
  $memb_user = mysql_query('SELECT * FROM `users` WHERE `id` = "'.$memb['user'].'"');
  $memb_user = mysql_fetch_array($memb_user);
  
    $title = $memb_user['login'];    

include './system/h.php';  

    if($memb['rank'] != 7 && $memb['rank'] < $clan_memb['rank']) {
  
  if($_GET['up'] == true) {
  
      mysql_query('UPDATE `clan_memb` SET `rank` = "'.($memb['rank'] + 1).'" WHERE `clan` = "'.$clan['id'].'" AND `id` = "'.$memb['id'].'"');

      header('location: /clan/memb/'.$memb['id'].'/');
  
  }
     
  }

  if($memb['rank'] < $clan_memb['rank'] && $memb['rank'] > 0) {

    if($_GET['down'] == true) {

      mysql_query('UPDATE `clan_memb` SET `rank` = "'.($memb['rank'] - 1).'" WHERE `clan` = "'.$clan['id'].'" AND `id` = "'.$memb['id'].'"');

        header('location: /clan/memb/'.$memb['id'].'/');

    }

  }

?>

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

<div class='content'>

<?

  switch($memb['rank']) {
  
    case 0:
    $rank = 'Новобранец';
     break;
    case 1:
    $rank = 'Послушник';
     break;
    case 2:
    $rank = 'Солдат';
     break;
    case 3:
    $rank = 'Воин';
     break;
    case 4:
    $rank = 'Рыцарь';
     break;
case 5:
$rank = 'Лорд';
break;
case 6:
$rank = 'Привратник';
break;
case 7:
$rank = 'Казначей';
break;
case 8:
$rank = 'Верховный Магистр';
break;
    
  }

?>

Звание: <?=$rank?><br/>
  <img src='/images/icon/exp.png' alt='*'/> Опыт: <?=n_f($memb['exp'])?><br/>
  <img src='/images/icon/gold.png' alt='*'/> <?=n_f($memb['g'])?><br/>
<img src='/images/icon/silver.png' alt='*'/> <?=n_f($memb['s'])?><br/>
Дата вступления: <?=date('d.m.y', $memb['time'])?>

</div>
<div class='line'></div>
<div class='content' align='center'>
<?

     if($memb['rank'] != 7 && $memb['rank'] < $clan_memb['rank']) {

?>

  <a href='/clan/memb/<?=$memb['id']?>/?up=true' class='button'>Повысить</a>

<?

  }

  if($memb['rank'] < $clan_memb['rank'] && $memb['rank'] > 0) {

?>

 <a href='/clan/memb/<?=$memb['id']?>/?down=true' class='button'>Понизить</a>

<?
  
  }


?>

</div>
</div>

<?

  if($clan_memb['rank'] == 7) {
  
    if($_GET['lider'] == true) {
    
      mysql_query('UPDATE `clan_memb` SET `rank` = "7" WHERE `id` = "'.$memb['id'].'"');
      mysql_query('UPDATE `clan_memb` SET `rank` = "7" WHERE `user` = "'.$user['id'].'"');
    
    header('location: /clan/');
    
    }
  
?>

<div class='line'></div>
   <div class='content' align='center'>

  <a href='/clan/memb/<?=$memb['id']?>/?lider=true' class='button'>Передать лидерство</a>

</div>

<?
  
  }

  if($memb['rank'] < $clan_memb['rank'] && $clan_memb['rank'] >= $clan['rank_for_delete']) {
  
  if($_GET['delete'] == true) {
  
    mysql_query('DELETE FROM `clan_memb` WHERE `id` = "'.$memb['id'].'"');
  
  header('location: /clan/');
  
  }
  
?>

<div class='line'></div>
   <div class='content' align='center'>

  <a href='/clan/memb/<?=$memb['id']?>/?delete=true' class='button'>Исключить</a>

</div>

<?

  }

include './system/f.php';
  break;

  case 'built':
    $title = 'Статуя клана';    

include './system/h.php';  

if(!$clan['id'] OR $clan['id'] != $i['id']) {

  header('location: /clan/');

exit;

}

  $progress = round(100 / (34 / $i['built_1']));

  function cost($i) {
    
    switch($i) {
      case 0:
      $cost = 60000; 
       break;
      case 1:
      $cost = 60000; 
       break;
      case 2:
      $cost = 120000; 
       break;
      case 3:
      $cost = 180000; 
       break;
      case 4:
      $cost = 1800; 
       break;
      case 5:
      $cost = 120000; 
       break;
      case 6:
      $cost = 240000; 
       break;
      case 7:
      $cost = 360000; 
       break;
      case 8:
      $cost = 3600; 
       break;
      case 9:
      $cost = 180000; 
       break;
      case 10:
      $cost = 360000; 
       break;
      case 11:
      $cost = 540000; 
       break;
      case 12:
      $cost = 7200; 
       break;
      case 13:
      $cost = 240000; 
       break;
      case 14:
      $cost = 480000; 
       break;
      case 15:
      $cost = 720000; 
       break;
      case 16:
      $cost = 14400; 
       break;
      case 17:
      $cost = 300000; 
       break;
      case 18:
      $cost = 600000; 
       break;
      case 19:
      $cost = 900000; 
       break;
      case 20:
      $cost = 28800; 
       break;
      case 21:
      $cost = 360000; 
       break;
      case 22:
      $cost = 720000; 
       break;
      case 23:
      $cost = 1080000; 
       break;
      case 24:
      $cost = 57600; 
       break;
      case 25:
      $cost = 420000; 
       break;
      case 26:
      $cost = 840000; 
       break;
      case 27:
      $cost = 1260000; 
       break;
      case 28:
      $cost = 115200; 
       break;
      case 29:
      $cost = 480000; 
       break;
      case 30:
      $cost = 960000; 
       break;
      case 31:
      $cost = 230400; 
       break;
      case 32:
      $cost = 540000; 
       break;
      case 33:
      $cost = 1080000; 
       break;
      case 34:
      $cost = 1620000;
       break;
      case 35:
      $cost = 1620000;
       break;
    }
  
  return $cost;
  
  }
  
  function value($i) {
  
    switch($i) {
      case 0:
      $value = 0; 
       break;
      case 1:
      $value = 0; 
       break;
      case 2:
      $value = 0; 
       break;
      case 3:
      $value = 0; 
       break;
      case 4:
      $value = 1; 
       break;
      case 5:
      $value = 0; 
       break;
      case 6:
      $value = 0; 
       break;
      case 7:
      $value = 0; 
       break;
      case 8:
      $value = 1; 
       break;
      case 9:
      $value = 0; 
       break;
      case 10:
      $value = 0; 
       break;
      case 11:
      $value = 0; 
       break;
      case 12:
      $value = 1; 
       break;
      case 13:
      $value = 0; 
       break;
      case 14:
      $value = 0; 
       break;
      case 15:
      $value = 0; 
       break;
      case 16:
      $value = 1; 
       break;
      case 17:
      $value = 0; 
       break;
      case 18:
      $value = 0; 
       break;
      case 19:
      $value = 0; 
       break;
      case 20:
      $value = 1; 
       break;
      case 21:
      $value = 0; 
       break;
      case 22:
      $value = 0; 
       break;
      case 23:
      $value = 0; 
       break;
      case 24:
      $value = 1; 
       break;
      case 25:
      $value = 0; 
       break;
      case 26:
      $value = 0; 
       break;
      case 27:
      $value = 0; 
       break;
      case 28:
      $value = 1; 
       break;
      case 29:
      $value = 0; 
       break;
      case 30:
      $value = 0; 
       break;
      case 31:
      $value = 1; 
       break;
      case 32:
      $value = 0; 
       break;
      case 33:
      $value = 0; 
       break;
      case 34:
      $value = 0; 
       break;

    }
    
  return $value;
      
  }

?>
<div class='title'><?=$title?></div>
 <div class='line'></div>
<div class='content'>
<img src='/images/icon/clan.png' alt='*'/> <b>Статуя клана:</b> <img src='/images/icon/level.png' alt='*'/> <?=$i['built_1']?> уровень<br/>
<?
  
  if($i['built_1'] > 0) {

?>

Бонус: <font color='#90c090'>+<?=clan_buff($i['built_1'])?></font> к сумме параметров<br/>

<?
  
  }

?>

<font color='#90b0c0'>Прогресс:</font> <?=$progress?>%
</div>
 <div class='line'></div>
<div class='menu'>

<?

  if($i['id'] == $clan['id'] && $clan_memb['rank'] == 8 && $i['built_1'] < 34) {

  if($_GET['up'] == true) {
  
    if($i[(value($i['built_1']) == 1 ? 'g':'s')] >= cost($i['built_1'])) {
    
      mysql_query('UPDATE `clans` SET `built_1` = `built_1` + 1,
     `'.(value($i['built_1']) == 1 ? 'g':'s').'` = `'.(value($i['built_1']) == 1 ? 'g':'s').'` - '.cost($i['built_1']).' WHERE `id` = "'.$i['id'].'"');
    
    header('location: /clan/built/');
    
    }
  
  }

?>

  <li align='center'><a href='/clan/built/?up=true' class='button'>Улучшить за <img src='/images/icon/<?=(value($i['built_1']) == 1 ? 'gold':'silver')?>.png' alt= '*' /> <?=cost($i['built_1'])?></a></li>

<?

  }

?>

  <li class='no_b'></li>

</div>

<?

include './system/f.php';
  break;


}

?>