<?php
/**
 * @var \Infortis\Base\Block\Html\Header $block
 */
?>
<?php
    //TODO: replace all $this with $block
    $theme = $block->getHelperTheme();
    $helperHeader = $block->getHelperHeader();
    $connectorMenu = $block->getHelperConnectorMenu();

    //Get HTML of header blocks
    //**************************************************************
    //TODO: print main containers: "header.container" and "header-wrapper"
    $currencySwitcherHtml = $this->getChildHtml('currency');
    $mobileCurrencySwitcherHtml = $this->getChildHtml('currency.mobile');
    $languageSwitcherHtml = $this->getChildHtml('store_language');
    $mobileLanguageSwitcherHtml = $this->getChildHtml('store_language.mobile');

    $cartHtml = $this->getChildHtml('minicart');
    $compareHtml = $this->getChildHtml('compareMini'); //TODO: original name is catalog.compare.sidebar. Add it through layout and add custom name.
    $compareLinkHtml = $this->getChildHtml('catalog.compare.link'); //TODO: this is original block in the header with the link to compare 
    $logoHtml = $this->getChildHtml('site-logo');
    $searchHtml = $this->getChildHtml('topSearch'); //TODO: 

    $accountLinksHtml = $this->getChildHtml('account-links'); //('top.links')
    //$userMenuHtml = $this->getChildHtml('header-wrapper') . $this->getChildHtml('store.links'); //TODO: tmp append 'store.links' here
    //$userMenuHtml = $this->getChildHtml('header-wrapper');
    $userMenuHtml = $this->getChildHtml('user-menu')
        . $this->getChildHtml('header-wrapper');
    $menuHtml = $this->getChildHtml('store.menu');

    // $currencySwitcherHtml = $this->getChildHtml('currency');
    // $languageSwitcherHtml = $this->getChildHtml('store_language');
    // $cartHtml = $this->getChildHtml('cart_sidebar');
    // $compareHtml = $this->getChildHtml('compareMini');
    // $logoHtml = $this->getChildHtml('logo');
    // $searchHtml = $this->getChildHtml('search_wrapper');
    // $accountLinksHtml = $this->getChildHtml('account_links');
    // $userMenuHtml = $this->getChildHtml('user_menu');
    // $menuHtml = $this->getChildHtml('topMenu');

    //Layout settings and basic flags
    //**************************************************************

    //Sticky header
    $sticky = $theme->getCfg('header/sticky');

    //Mobile header
    $mobileHeaderMode = true; // TODO: remove. This option is deprecated since Ultimo 2.4.0 // $theme->getCfg('header/mode');
    $mobileMenuThreshold = 770; // Set default value, in case mobile menu threshold doesn't exist
    if ($sticky)
    {
        $val = $connectorMenu->getMobileMenuThreshold();
        if ($val !== NULL)
        {
            $mobileMenuThreshold = $val;
        }
    }

    //This flag indicates that in mobile header the default language/currency switchers should be hidden
    //and replaced with mobile versions of those switchers
    $moveSwitchers = $theme->getCfg('header/mobile_move_switchers');

    //Get grid classes for header sections
    $grid = $helperHeader->getGridClasses();

    //Get positions of header blocks
    $position = $helperHeader->getPositions();

    //Check if main menu is displayed inisde a section (full-width section) at the bottom of the header
    $menuDisplayedInFullWidthContainer = $helperHeader->isMenuDisplayedInFullWidthContainer();

    //Additional classes for primary header blocks holder
    //TODO: improve: add this class only when menu inside primary header, not in top header.
    $hpClasses = '';
    if ($menuDisplayedInFullWidthContainer === false)
    {
        $hpClasses = ' hp-blocks-holder--stacked'; //Important: keep the space at the beginning
    }

    //Additional classes
    //**************************************************************
    //Header
    $classes['root'] = '';
    if ($moveSwitchers)
        $classes['root'] .= ' move-switchers';
    
    //Menu
    $classes['nav-container'] = '';
    if ($sticky && $theme->getCfg('header/sticky_full_width'))
        $classes['nav-container'] .= ' sticky-container--full-width';

    if ($menuDisplayedInFullWidthContainer === false)
        $classes['nav-container'] .= ' simple';

    //Assign blocks to selected positions
    //**************************************************************
    $html = array();

    //Logo (before search, to keep the search box below the logo if blocks stacked vertically)
    $html[$position['logo']][] = '<div class="logo-wrapper logo-wrapper--regular">' . $logoHtml . '</div>';

    //If mobile mode enabled, place block markers on configured positions
    if ($mobileHeaderMode)
    {
        //Mini cart
        if (!empty($cartHtml))
        {
            $html[$position['cart']][] = '<div id="mini-cart-marker-regular"></div>';
        }

        //Mini compare
        if (!empty($compareHtml))
        {
            $html[$position['compare']][] = '<div id="mini-compare-marker-regular"></div>';
        }

        //Search
        if (!empty($searchHtml))
        {
            $html[$position['search']][] = '<div id="search-marker-regular"></div>';
        }

        //Account links
        if (!empty($accountLinksHtml))
        {
            $html[$position['account-links']][] = '<div id="account-links-marker-regular"></div>';
        }
    }
    else //Mobile mode disabled (deprecated)
    {
        //Mini cart
        if (!empty($cartHtml))
        {
            $html[$position['cart']][] = '<div id="mini-cart-marker-regular"></div>' . $cartHtml;
        }

        //Mini compare
        if (!empty($compareHtml))
        {
            $html[$position['compare']][] = '<div id="mini-compare-marker-regular"></div>' . $compareHtml;
        }

        //Search
        if (!empty($searchHtml))
        {
            $html[$position['search']][] = '<div id="search-marker-regular"></div>' . $searchHtml;
        }

        //Account links
        if (!empty($accountLinksHtml))
        {
            $html[$position['account-links']][] = '<div id="account-links-marker-regular"></div>' . $accountLinksHtml;
        }
    }

    //User menu
    $html[$position['user-menu']][] = $userMenuHtml;

    //Main menu
    $menuContainerHtml = '
    <div class="nav-container skip-content sticky-container' . ($classes['nav-container'] ? $classes['nav-container'] : '') . '">
        <div class="nav container clearer">
            <div class="inner-container">' . $menuHtml . '</div>
        </div>
    </div>';
    if ($menuDisplayedInFullWidthContainer === false)
    {
        $html[$position['main-menu']][] = $menuContainerHtml;
    }

    //Currency switcher
    if (!empty($currencySwitcherHtml))
    {
         $html[$position['currency']][] = '<div id="currency-switcher-wrapper-regular">' . $currencySwitcherHtml . '</div>';
    }

    //Language switcher
    if (!empty($languageSwitcherHtml))
    {
        $html[$position['language']][] = '<div id="lang-switcher-wrapper-regular">' . $languageSwitcherHtml . '</div>';
    }

    //Skip links and blocks displayed via skip links
    //**************************************************************
    //Skip links count
    $skipLinksCount = 0;

    //Search
    if (!empty($searchHtml))
    {
        $skipLinksCount++;
    }

    //Account links
    if (!empty($accountLinksHtml))
    {
        $skipLinksCount++;
    }

    //Mini cart
    if (!empty($cartHtml))
    {
        $skipLinksCount++;
    }

    //Mini compare
    if (!empty($compareHtml))
    {
        $skipLinksCount++;
    }

    //Main menu
    if (!empty($menuHtml)) 
    {
        $skipLinksCount++;
    }
?>
<div id="header-container" class="header-container header-mobile page-header<?php if ($classes['root']) echo $classes['root']; ?>" <?php /* style="display:none;" */?> >
<div id="top" class="header-container2">
<div class="header-container3">

    <?php if ($mobileHeaderMode): ?>
    <!-- Part of the header displayed only in mobile mode -->
    <div class="header-m-container">

        <div class="header-m-top-container">
            <div class="header-m-top header container clearer">
                <div class="inner-container">

                    <?php echo $this->getChildHtml('container_mobile_header_top'); ?>

                    <?php if ($moveSwitchers == false): ?>
                        <?php if ($mobileCurrencySwitcherHtml): ?>
                            <div id="currency-switcher-wrapper-mobile" class="item item-right"><?php echo $mobileCurrencySwitcherHtml; ?></div>
                        <?php endif; ?>
                        <?php if ($mobileLanguageSwitcherHtml): ?>
                            <div id="lang-switcher-wrapper-mobile" class="item item-right"><?php echo $mobileLanguageSwitcherHtml; ?></div>
                        <?php endif; ?>
                    <?php endif; ?>

                </div> <!-- end: inner-container -->
            </div> <!-- end: header-m-top -->
        </div> <!-- end: header-m-top-container -->

        <div class="header-m-primary-container">
            <div class="header-m-primary header container">
                <div class="inner-container">

                    <?php if ($moveSwitchers && ($mobileCurrencySwitcherHtml || $mobileLanguageSwitcherHtml)): ?>
                        <div id="header-collateral" class="header-collateral">
                            <?php echo $mobileLanguageSwitcherHtml; ?>
                            <?php echo $mobileCurrencySwitcherHtml; ?>
                        </div>
                    <?php endif; ?>

                    <!-- Mobile logo -->
                    <div class="logo-wrapper--mobile">
                        <?php echo $logoHtml; ?>
                        <?php /* //TODO: original code for small logo. We may need to remove this in M2
                        <a class="logo logo--mobile" href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>">
                            <img src="<?php echo (($small = $this->getLogoSrcSmall()) ? $small : $this->getLogoSrc()); ?>" alt="<?php echo $this->getLogoAlt() ?>" />
                        </a>
                        */ ?>
                    </div>
                    <div class="clearer after-mobile-logo"></div>

                    <!-- Skip links -->
                    <div class="skip-links-wrapper skip-links--<?php echo $skipLinksCount; ?>">

                        <?php if (!empty($menuHtml)): ?>
                            <a href="#header-nav" class="skip-link skip-nav">
                                <span class="icon ic ic-menu"></span>
                                <span class="label"><?php echo __('Menu'); ?></span>
                            </a>
                        <?php endif; ?>

                        <?php if (!empty($searchHtml)): ?>
                            <a href="#header-search" class="skip-link skip-search">
                                <span class="icon ic ic-search"></span>
                                <span class="label"><?php echo __('Search'); ?></span>
                            </a>
                        <?php endif; ?>

                        <?php if (!empty($accountLinksHtml)): ?>
                            <a href="#header-account" class="skip-link skip-account">
                                <span class="icon ic ic-user"></span>
                                <span class="label"><?php echo __('My Account'); ?></span>
                            </a>
                        <?php endif; ?>

                        <?php if (!empty($compareHtml)): ?>
                            <a href="#header-compare" class="skip-link skip-compare">
                                <span class="icon ic ic-compare"></span>
                                <?php if (($compareCount = Mage::registry('miniCompareProductCount')) > 0): ?>
                                    <span class="count"><?php echo $compareCount; ?></span>
                                <?php endif; ?>
                                <span class="label"><?php echo __('Compare'); ?></span>
                            </a>
                        <?php endif; ?>

                        <?php //Skip content - ontainers for blocks displayed on mobile devices ?>

                        <?php //Mini cart (and its skip link) on mobile devices - it has to be just after the other skip links and before other blocks ?>
                        <?php if (!empty($cartHtml)): ?>
                            <div id="mini-cart-marker-mobile"></div>
                            <?php echo $cartHtml; ?>
                        <?php endif; ?>

                        <?php //Menu on mobile devices ?>
                            <div id="header-nav" class="skip-content skip-content--style">
                                <div id="nav-marker-mobile"></div>
                            </div>

                        <?php if (!empty($searchHtml)): ?>
                            <div id="search-marker-mobile"></div>
                            <?php echo $searchHtml; ?>
                        <?php endif; ?>

                        <?php if (!empty($accountLinksHtml)): ?>
                            <div id="account-links-marker-mobile"></div>
                            <?php echo $accountLinksHtml; ?>
                        <?php endif; ?>

                        <?php if (!empty($compareHtml)): ?>
                            <div id="mini-compare-marker-mobile"></div>
                            <?php echo $compareHtml; ?>
                        <?php endif; ?>

                            <div class="skip-links-clearer clearer"></div>

                    </div> <!-- end: skip-links-wrapper -->

                </div> <!-- end: inner-container -->
            </div> <!-- end: header-m-primary -->
        </div> <!-- end: header-m-primary-container -->

    </div> <!-- end: header-m-container -->
    <?php endif; //end: mode ?>

    <!-- Part of the header displayed only in regular mode -->
    <div class="header-top-container">
        <div class="header-top header container clearer">
            <div class="inner-container">

                <?php echo $this->getChildHtml('skip_to_content'); ?>

                <?php
                    //Central column is displayed above left and right columns to avoid incorrect layout
                    //because all columns contain inline elements.
                    $containerHeaderTopCentral = $this->getChildHtml('container_header_top_central_1'); 
                ?>
                <?php if (isset($html['topCentral']) || $containerHeaderTopCentral): ?>
                    <div class="central-column">

                        <?php if (isset($html['topCentral'])): ?>
                            <?php foreach ($html['topCentral'] as $element): ?>
                                <div class="item item-interface"><?php echo $element; ?></div>
                            <?php endforeach; ?>
                        <?php endif; ?>

                        <?php if ($containerHeaderTopCentral): ?>
                            <div class="item">
                                <?php echo $containerHeaderTopCentral; ?>
                            </div>
                        <?php endif; ?>

                    </div> <!-- end: central column -->
                <?php endif; ?>

                <div class="left-column">

                    <?php if (isset($html['topLeft'])): ?>
                        <?php foreach ($html['topLeft'] as $element): ?>
                            <div class="item item-left item-interface"><?php echo $element; ?></div>
                        <?php endforeach; ?>
                    <?php endif; ?>

                    <?php if ($tmpHtml = $this->getChildHtml('container_header_top_left_1')): ?>
                        <div class="item item-left"><?php echo $tmpHtml; ?></div>
                    <?php endif; ?>

                    <?php if ($tmpHtml = $this->getChildHtml('block_header_top_left')): ?>
                        <div class="item item-left"><?php echo $tmpHtml; ?></div>
                    <?php endif; ?>

                    <?php if ($tmpHtml = $this->getWelcome()): ?>
                        <div class="item item-left hide-below-960">
                            <div class="welcome"><?php echo $tmpHtml; ?></div>
                        </div>
                    <?php endif; ?>

                    <?php if ($compareLinkHtml): ?>
                        <div class="item item-left">
                            <?php echo $compareLinkHtml; ?>
                        </div>
                    <?php endif; ?>

                </div> <!-- end: left column -->

                <div class="right-column">

                    <?php if (isset($html['topRight'])): ?>
                        <?php foreach ($html['topRight'] as $element): ?>
                            <div class="item item-right item-interface"><?php echo $element; ?></div>
                        <?php endforeach; ?>
                    <?php endif; ?>

                    <?php if ($tmpHtml = $this->getChildHtml('container_header_top_right_1')): ?>
                        <div class="item item-right"><?php echo $tmpHtml; ?></div>
                    <?php endif; ?>

                    <?php if ($tmpHtml = $this->getChildHtml('block_header_top_right')): ?>
                        <div class="item item-right"><?php echo $tmpHtml; ?></div>
                    <?php endif; ?>

                </div> <!-- end: right column -->

            </div> <!-- end: inner-container -->
        </div> <!-- end: header-top -->
    </div> <!-- end: header-top-container -->

    <div class="header-primary-container">
        <div class="header-primary header container">
            <div class="inner-container">

                <?php echo $this->getChildHtml('topContainer'); ?>

                <?php
                //Important: do not add any additional blocks directly inside the "hp-blocks-holder" div.
                //Additional blocks, if needed, can be added inside columns (left, central, righ).
                ?>
                <div class="hp-blocks-holder<?php if($hpClasses) echo $hpClasses; ?>">

                    <?php if (isset($grid['primLeftCol'])): ?>
                        <!-- Left column -->
                        <div class="hp-block left-column <?php echo $grid['primLeftCol']; ?>">
                            <?php echo $this->getChildHtml('container_header_primary_left_1'); ?>
                            <?php if (isset($html['primLeftCol'])): ?>
                                <?php foreach ($html['primLeftCol'] as $element): ?>
                                    <div class="item"><?php echo $element; ?></div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div> <!-- end: left column -->
                    <?php endif; ?>

                    <?php if (isset($grid['primCentralCol'])): ?>
                        <!-- Central column -->
                        <div class="hp-block central-column <?php echo $grid['primCentralCol']; ?>">
                            <?php echo $this->getChildHtml('container_header_primary_central_1'); ?>
                            <?php if (isset($html['primCentralCol'])): ?>
                                <?php foreach ($html['primCentralCol'] as $element): ?>
                                    <div class="item"><?php echo $element; ?></div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div> <!-- end: central column -->
                    <?php endif; ?>

                    <?php if (isset($grid['primRightCol'])): ?>
                        <!-- Right column -->
                        <div class="hp-block right-column <?php echo $grid['primRightCol']; ?>">
                            <?php echo $this->getChildHtml('container_header_primary_right_1'); ?>
                            <?php if (isset($html['primRightCol'])): ?>
                                <?php foreach ($html['primRightCol'] as $element): ?>
                                    <div class="item"><?php echo $element; ?></div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div> <!-- end: right column -->
                    <?php endif; ?>
                    
                </div> <!-- end: hp-blocks-holder -->

            </div> <!-- end: inner-container -->
        </div> <!-- end: header-primary -->
    </div> <!-- end: header-primary-container -->

    <?php if ($menuDisplayedInFullWidthContainer): ?>
        <?php echo $menuContainerHtml; ?>
    <?php endif; ?>

</div> <!-- end: header-container3 -->
</div> <!-- end: header-container2 -->
</div> <!-- end: header-container -->
<script type="text/javascript">
//<![CDATA[

requirejs(['jquery', 'smartheader', 'stickyheader'], function(jQuery, smartheader, stickyheader) {

    var theHeaderContainer = jQuery('#header-container');

    <?php //Important: mobile header code must be executed before sticky header code ?>
    <?php if ($mobileHeaderMode): ?>

        //alert('header tpl, before smartheader'); ///TODO

        theHeaderContainer.smartheader();

    <?php endif; //end: mode  ?>

    jQuery(function($) {

        //console.log('header tpl, on(ready), ater smartheader'); ///
        //alert('header tpl, on(ready), ater smartheader'); ///TODO

        <?php if ($mobileHeaderMode): ?>

            //Skip Links
            var skipContents = $('.skip-content');
            var skipLinks = $('.skip-link');
            skipLinks.on('click', function (e) {
                e.preventDefault();

                var self = $(this);
                var target = self.attr('href');

                //Get target element
                var elem = $(target);

                //Check if stub is open
                var isSkipContentOpen = elem.hasClass('skip-active') ? 1 : 0;

                //Hide all stubs
                skipLinks.removeClass('skip-active');
                skipContents.removeClass('skip-active');

                //Toggle stubs
                if (isSkipContentOpen) {
                    self.removeClass('skip-active');
                } else {
                    self.addClass('skip-active');
                    elem.addClass('skip-active');
                }
            });

        <?php endif; //end: mode  ?>

        <?php if ($sticky): ?>

            var stickyHeaderSettings = {
                stickyThreshold: <?php echo $mobileMenuThreshold; ?>
            };
            theHeaderContainer.stickyheader(stickyHeaderSettings);

        <?php endif; //end: if sticky  ?>

    }); //end: on document ready

}); //end: requirejs

//]]>
</script>
<script type="text/javascript">
//<![CDATA[

    //Expose the header container if jQuery script (smartheader) failed

    // var jsHeaderContainerObject = document.getElementById("header-container");
    // if (jsHeaderContainerObject.style.display == 'none')
    // {
    //     jsHeaderContainerObject.style.display = "block";
    //     jsHeaderContainerObject.classList.add("js-shown"); ///
    // }

//]]>
</script>
