<?php
/*
  $Id$

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

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

if (isset($HTTP_GET_VARS['products_id'])) {
	$orders_query = tep_db_query("select p.products_id, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, pd.products_description, p.products_tax_class_id from " . TABLE_ORDERS_PRODUCTS . " opa, " . TABLE_ORDERS_PRODUCTS . " opb, " . TABLE_ORDERS . " o, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p where opa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and opa.orders_id = opb.orders_id and opb.products_id != '" . (int)$HTTP_GET_VARS['products_id'] . "' and opb.products_id = p.products_id and opb.orders_id = o.orders_id AND opa.products_id = pd.products_id and p.products_status = '1' group by p.products_id order by o.date_purchased desc limit " . ALSO_PURCHASED_MODUL_MAX_DISPLAY);
	$num_products_ordered = tep_db_num_rows($orders_query);

	if ($num_products_ordered >= ALSO_PURCHASED_MODUL_MIN_DISPLAY) {
	  $row = 0;
	  $col = 0;
		$image_width = (ALSO_PURCHASED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH : ALSO_PURCHASED_MODUL_IMAGE_WIDTH;
		$image_height = (ALSO_PURCHASED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT : ALSO_PURCHASED_MODUL_IMAGE_HEIGHT;
		$image_width_row = (ALSO_PURCHASED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH_ROW : ALSO_PURCHASED_MODUL_IMAGE_WIDTH_ROW;
		$image_height_row = (ALSO_PURCHASED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT_ROW : ALSO_PURCHASED_MODUL_IMAGE_HEIGHT_ROW;
	  $col_items = (MAX_DISPLAY_ALSO_PURCHASED_MODUL_PER_ROW);
		if (MAX_DISPLAY_ALSO_PURCHASED_MODUL_PER_ROW  <= 1)	{
			$products_block_width = 100;
			$products_block_width .= '%';
			$view = 'r_view';			
			$equal_height_products_block = 'equal-height_also_pur_prods_block';
			$equal_height_products_name = 'equal-height_also_pur_prods_name';
			$equal_height_products_desc = 'equal-height_also_pur_prods_desc'; 
		}else{
			$products_block_width = ALSO_PURCHASED_MODUL_BLOCK_WIDTH.'px';
			$view = 't_view';
			$equal_height_products_block = 'equal-height_also_pur_prods_block';
			$equal_height_products_name = 'equal-height_also_pur_prods_name';
			$equal_height_products_desc = 'equal-height_also_pur_prods_desc'; 
		}
  	$also_pur_prods_content .= '<div class="content '.$view.'">';
		$also_pur_prods_content_row .= '<div class="content '.$view.'">';

	while ($orders = tep_db_fetch_array($orders_query)) {
		$also_pur_prods_content .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$also_pur_prods_content_row .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$also_pur_prods_content .= ($col === 0) ? '<ul class="row_also_pur_prods_name row_also_pur_prods_block" id="view2_grid-'.$row.'">' : '<li class="prods_vseparator '.$equal_height_products_block.'">'.tep_draw_separator('spacer.gif', '1', '1').'';
		$also_pur_prods_content_row .= ($col === 0) ? '<ul class="row_also_pur_prods_name row_also_pur_prods_block" id="view2_row-'.$row.'">' : '<li class="prods_vseparator">'.tep_draw_separator('spacer.gif', '1', '1').'';
	  $orders['products_name'] = tep_get_products_name($orders['products_id']); 
	  $p_id = $orders['products_id'];
  	$featured_query = tep_db_query("select status, products_id from " . TABLE_FEATURED . " where products_id = '" . (int)$orders['products_id'] . "'");
    $featured_product = tep_db_fetch_array($featured_query);
		$p_featured = ((ALSO_PURCHASED_MODUL_SHOW_FEATURED_LABEL == 'Show' && $featured_product['status']) ? '<div class="featured_label '.$color.'"><strong></strong></div>' : '');			
		if ($new_price = tep_get_products_special_price($orders['products_id'])) {
				$products_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' .  $currencies->display_price($orders['products_price'], tep_get_tax_rate($orders['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($orders['products_tax_class_id'])) . '</span></div>';
				$extra = 'extra';
				$persent = ($currencies->display_sale_percent($orders['products_price'], tep_get_tax_rate($orders['products_tax_class_id']), $new_price) * -1);
				$sale_percent = ((ALSO_PURCHASED_MODUL_SHOW_SPECIALS_LABEL == 'Show') ? '<div class="sale_percent '.$color.'"><strong>'.$persent.'%</strong></div>' : '');
		} else {
				$products_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><span class="productSpecialPrice">'.$currencies->display_price($orders['products_price'], tep_get_tax_rate($orders['products_tax_class_id']).'').'</span></div>';
				$extra = '';
				$sale_percent	= '';
    }
		$p_price = ((ALSO_PURCHASED_MODUL_SHOW_PRICE == 'Show') ? $products_price : '');
		$p_pic = ((ALSO_PURCHASED_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=' . $orders['products_id']) . '" style="width:'.$image_width.'px;height:'.$image_height.'px;">' . tep_image(DIR_WS_IMAGES . $orders['products_image'], $orders['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"') . '</a></div>' . "\n" : '');
		$p_pic_row = ((ALSO_PURCHASED_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=' . $orders['products_id']) . '" style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;">' . tep_image(DIR_WS_IMAGES . $orders['products_image'], $orders['products_name'], $image_width_row, $image_height_row, ' style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;"') . '</a></div>' . "\n" : '');
		$p_name =	((ALSO_PURCHASED_MODUL_SHOW_NAME == 'Show') ? '<h3 class="name '.$equal_height_products_name.'"><span><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $orders['products_id']) . '">' .$orders['products_name'] . '</a></span></h3>' : '');
		$p_desc = ((ALSO_PURCHASED_MODUL_MAX_DESCR != 0) ? '<div class="desc">'.mb_substr(strip_tags($orders['products_description']), 0, ALSO_PURCHASED_MODUL_MAX_DESCR, 'UTF-8').'...</div>' . "\n" : '');
		$p_desc_row = ((ALSO_PURCHASED_MODUL_MAX_DESCR_ROW != '') ? '<div class="desc">'.mb_substr(strip_tags($orders['products_description']), 0, ALSO_PURCHASED_MODUL_MAX_DESCR_ROW, 'UTF-8').'...</div>' . "\n" : '');
	  $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 = ((ALSO_PURCHASED_MODUL_SHOW_BUTTONS == 'Show') ?  '<div class="product_button_wrapper">'.$p_buy_now_text.' '.$p_details_text.'</div>'  : '');
// *************************************
		if ($p_name !='' || $p_price !='' || $button_block !='') {
			$wrapper_top = '<div class="product_info_wrapper">';
			$wrapper_bottom = '</div>';
		}else {
			$wrapper_top = '';
			$wrapper_bottom = '';
		}
   $also_pur_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";	
   $also_pur_prods_content_row .= ''. "\n".
					'<li class="responsive_block">'."\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-1) {
       $also_pur_prods_content .= '</ul>';
			 $also_pur_prods_content_row .= '</ul>';
	   	 $row ++;
       $col = 0;
    }else{
	   $also_pur_prods_content .= '</li>';	
		 $also_pur_prods_content_row .= '</li>';	
  	}
	}


  $also_pur_prods_content .= '</div>';
	$also_pur_prods_content_row .= '</div>';
?>
<div class="contentContainer modul_also_purchase">
<?php
echo ((ALSO_PURCHASED_MODUL_LYOUT =='Grid') ? '
	  <div id="tabs-new_grid1">
        <div class="contentBlock grid_view">'.$also_pur_prods_content.'</div>
	  </div>
<script type="text/javascript">
$(document).ready(function(){ 				
	var row_list_also_pur_prods_name = $(\'.row_also_pur_prods_name\');
		 row_list_also_pur_prods_name.each(function(){
			new equalHeights_also_pur_prods_name($(\'#\' + $(this).attr("id")));
	});			 			 			  			  			  			  			   
	 var row_list_also_pur_prods_block = $(\'.row_also_pur_prods_block\');
		 row_list_also_pur_prods_block.each(function(){
			new equalHeights_also_pur_prods_block($(\'#\' + $(this).attr("id")));
	});	
	var row_list_also_pur_prods_desc = $(\'.row_also_pur_prods_desc\');
		 row_list_also_pur_prods_desc.each(function(){
			new equalHeights_also_pur_prods_desc($(\'#\' + $(this).attr("id")));
	});	
});	
</script>
		' : '
	  <div id="tabs-new_row1">
        <div class="contentBlock row_view">'.$also_pur_prods_content_row.'</div>
	  </div>');
?>	  

</div>
<?php echo tep_draw_content_bottom();?> 
<?php
    }
  }
?>
