<?

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
    define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );
    session_start();
    header( 'Content-type: text/html; charset=windows-1251' );
    include_once ROOT_DIR . '/engine/config.php';
    include_once ROOT_DIR . '/engine/function.php';
} else {
    if ( !defined( 'MISOFT' ) ) { exit(); }
}

if ( isset( $_GET['id'] ) ) {
    $get_cat_id = mysql_query( "SELECT id, cat_id, title FROM catalog_id WHERE id = '" . ( int ) $_GET['id'] . "'" );
    if ( mysql_num_rows( $get_cat_id ) > 0 ) {
        $get_cat_id_row = mysql_fetch_array( $get_cat_id );
        $get_cat_title = mysql_fetch_array( mysql_query( "SELECT title FROM catalog WHERE id = '" . $get_cat_id_row['cat_id'] . "'" ) );
        $catalog_mes_all = mysql_query( "SELECT * FROM catalog_mes WHERE cat_id = '" . ( int ) $_GET['id'] . "' ORDER BY likes DESC" );
        
        $kolvo = 15;
		$all = mysql_num_rows( $catalog_mes_all );
		$allpage = ceil( $all / $kolvo );
		$this_page = ( isset( $_GET['page'] ) ) ? intval( $_GET['page'] ) : 1 ;
		if ( $this_page < 1 ) $this_page = 1;
		if ( $this_page > $allpage ) $this_page = $allpage;
		$start = $this_page * $kolvo - $kolvo;
		if ( $start == -$kolvo ) { $start = 1; }
		
		$catalog_mes = mysql_query( "SELECT * FROM catalog_mes WHERE cat_id = '" . ( int ) $_GET['id'] . "' ORDER BY likes DESC LIMIT $start,$kolvo" );
		?>
		<div class="content_bar content_bar_fix_chain" >
		 <article class="postBox"> 
		  
		  <div class="navname">
			<a href="/catalog"  class="a_box">Каталог</a> 
			| <span class="navname_act"><?=$get_cat_id_row['title']?></span>
		  </div><hr>
		  <script type="text/javascript">
			$(document).ready(function(){
			  $('.catalog-time-add').tipTip({defaultPosition:'right'});
			});
			function likes_catalog(a) {
				$.post('/ajax.php?func=likes_catalog', {
					id:a
				}, function(data) {
					if (data == 1) {
						Page.Go('');
					} else {
						$('body').Qnotify({text:data, sound:'error'});
					}
				});
				return false;
			}
		  </script>
		  <?
		  if ( isset( $_SESSION['iduser'] ) and $config['catalogPrice'] <= $config_user['adv_money'] ) {
			?>
			  <div class="block-catalog-site-add">
				<a href="#" onclick="catalog.Show();return false;" class="btn-forum-topic-add"><i class="fa fa-plus" ></i> Добавить свой сайт</a>
			  </div>
			<?
		  }	
		  if( mysql_num_rows( $catalog_mes ) > 0 ) {
		    while ( $catalog_mes_row = mysql_fetch_array( $catalog_mes ) ) {
				$mmm = array( 'янв', 'фев', 'мар', 'апр', 'май', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек' );
				$catalog_mes_time = date( 'd', $catalog_mes_row['date'] ) . ' ' . $mmm[( int ) date( 'm', $catalog_mes_row['date'] ) - 1] . ' ' . date( 'Y', $catalog_mes_row['date'] );
				$InfoLike = mysql_query( "SELECT id FROM like_catalog_temp WHERE iduser = '".$_SESSION['iduser']."' AND id_catalog = '".$catalog_mes_row['id']."' " );
				if(mysql_num_rows( $InfoLike ) > 0) {
					$catalog_icon_like_type = 'fa-heart';
				} else {
					$catalog_icon_like_type = 'fa-heart-o';
				}
				?>
				<script type="text/javascript">
				  $(document).ready(function() {
					$.post('modules/catalog/seo.php?type=google_pr',{
					  url: '<?=$catalog_mes_row['url']?>'
					}, function(data) {
					  $('#cat_pr_<?=$catalog_mes_row['id']?>').html(data);
					});
					$.post('modules/catalog/seo.php?type=yandex_tic',{
					  url: '<?=$catalog_mes_row['url']?>'
					}, function(data) {
					  $('#cat_tic_<?=$catalog_mes_row['id']?>').html(data);
					});
					$.post('modules/catalog/seo.php?type=alexa',{
					  url: '<?=$catalog_mes_row['url']?>'
					}, function(data) {
					  $('#cat_alexa_<?=$catalog_mes_row['id']?>').html(data);
					});
				  });
				</script> <div id="modal_windows_open"></div>
				<div class="wrap_catalog_site">
				  <img src="http://mini.s-shot.ru/1024x768/180/png/?<?=$catalog_mes_row['url']?>" class="catalog-shot-style-full" />
				  <div class="catalog-likes-block">
				    <div class="catalog-likes-add">
					  <span class="catalog-likes">
					    <i class="fa <?=$catalog_icon_like_type?> catalog-likes-button" onclick="likes_catalog(<?=$catalog_mes_row['id']?>); return false;" aria-hidden="true"></i> 
						<?=$catalog_mes_row['likes']?>
					  </span>
					  <div class="catalog-time-add" title="Дата публикации сайта в нашем каталоге."><i class="fa fa-clock-o" aria-hidden="true"></i> <?=$catalog_mes_time?></div>
					</div>
				  </div>
				  <div class="wrap_catalog_mes">
					<div class="catalog_mes_title"><?=$catalog_mes_row['title']?></div>
					<div class="catalog_mes_text">
					  <?=$catalog_mes_row['c_text']?>
					  <div class="catalog_mes_url">
					    <a href="<?=$catalog_mes_row['url']?>" target="_blank" class="a_box">
					      <i class="fa fa-external-link" aria-hidden="true"></i> Перейти на сайт (в новой вкладке)
					    </a>
					  </div>
					</div>
					<div class="catalog_mes_bot_panel">
					  <span class="catalog_mes_seo">PR: <span id="cat_pr_<?=$catalog_mes_row['id']?>"></span> </span>
					  <span class="catalog_mes_seo">ТИЦ: <span id="cat_tic_<?=$catalog_mes_row['id']?>"></span> </span>
					  <span class="catalog_mes_seo">Alexa: <span id="cat_alexa_<?=$catalog_mes_row['id']?>"></span> </span>
					</div>
				  </div>
				</div>
			  <?
			}
		  } else {
			echo '<div class="alert alert-info " role="alert" >
			  <i class="fa fa-frown-o" aria-hidden="true"></i> Сожалеем но в этом разделе все еще пока пусто. 
			</div>';
		  }
		  ?>
		  <div class="news_navigation-page"><?=navigation( 15, ( int ) $_GET['page'], $all, 'catalog' . ( int ) $_GET['id'] . '&p=' );?></div>
		 </article>
		</div>
	  <?
	} else {
		echo '<div class="alert alert-warning page-error-fix" role="alert" >
		  <i class="fa fa-exclamation-circle" aria-hidden="true"></i> Ошибка! Раздел не существует или удален. 
		</div>';
	}
} else {
   ?>
    <div class="content_bar content_bar_fix_chain" style="min-height: 1150px;">
      <article class="postBox">
		<div class="h2-title-header"><h2><a>Каталог сайтов</a></h2></div>
		<div class="image_catalog_p"></div>
		<div class="textPreview" style="">
		  <p class="earnings_text" style="color: #545454;">
			Добро пожаловать в наш каталог сайтов, здесь вы найдете ресурсы, с описанием и SEO показателями, на любые тематики, будь то сайт по интернет маркетингу, или сайт с рецептами для домохозяек, интернет-магазины, и другие. Вы можете ставить лайк сайтам которые вам понравились, лидеры будут отображаться выше в списке выдачи. 
		  </p>
		</div>
		<?
		  if ( isset( $_SESSION['iduser'] ) and $config['catalogPrice'] <= $config_user['adv_money'] ) {
			?>
			  <div class="block-catalog-site-add-home">
				<a href="#" onclick="catalog.Show();return false;" class="btn-forum-topic-add"><i class="fa fa-plus" ></i> Добавить свой сайт</a>
			  </div>
			<?
		  }	
		?>
		<script type="text/javascript">
		  function likes_catalog(a) {
			$.post('/ajax.php?func=likes_catalog', {
				id:a
			}, function(data) {
				if (data == 1) {
					Page.Go('');
				} else {
					$('body').Qnotify({text:data, sound:'error'});
				}
			});
			return false;
		  }
		</script>
		<div class="wrap_catalog">
		<?
		  $cat = mysql_query( "SELECT * FROM catalog" );
		  while ( $cat_row = mysql_fetch_array( $cat ) ) {
			$cat_id = mysql_query( "SELECT * FROM catalog_id WHERE cat_id = '" . $cat_row['id'] . "'" );
			?>
			  <div class="wrapper-catalog">
                <div class="wrapper-catalog">
				  <div class="children-catalog">
					  <div class="catalog_title">
					   <?
					    if ($cat_row['id'] == 1) { $icon_catalog = "<i class='fa fa-globe' aria-hidden='true'></i> "; } else
					    if ($cat_row['id'] == 2) { $icon_catalog = "<i class='fa fa-cubes' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 3) { $icon_catalog = "<i class='fa fa-bullhorn' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 4) { $icon_catalog = "<i class='fa fa-graduation-cap' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 5) { $icon_catalog = "<i class='fa fa-money' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 6) { $icon_catalog = "<i class='fa fa-laptop' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 7) { $icon_catalog = "<i class='fa fa-newspaper-o' aria-hidden='true'></i> "; } else 
					    if ($cat_row['id'] == 8) { $icon_catalog = "<i class='fa fa-coffee' aria-hidden='true'></i> "; } 
					  ?>
					  <?=$icon_catalog?><?=$cat_row['title']?>
					  </div>
					  <div class="catalog-block-link">
					  <?
						while ( $cat_id_row = mysql_fetch_array( $cat_id ) ) {
							$sql_cat_coll = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM catalog_mes WHERE cat_id = '" . $cat_id_row['id'] . "'" ) );
							?>
							  <i class="fa fa-angle-right" aria-hidden="true"></i> <a href="/catalog=<?=$cat_id_row['id']?>" class="catalog-link"><?=$cat_id_row['title']?> [<?=$sql_cat_coll['count']?>] </a><br>
							<?
						}
					  ?> 
					  </div>
					  <div class="catalog-shot-site-main">
					   <?
						$cat_site_id = mysql_query( "SELECT * FROM catalog_mes WHERE cat_id_main = '" . $cat_row['id'] . "' ORDER BY likes DESC LIMIT 4" );
						while ( $cat_site_row = mysql_fetch_array( $cat_site_id ) ) {
						  $InfoLike = mysql_query( "SELECT id FROM like_catalog_temp WHERE iduser = '".$_SESSION['iduser']."' AND id_catalog = '".$cat_site_row['id']."' " );
						  if(mysql_num_rows( $InfoLike ) > 0) {
							$catalog_icon_like_type = 'fa-heart';
						  } else {
							$catalog_icon_like_type = 'fa-heart-o';
						  }
						  ?>
							<span class="like-home-catalog">
							  <span class="likes-catalog">
							    <i class="fa <?=$catalog_icon_like_type?> " onclick="likes_catalog(<?=$cat_site_row['id']?>); return false;" aria-hidden="true"></i> <?=$cat_site_row['likes']?>
							  </span>
							  <a href="<?=$cat_site_row['url']?>" target="_blank"><img src="http://mini.s-shot.ru/1024x768/50/png/?<?=$cat_site_row['url']?>" class="catalog-shot-style" /></a>
							</span>
						  <?
						}
					   ?>
					  </div>
				  </div>
				</div>
			  </div>
			<?
		  }
		?>
		</div>
	  </article>
    </div>
    <?
}

?>