<?php
/*
  $Id$

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

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/
 if((defined('FEATURED_MODUL_DISPLAY') AND FEATURED_MODUL_DISPLAY == 'Show') || (defined('FEATURED_MODUL_DISPLAY_FIRST_PAGE') AND FEATURED_MODUL_DISPLAY_FIRST_PAGE == 'Show')) {
  $featured_products_category_id =  (isset($new_products_category_id))?$new_products_category_id:'0';
  $cat_name_query = tep_db_query('SELECT `categories_name` FROM ' . TABLE_CATEGORIES_DESCRIPTION . " WHERE `categories_id` = '" . $featured_products_category_id . "' limit 1");
  $cat_name_fetch = tep_db_fetch_array($cat_name_query);
  $cat_name = $cat_name_fetch['categories_name'];
  $info_box_contents = array();
  list($usec, $sec) = explode(' ', microtime());
  srand( (float) $sec + ((float) $usec * 100000) );
  $mtm= rand();
	$cooke_name	= (($current_page != FILENAME_DEFAULT) ? 'startTab_module_featured_product' : 'startTab_module_featured_product2');
  if ( (!isset($featured_products_category_id)) || ($featured_products_category_id == '0') ) {
    $title = '<a href="' . tep_href_link(FILENAME_FEATURED_PRODUCTS) . '">' . TABLE_HEADING_FEATURED_PRODUCTS . '</a>';
    // Phocea Optimize featured query
    // Ben: Option to only show featured products on sale
    $query = 'SELECT p.products_id, p.products_image, p.products_tax_class_id, IF (s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, p.products_price, pd.products_name ';

		if ( defined('FEATURED_MODUL_SPECIALS_ONLY') AND FEATURED_MODUL_SPECIALS_ONLY == 'true' ) {
				$query .= 'FROM ' . TABLE_SPECIALS . ' s LEFT JOIN ' . TABLE_PRODUCTS . ' p ON s.products_id = p.products_id ';
		} else {
				$query .= 'FROM ' . TABLE_PRODUCTS . ' p LEFT JOIN ' . TABLE_SPECIALS . ' s ON p.products_id = s.products_id ';
		}
		if ($current_page != FILENAME_DEFAULT)	{ 
			$query .= 'LEFT JOIN ' . TABLE_PRODUCTS_DESCRIPTION . " pd ON p.products_id = pd.products_id AND pd.language_id = '" . $languages_id . "'
			LEFT JOIN " . TABLE_FEATURED . " f ON p.products_id = f.products_id
			WHERE p.products_status = '1' AND f.status = '1' order by rand($mtm) DESC limit " . FEATURED_MODUL_MAX_DISPLAY;
		}else{
			$query .= 'LEFT JOIN ' . TABLE_PRODUCTS_DESCRIPTION . " pd ON p.products_id = pd.products_id AND pd.language_id = '" . $languages_id . "'
			LEFT JOIN " . TABLE_FEATURED . " f ON p.products_id = f.products_id
			WHERE p.products_status = '1' AND f.status = '1' order by rand($mtm) DESC limit " . FEATURED_MODUL_MAX_DISPLAY_FIRST_PAGE;
		}
    $featured_products_morule_query = tep_db_query( $query );	
  } else {
    $title = sprintf(TABLE_HEADING_FEATURED_PRODUCTS_CATEGORY, $cat_name);
    $subcategories_array = array();
    tep_get_subcategories($subcategories_array, $featured_products_category_id);
    $featured_products_category_id_list = tep_array_values_to_string($subcategories_array);
    if ($featured_products_category_id_list == '') {
      $featured_products_category_id_list .= $featured_products_category_id;
    } else {
      $featured_products_category_id_list .= ',' . $featured_products_category_id;
    }
	if ( defined('FEATURED_MODUL_SUB_CATEGORIES') AND FEATURED_MODUL_SUB_CATEGORIES == 'true') {
      // current catID as starting value
      $cats[] = $new_products_category_id; 
      // put cat-IDs of all cats nested in current branch into $cats array, 
      // go through all subbranches
      for($i=0; $i<count($cats); $i++) {
        $categorie_query = tep_db_query('SELECT `categories_id` FROM ' . TABLE_CATEGORIES . " WHERE parent_id = '" . (int)$cats[$i] . "'"); 
        while ($categorie = tep_db_fetch_array($categorie_query)) {
          $cats[] = $categorie['categories_id'];
        }
	    // sort out doubles
        $cats = array_unique($cats);
      }
      $catIdSql = implode(', ', $cats);
	} else {
      $catIdSql = $featured_products_category_id_list;
	}
    // Phocea Optimize featured query
	$query = 'SELECT distinct p.products_id, p.products_image, p.products_tax_class_id, IF (s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, p.products_price, pd.products_name
    FROM ' . TABLE_PRODUCTS . ' p LEFT JOIN ' . TABLE_PRODUCTS_TO_CATEGORIES . ' p2c using(products_id)
    LEFT JOIN ' . TABLE_CATEGORIES . ' c USING (categories_id)
    LEFT JOIN ' . TABLE_FEATURED . ' f ON p.products_id = f.products_id
    LEFT JOIN ' . TABLE_SPECIALS . ' s ON p.products_id = s.products_id
    LEFT JOIN ' . TABLE_PRODUCTS_DESCRIPTION . " pd ON p.products_id = pd.products_id AND pd.language_id = '" . $languages_id . "' 
    where c.categories_id IN(" . $catIdSql . ") AND p.products_status = '1' AND f.status = '1' ";
	if ( defined('FEATURED_MODUL_SPECIALS_ONLY') AND FEATURED_MODUL_SPECIALS_ONLY == 'true' ) {
		$query .= " AND s.status = '1' ";
	}
  if ($current_page != FILENAME_DEFAULT)	{ 	
	$query .= 'ORDER BY rand(' . $mtm . ') DESC LIMIT ' . FEATURED_MODUL_MAX_DISPLAY;
  }else{
	$query .= 'ORDER BY rand(' . $mtm . ') DESC LIMIT ' . FEATURED_MODUL_MAX_DISPLAY_FIRST_PAGE;
  }
    $featured_products_morule_query = tep_db_query( $query );
}
  $num_featured_products = tep_db_num_rows($featured_products_morule_query);
  if ($num_featured_products > 0) {
  $col = 0;
  $row = 0;
	$col_items = (($current_page == FILENAME_DEFAULT) ? MAX_DISPLAY_FEATURED_MODUL_PER_ROW_FIRST_PAGE - 1 : MAX_DISPLAY_FEATURED_MODUL_PER_ROW - 1);
	$name = ($current_page == FILENAME_DEFAULT) ? FEATURED_MODUL_SHOW_NAME_FIRST_PAGE : FEATURED_MODUL_SHOW_NAME;
	if ($current_page == FILENAME_DEFAULT)	{
	$image_width = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE_FIRST_PAGE == 'General') ? GENERAL_IMAGE_WIDTH : FEATURED_MODUL_IMAGE_WIDTH_FIRST_PAGE;
	$image_height = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE_FIRST_PAGE == 'General') ? GENERAL_IMAGE_HEIGHT : FEATURED_MODUL_IMAGE_HEIGHT_FIRST_PAGE;
	$image_width_row = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE_FIRST_PAGE == 'General') ? GENERAL_IMAGE_WIDTH_ROW : FEATURED_MODUL_IMAGE_WIDTH_ROW_FIRST_PAGE;
	$image_height_row = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE_FIRST_PAGE == 'General') ? GENERAL_IMAGE_HEIGHT_ROW : FEATURED_MODUL_IMAGE_HEIGHT_ROW_FIRST_PAGE;
	$row_new_products_desc = (FEATURED_MODUL_MAX_DESCR_FIRST_PAGE != 0) ? 'row_featured_desc' : '';	
	$row_new_products_name = (FEATURED_MODUL_SHOW_NAME_FIRST_PAGE == 'Show') ? 'row_featured_name' : '';
	
		if (MAX_DISPLAY_FEATURED_MODUL_PER_ROW_FIRST_PAGE  >= 1 && FEATURED_MODUL_LYOUT_FIRST_PAGE =='Grid')	{
			$products_block_width = FEATURED_MODUL_BLOCK_WIDTH_FIRST_PAGE.'px';
			$view = 't_view';
			$equal_height_products_block = 'equal-height_featured_block';
			$equal_height_products_name = 'equal-height_featured_name';
			$equal_height_products_desc = 'equal-height_featured_desc';
		}else{
			$products_block_width = 100;
			$products_block_width .= '%';
			$view = 'r_view';
			$equal_height_products_block = 'equal-height_featured_block';
			$equal_height_products_name = 'equal-height_featured_name';
			$equal_height_products_desc = 'equal-height_featured_desc';
		}
	}else{
	$image_width = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH : FEATURED_MODUL_IMAGE_WIDTH;
	$image_height = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT : FEATURED_MODUL_IMAGE_HEIGHT;
	$image_width_row = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH_ROW : FEATURED_MODUL_IMAGE_WIDTH_ROW;
	$image_height_row = (FEATURED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT_ROW : FEATURED_MODUL_IMAGE_HEIGHT_ROW;		
	$row_new_products_desc = (FEATURED_MODUL_MAX_DESCR != 0) ? 'row_featured_desc' : '';	
	$row_new_products_name = (FEATURED_MODUL_SHOW_NAME == 'Show') ? 'row_featured_name' : '';
			
		if (MAX_DISPLAY_FEATURED_MODUL_PER_ROW  >= 1 && FEATURED_MODUL_LYOUT =='Grid')	{	
			$products_block_width = FEATURED_MODUL_BLOCK_WIDTH.'px';
			$view = 't_view';
			$equal_height_products_block = 'equal-height_featured_block';
			$equal_height_products_name = 'equal-height_featured_name';
			$equal_height_products_desc = 'equal-height_featured_desc';			
		}else{
			$products_block_width = 100;
			$products_block_width .= '%';
			$view = 'r_view';
			$equal_height_products_block = 'equal-height_featured_block';
			$equal_height_products_name = 'equal-height_featured_name';
			$equal_height_products_desc = 'equal-height_featured_desc';
		}
	}
  $featured_prods_content .= '<div class="content '.$view.'">';
	$featured_prods_content_row .= '<div class="content '.$view.'">';
  while ($featured_products = tep_db_fetch_array($featured_products_morule_query)) {
		$featured_prods_content .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$featured_prods_content_row .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$featured_prods_content .= ($col === 0) ? '<ul class="'.$row_new_products_name.' '.$row_new_products_desc.' row_featured_block" id="view2_grid-'.$row.'">' : '<li class="prods_vseparator '.$equal_height_products_block.'">'.tep_draw_separator('spacer.gif', '1', '1').'';
		$featured_prods_content_row .= ($col === 0) ? '<ul class="'.$row_new_products_name.' '.$row_new_products_desc.' row_featured_block" id="view2_row-'.$row.'">' : '<li class="prods_vseparator">'.tep_draw_separator('spacer.gif', '1', '1').'';
// *************************************		
	  $p_id = $featured_products['products_id'];
		$product_query = tep_db_query("select products_description, products_id from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$featured_products['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
    $product = tep_db_fetch_array($product_query);
		
		$p_name =	(($name == 'Show') ? '<h3 class="product_name_wrapper name '.$equal_height_products_name.'"><span><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '">' . $featured_products['products_name'] . '</a></span></h3>' : '');
		$p_desc = (($current_page == FILENAME_DEFAULT) ? ((FEATURED_MODUL_MAX_DESCR_FIRST_PAGE !=0) ? '<div class="product_desc_wrapper desc '.$equal_height_products_desc.'">'.mb_substr(strip_tags($product['products_description']), 0, FEATURED_MODUL_MAX_DESCR_FIRST_PAGE, 'UTF-8').'...</div>' : '') : ((FEATURED_MODUL_MAX_DESCR !=0) ? '<div class="product_desc_wrapper desc '.$equal_height_products_desc.'">'.mb_substr(strip_tags($product['products_description']), 0, FEATURED_MODUL_MAX_DESCR, 'UTF-8').'...</div>' : ''));
		$p_desc_row = (($current_page == FILENAME_DEFAULT) ? ((FEATURED_MODUL_MAX_DESCR_ROW_FIRST_PAGE !=0) ? '<div class="product_desc_wrapper desc '.$equal_height_products_desc.'">'.mb_substr(strip_tags($product['products_description']), 0, FEATURED_MODUL_MAX_DESCR_ROW_FIRST_PAGE, 'UTF-8').'...</div>' : '') : ((FEATURED_MODUL_MAX_DESCR_ROW !=0) ? '<div class="product_desc_wrapper desc '.$equal_height_products_desc.'">'.mb_substr(strip_tags($product['products_description']), 0, FEATURED_MODUL_MAX_DESCR_ROW, 'UTF-8').'...</div>' : ''));
		$p_featured = ((FEATURED_MODUL_SHOW_FEATURED_LABEL == 'Show') ? '<div class="featured_label '.$color.'"><strong></strong></div>' : '');
		if (tep_not_null($featured_products['specials_new_products_price'])) {
				$extra = 'extra';
				$persent = ($currencies->display_sale_percent($featured_products['products_price'], tep_get_tax_rate($featured_products['products_tax_class_id']), $featured_products['specials_new_products_price']) * -1);
				$sale_percent = ((FEATURED_MODUL_SHOW_SPECIALS_LABEL == 'Show') ? '<div class="sale_percent '.$color.'"><strong>'.$persent.'%</strong></div>' : '');
				$products_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' .  $currencies->display_price($featured_products['products_price'], tep_get_tax_rate($featured_products['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($featured_products['specials_new_products_price'], tep_get_tax_rate($featured_products['products_tax_class_id'])) . '</span></div>';
		} else {
				$products_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><span class="productSpecialPrice">' . $currencies->display_price($featured_products['products_price'], tep_get_tax_rate($featured_products['products_tax_class_id'])) . '</span></div>';
				$extra = '';
				$sale_percent	= '';
		}
		$p_price =	((FEATURED_MODUL_SHOW_PRICE == 'Show') ? $products_price : '');
		$p_pic = ((FEATURED_MODUL_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_PRODUCT_INFO, 'products_id=' . $featured_products['products_id']) . '" style="width:'.$image_width.'px;height:'.$image_height.'px;">' . tep_image(DIR_WS_IMAGES . $featured_products['products_image'], $featured_products['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"') . '</a></div>' : '');		
		$p_pic_row = ((FEATURED_MODUL_SHOW_THUMBS == 'Show') ? '<div class="product_pic_wrapper" style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;"><a class="product_img" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . 	
		$featured_products['products_id']) . '" style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;">' . tep_image(DIR_WS_IMAGES . $featured_products['products_image'], $featured_products['products_name'], $image_width_row, $image_height_row, ' style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;"') . '</a></div>' : '');				
	  $p_details_text = '' .tep_draw_button2_top() . '<a href="' . tep_href_link('product_info.php?products_id='.$p_id) . '" role="button"><span class="ui-button-text">'.  IMAGE_BUTTON_DETAILS .'</span></a>' . tep_draw_button2_bottom().'';
	  $p_buy_now_text = '' .tep_draw_button1_top() . '<a href="'.tep_href_link("products_new.php","action=buy_now&products_id=".$p_id).'" role="button"><span class="ui-button-text">'.  IMAGE_BUTTON_IN_CART .'</span></a>' . tep_draw_button1_bottom().'';
		$button_block = ((FEATURED_MODUL_SHOW_BUTTONS == 'Show') ? '<div class="product_button_wrapper">'.$p_buy_now_text.''.$p_details_text.'</div>' : '');
// *************************************
		if ($p_name !='' || $p_desc !='' || $p_price !='' || $button_block !='') {
			$wrapper_top = '<div class="product_info_wrapper">';
			$wrapper_bottom = '</div>';
		}else {
			$wrapper_top = '';
			$wrapper_bottom = '';
		}
   $featured_prods_content .= ''. "\n".
					'<li class="responsive_block" style="width:'.$products_block_width.'">'. "\n".
				  '		<div class="product_block '.$equal_height_products_block.'">'. "\n".
					'			'.$p_pic.''. "\n". 
					'			'.$p_featured . "\n".
					'			'.$sale_percent . "\n".
          '			'. "\n".	
				  '			'.$wrapper_top.''."\n".
		//			'   			'.$p_data.''. "\n".	
					'    		<div class="row_01">'. "\n".								
					'					'.$p_name.''."\n".	
		//			'					'.$p_manufacturers.''. "\n".	
					'					'.$p_desc.''. "\n".	
					'				</div>'. "\n".	
					'				<div class="row_02">'. "\n".	
					'					<div class="cl_both block">'.$p_data_exp.''. "\n".																	
					'					'.$p_price.'</div>'. "\n".
					'					'.$button_block.''. "\n".	
					'				</div>'. "\n".	
				  '		'.$wrapper_bottom.''."\n".			  
				  '		</div>'. "\n";	
   $featured_prods_content_row .= ''. "\n".
					'<li class="responsive_block" style="width:'.$products_block_width.'">'. "\n".
				  '		<div class="product_block '.$equal_height_products_block.'"><div>'. "\n".
					'			'.$p_pic_row.''. "\n". 
					'			'.$p_featured . "\n".
					'			'.$sale_percent . "\n".
          '			'. "\n".	
				  '			'.$wrapper_top.''."\n".
		//			'   			'.$p_data.''. "\n".	
					'				<div class="row_02">'. "\n".	
					'					'.$p_price.''. "\n".
					'					'.$p_data_exp_row.''. "\n".																	
					'					'.$button_block.''. "\n".	
					'				</div>'. "\n".	
					'    		<div class="row_01">'. "\n".								
					'					'.$p_name.''. "\n".	
		//			'					'.$p_manufacturers.''. "\n".	
					'					'.$p_desc_row.''. "\n".	
					'				</div>'. "\n".	
				  '		'.$wrapper_bottom.''."\n".			  
				  '		</div></div>'. "\n";			  
							  
    $col ++;
    if ($col > $col_items) {
      	$featured_prods_content .= '</ul>';
				$featured_prods_content_row .= '</ul>';
	  	$row ++;
      	$col = 0;
    }else{
		$featured_prods_content .= '</li>';	
		$featured_prods_content_row .= '</li>';	
		}
  }
  $featured_prods_content .= '</div>';
	$featured_prods_content_row .= '</ul></div>';
?>

<div class="contentContainer module_featured">
<?php


echo (($current_page == FILENAME_DEFAULT) ? ((FEATURED_MODUL_LYOUT_FIRST_PAGE =='Grid') ? '
	  <div id="tabs-featured_grid">'."\n".	
'        <div class="contentBlock grid_view">'.$featured_prods_content.'</div>'."\n".	
'	  </div>'."\n".	
'<script type="text/javascript">'."\n".
'$(document).ready(function(){' ."\n".				

'				var row_list_featured_name = $(\'.row_featured_name\');'."\n".
'						row_list_featured_name.each(function(){'."\n".
'						new equalHeights_featured_name($(\'#\' + $(this).attr("id")));'."\n".
'				});	'."\n".	
'				var row_list_featured_block = $(\'.row_featured_block\');'."\n".
'		  			row_list_featured_block.each(function(){'."\n".
'						new equalHeights_featured_block($(\'#\' + $(this).attr("id")));'."\n".
'				});'."\n".
'				var row_list_featured_desc = $(\'.row_featured_desc\');'."\n".
'						row_list_featured_desc.each(function(){'."\n".
'						new equalHeights_featured_desc($(\'#\' + $(this).attr("id")));'."\n".
'				});	'."\n".		 			 			  			  			  			  			   
'})'."\n".
'</script>'.
		
'' : '

	  <div id="tabs-featured_row">'."\n".
'        <div class="contentBlock row_view">'.$featured_prods_content_row.'</div>'."\n".
'	  </div>'."\n".

'		') : ((FEATURED_MODUL_LYOUT =='Grid') ? "\n".
'	  <div id="tabs-featured_grid">'."\n".
'        <div class="contentBlock grid_view">'.$featured_prods_content.'</div>'."\n".
'		</div>'."\n".	
'<script type="text/javascript">'."\n".
'$(document).ready(function(){' ."\n".				
'				var row_list_featured_name = $(\'.row_featured_name\');'."\n".
'						row_list_featured_name.each(function(){'."\n".
'						new equalHeights_featured_name($(\'#\' + $(this).attr("id")));'."\n".
'				});	'."\n".	
'				var row_list_featured_block = $(\'.row_featured_block\');'."\n".
'		  			row_list_featured_block.each(function(){'."\n".
'						new equalHeights_featured_block($(\'#\' + $(this).attr("id")));'."\n".
'				});'."\n".
'				var row_list_featured_desc = $(\'.row_featured_desc\');'."\n".
'						row_list_featured_desc.each(function(){'."\n".
'						new equalHeights_featured_desc($(\'#\' + $(this).attr("id")));'."\n".
'				});	'."\n".		 			 			  			  			  			  			   
'})'."\n".
'</script>'."\n".
'' : 
'	  <div id="tabs-featured_row">'."\n".
'        <div class="contentBlock row_view">'.$featured_prods_content_row.'</div>'."\n".
'	  </div>'."\n"
));
?>
</div>
<?php }} ?>
