<?php
session_start();

if( isset($_SESSION['init_alert']) )
{
	if( ( $str_temp = trim($_SESSION['init_alert']) ) != '' )
		$str_init_alert = $str_temp;

	unset($_SESSION['init_alert']);
}

if( !isset($_REQUEST['page_no']) || ( $uint_page_no = (int)$_REQUEST['page_no'] ) <= 0 )
	$static_uint_page_no = 1;
else
	$static_uint_page_no = $uint_page_no;

$uint_row_count = 12;

require_once('includes/common.php');

require_once('includes/data.php');
$obj_class_data = new class_data();

if( !( $arr_news_cates = $obj_class_data->get_news_cates() ) )
{
	$_SESSION['init_alert'] = '指定的新聞分類不存在';

	header('Location: latest_news_list.php');
	exit();
}

if( !isset($_REQUEST['cate_id']) || ( $uint_cate_id = (int)$_REQUEST['cate_id'] ) <= 0 || false === ( $uint_index = in_2d_array($uint_cate_id, $arr_news_cates, 'NewsCateId') ) )
{
	$_SESSION['init_alert'] = '指定的新聞分類不存在';

	header('Location: latest_news_list.php');
	exit();
}
else
	$static_uint_cate_id = $uint_cate_id;

$arr_page_info = $obj_class_data->page_info('`news`.`NewsId`', 'news` INNER JOIN `news_cates_news` ON `news_cates_news`.`NewsCateId` = \'' . (int)$static_uint_cate_id . '\' AND `news_cates_news`.`NewsId` = `news`.`NewsId', '`news`.`Active` = \'1\' AND `news`.`Date` <= \'' . date('Y-m-d') . '\'', $uint_row_count);

if( $arr_page_info['PageCount'] && $static_uint_page_no > $arr_page_info['PageCount'] )
	$uint_page_no = $arr_page_info['PageCount'];
else
	$uint_page_no = $static_uint_page_no;

$arr_top_news = $obj_class_data->get_top_news($static_uint_cate_id);

$arr_news = $obj_class_data->get_news($static_uint_cate_id, ( $uint_page_no - 1 ) * $uint_row_count, $uint_row_count);

$arr_banner = $obj_class_data->get_banners($static_uint_cate_id, 2);

$arr_suggest_news = $obj_class_data->get_suggest_news(4);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?=$arr_news_cates[$uint_index]['NewsCateName']?> | 國防新聞網</title>
    <!-- Bootstrap -->
	<link href="css/bootstrap.css" rel="stylesheet">
	<link href="css/css.css" rel="stylesheet" type="text/css">
	<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />

	<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
		  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
		  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
		<![endif]-->
  </head>
  <body<?=( isset($str_init_alert) ? ' onLoad="javascript:alert(\'' . $str_init_alert . '\');"' : '' )?>>
<? include('_title.php'); ?>
  <div id="c_contents" class="container" >
    <div id="list_contents_l">
    <div class="top_itle"><?=$arr_news_cates[$uint_index]['NewsCateName']?></div>
    <div>
<?
if( $arr_top_news )
{
?>
    <div class="contents_pic">
    <div id="carousel2" class="carousel slide" data-ride="carousel">
        <div class="carousel-inner" role="listbox">
<?
	foreach( $arr_top_news as $n=>$arr_row )
	{
?>
         <div class="item<?=( !$n ? ' active' : '' )?>"><img src="images/news/<?=$arr_row['NewsId']?>.jpg" alt="<?=$arr_row['Subject']?>" class="center-block">
           <div class="carousel-caption">
             <h3><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$static_uint_cate_id?>"><?=$arr_row['Subject']?></a></h3>
             <p><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$static_uint_cate_id?>"><?=mb_substr($arr_row['Abstract'], 0, 25, 'UTF-8')?>...</a></p>
           </div>
         </div>
<?
	}
?>
        </div>
        <a class="left carousel-control" href="#carousel2" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span></a><a class="right carousel-control" href="#carousel2" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span></a>
    </div>
    </div>
<?
}
?>      
    </div>
<div class="container column_6" >
  <ul>
<?
if( $arr_news )
{
	foreach( $arr_news as $arr_row )
	{
?>
      <li><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$static_uint_cate_id?>"><img src="images/news/<?=$arr_row['NewsId']?>.jpg" /></a>
      <h1><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$static_uint_cate_id?>"><?=$arr_row['Subject']?></a></h1>
      <p><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$static_uint_cate_id?>"><?=mb_substr($arr_row['Abstract'], 0, 25, 'UTF-8')?>...</a></p>
      </li>
<?
	}
}
?>
    </ul>
    
  </div>
     <nav class="paging">
       <!-- Add class .pagination-lg for larger blocks or .pagination-sm for smaller blocks-->
       <ul class="pagination">
         <li<?=( $static_uint_page_no <= 1 ? ' class="disabled"' : '' )?>><a href="<?=( $static_uint_page_no <= 1 ? '#' : ('news_list.php?cate_id=' . $static_uint_cate_id . '&page_no=' . ($static_uint_page_no - 1)) )?>" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
<?
for( $i=-2; $i<=2; $i++ )
{
	$uint_page_no_this = $static_uint_page_no + $i;

	if( $uint_page_no_this < 1 || $uint_page_no_this > $arr_page_info['PageCount'] )
		continue;
	
	echo('         <li' . ( !$i ? ' class="active"' : '' ) . '><a href="news_list.php?cate_id=' . $static_uint_cate_id . '&page_no=' . $uint_page_no_this . '">' . $uint_page_no_this . '</a></li>' . "\n");
}
?>
         <li<?=( $static_uint_page_no >= $arr_page_info['PageCount'] ? ' class="disabled"' : '' )?>><a href="<?=( $static_uint_page_no >= $arr_page_info['PageCount'] ? '#' : ('news_list.php?cate_id=' . $static_uint_cate_id . '&page_no=' . ($static_uint_page_no + 1)) )?>" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
       </ul>
     </nav>
    </div>
  
  <div id="c_contents_r">
  
<?
if( isset($arr_banner[0]) && $arr_banner[0] )
{
?>
     <div class="contents_r_ad"><a href="<?=$arr_banner[0]['Url']?>"<?=( $arr_banner[0]['NewTarget'] ? ' target="_blank"' : '' )?>><img alt="<?=$arr_banner[0]['Slogan']?>" title="<?=$arr_banner[0]['Slogan']?>" src="images/banners/<?=$arr_banner[0]['BannerId']?>.jpg" /></a></div>
<?
}
?>
     <div class="container top_title_a">推薦要聞</div>
    <div class="container column_5" >
    <ul>
<?
if( $arr_suggest_news )
{
	foreach( $arr_suggest_news as $arr_row )
	{
?>
      <li>
      <a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><img src="images/news/<?=$arr_row['NewsId']?>.jpg" /></a>
      <h1><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><?=mb_substr($arr_row['Subject'], 0, 25, 'UTF-8')?>...</a></h1>
      <h2><?=$arr_row['Date']?>｜<span class="category_subhead"><a href="news_list.php?cate_id=<?=$arr_row['NewsCateId']?>"><?=$arr_row['NewsCateName']?></a></span></h2>
      </li>
<?
	}
}
?>
    </ul>
    
    </div> 
<?
if( isset($arr_banner[1]) && $arr_banner[1] )
{
?>
     <div class="contents_r_ad"><a href="<?=$arr_banner[1]['Url']?>"<?=( $arr_banner[1]['NewTarget'] ? ' target="_blank"' : '' )?>><img alt="<?=$arr_banner[1]['Slogan']?>" title="<?=$arr_banner[1]['Slogan']?>" src="images/banners/<?=$arr_banner[1]['BannerId']?>.jpg" /></a></div>
<?
}
?>
<div class="contents_r_ad">
<!--fbad-->
<div style="display:none; position: relative;">
  <iframe style="display:none;"></iframe>
  <script type="text/javascript">
    var data = {
      placementid: '237860413299796_237860436633127',
      format: 'native',
      testmode: false,
      onAdLoaded: function(element) {
        console.log('Audience Network [237860413299796_237860436633127] ad loaded');
        element.style.display = 'block';
      },
      onAdError: function(errorCode, errorMessage) {
        console.log('Audience Network [237860413299796_237860436633127] error (' + errorCode + ') ' + errorMessage);
      }
    };
    (function(w,l,d,t){var a=t();var b=d.currentScript||(function(){var c=d.getElementsByTagName('script');return c[c.length-1];})();var e=b.parentElement;e.dataset.placementid=data.placementid;var f=function(v){try{return v.document.referrer;}catch(e){}return'';};var g=function(h){var i=h.indexOf('/',h.indexOf('://')+3);if(i===-1){return h;}return h.substring(0,i);};var j=[l.href];var k=false;var m=false;if(w!==w.parent){var n;var o=w;while(o!==n){var h;try{m=m||(o.$sf&&o.$sf.ext);h=o.location.href;}catch(e){k=true;}j.push(h||f(n));n=o;o=o.parent;}}var p=l.ancestorOrigins;if(p){if(p.length>0){data.domain=p[p.length-1];}else{data.domain=g(j[j.length-1]);}}data.url=j[j.length-1];data.channel=g(j[0]);data.width=screen.width;data.height=screen.height;data.pixelratio=w.devicePixelRatio;data.placementindex=w.ADNW&&w.ADNW.Ads?w.ADNW.Ads.length:0;data.crossdomain=k;data.safeframe=!!m;var q={};q.iframe=e.firstElementChild;var r='https://www.facebook.com/audiencenetwork/web/?sdk=5.3';for(var s in data){q[s]=data[s];if(typeof(data[s])!=='function'){r+='&'+s+'='+encodeURIComponent(data[s]);}}q.iframe.src=r;q.tagJsInitTime=a;q.rootElement=e;q.events=[];w.addEventListener('message',function(u){if(u.source!==q.iframe.contentWindow){return;}u.data.receivedTimestamp=t();if(this.sdkEventHandler){this.sdkEventHandler(u.data);}else{this.events.push(u.data);}}.bind(q),false);q.tagJsIframeAppendedTime=t();w.ADNW=w.ADNW||{};w.ADNW.Ads=w.ADNW.Ads||[];w.ADNW.Ads.push(q);w.ADNW.init&&w.ADNW.init(q);})(window,location,document,Date.now||function(){return+new Date;});
  </script>
  <script type="text/javascript" src="https://connect.facebook.net/en_US/fbadnw.js" async></script>
  <style>
   .thirdPartyRoot {
        background-color: white;
        color: #444;
        border: 1px solid #ccc;
        border-left: 0;
        border-right: 0;
        font-family: sans-serif;
        font-size: 14px;
        line-height: 16px;
        width: 320px;
        text-align: left;
        position: relative;
      }
      .thirdPartyMediaClass {
        width: 320px;
        height: 168px;
        margin: 12px 0;
      }
      .thirdPartySubtitleClass {
        font-size: 18px;
        -webkit-line-clamp: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        height: 16px;
        -webkit-box-orient: vertical;
      }
      .thirdPartyTitleClass {
        padding-right: 12px;
        line-height: 18px;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        height: 36px;
        -webkit-box-orient: vertical;
      }
      .thirdPartyCallToActionClass {
        background-color: #416BC4;
        color: white;
        border-radius: 4px;
        padding: 6px 20px;
        float: right;
        text-align: center;
        text-transform: uppercase;
        font-size: 11px;
      }
      .fbDefaultNativeAdWrapper {
        font-size: 12px;
        line-height: 14px;
        margin: 12px 0;
        height: 36px;
        vertical-align: top;
      }
  </style>
  <div class="thirdPartyRoot">
  <a class="fbAdLink">
    <div class="fbAdMedia thirdPartyMediaClass"></div>
    <div class="fbAdSubtitle thirdPartySubtitleClass"></div>
    <div class="fbDefaultNativeAdWrapper">
      <div class="fbAdCallToAction thirdPartyCallToActionClass"></div>
      <div class="fbAdTitle thirdPartyTitleClass"></div>
    </div>
  </a>
</div>
</div>


<!--fbad-->



</div>
  </div>


 
  </div>
    
   
    
  <div class="container-fluid" id="footer">
    <div class="container footer_nav">
      <ul>
      <li><a href="terms.php">使用條款</a></li>
      <li><a href="about.php">關於國防新聞網</a></li>
      <li><a href="policy.php">隱私政策</a></li>
      <li><a href="contact.php">聯絡我們</a></li>
      </ul>
      
    </div>
    <div class="container">
      <div class="copyright">Copyright © 2015 國防新聞網 All Rights Reserved.</div>
      <div class="go_top">
      <ul>
      <li><a id="footer_menu_2" title="返回頂端" href="#">go top</a></li>
      <li><a id="footer_menu_1" href="https://www.facebook.com/%E9%98%B2%E5%8B%99%E5%BF%AB%E9%96%80-243554065699495/" title="防務快門粉絲團" target="_blank" >防務快門粉絲團</a></li>
      </ul>
      </div>
    </div>
  </div>
  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
	<script src="js/jquery-1.11.2.min.js"></script>

	<!-- Include all compiled plugins (below), or include individual files as needed --> 
	<script src="js/bootstrap.js"></script>
    <?php include_once("analyticstracking.php") ?>
  </body>
</html>
