<?php

/*
  $Id: optional_related_products.php, ver 1.0 02/05/2007 Exp $

  Copyright (c) 2007 Anita Cross (http://www.callofthewildphoto.com/)

  Part of Contribution: Optional Related Products Ver 4.0

  Based on code from Optional Relate Products, ver 2.0 05/01/2005
  Copyright (c) 2004-2005 Daniel Bahna (daniel.bahna@gmail.com)

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

  Released under the GNU General Public License
*/
if	(defined('RELATED_PRODUCTS_MAX_DISP'))	{
  $orderBy = 'ORDER BY ';
  $orderBy .= (RELATED_PRODUCTS_RANDOMIZE)?'rand()':'pop_order_id, pop_id';
  $orderBy .= (RELATED_PRODUCTS_MAX_DISP)?' limit ' . RELATED_PRODUCTS_MAX_DISP:'';
  $attributes = "
         SELECT
         pop_products_id_slave,
         products_name,
				 pa.products_description,
				 products_price,
         products_model,
         products_price,
         products_quantity,
         products_tax_class_id,
         products_image
         FROM " .
         TABLE_PRODUCTS_RELATED_PRODUCTS . ", " .
         TABLE_PRODUCTS_DESCRIPTION . " pa, ".
         TABLE_PRODUCTS . " pb
         WHERE pop_products_id_slave = pa.products_id
         AND pa.products_id = pb.products_id
         AND language_id = '" . (int)$languages_id . "'
         AND pop_products_id_master = '".$HTTP_GET_VARS['products_id']."'
         AND products_status='1' " . $orderBy;
  			 
 		$attribute_query = tep_db_query($attributes);

	  $row = 0;
	  $col = 0;
	$image_width = (RELATED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH : RELATED_MODUL_IMAGE_WIDTH;
	$image_height = (RELATED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT : RELATED_MODUL_IMAGE_HEIGHT;
	$image_width_row = (RELATED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_WIDTH_ROW : RELATED_MODUL_IMAGE_WIDTH_ROW;
	$image_height_row = (RELATED_MODUL_GENERAL_CUSTOM_IMAGE_SIZE == 'General') ? GENERAL_IMAGE_HEIGHT_ROW : RELATED_MODUL_IMAGE_HEIGHT_ROW;
	  $col_items = (RELATED_PRODUCTS_PER_ROW);
		if (RELATED_PRODUCTS_PER_ROW  <= 1)	{
			$products_block_width = 100;
			$products_block_width .= '%';
			$view = 'r_view';
			$equal_height_products_block = 'equal-height_slave_block';
			$equal_height_products_name = 'equal-height_slave_name';
		}else{
			$products_block_width = RELATED_PRODUCTS_BLOCK_WIDTH.'px';
			$view = 't_view';
			$equal_height_products_block = 'equal-height_slave_block';
			$equal_height_products_name = 'equal-height_slave_name';
		}
		if (mysql_num_rows($attribute_query)>0) {			
  	$slave_prods_content .= '<div class="content '.$view.'">';
  	$slave_prods_content_row .= '<div class="content '.$view.'">';		
    while ($attributes_values = tep_db_fetch_array($attribute_query)) {
		$slave_prods_content .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$slave_prods_content_row .= (($col === 0) && ($row != 0)) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '';
		$slave_prods_content .= ($col === 0) ? '<ul class="row_slave_name row_slave_block" id="view_grid-'.$row.'">' : '<li class="prods_vseparator '.$equal_height_products_block.'">'.tep_draw_separator('spacer.gif', '1', '1').'';
		$slave_prods_content_row .= ($col === 0) ? '<ul class="row_slave_name row_slave_block" id="view_row-'.$row.'">' : '<li class="prods_vseparator">'.tep_draw_separator('spacer.gif', '1', '1').'';
	  $p_id = '' . $attributes_values['pop_products_id_slave'] . '';		
// *************************************
  	$featured_query = tep_db_query("select status, products_id from " . TABLE_FEATURED . " where products_id = '" . (int)$attributes_values['pop_products_id_slave'] . "'");
    $featured_product = tep_db_fetch_array($featured_query);
		$p_featured = ((RELATED_MODUL_SHOW_FEATURED_LABEL == 'Show' && $featured_product['status']) ? '<div class="featured_label '.$color.'"><strong></strong></div>' : '');			

		if ($new_price = tep_get_products_special_price($p_id)) {
						$products_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' . $currencies->display_price($attributes_values['products_price'], tep_get_tax_rate($attributes_values['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($attributes_values['products_tax_class_id'])) . '</span></div>';
						$extra = 'extra';
						$persent = ($currencies->display_sale_percent($attributes_values['products_price'], tep_get_tax_rate($attributes_values['products_tax_class_id']), $new_price) * -1);
						$sale_percent = ((RELATED_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($attributes_values['products_price'], tep_get_tax_rate($attributes_values['products_tax_class_id']).'').'</span></div>';
						$extra = '';
						$sale_percent	= '';
    }
		$p_price = ((RELATED_PRODUCTS_SHOW_PRICE == 'True') ? $products_price : '');
		$p_pic = ((RELATED_PRODUCTS_SHOW_THUMBS == 'True') ? '<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=' . $p_id) . '" style="width:'.$image_width.'px;height:'.$image_height.'px;">' . tep_image(DIR_WS_IMAGES . $attributes_values['products_image'], $attributes_values['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"') . '</a></div>' . "\n" : '');
		$p_pic_row = ((RELATED_PRODUCTS_SHOW_THUMBS == 'True') ? '<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=' . $p_id) . '" style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;">' . tep_image(DIR_WS_IMAGES . $attributes_values['products_image'], $attributes_values['products_name'], $image_width_row, $image_height_row, ' style="width:'.$image_width_row.'px;height:'.$image_height_row.'px;"') . '</a></div>' . "\n" : '');
		$p_name = ((RELATED_PRODUCTS_SHOW_NAME == 'True') ? '<h3 class="name '.$equal_height_products_name.'"><span><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $p_id) . '">' . $attributes_values['products_name'] . '</a></span></h3>' . "\n" : '');
		$quantity = ((RELATED_PRODUCTS_SHOW_QUANTITY == 'True') ? '<div class="data"><span>' . sprintf(RELATED_PRODUCTS_QUANTITY_TEXT,  $attributes_values['products_quantity']) . '</span></div>' . "\n" : '');
		$p_model = ((RELATED_PRODUCTS_SHOW_MODEL == 'True') ? '<div class="desc">' . $attributes_values['products_model'] . '</div>' . "\n" : '');
		$p_desc = ((RELATED_PRODUCTS_SHOW_DESCRIPTION != 0) ? '<div class="desc">'.mb_substr(strip_tags($attributes_values['products_description']), 0, RELATED_PRODUCTS_SHOW_DESCRIPTION, 'UTF-8').'...</div>' . "\n" : '');
		$p_desc_row = ((RELATED_PRODUCTS_SHOW_DESCRIPTION_ROW != 0) ? '<div class="desc">'.mb_substr(strip_tags($attributes_values['products_description']), 0, RELATED_PRODUCTS_SHOW_DESCRIPTION_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 = ((RELATED_PRODUCTS_SHOW_BUY_NOW == 'True') ?  '<div class="product_button_wrapper">'.$p_buy_now_text.' '.$p_details_text.'</div>'  : '');
// *************************************
		if ($p_name !='' || $p_desc !='' || $p_price !='' || $p_buttons_text !='' || $p_model !='' || $button_block !='') {
			$wrapper_top = '<div class="product_info_wrapper">';
			$wrapper_bottom = '</div>';
		}else {
			$wrapper_top = '';
			$wrapper_bottom = '';
		}
   $slave_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_model.''. "\n".	
					'					'.$quantity.''. "\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";	
							  
   $slave_prods_content_row .= ''. "\n".
					'<li class="responsive_block">'."\n".
				  '		<div class="product_block '.$equal_height_products_block.'">'. "\n".
					'			'.$p_pic_row.''. "\n". 
					'			'.$p_featured . "\n".
					'			'.$sale_percent . "\n".
          '			'. "\n".
				  '			'.$wrapper_top.''."\n".
		//			'   			'.$p_data.''. "\n".
					'					'.$quantity.''. "\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_model.''. "\n".	
					'					'.$p_desc_row.''. "\n".	
					'				</div>'. "\n".	
				  '		'.$wrapper_bottom.''."\n".			  
				  '		</div>'. "\n";			  
    $col ++;
    if ($col > $col_items-1) {
       $slave_prods_content .= '</ul>';
			 $slave_prods_content_row .= '</ul>';
	   $row ++;
       $col = 0;
    }else{
	   $slave_prods_content .= '</li>';	
		 $slave_prods_content_row .= '</li>';	
		}
	}
  $slave_prods_content .= '</ul></div>';
	$slave_prods_content_row .= '</ul></div>';
?>
<div class="contentContainer module_related">
<?php
echo ((RELATED_MODUL_LYOUT =='Grid') ? '
	  <div id="tabs-new_grid">
        <div class="contentBlock grid_view">'.$slave_prods_content.'</div>
	  </div>
<script type="text/javascript">
$(document).ready(function(){ 				
	var row_list_slave_name = $(\'.row_slave_name\');
		 row_list_slave_name.each(function(){
			new equalHeights_slave_name($(\'#\' + $(this).attr("id")));
	});			 			 			  			  			  			  			   
	 var row_list_slave_block = $(\'.row_slave_block\');
		 row_list_slave_block.each(function(){
			new equalHeights_slave_block($(\'#\' + $(this).attr("id")));
	});	
});	
</script>
		' : '
	  <div id="tabs-new_row">
        <div class="contentBlock row_view">'.$slave_prods_content_row.'</div>
	  </div>');
?>	  
</div>
<?php echo tep_draw_content_bottom();?>
<?php } ?>
<?php } ?>