<?php
if (Mage::getStoreConfig('easy_catalogimg/general/showOnMain')):
    
$columnsCount = Mage::getStoreConfig('easy_catalogimg/general/columnsCount');
$_categories = $this->getStoreCategories();
$showImageThumbnails = (bool)Mage::getStoreConfig('easy_catalogimg/general/showImageThumbnails');
$showSubChild = (bool)Mage::getStoreConfig('easy_catalogimg/general/showSubChild');
$height = Mage::getStoreConfig('easy_catalogimg/general/height');
$width = Mage::getStoreConfig('easy_catalogimg/general/width');
$countSubChild = Mage::getStoreConfig('easy_catalogimg/general/countSubChild');

if (is_array($_categories)) {
    $_collectionSize = count($_categories);
} else {
    $_collectionSize = $_categories->count();
}

$_collectionSize = $_collectionSize <= 8 ? $_collectionSize : 8;
?>
<div class="category-images">
<div class="categories-img-title"><span><?php echo $this->__('Featured Categories') ?></span></div>        
<div class="listing-catalogimg-grid catalogimg-listing">
    <?php $i=0; foreach ($_categories as $_category): 
        if (!$_category->getIsActive()): continue; endif;
        $cur_category = Mage::getModel('catalog/category')->load($_category->getId());
        $imageUrl = $cur_category->getImageUrl();
        if (empty($imageUrl)): $imageUrl = Mage::getBaseUrl('media').'catalog/category/np_thumb2.gif'; endif;
    ?>
    <?php if($i++%$columnsCount==0): ?>
        <ol class="grid-row">
    <?php endif; ?>
        <li class="item">
            <div class="category-entry">
                <?php if ($showImageThumbnails) : ?>
                <a href="<?php echo $cur_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image" >
                	<?php if(!Mage::getStoreConfig('easy_catalogimg/general/resizeImageThumbnails')){ ?>
                    	<img src="<?php echo $imageUrl ?>" <?php echo !empty($height) ? ' height="' . $height . '"' : ''; echo !empty($width) ? ' width="' . $width . '"' : '' ?> alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
					<?php } else { ?>
						<img src="<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?>" width="<?php echo $width; ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
					<?php } ?>
                </a>
                <?php endif; ?>
                <h2><a href="<?php echo $cur_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"  class="easycategoriesroot"><?php echo $this->htmlEscape($_category->getName()) ?></a></h2>
				<?php if ($showSubChild) : ?>
                <ul>
                    <?php 
                        $j = 0;
                        if (Mage::helper('catalog/category_flat')->isEnabled()) {
                            $subcategories = $_category->getChildrenNodes();
                            $subcategoriesCount = count($subcategories);
                        } else {
                            $subcategories = $_category->getChildren();
                            $subcategoriesCount = $subcategories->count();
                        }
                        //$subcategories = $_category->getChildrenNodes();
                        if ($subcategoriesCount) :
                        foreach ($subcategories as $subcategory) :
                            $currentSubCategory = Mage::getModel('catalog/category')
                                ->load($subcategory->getId());
                            if ($j > $countSubChild - 1) : ?>
                                <li class="more">
                                    <a href="<?php echo $cur_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"  class="easycategoriesmore"><?php echo $this->__('More Categories >>'); ?></a>
                                </li>
                            <?php
                            break;
                            endif;
                        ?>
                        <li>
                                <a href="<?php echo $currentSubCategory->getUrl()?>" title="<?php echo $this->htmlEscape($subcategory->getName())?>" class="easycategorieschild"><?php echo $this->htmlEscape($subcategory->getName())?></a>
                        </li>
                    <?php 
                        $j++;
                        endforeach;endif;?>
                </ul>
				<?php endif; ?>
            </div>
        </li>   
    <?php if($i%$columnsCount==0 || $i==$_collectionSize): ?>

            </ol>
     <div class="clear">&nbsp;</div>
    <?php endif; ?>
    <?php if ($_collectionSize == $i) break; ?>
    <?php endforeach ?> 
    <script type="text/javascript">decorateGeneric($$('.grid-row'), ['last', 'odd', 'even']);</script>
</div>
</div>       
<?php endif; ?>