<?php
    $first_page = false;
		$current_page = FILENAME_CATEGORIES_NESTED;
	$category_query = tep_db_query("select cd.categories_name, c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$current_category_id . "' and cd.categories_id = '" . (int)$current_category_id . "' and cd.language_id = '" . (int)$languages_id . "'");
    $category = tep_db_fetch_array($category_query);
if	(TITLE_PIC == 'true'){	
	if	($category['categories_image'] !=''){  $pis = ''. tep_image(DIR_WS_IMAGES . $category['categories_image'], $category['categories_name'], HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT).''; }
}
?>

<?php echo tep_draw_content_top();?>

<?php echo tep_draw_heading_top();?>
<div class="title_pic"><?php echo $pis ?></div><h2><?php echo $category['categories_name']; ?></h2>
<?php echo tep_draw_heading_bottom();?>

<div class="contentContainer block_subcategories">
    
<?php
    if (isset($cPath) && strpos('_', $cPath)) {
// check to see if there are deeper categories within the current category
      $category_links = array_reverse($cPath_array);
      for($i=0, $n=sizeof($category_links); $i<$n; $i++) {
        $categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "'");
        $categories = tep_db_fetch_array($categories_query);
        if ($categories['total'] < 1) {
			$categories_row = $categories['total'];
          // do nothing, go through the loop
        } else {
          $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
          break; // we've found the deepest category the customer is in
        }
      }
    } else {
      $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
    }
    $number_of_categories = tep_db_num_rows($categories_query);

    $rows = 0;
  	$col = 0;
  	$row = 0;
	$image_width = SUBCATEGORY_IMAGE_WIDTH;
	$image_height = SUBCATEGORY_IMAGE_HEIGHT;
	
    $prods_content = ''. "\n";	
		$prods_content .= '	<ul id="sub_categories" class="thumbnails row_sub_categories_name">'. "\n";	
    while ($categories = tep_db_fetch_array($categories_query)) {
			$image_pathe = HTTP_SERVER.DIR_WS_HTTP_CATALOG.DIR_WS_IMAGES . $categories['categories_image'];
			$cPath_new = tep_get_path($categories['categories_id']);
			$rows++;
			$p_name = '<span class="equal-height_sub_categories_name"><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . $categories['categories_name'] . '</a></span>';
			$p_pic = ((SUBCATEGORY_PAGE_SHOW_THUMBS == 'Show') ? '<div class="product_pic_wrapper" style="width:'.$image_width.'px;height:'.$image_height.'px;"><a class="product_img" href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '" rel="'. $image_pathe .'">' . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], $image_width, $image_height, '') . '</a></div>' : '');			
				
			 $prods_content .= "\n".
			 '		<li class="responsive_block" style="width:'.SUBCATEGORY_BLOCK_WIDTH.'px;"><div>'. "\n".
			 '			'.$p_pic.''. "\n".
			 '			'.$p_name.''. "\n".
			 '		</div></li>'. "\n";
    }
	  $prods_content .= '	</ul>'. "\n";	
// needed for the new products module shown below
    $new_products_category_id = $current_category_id;
?>
        <div class="contentBlock"><?php echo $prods_content; ?></div>
<script type="text/javascript">
$(document).ready(function(){ 				
	var row_list_sub_categories_name = $('.row_sub_categories_name');
			row_list_sub_categories_name.each(function(){
			new equalHeights_sub_categories_name($('#sub_categories'));
	});	
})
</script>
</div>      
<?php echo tep_draw_content_bottom(); ?>

<?php
		$title_upcom = $title_new = $title_special = $title_popular = $title_reviews = false;
			if (isset($new_products_category_id)) {
				$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, pd.products_name, p.products_date_added, p.products_last_modified, s.specials_new_products_price, p.products_price  from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, 
				" . TABLE_PRODUCTS_DESCRIPTION . " pd, 
				" . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, 
				" . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by p.products_date_added desc limit 1");
		}
		$title_new = (tep_db_num_rows($new_products_query) ? true : false );
		$expected_query = tep_db_query("select p.products_id, pd.products_name, products_date_available as date_expected from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where to_days(products_date_available) >= to_days(now()) and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by " . EXPECTED_PRODUCTS_FIELD . " " . EXPECTED_PRODUCTS_SORT . " limit " . MAX_DISPLAY_UPCOMING_PRODUCTS);
		$title_upcom = ((tep_db_num_rows($expected_query) > 0) ? true : false);
		 
		if (isset($current_category_id) && ($current_category_id > 0)) {
			$best_sellers_query2 = tep_db_query("select distinct p.products_id, pd.products_description, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and '" . (int)$current_category_id . "' in (c.categories_id, c.parent_id) order by p.products_ordered desc, pd.products_name limit 1");
		} else {
			$best_sellers_query2 = tep_db_query("select distinct p.products_id, pd.products_description, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by p.products_ordered desc, pd.products_name limit " . BESTSELLERS_BOX_MULTY_MAX_DISPLAY);
		}
		$title_popular = ((tep_db_num_rows($best_sellers_query2) > 0) ? true : false);
		
  	$specials_query_raw = "select p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, p.products_image, s.specials_new_products_price from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_SPECIALS . " s where p.products_status = '1' and s.products_id = p.products_id and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and s.status = '1' order by s.specials_date_added DESC";
  	$specials_split = new splitPageResults($specials_query_raw, SPECIALS_BOX_MULTY_MAX_DISPLAY);
		$specials_query = tep_db_query($specials_split->sql_query);
		$title_special = ((tep_db_num_rows($specials_query) > 0) ? true : false);
	
		$random_select_rid = " order by r.reviews_id desc  limit " . (REVIEWS_BOX_MULTY_MAX_DISPLAY + 1);
		$random_select2 = tep_db_query("select r.reviews_id, r.reviews_rating, p.products_id, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price from " . TABLE_REVIEWS . " r, " . TABLE_REVIEWS_DESCRIPTION . " rd, " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = r.products_id and r.reviews_id = rd.reviews_id and rd.languages_id = '" . (int)$languages_id . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and r.reviews_status = 1" . $random_select_pid . $random_select_rid);
		$title_reviews = ((tep_db_num_rows($random_select2) > 0) ? true : false);
		
	$featured_products_query_raw = "select p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, p.products_date_added, m.manufacturers_name
   from " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id
   left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "'
   left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id
   left join " . TABLE_FEATURED . " f on p.products_id = f.products_id
   where p.products_status = '1' and f.status = '1' order by f.featured_last_modified DESC, f.featured_date_added DESC, f.date_status_change DESC";
   $featured_products_split2 = new splitPageResults($featured_products_query_raw, 1);
	 $featured_query2 = tep_db_query($featured_products_split2->sql_query);
	 $title_featured = ((tep_db_num_rows($featured_query2) > 0) ? true : false);
		 
			if	((FEATURED_MODUL_DISPLAY == 'Show') || (NEW_PRODUCTS_MODULE_DISPLAY == 'Show') || (UPCOMING_MODUL_DISPLAY == 'Show'))	{
			
			echo '<div id="tabvanilla_nested" class="widget">
						<script type="text/javascript">
						$(document).ready(function(){
							var li = (document.getElementById(\'tabnav_nested\').getElementsByTagName(\'li\').length);
									$(\'#tabnav_nested li\').css(\'width\', 100 / li+\'%\')
						});
						</script>
						<ul class="tabnav" id="tabnav_nested">';
						if (NEW_PRODUCTS_MODUL_DISPLAY == 'Show' && $title_new)	{ echo '
								<li><a href="#new_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TABLE_HEADING_NEW_PRODUCTS_TOOGLE.'</strong></strong></a></li>';
						}
						if (FEATURED_MODUL_DISPLAY == 'Show' && $title_featured)	{ echo '
								<li><a href="#featured_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TABLE_HEADING_FEATURED_PRODUCTS_TOOGLE.'</strong></strong></a></li>';
						}					
						if (UPCOMING_MODUL_DISPLAY == 'Show' && $title_upcom)	{ echo '
								<li><a href="#expected_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TABLE_HEADING_UPCOMING_PRODUCTS_TOOGLE.'</strong></strong></a></li>';
						}
						if ($oscTemplate->hasBlocks('box_toogle_pos_1') && $title_popular)	{ echo '
								<li><a href="#box_toogle_pos_1_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.MODULE_BOXES_MULTY_BEST_SELLERS_BOX_TOOGLE.'</strong></strong></a></li>';
						}
						if ($oscTemplate->hasBlocks('box_toogle_pos_2') && $title_special)	{ echo '
								<li><a href="#box_toogle_pos_2_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.MODULE_BOXES_MULTY_SPECIALS_BOX_TOOGLE.'</strong></strong></a></li>';
						}
						if ($oscTemplate->hasBlocks('box_toogle_pos_3') && $title_reviews)	{ echo '
								<li><a href="#box_toogle_pos_3_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.MODULE_BOXES_MULTY_REVIEWS_BOX_TOOGLE.'</strong></strong></a></li>';
						}
						if ($oscTemplate->hasBlocks('box_toogle_pos_4') && $title_featured)	{ echo '
								<li><a href="#box_toogle_pos_4_nested"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.MODULE_BOXES_MULTY_FEATURED_BOX_TOOGLE.'</strong></strong></a></li>';
						}											
			echo '</ul>';
			if	(NEW_PRODUCTS_MODUL_DISPLAY == 'Show' && $title_new)	{
				echo '<div id="new_nested" class="tabdiv">';			
				echo tep_draw_fpage_content_top();
				include(DIR_WS_MODULES . FILENAME_NEW_PRODUCTS);
				echo tep_draw_fpage_content_bottom();
				echo '</div>';
			}	
			if	(FEATURED_MODUL_DISPLAY == 'Show' && $title_featured)	{
				echo '<div id="featured_nested" class="tabdiv">';
				echo tep_draw_fpage_content_top();
				include(DIR_WS_MODULES . FILENAME_FEATURED); 
				echo tep_draw_fpage_content_bottom();
				echo '</div>';
			}			
			if	(UPCOMING_MODUL_DISPLAY == 'Show' && $title_upcom)	{
				echo '<div id="expected_nested" class="tabdiv">';				
				echo tep_draw_fpage_content_top();
				include(DIR_WS_MODULES . FILENAME_UPCOMING_PRODUCTS);
				echo tep_draw_fpage_content_bottom();
				echo '</div>';
			}
			if (($oscTemplate->hasBlocks('box_toogle_pos_1')) && $title_popular)	{
				echo '<div id="box_toogle_pos_1_nested" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_1').'</div>';
			}             
			if (($oscTemplate->hasBlocks('box_toogle_pos_2') && $title_special))	{
				echo '<div id="box_toogle_pos_2_nested" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_2').'</div>';
			}             
			if ($oscTemplate->hasBlocks('box_toogle_pos_3') && $title_reviews)	{
				echo '<div id="box_toogle_pos_3_nested" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_3').'</div>';
			} 
			if ($oscTemplate->hasBlocks('box_toogle_pos_4') && $title_featured)	{
				echo '<div id="box_toogle_pos_4_nested" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_4').'</div>';
			}             		            
		}
			echo '</div>';	
?>