<?php

// determine the topmost parent of a term

function get_banners(){
    if(is_tax('product_cat')){
        $tax = get_taxonomy( get_query_var( 'taxonomy' ) );
        $tax_slug = $tax->query_var;
        $term = $wp_query->queried_object;
        $id = $term->term_id;
        $parent = get_term_by( 'id', $term->parent, 'product_cat' );
        if($parent){
            while ( $parent->parent != 0 ){
                $parent  = get_term_by( 'id', $parent->parent, 'product_cat' );
            }
            $id = $parent->term_id;
        }
        $images = get_field("cat_img_banner", $id);
    }elseif(is_singular('product')){
        $terms = wp_get_post_terms( get_the_ID(), 'product_cat');
        $term = end($terms);
        $term_id = $term->term_id;
        $parent  = get_term_by( 'id', $term_id, 'product_cat' );
        while ( $parent->parent != 0 ){
            $parent  = get_term_by( 'id', $parent->parent, 'product_cat' );
        }
        $id = $parent->term_id;
        $images = get_field("cat_img_banner", $id);
    }else{
        if(is_cart() || is_checkout() || is_account_page()){
            $id = get_option( 'woocommerce_shop_page_id' );
        }else{
            $id = get_the_ID();
        }
        $images = get_children( 
            array( 
                'post_parent' => $id, 
                'post_type' => 'attachment', 
                'post_mime_type' => 'image', 
                'orderby' => 'menu_order', 
                'order' => 'ASC', 
                'numberposts' => 999 
            ) 
        ); 
        $own_slides = 0;
        foreach ( $images as $attachment ) {
            $attachment_id = $attachment->ID;
            if(get_field("show_in_banner",$attachment_id)){
                $own_slides++;
                break;
            }
        }
        
        if($own_slides == 0){
            $post_parent = end(get_post_ancestors());
            $post_parent_id = $post_parent;
            if($post_parent_id){
                $top_level_parent = is_object($post_parent) ? $post_parent->ID : $post_parent;
            }
            if(!$top_level_parent){
                $top_level_parent = 9;
            }
            $images = get_children( 
                array( 
                    'post_parent' => $top_level_parent, 
                    'post_type' => 'attachment', 
                    'post_mime_type' => 'image', 
                    'orderby' => 'menu_order', 
                    'order' => 'ASC', 
                    'numberposts' => 999 
                ) 
            ); 
        }
    }
    return $images;

}
    if(is_front_page()){
        ?>
        <div class="container full main-slider header-slider of-hidden">  
        <?php
            $id = get_the_ID();
            $images = get_banners();
            foreach ( $images as $attachment ) {
                $attachment_id = $attachment->ID;
                if(get_field("show_in_banner",$attachment_id)){
                    $url_att = wp_get_attachment_image_src($attachment_id, "full");
                    $caption = get_post_field('post_excerpt', $attachment_id);
                    $caption_title = get_post_field('post_title', $attachment_id);
                    $caption_url = get_post_field('post_content', $attachment_id);

                    ?>
                        <div class="item">
                            <div class="main-slider-lazy image" data-src="<?php echo $url_att[0]; ?>"></div>
                            <?php if($caption_url){
                                ?>
                                <a href="<?php echo $caption_url; ?>">
                                <?php
                            } ?>
                            <div class="container center caption-wrapper">
                                <div class="aligner"></div>
                                <div class="caption align-middle">
                                        <?php 
                                        if($caption_title){
                                            ?>
                                            <div class="caption-title size-1 anim-medium delay-c1"><?php echo $caption_title; ?></div>
                                            <?php
                                        }
                                        if($caption){
                                            ?>
                                            <div class="caption-text anim-medium delay-c2"><?php echo $caption; ?></div>
                                            <?php
                                        } 
                                        ?>
                                </div>
                            </div>

                            <?php if($caption_url){
                                ?>
                                </a>
                                <?php
                            } ?>
                        </div>
                    <?php   
                }
            }   
        ?>
        </div>
        <?php
    }elseif(is_tax('product_cat')){
        $tax = get_taxonomy( get_query_var( 'taxonomy' ) );
        $tax_slug = $tax->query_var;
        $term = $wp_query->queried_object;
        $id = $term->term_id;
        $parent = get_term_by( 'id', $term->parent, 'product_cat' );
        if($parent){
            while ( $parent->parent != 0 ){
                $parent  = get_term_by( 'id', $parent->parent, 'product_cat' );
            }
            $id = $parent->term_id;
        }
        $images = get_field("cat_img_banner", "product_cat_".$id);
        $url_att = wp_get_attachment_image_src($images, "full");
        $caption = get_post_field('post_excerpt', $images);
        $caption_title = get_post_field('post_title', $images);
        ?>
        <div class="container full std-page-slider header-slider of-hidden">  
            <div class="item">
                <div class="main-slider-lazy image" data-src="<?php echo $url_att[0]; ?>"></div>
                <div class="container center caption-wrapper">
                    <div class="aligner"></div>
                    <div class="caption align-middle">
                            <?php 
                            if($caption_title){
                                ?>
                                <div class="caption-title size-1 anim-medium delay-c1"><?php echo $caption_title; ?></div>
                                <?php
                            }
                            if($caption){
                                ?>
                                <div class="caption-text anim-medium delay-c2"><?php echo $caption; ?></div>
                                <?php
                            } 
                            ?>
                    </div>
                </div>
            </div>
        </div>
        <?php   
    }elseif(is_singular('product')){
        $terms = wp_get_post_terms( get_the_ID(), 'product_cat');
        $term = end($terms);
        $term_id = $term->term_id;
        $parent  = get_term_by( 'id', $term_id, 'product_cat' );
        while ( $parent->parent != 0 ){
            $parent  = get_term_by( 'id', $parent->parent, 'product_cat' );
        }
        $id = $parent->term_id;
        $images = get_field("cat_img_banner", "product_cat_".$id);
        $url_att = wp_get_attachment_image_src($images, "full");
        $caption = get_post_field('post_excerpt', $images);
        $caption_title = get_post_field('post_title', $images);
        ?>
        <div class="container full std-page-slider header-slider of-hidden">  
            <div class="item">
                <div class="main-slider-lazy image" data-src="<?php echo $url_att[0]; ?>"></div>
                <div class="container center caption-wrapper">
                    <div class="aligner"></div>
                    <div class="caption align-middle">
                            <?php 
                            if($caption_title){
                                ?>
                                <div class="caption-title size-1 anim-medium delay-c1"><?php echo $caption_title; ?></div>
                                <?php
                            }
                            if($caption){
                                ?>
                                <div class="caption-text anim-medium delay-c2"><?php echo $caption; ?></div>
                                <?php
                            } 
                            ?>
                    </div>
                </div>
            </div>
        </div>
        <?php   
    }else{
        ?>
        <div class="container full std-page-slider header-slider of-hidden">  
        <?php
            $id = get_the_ID();
            $images = get_banners();
            $own_slides = 0;
            foreach ( $images as $attachment ) {
                $attachment_id = $attachment->ID;
                if(get_field("show_in_banner",$attachment_id)){
                    $own_slides++;
                    break;
                }
            }
            if($own_slides >= 1){
                foreach ( $images as $attachment ) {
                    $attachment_id = $attachment->ID;
                    if(get_field("show_in_banner",$attachment_id)){
                        $url_att = wp_get_attachment_image_src($attachment_id, "full");
                        $caption = get_post_field('post_excerpt', $attachment_id);
                        $caption_title = get_post_field('post_title', $attachment_id);
                        ?>
                            <div class="item">
                                <div class="main-slider-lazy image" data-src="<?php echo $url_att[0]; ?>"></div>
                                <div class="container center caption-wrapper">
                                    <div class="aligner"></div>
                                    <div class="caption align-middle">
                                            <?php 
                                            if($caption_title){
                                                ?>
                                                <div class="caption-title size-1 anim-medium delay-c1"><?php echo $caption_title; ?></div>
                                                <?php
                                            }
                                            if($caption){
                                                ?>
                                                <div class="caption-text anim-medium delay-c2"><?php echo $caption; ?></div>
                                                <?php
                                            } 
                                            ?>
                                    </div>
                                </div>
                            </div>
                        <?php   
                    }
                }
            }else{
                $post_parent = end(get_post_ancestors());
                $post_parent_id = $post_parent;
                if($post_parent_id){
                    $top_level_parent = is_object($post_parent) ? $post_parent->ID : $post_parent;
                }
                $images_top_parent = get_children( 
                    array( 
                        'post_parent' => $top_level_parent, 
                        'post_type' => 'attachment', 
                        'post_mime_type' => 'image', 
                        'orderby' => 'menu_order', 
                        'order' => 'ASC', 
                        'numberposts' => 999 
                    ) 
                ); 
                foreach ( $images_top_parent as $attachment ) {
                    $attachment_id = $attachment->ID;
                    if(get_field("show_in_banner",$attachment_id)){
                        $url_att = wp_get_attachment_image_src($attachment_id, "full");
                        $caption = get_post_field('post_excerpt', $attachment_id);
                        $caption_title = get_post_field('post_title', $attachment_id);
                        ?>
                            <div class="item">
                                <div class="main-slider-lazy image" data-src="<?php echo $url_att[0]; ?>"></div>
                                <div class="container center caption-wrapper">
                                    <div class="aligner"></div>
                                    <div class="caption align-middle">
                                            <?php 
                                            if($caption_title){
                                                ?>
                                                <div class="caption-title size-1 delay-c1 anim-medium"><?php echo $caption_title; ?></div>
                                                <?php
                                            }
                                            if($caption){
                                                ?>
                                                <div class="caption-text delay-c2 anim-medium"><?php echo $caption; ?></div>
                                                <?php
                                            } 
                                            ?>
                                    </div>
                                </div>
                            </div>
                        <?php   
                    }
                }
            } 

        ?>
        </div>
        <?php
    }
?>

