<?php
//{{block type="core/template" show_total="4" name="product_bestseller" as="product_bestseller" template="catalog/product/bestseller.phtml}}

$totalPerPage = ($this->show_total) ? $this->show_total : 9;
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );

$storeId = Mage::app()->getStore()->getId();

$_productCollection = Mage::getResourceModel('reports/product_collection')
						->addOrderedQty()
						->addAttributeToSelect('*')     
					    ->setStoreId($storeId)
                        ->addStoreFilter($storeId)	
					    ->addAttributeToFilter('visibility', $visibility)
						->setOrder('ordered_qty', 'desc')
						->addAttributeToSelect('status') 
						->setPageSize($totalPerPage);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);


// // if product is part of configurable product get first parent product
// $junkProducts = new Varien_Data_Collection();
// foreach ($_productCollection as $k => $p) {
	// $p = $p->loadParentProductIds();
	// $parentIds = $p->getData('parent_product_ids');	
	// if (is_array($parentIds) && !empty($parentIds)) {
		// if (!$junkProducts->getItemById($parentIds[0])) {
			// $parentProduct = Mage::getModel('catalog/product')->setStoreId($storeId)->load($parentIds[0]);
			// if ($parentProduct->isVisibleInCatalog()) {
				// $junkProducts->addItem($parentProduct);
			// }
		// }
	// } else {
		// if (!$junkProducts->getItemById($k)) {
			// $junkProducts->addItem($p);
		// }
	// }		
// }
// $_productCollection = $junkProducts;
// //end if product is part of configurable product get first parent product
?>
<div class="home_head">
	<div class="left_home_head"><span></span></div>
	<div class="center_home_head">
		<h2>Best Selling Products</h2>
	</div>	
	<div class="right_home_head"><span></span></div>
	<div class="clear_both"><span></span></div>
</div>

<div class="content_best" >
<?php $_collectionSize = $_productCollection->count() ?>
    <table class="generic-product-grid" id="product-list-table">
    <?php $i=0; foreach ($_productCollection as $_product): ?>
    <?php if ($i++%3==0): ?>
    <tr>
    <?php endif ?>
        <td>
			<div class="left_cart_img">
				<p class="product-image">
					<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
						<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(96, 96); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
					</a>
				</p>
			</div>
			<div class="right_name_price name_home">
            <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>
			<p class="see_all" style="margin-top:2px;">
			<?php				
				try{
					$categories = $_product->getCategoryIds();				
					$category = Mage::getModel('catalog/category')->load($categories[0]);
					echo 'See all '.'<a href="'.$category->getUrl().'" style="color:#171717;">'.$category->getName().'</a>';				
				} catch(Exception $e){}
			?>
			</p>	
			</div>	
			<div class="clear_both" ><span></span></div>
        </td>
    <?php if ($i%3==0 && $i!=$_collectionSize): ?>
    </tr>
    <?php endif ?>
    <?php endforeach ?>
    <?php for($i;$i%3!=0;$i++): ?>
          <td class="empty-product">&nbsp;</td>
    <?php endfor ?>
    <?php if ($i%3==0): ?>
    </tr>
    <?php endif ?>
    </table>
    <script type="text/javascript">decorateTable('product-list-table')</script>
</div>
<div class="bottom_best" ><span></span></div>

