<?php 
	global $redux_demo;
	$catSecTitle = $redux_demo['cat-sec-title'];
	$catSecDesc = $redux_demo['cat-sec-desc'];
	$allCatURL = $redux_demo['all-cat-page-link'];
	$trnsCatVAll = $redux_demo['trns-cat-viewall-btn'];
?>
<!-- Ads Categories -->
<section id="adsCategory">
    <div class="row">
        <div class="mainHeading">
            <div class="large-7 medium-8 large-centered medium-centered columns">
                <h2><?php echo $catSecTitle; ?></h2>
                <p><span class="line"><i class="fa fa-circle"></i></span></p>
                <p><?php echo $catSecDesc; ?></p>
            </div><!--large7-->
        </div><!--mainHeading-->
    </div><!--heading row-->
    <div class="row cat">
	<?php 
	$argsmaino = array(	
				'order' => 'DESC',
				'hide_empty'               => 0,
				'number'                   => '8',
				'taxonomy'                 => 'category',
				'pad_counts'               => false
			);
	$categories = get_categories($argsmaino);
	$currentCat = 0;
	foreach ($categories as $category) {
				if ($category->category_parent == 0) {
					$currentCat++;
				}
			}
	$argsmain = array(
					'order'                    => 'DESC',
					'hide_empty'               => 0,						
					'number'                   => '8',
					'taxonomy'                 => 'category',
					'pad_counts'               => false 
				);
	$cat_counter = $redux_demo['home-cat-counter'];
	$primaryColor = $redux_demo['color-primary'];
	$categories = get_terms(
				'category', 
				array('hide_empty' => 0,'parent' => 0,'number' => $cat_counter,'order'=> 'ASC')	
					);
	$current = -1;				
	?>
	<?php 
		foreach ($categories as $category) {
			$tag = $category->term_id;
			$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
			if(isset($tag_extra_fields[$tag])){
					$category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
					$caticoncolor = $tag_extra_fields[$tag]['category_icon_color'];
					$categoryIMG = $tag_extra_fields[$tag]['category_image'];
				}
			$cat = $category->count;
			$catName = $category->term_id;
			$mainID = $catName;
			if(empty($caticoncolor)){
					$iconColor = $primaryColor;
				}else{
					$iconColor = $caticoncolor;
				}
			if(empty($categoryIMG)){
				$catIMG = get_template_directory_uri().'/images/category.png';
			}else{
				$catIMG = $categoryIMG;
			}	
			$current++;
			$allPosts = 0;
			$categories = get_categories('child_of='.$catName);
			foreach($categories as $category){
						$allPosts += $category->category_count;
					}
	?>
        <div class="large-3 medium-4 columns category">
            <div class="categoryBox categoryBoxV2">
               <div class="category-img">
                   <img src="<?php echo $catIMG; ?>" alt="category image">
                   <div class="cat-head">
						<h4>
							<a href="<?php echo get_category_link( $mainID ) ?>">
								<i style="background-color:<?php echo $iconColor; ?>;" class="<?php $category_icon = stripslashes($category_icon_code); echo $category_icon; ?>"></i>
								<?php echo get_cat_name( $catName ); ?>
							</a>
						</h4>
                   </div>
               </div><!--category-img-->
                <div class="categories">
                    <ul>
					<?php 
					$currentCat = 0;
					$args2 = array(
					'type' => 'post',
					'child_of' => $catName,
					'parent' => get_query_var(''),
					'orderby' => 'name',
					'order' => 'ASC',
					'hide_empty' => 0,
					'hierarchical' => 1,
					'exclude' => '',
					'include' => '',
					'number' => '',
					'taxonomy' => 'category',
					'pad_counts' => true );
					$categories2 = get_categories($args2);
					foreach($categories2 as $category2) { 
						$currentCat++;
					}
					$args = array(
					'type' => 'post',
					'child_of' => $catName,
					'parent' => get_query_var(''),
					'orderby' => 'name',
					'order' => 'ASC',
					'hide_empty' => 0,
					'hierarchical' => 1,
					'exclude' => '',
					'include' => '',
					'number' => '5',
					'taxonomy' => 'category',
					'pad_counts' => true );
					$categories = get_categories($args);
					foreach($categories as $category) {
						?>
						<?php 
							$categoryTitle = $category->name;
							if(is_rtl()){
								$categoryTitle = $category->name;
							}else{
								$categoryTitle = (strlen($categoryTitle) > 30) ? substr($categoryTitle,0,27).'...' : $categoryTitle;
							}
						?>
                        <li>
							<a href="<?php echo get_category_link( $category->term_id )?>" title="View posts in <?php echo $category->name?>"><?php echo $categoryTitle; ?></a><span>(<?php echo $category->count ?>)</span>
						</li>
                    <?php }/*End For Each for sub Category*/?>
					<?php if($currentCat > 5) { ?>
						<li>
							<a href="<?php echo get_category_link( $catName ) ?>"><?php esc_html_e( 'Others', 'classiera' ); ?> </a>
							<span class="category-counter"><?php echo $allPosts; ?></span>
						</li>
					<?php } ?>	
                    </ul>
                </div><!--sub categories-->
                <div class="category-button-v2 text-center">                   
					<a class="button round" href="<?php echo get_category_link( $mainID ) ?>" onMouseOver="this.style.backgroundColor='<?php echo $caticoncolor; ?>'" onMouseOut="this.style.backgroundColor='inherit'"><?php esc_html_e( 'VIEW ALL', 'classiera' ); ?></a>
                </div><!--view button-->
            </div><!--categoryBox categoryBoxV2-->
        </div><!--category box main--> 
<?php }/*End For Each */?>		
    </div><!--row cat-->
    <div class="row">
        <div class="loadMore">
            <p><a href="<?php echo $allCatURL; ?>"><i class="fa fa-refresh"></i><?php echo $trnsCatVAll; ?></a></p>
        </div>
    </div>
</section>
<!-- End Ads Categories -->