<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

// the following cPath references come from application_top.php
  $category_depth = 'top';
  if (isset($cPath) && tep_not_null($cPath)) {
    $categories_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
    $categories_products = tep_db_fetch_array($categories_products_query);
    if ($categories_products['total'] > 0) {
      $category_depth = 'products'; // display products
    } else {
      $category_parent_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$current_category_id . "'");
      $category_parent = tep_db_fetch_array($category_parent_query);
      if ($category_parent['total'] > 0) {
        $category_depth = 'nested'; // navigate through the categories
      } else {
        $category_depth = 'products'; // category has no products, but display the 'no products' message
      }
    }
  }
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_DEFAULT);
  if ($category_depth == 'nested') {
		$current_page = FILENAME_CATEGORIES_NESTED;
  	require(DIR_WS_INCLUDES . 'template_top.php');
  		include(FILENAME_CATEGORIES_NESTED);
		
  } elseif ($category_depth == 'products' || (isset($HTTP_GET_VARS['manufacturers_id']) && !empty($HTTP_GET_VARS['manufacturers_id']))) {
  	$current_page = FILENAME_CATEGORIES_LISTING;
  	require(DIR_WS_INCLUDES . 'template_top.php');

  	include(FILENAME_CATEGORIES_LISTING);
  } else { // default page
  	$current_page = FILENAME_DEFAULT;
  	require(DIR_WS_INCLUDES . 'template_top.php');
		$title_upcom = $title_new = $title_special = $title_popular = $title_reviews = false;
		$title_new = (!isset($new_products_category_id) ? 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_raw2 = "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_raw2, 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_FIRST_PAGE == 'Show') || (NEW_PRODUCTS_MODULE_DISPLAY_FIRST_PAGE == 'Show') || (UPCOMING_MODUL_DISPLAY_FIRST_PAGE == 'Show'))	{
		
		echo '<div id="tabvanilla" class="widget">
					<script type="text/javascript">
					$(document).ready(function(){
						var li = (document.getElementById(\'tabnav\').getElementsByTagName(\'li\').length);
								$(\'#tabnav li\').css(\'width\', 100 / li - 0+\'%\')
					});
					</script>
          <ul class="tabnav" id="tabnav">';
					if (NEW_PRODUCTS_MODULE_DISPLAY_FIRST_PAGE == 'Show' && $title_new)	{ echo '
							<li><a href="#new"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.sprintf(TABLE_HEADING_NEW_PRODUCTS_TOOGLE, strftime('%B')).'</strong></strong></a></li>';
					}
					if (FEATURED_MODUL_DISPLAY_FIRST_PAGE == 'Show' && $title_featured)	{ echo '
							<li><a href="#featured"><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_FIRST_PAGE == 'Show' && $title_upcom)	{ echo '
							<li><a href="#expected"><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"><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"><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"><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"><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_MODULE_DISPLAY_FIRST_PAGE == 'Show')	{
			echo '<div id="new" 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_FIRST_PAGE == 'Show' && $title_featured)	{
			echo '<div id="featured" 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_FIRST_PAGE == 'Show')	{
			echo '<div id="expected" 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" 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" 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" 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" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_4').'</div>';
    }             		            
	}
		echo '</div>';	
		
	}
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>