<?php
$translation = $product['Translation'][$culture];
slot('title', $translation['title']);
use_javascript('fotorama.js');
use_stylesheet('fotorama.css');

$offers = $product['Offer'];
$images = array();
$images = explode(';', $product['product_img']);
$offers_images = array();
foreach ($offers as $offer) {
    if($offer['img']){
        echo '<!--';
        print_r($offer['img']);
        echo '-->';
        $images[] = $offer['img'];
        if(!isset($offers_images[$offer['id']])){
            $offers_images[$offer['id']] = count($images) - 1;
        }
    }
}
if(count($offers_images) > 0){
    ?>
    <script type="text/javascript">
        counterLite.offerImages = JSON.parse('<?php echo json_encode($offers_images);?>');
    </script>
    <?php
}

$view = 'much';
?>
<style type="text/css">
    .content__main {
        padding-bottom: 40px;
    }
</style>
<div class="content__main">
    <div class="max_width">
        <?php
        include_component('main', 'breadcrumbs', array('product_id' => $product['id']));

        echo '<h1 class="content_title">' . $translation['title'] . '</h1>';

        ?>
        <div class="catalog_product_wrap">
            <table class="to_div" cellspacing="0" cellpadding="0" width="100%">
                <tbody valign="top">
                <tr class="product_main product_main_<?php echo $view;?>">
                    <td width="48%">
                        <div id="fotorama" class="fotorama" data-width="100%" data-ratio="3/2" width="100%" max-height="400px" data-nav="thumbs" data-auto="false">
                            <?php
                            if (count($images) == 1 && strlen($images[0]) < 3) {
                                echo '<img src="/i/no_photo.png">';
                            }else {
                                foreach ($images as $image) {
                                    echo '<img src="/u/i/' . Page::replaceImageSize($image, 'M') . '">';
                                }
                            }
                            ?>
                        </div>
                    </td>
                    <td width="50"></td>
                    <td>
                        <table cellpadding="0" cellspacing="0" width="100%">
                            <tbody valign="middle" align="left">
                            <tr>
                                <?php
                                if ($view == 'much') { ?>
                                    <td>
                                        <?php
                                        $first_cost = 0;
                                        if (count($offers) > 0) {
                                            $sum = 0;
                                            foreach ($offers as $offer) {
                                                if ($i == 1) {
                                                    $first_cost = $offer['price'];
                                                }

                                                $quantity_type = $offer->getQuantityTypeValue($culture);
                                                $quantity_type = ($quantity_type != '' ? ', ' . $quantity_type : '');
                                                $infoAmount = false;
                                                if (trim($quantity_type) == ', ведро')
                                                {
                                                    if ($product->getTheWeight()) {
                                                        $quantity_type .= ', ' . $product->getTheWeight() . 'кг';
                                                    }else {
                                                        $quantity_type .= ', 10кг';
                                                    }
                                                }

                                                if (isset($offer['BasketOffer'][0]) && $offer['BasketOffer'][0]['amount'] > 0)
                                                {
                                                    $infoAmount = $offer['BasketOffer'][0]['amount'];
                                                }
                                                $sum += intval($infoAmount)*$offer['price'];

                                                $array_price = array_map('intval', str_split($offer['price']));
                                                if (strlen($offer['price']) > 3)
                                                {
                                                    $offer_str = '';
                                                    $length_str_price = 0;
                                                    if (count($array_price) > 6)
                                                    {
                                                        $offer_str .= implode('', array_slice($array_price, 0, count($array_price)-6)) . ' ';
                                                        $length_str_price = strlen($offer_str)-1;
                                                    }
                                                    $offer_str .= implode('', array_slice($array_price, $length_str_price, count($array_price)-$length_str_price-3)) . ' ' . implode('', array_slice($array_price, count($array_price)-3));
                                                }else
                                                {
                                                    $offer_str = '' . $offer['price'];
                                                }

                                                ?>
                                                <label class="product_offer">
                                                    <input data-initialized="1" data-id="<?php echo $offer['id']; ?>" type="radio" name="offer" onchange="counterLite.change($(this))">
                                                    <?php if ($offer['price'] > 1) { ?>
                                                    <table width="100%" cellpadding="0" cellspacing="0">
                                                        <tr>
                                                            <td><?php echo $offer['title'] . $quantity_type; ?></td>
                                                            <td style="white-space: nowrap;"  align="right">
                                                                <?php echo $offer_str; ?>
                                                                <span class="rub">i</span>
                                                            </td>
                                                            <td width="1" align="right">
                                                                <div class="product_counter min" data-id="<?php echo $offer['id']; ?>" data-price="<?php echo $offer['price'];?>">
                                                                    <input type="button" class="product_counter_minus" data-symbol="-" onclick="counterLite.update($(this))">
                                                                    <input autocomplete="off" type="text" class="product_counter_val" value="<?php echo ( $infoAmount ? $infoAmount : 0 ) ?>" data-symbol="-+" onkeyup="counterLite.update($(this))">
                                                                    <input type="button" class="product_counter_plus" data-symbol="+" onclick="counterLite.update($(this)); yaCounter48466937.reachGoal('ADD');">
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                    <?php }else
                                                        { ?>
                                                    <table width="100%" cellpadding="0" cellspacing="0">
                                                        <tr>
                                                            <td><?php echo $offer['title'] . $quantity_type; ?></td>
                                                            <td colspan="2"  width="1"  align="right">
                                                                <div class="find_price" onclick="callback.open($(this));return false;" data-offer-id="<?php echo $offer['id']; ?>" >
                                                                    <span>Узнать цену</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                    <?php } ?>
                                                </label>
                                                <?php
                                                $i++;
                                            }
                                        }
                                        $array_sum  = array_map('intval', str_split($sum));
                                        if (count($array_sum) > 3)
                                        {
                                            $sum_str = '';
                                            $length_str = 0;
                                            if (count($array_sum) > 6)
                                            {
                                                $sum_str .= implode('', array_slice($array_sum, 0, count($array_sum)-6)) . ' ';
                                                $length_str = strlen($sum_str)-1;
                                            }
                                            $sum_str .= implode('', array_slice($array_sum, $length_str, count($array_sum)-$length_str-3)) . ' ' . implode('', array_slice($array_sum, count($array_sum)-3));
                                        }else
                                        {
                                            $sum_str = '' . $sum;
                                        }
                                        ?>
                                        <i class="br10"></i>
                                        <table cellspacing="0" cellpadding="0" width="100%">
                                            <tbody align="right">
                                            <tr>
                                                <td style="padding-right: 25px;">
                                                    <span class="product_price">
                                                        <span class="product_price_value" data-price="0"><?php echo ($sum > 0 ? $sum_str : '') ?></span>
                                                        <span class="rub fs_10em" <?php echo ($sum > 0 ? ' style="visibility: visible;"' : '')?>>i</span></span>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                    <?php
                                }
                                ?>
                            </tr>
                            <tr>
                                <td class="product__advantages__info" colspan="3">
                                    <?php
                                    $productDescription = $product->getDescription(ESC_RAW);
                                    $productPropValue = $product->getPropertyArray();
                                    echo '<i class="br10"></i>';
                                    if ($productDescription) {
                                        echo '<a href="" class="product__advantages__info__link fs_18 bbs_d green_link active" data-tab="description">' . __('Описание') . '</a>';
                                    }
                                    if (count($productPropValue) > 0) {?>
                                        <a href="" class="product__advantages__info__link fs_18 bbs_d green_link <?php echo (!$productDescription) ? 'active' : '' ?>" data-tab="features"><?php echo __('Характеристики'); ?></a>
                                <?php
                                    }
                                    if ($product->getCertificate()) {?>
                                        <a href="" class="product__advantages__info__link fs_18 bbs_d green_link <?php echo (!$productDescription && count($productPropValue) <= 0) ? 'active' : '' ?>" data-tab="certificate"><?php echo __('Сертификат'); ?></a>
                                    <?php
                                    }
                                    ?>
                                    <i class="br20"></i>
                                    <div class="product__advantages__info__tab active" data-tab="description">
                                        <div class="product__advantages__info__tab__description">
                                            <div><?php echo $productDescription ?></div>
                                        </div>
                                        <a href="" class="product__advantages__info__tab__description_btn" onclick="$('.product__advantages__info__tab__description').toggleClass('show');return false;"></a>
                                    </div>
                                    <div class="product__advantages__info__tab <?php echo (!$productDescription) ? 'active' : '' ?>" " data-tab="features">
                                        <table cellspacing="0" cellpadding="0" width="100%" style="max-width: 450px;">
                                            <tbody valign="top" align="left">
                                            <?php
                                            foreach ($productPropValue as $prop) {
                                                if($prop['value'] != ''){
                                                    echo '<tr>';
                                                    echo '<td style="font-family: \'robotoregular\';">' . $prop['prop'] . '</td>';
                                                    echo '<td></td>';
                                                    echo '<td>' . $prop['value'] . '</td>';
                                                    echo '</tr>';
                                                }
                                            }
                                            ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="product__advantages__info__tab  <?php echo (!$productDescription && count($productPropValue) <= 0) ? 'active' : '' ?>" data-tab="certificate">
                                        <?php
                                        if($product->getCertificate()){
                                            echo '<a download="" href="/u/i/' . $product->getCertificate() . '" class="document_file_icon" data-val="' . $product->getCertificate() . '"></a>';
                                        }
                                        ?>
                                    </div>
                                    <script type="text/javascript">
                                        $('.product__advantages__info__link').click(function (e) {
                                            $('.product__advantages__info a, .product__advantages__info__tab').removeClass('active');
                                            $(this).addClass('active');
                                            $('.product__advantages__info__tab[data-tab=' + $(this).data('tab') + ']').addClass('active');
                                            return false;
                                        })
                                    </script>
                                </td>
                            </tr>
                            </tbody>
                        </table>

                    </td>
                </tr>
                </tbody>
            </table>
        </div>

    </div>
</div>
<?php
foreach ($product->getCats() as $cat){
    if($cat['Translation'][$sf_user->getCulture()]['title'] == 'Камень для бани'){
        echo '<div class="product_section product__calculator">';
        echo '<div class="max_width">';
        include_partial("catalog/product_calculator", array('offers' => $product['Offer']));
        echo '</div>';
        echo '</div>';
        break;
    }
}
?>
<div style="border-top: 1px solid #ececec; width: 100%; height: 1px"></div>
<?php include_partial("pages/landingArea2"); ?>
<?php
/*
<div class="product_section product__advantages">
    <div class="caption">С этим товаром покупают</div>
    <div class="max_width">
        <table cellpadding="0" cellspacing="0" width="100%">
            <tbody valign="top" align="left">
            <tr>
                <td>
                    <?php include_partial("product/product", array("count" => 4)); ?>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
*/
?>
<?php
//print_r($product->getId());
include_component('video', 'video', array('limit' => 2, 'tag_id' => $product->getId()));
include_component('article', 'article', array('limit' => 4, 'tag_id' => $product->getId()));
include_component('review', 'review', array('limit' => 5, 'tag_id' => $product->getId()));
?>
<script type="text/javascript">
    var $fotoramaDiv = $('#fotorama').fotorama();
    var fotorama = $fotoramaDiv.data('fotorama');

    $(document).ready(function(){
        var diff = parseInt($('.product__advantages__info__tab__description > div').height()) - parseInt($('.product__advantages__info__tab__description').height());
        if(diff > 10 && diff < 85) {
            $('.product__advantages__info__tab__description').css('max-height','225px')
        } else if(diff > 10) {
            $('.product__advantages__info__tab__description_btn').addClass('show')
        }
    });
</script>
