<?php
/**
 * @var $this WorkController
 * @var $currentBrand WorkBrand
 * @var $currentModel WorkModel
 * @var $works Work[]
 * @var $contacts Contact[]
 */
?>
<?php $this->renderPartial('//snippets/headerGallery'); ?>
<?php
$galleryItems = array();
$galleryItem = 0;
/*
 *  select gallery_photo.* from work inner join gallery_photo on gallery_photo.gallery_id = work.gallery_id
 *  where work.id < 10
 *  order by work.id asc, gallery_photo.rank
 */
$gbrands = $this->getBrands();
$gmodels = $this->getModels();
/** @var $works Work[] */
$gworks = Work::model()->with('i18n')->findAll(array(
    'index' => 'id',
));
$command = new CDbCommand(Yii::app()->db, <<<SQL
select gallery_photo.id as id, work.id as work_id from work
inner join gallery_photo on gallery_photo.gallery_id = work.gallery_id
where work_model_id = :current_model_id
order by work.id asc, gallery_photo.rank
SQL
);
$command->params[':current_model_id'] = $currentModel->id;
foreach ($command->queryAll() as $row) {

    $gwork = $gworks[$row['work_id']];
    $gmodel = $gmodels[$gwork->work_model_id];
    $gbrand = $gbrands[$gmodel->work_brand_id];

    $galleryItems[] = array(
        'image' => GalleryPhoto::photoUrl('medium', $row['id']),
        'type' => 'image',
        'preview' => GalleryPhoto::photoUrl('small', $row['id']),
        'title' => $gwork->i18n->name,
        'info' => CHtml::link($gwork->i18n->name, array(
            'work/view',
            'brand' => $gbrand->link,
            'model' => $gmodel->link,
            'set' => $gwork->link,
        )), //.'&nbsp;'.$currentWork->i18n->name,
    );
    $galleryItem += 1;
}
?>

<div class="wrapper content-wrapper">
    <div class="col-wrapper">
        <div class="col2">
            <?php
            $services = $this->getServices();
            if (count($services)):?>
                <p class="section-title"><?php echo Yii::t('site', 'Наши услуги');?></p>
                <ul class="sidebar-nav">
                    <?php foreach ($services as $item): ?>
                    <li><?php echo CHtml::link($item->i18n->name, array('tuning/service', 'link' => $item->link));?></li>
                    <?php endforeach; ?>
                </ul>
                <?php endif;?>
            <p class="section-title"><?php echo Yii::t('site', 'Марки авто');?></p>

            <ul class="sidebar-nav">
                <?php foreach ($this->getBrands() as $brand): ?>
                <?php if ($brand->id == $currentBrand->id): ?>
                    <li class="active">
                        <?php echo CHtml::link($brand->i18n->name, array('work/brand', 'brand' => $brand->link));?>
                        <ul>
                            <?php foreach ($brand->workModels as $model) if (!$model->hidden): ?>
                            <li <?php if ($model->id == $currentModel->id) echo'class="active"' ?>>
                                <?php
                                echo CHtml::link($model->i18n->name, array(
                                        'work/model',
                                        'brand' => $model->workBrand->link,
                                        'model' => $model->link)
                                );
                                ?>
                            </li>
                            <?php endif; ?>
                        </ul>
                    </li>
                    <?php else: ?>
                    <li><?php echo CHtml::link($brand->i18n->name, array('work/brand', 'brand' => $brand->link));?></li>
                    <?php endif; ?>
                <?php endforeach; ?>
            </ul>

        </div>
        <div class="col8">
            <ul class="crumbs">
                <li><?php echo CHtml::link(Yii::t('site', 'Наши работы'), array('work/index'));?>→</li>
                <li><?php echo CHtml::link($currentBrand->i18n->name, array(
                    'work/brand',
                    'brand' => $currentBrand->link,
                ));?>→
                </li>
            </ul>

            <div class="header-combined">
                <h1><?php echo $this->pageName?></h1>
                <a href="#" class="gallery-link" id="view_gallery">Посмотреть в галерее</a>
            </div>
            <div class="work-list">
                <?php foreach ($works as $work): ?>
                <?php
                $url = $this->createUrl('work/view', array(
                    'brand' => $work->workModel->workBrand->link,
                    'model' => $work->workModel->link,
                    'set' => $work->link,
                ));
                ?>
                <div class="work-item">
                    <div class="col5">
                        <?php echo CHtml::link(CHtml::image($work->imageBehavior->getFileUrl('medium'), $work->i18n->name), $url)?>
                    </div>
                    <div class="col3 with-margin">
                        <div class="text-content">
                            <p><?php echo CHtml::link($work->i18n->name, $url)?></p>
                            <?php echo $work->i18n->short ?>
                        </div>
                    </div>
                </div>
                <?php endforeach;?>
            </div>
            <div class="col8">
                <?php echo $currentModel->i18n->content?>
            </div>

            <h2><?php echo Yii::t('site', 'Мы с удовольствием ответим на все ваши вопросы по тюнингу автомобиля');?></h2>

            <?php echo $this->workRoot->i18n->contact_info?>
        </div>
        <div class="col2">
            <p class="section-title"><?php echo Yii::t('site', 'Есть вопросы?');?></p>

            <div class="text-content">
                <?php echo Yii::t('site', 'Обращайтесь к вашему персональному консультанту (наши работы)!');?>
            </div>
            <?php $this->renderPartial('//snippets/sidebarContacts');?>
        </div>
    </div>
</div>
<?php
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery');
$galleryItems = CJavaScript::encode($galleryItems);
$title = CJavaScript::encode($this->pageName);
$cs->registerScript('bigGallery', <<<JS
makeGallery($galleryItems,$title);
$('#view_gallery').click(function(e){
        e.preventDefault();
        window.bigGallery.switchTo(0);
        return false;
    });
JS
);
?>