<?php
/**
 * @var $this WorkController
 * @var $currentBrand WorkBrand
 * @var $currentModel WorkModel
 * @var $currentWork Work
 * @var $contacts Contact[]
 */
?>
<?php $this->renderPartial('//snippets/headerGallery'); ?>
<?php
$galleryItems = array();
$galleryItem = 0;
$galleryStart = 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
 */
$brands = $this->getBrands();
$models = $this->getModels();
/** @var $works Work[] */
$works = 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.id < :current_work_id and work_model_id = :current_model_id
order by work.id asc, gallery_photo.rank
SQL
);
$command->params[':current_work_id']= $currentWork->id;
$command->params[':current_model_id']= $currentWork->work_model_id;
foreach($command->queryAll() as $row){

    $work = $works[$row['work_id']];
    $model = $models[$work->work_model_id];
    $brand = $brands[$model->work_brand_id];

    $galleryItems[]=array(
        'image'=>GalleryPhoto::photoUrl('medium',$row['id']),
        'type'=>'image',
        'preview'=>GalleryPhoto::photoUrl('small',$row['id']) ,
        'title'=>$work->i18n->name,
        'info'=>CHtml::link($work->i18n->name, array(
            'work/view',
            'brand' => $brand->link,
            'model' => $model->link,
            'set' => $work->link,
        )),//.'&nbsp;'.$currentWork->i18n->name,
    );
    $galleryItem+=1;
}
$galleryStart = $galleryItem;
?>
<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>
            </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>
                <li><?php echo CHtml::link($currentModel->i18n->name, array(
                    'work/model',
                    'brand' => $currentBrand->link,
                    'model' => $currentModel->link,
                ));?>→
                </li>
            </ul>
            <div class="header-combined">
                <h1><?php echo $this->pageName?></h1>
                <a href="#" class="gallery-link" id="view_gallery"><?php echo Yii::t('site', 'Посмотреть в галерее')?></a>
            </div>
            <?php
            if ((!empty($currentWork->i18n->owner_name))
                || (!empty($currentWork->i18n->owner_age))
                || (!empty($currentWork->i18n->owner_status))
                || (!empty($currentWork->i18n->owner_favorite_author))
                || (!empty($currentWork->i18n->owner_hobby))
                || (!empty($currentWork->i18n->owner_expectations))
            ): ?>
                <div class="car-owner">
                    <div class="col2">
                        <?php echo CHtml::image($currentWork->ownerImageBehavior->getFileUrl('medium'));?>
                    </div>
                    <div class="col6 with-margin">
                        <?php if (!empty($currentWork->i18n->owner_name)): ?>
                        <div class="owner-info-line">
                            <div class="col2"><b><?php echo Yii::t('site', 'Владелец');?>:</b></div>
                            <div class="col4"><?php echo CHtml::encode($currentWork->i18n->owner_name);?></div>
                        </div>
                        <?php endif;?>
                        <?php if (!empty($currentWork->i18n->owner_age)): ?>
                        <div class="owner-info-line">
                            <div class="col2"><b><?php echo Yii::t('site', 'Возраст');?>:</b></div>
                            <div class="col4"><?php echo CHtml::encode($currentWork->i18n->owner_age);?></div>
                        </div>
                        <?php endif;?>
                        <?php if (!empty($currentWork->i18n->owner_status)): ?>
                        <div class="owner-info-line">
                            <div class="col2"><b><?php echo Yii::t('site', 'Личный статус');?>:</b></div>
                            <div class="col4"><?php echo CHtml::encode($currentWork->i18n->owner_status);?></div>
                        </div>
                        <?php endif;?>

                        <?php if (!empty($currentWork->i18n->owner_favorite_author)): ?>
                        <div class="owner-info-line">
                            <div class="col2"><b><?php echo Yii::t('site', 'Любимый автор');?>:</b></div>
                            <div
                                class="col4"><?php echo CHtml::encode($currentWork->i18n->owner_favorite_author);?></div>
                        </div>
                        <?php endif;?>

                        <?php if (!empty($currentWork->i18n->owner_hobby)): ?>
                        <div class="owner-info-line">
                            <div class="col2"><b><?php echo Yii::t('site', 'Хобби');?>:</b></div>
                            <div class="col4"><?php echo CHtml::encode($currentWork->i18n->owner_hobby);?></div>
                        </div>
                        <?php endif;?>

                        <?php if (!empty($currentWork->i18n->owner_expectations)): ?>
                        <hr/>
                        <div class="text-content">
                            <blockquote><?php echo CHtml::encode($currentWork->i18n->owner_expectations);?></blockquote>
                        </div>
                        <?php endif;?>
                    </div>
                </div>
                <?php endif;?>
            <div class="text-content">
                <?php echo $currentWork->i18n->content ?>
                <?php
                if (isset($currentWork->gallery_id)) {
                    $photos = $currentWork->galleryBehavior->getGalleryPhotos();
                    if (count($photos) > 0) {
                        ?>
                        <div class="col10">
                            <div class="col8">
                                <h2><?php echo Yii::t('site', 'Фотогалерея');?></h2>

                                <div class="catalog-item-gallery">
                                    <?php
                                    foreach ($photos as $photo) {
                                        $galleryItems[]=array(
                                            'image'=>$photo->getUrl('medium'),
                                            'type'=>'image',
                                            'preview'=>$photo->getUrl('small') ,
                                            'title'=>$currentWork->i18n->name,
                                            'info'=>CHtml::link($currentWork->i18n->name, array(
                                                'work/view',
                                                'brand' => $currentBrand->link,
                                                'model' => $currentModel->link,
                                                'set' => $currentWork->link,
                                            )),
                                        );
                                        echo CHtml::link(
                                            CHtml::image($photo->getUrl('small'), $photo->i18n->name),
                                            $photo->getUrl('medium'),
                                            array(
                                                'rel' => 'gallery',
                                                'data-name' => $photo->i18n->name,
                                                'data-description' => $photo->i18n->description,
                                               // 'onclick' => 'window.bigGallery.switchTo(' . $galleryItem . '); return false;'
                                            )
                                        );
                                        $galleryItem+=1;
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                }
                ?>
            </div>

            <div class="col10">
                <div class="col8">
                    <div class="text-content">
                        <h2><?php echo Yii::t('site', 'Заказать тюнинг для своего авто')?></h2>

                        <p><?php echo Yii::t('site', 'Узнать стоимость и потенциал для тюнинга вашего автомобиля вы можете у нашого специалиста:');?></p>
                    </div>
                    <?php echo $this->workRoot->i18n->contact_info?>
                </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','Обращайтесь к вашему персональному консультанту (наши работы)!');?>
            </div>
            <?php $this->renderPartial('//snippets/sidebarContacts');?>
        </div>
    </div>
</div>
<?php

$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.id > :current_work_id   and work_model_id = :current_model_id
order by work.id asc, gallery_photo.rank
SQL
);
$command->params[':current_work_id']= $currentWork->id;
$command->params[':current_model_id']= $currentWork->work_model_id;
foreach($command->queryAll() as $row){

    $work = $works[$row['work_id']];
    $model = $models[$work->work_model_id];
    $brand = $brands[$model->work_brand_id];

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

<?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
var gallery = $("a[rel=gallery]").fancybox({
	'transitionIn'	: 'none',
	'transitionOut'	: 'none',
	'titlePosition' : 'over',
	'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {

	    var name = $(currentArray[currentIndex]).data('name'), r = '';
	    var description = $(currentArray[currentIndex]).data('description');
	    name = name?name:''; description = description?description:'';
	    if(name.length) r+='<b>'+name+'</b> ';
		if(description.length) r+=description;
		if(r.length) return '<span id="fancybox-title-over">'+r+'</span>';
        else return '';
	}
});
if(gallery.length >0){
//    $('#view_gallery').click(function(e){
//        e.preventDefault();
//        $(gallery[0]).click();
//        return false;
//    });
    }
else
    $('#view_gallery').hide();
JS;

$cs->registerScript('fancybox_gallery', $src);
$galleryItems = CJavaScript::encode($galleryItems);
$galleryStart = CJavaScript::encode($galleryStart);
$title = CJavaScript::encode($this->pageName);
$cs->registerScript('bigGallery', <<<JS
makeGallery($galleryItems,$title);
$('#view_gallery').click(function(e){
        e.preventDefault();
        window.bigGallery.switchTo({$galleryStart});
        return false;
    });
JS
);
?>