        <?php
                // some helpers
                $_helper = $this->helper('catalog/output');
                $storeId = Mage::app()->getStore()->getId();
                $catalog = $this->getLayout()->createBlock('catalog/product_list')->setStoreId($storeId);
$productCount = $this->getData('num_products');
            ?>
<?php 
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );
$collection = Mage::getResourceModel('reports/product_collection')
                        ->addAttributeToSelect('*')    
                        ->setStoreId($storeId)
                        ->addStoreFilter($storeId)    
                        ->addAttributeToFilter('visibility', $visibility)             
                        ->addOrderedQty()
                        ->setOrder('ordered_qty', 'desc')
                        ->setPageSize($productCount);
/* To display product by category id */
  if($categoryId=$this->getData('category_id')){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
      }
                ?>
          <?php      // if no products are currently sell, display some text
                 if (!$collection->count()) :
        ?>
        <p class="note-msg"><?php echo $this->__('There are no products sell at the moment.') ?></p>
        <?php else : ?>

<!-- BEGIN CATEGORY PRODUCTS --> 
<div class="category-products">
  <div class="new-arrow1">
                  <h2><?php echo $this->__('Bestseller Products');?></h2>
</div><!--new-arrow1-->
<a id="products_slider_prev" class="jcarousel-prev-horizontal" href="#"></a>
<a id="products_slider_next" class="jcarousel-next-horizontal" href="#"></a>

<div class="flexslider carousel" id="secondary-slider1">
<div class="flex-viewport" style="overflow: hidden; position: relative;">
<div class="slides" style="width: 2400%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px); margin-left: 0px;">
  
        <?php
                $_collectionSize = $collection->count();
                $_columnCount = 3;
                $i = 0;

                foreach ($collection as $_product) :
                        $_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($_product->getId());

        ?>
            <?php if ($i++%$_columnCount==0): ?>
            
            <?php endif ?>

             <ul style="margin-right: 14px; width: 295px; float: left; display: block; padding: 0px;" class="home-grid<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> even<?php elseif($i%$_columnCount!==0): ?> odd<?php endif; ?>">
 <li class="item first">
               <a id="fancybox358" class="fancybox quick-view" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('Details');?></a>                   
                  <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image') ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                  <div class="productgrid-area">
                  <h2 class="product-name">
                  <a  href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php //echo $this->htmlEscape($_product->getName()) ?>
                     <?php $prod_name = $this->htmlEscape($_product->getName()) ; 
                        if(strlen($prod_name) > 30) {echo substr($prod_name,0,30)."...";}else {echo $prod_name;}?></a></h2>
                  <?php  //if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                  <?php  //endif;  ?>
                  
                  <?php echo $this->getPriceHtml($_product, true) ?>
                  <div class="actions">
                  <?php if($_product->isSaleable()): ?>
                      <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart ajx-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                  <?php else: ?>
                      <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                  <?php endif; ?> 
                  <ul class="add-to-links">  
                  <li>                                           
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" title="<?php echo $this->__('Add to wishlist') ?>"></a>
                    <?php endif; ?>     
                    </li>
                    <li>                             
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <a href="<?php echo $_compareUrl ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>"></a>
                    <?php endif; ?>
                    </li>                             
                  </ul> <!--add-to-links-->
                  </div><!--actions-->
                  </div><!--productgrid-area-->                                        
            </li>
             </ul>
            <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
            
            <?php endif ?>
                <?php endforeach ?>
 
  </div><!--jcarousel-clip jcarousel-clip-horizonta-->
 </div>
</div> <!--jcarousel-container jcarousel-container-horizontal-->
 </div><!--category products-->   

        <?php endif ?>
<script type="text/javascript">

j$('#secondary-slider1').flexslider({
   selector: ".slides > ul",
    animation: "slide",
    animationLoop: false,
    itemWidth:295,
    itemMargin:18,
    minItems: 1,
    maxItems: 3,
    move:1,
    controlNav: false,
    slideshowSpeed: 7000,          
    animationSpeed: 1000,
    slideshow: false,
    start: function(slider){
      j$('div').removeClass('loading');
    }
       });
</script>