<?php
/**
 * @var $this NewsController
 * @var $galleryIds array
 */
?>


<?php
if (false) {

    $fact = $this->getPhotoFact();
    if ($fact !== null) {
        echo '<p class="section-title">', Yii::t('site', 'Фотофакты'), '</p><div class="photofact">';
        echo CHtml::link(CHtml::image($fact->imageBehavior->getFileUrl('preview')), array('news/view', 'link' => $fact->link));
        echo '<p>', CHtml::link($fact->i18n->name, array('news/view', 'link' => $fact->link)), '</p>';
        echo '</div>';
    }
} else {

    $criteria = new CDbCriteria(array(
        'order' => 'rand()'
    ));
    $criteria->addInCondition('gallery_id', $galleryIds);
    /** @var $photos GalleryPhoto[] */
    $photos = GalleryPhoto::model()->with('i18n')->findAll($criteria);
    if (count($photos) > 0) {
        echo '<p class="section-title">', Yii::t('site', 'Фотофакты'), '</p><div class="photofact">';
        $data = array();
        foreach ($photos as $photo) {
            $data[] = array(
                'image' => $photo->getUrl('small'),
                'url' => $photo->i18n->link,
            );
        }
        $data = CJavaScript::encode($data);
        echo '<a href="#" id="photofact"><img class="image1"/><img class="image2"/></a>';
        $cs = Yii::app()->getClientScript();
        $cs->registerCoreScript('jquery');
        $cs->registerScript('photofact', <<<JS
   (function(){
       var data = {$data};
       var slider = $('#photofact').css({
            'display':'block',
            'position':'relative'
       });
       var image1 = $('.image1', slider);
       var image2 = $('.image2', slider);
       slider.attr('href',data[0]['url']);
       image1.attr('src',data[0]['image']);
       image2.attr('src',data[0]['image']).css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'opacity':0
       });
       var i = 0;
       setInterval(function(){
           i=(i+1)%data.length;
           slider.attr('href',data[i]['url']);
           image2.attr('src',data[i]['image']);
           image2.animate({
                'opacity':1
           },{
                'duration':400,
                'complete':function(){
                    image1.attr('src',data[i]['image']);
                    image2.css({
                        'opacity':0
                    })
                }
           })
       },7000);
   })();
JS
        );
        echo '</div>';
    }
}
?>