<?php
/**
 * @var $this NewsController
 * @var $videos Show[]
 * @var $contacts Contact[]
 * @var $pages CPagination
 * @var $category NewsCategory
 */
?>
<?php $this->renderPartial('//snippets/headerGallery'); ?>
<?php
$currentVideo = 0;
?>
<div class="wrapper content-wrapper">
    <div class="col-wrapper">
        <div class="col2">
            <p class="section-title"><?php echo Yii::t('site', 'Рубрики');?></p>
            <ul class="sidebar-nav">
                <?php
                foreach ($this->getNewsCategories() as $item) {
                    if (isset($category) && $category->id == $item->id)
                        echo '<li class="active">';
                    else
                        echo '<li>';
                    if ($item->type == 2)
                        echo CHtml::link($item->i18n->name, array('news/publications', 'link' => $item->link)), '</li>';
                    elseif ($item->type == 1 || $item->type == 3)
                        echo CHtml::link($item->i18n->name, array('news/category', 'link' => $item->link)), '</li>';
                    else
                        echo CHtml::link($item->i18n->name, array('news/show', 'link' => $item->link)), '</li>';
                }
                ?>
            </ul>
        </div>
        <div class="col8">
            <h1><?php echo CHtml::encode($this->pageName);?></h1>

            <div class="news-page-list">
                <?php foreach ($videos as $item):
                $currentVideo += 1;
                ?>
                <div class="news-page-item">
                    <div class="video-box">

                        <?php
                        preg_match('#(?<=(?:v|i)=)[a-zA-Z0-9-]+(?=&)|(?<=(?:v|i)\/)[^&\n]+|(?<=embed\/)[^"&\n]+|(?<=(?:v|i)=)[^&\n]+|(?<=youtu.be\/)[^&\n]+#', $item->video_url, $matches);
                        if (count($matches) > 0) {
                            $videoId = $matches[0];
                            $url = 'http://www.youtube.com/embed/' . $videoId . '?feature=player_detailpage';

                        } else {
                            $url = $item->video_url;
                        }
                        echo '<img width="168" height="126" data-video-id="' . $videoId . '" class="vid-preview"/><div class="overlay">';
                        echo CHtml::link('', $url, array('class' => 'play-btn iframe', 'rel' => 'video-gallery', 'id' => 'video' . $currentVideo));
                        echo '</div>';
                        ?>
                    </div>
                    <div class="video-desc">
                        <h2>
                            <?php
                            echo CHtml::link($item->i18n->name, $url, array('onclick' => '$("#video' . $currentVideo . '").click(); return false;'));
                            ?>
                        </h2>

                        <p class="news-info">
                            <?php
                            $timestamp = CDateTimeParser::parse($item->date, 'yyyy-MM-dd hh:mm:ss');
                            echo Yii::app()->getDateFormatter()->format('d MMMM yyyy', $timestamp);
                            echo ' ', Yii::t('site', 'Рубрика'), ': ';
                            echo CHtml::link($item->newsCategory->i18n->name, array('news/show', 'link' => $item->newsCategory->link))
                            ?>
                        </p>

                        <div class="text-content">

                            <p><?php echo $item->i18n->short;?></p>
                        </div>
                        <?php
                        echo CHtml::link(Yii::t('site', 'Смотреть'), $url, array('class' => 'iframe button', 'onclick' => '$("#video' . $currentVideo . '").click(); return false;'));
                        ?>
                    </div>
                </div>
                <?php endforeach;?>
            </div>
            <div class="paginator-wrapper">
                <div class="paginator-to-left">
                    <div class="paginator-to-right">
                        <?php $this->widget(
                        'LinkPager',
                        array(
                            'pages' => $pages,
                            'cssFile' => false,
                            'nextPageLabel' => Yii::t('site', 'Следующая'),
                            'prevPageLabel' => Yii::t('site', 'Предыдущая'),
                            'header' => false, //Yii::t('site', 'Страницы:'),
                        ));  ?>

                    </div>
                </div>
            </div>

        </div>
        <div class="col2">
            <p class="section-title"><?php echo Yii::t('site','Контакты для прессы');?></p>

            <div class="text-content">
                <?php echo Yii::t('site','По всем вопросам связанным с прессой обращайтесь в наш PR-отдел');?>
            </div>

            <?php $this->renderPartial('//snippets/sidebarContacts');?>

            <?php
            if (isset($category)) {
                $this->renderPartial('_photofact', array('galleryIds' => array($category->photofact_gallery_id)));
            } else {
                /** @var $cats NewsCategory[] */
                $cats = NewsCategory::model()->findAll();
                $galleryIds = array();
                foreach($cats as $cat){
                    if(isset($cat->photofact_gallery_id))
                        $galleryIds[]=$cat->photofact_gallery_id;
                }
                $this->renderPartial('_photofact', array('galleryIds' => $galleryIds));
            }
            ?>
        </div>
    </div>
    <div class="col-wrapper">
        <div class="col2">&nbsp;</div>
        <div class="col10">
            <?php $this->renderPartial('_newsFooter');?>
        </div>
    </div>
</div>


<?php
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery');
//$cs->registerScriptFile(Yii::app()->baseUrl . '/js/fancybox/jquery.mousewheel-3.0.4.pack.js');
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/fancybox/jquery.fancybox-1.3.4.pack.js');
$cs->registerCssFile(Yii::app()->baseUrl . '/js/fancybox/jquery.fancybox-1.3.4.css');
$src = <<<JS
$("a[rel=video-gallery]").fancybox({
	'transitionIn'	: 'none',
	'type'			: 'iframe',
	'transitionOut'	: 'none',
	'titlePosition' : 'over'
});

$('.video-box img.vid-preview').each(function(){
    var dst =$(this);
    var videoId = dst.data('videoId');
    $.getJSON(
        'http://gdata.youtube.com/feeds/api/videos/' + videoId + '?v=2&alt=jsonc&callback=?',
        function (json) {
            dst.attr('src',json["data"]["thumbnail"]["sqDefault"]);
        }
    );
})
JS;

$cs->registerScript('fancybox_gallery', $src);
?>