<?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');
  if (!isset($HTTP_GET_VARS['products_id'])) {
    tep_redirect(tep_href_link(FILENAME_DEFAULT));
  }
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);
  $current_page = FILENAME_PRODUCT_INFO;
  $product_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
  $product_check = tep_db_fetch_array($product_check_query);

  require(DIR_WS_INCLUDES . 'template_top.php');
?>
<?php echo tep_draw_content_top();?>
<?php
  if ($product_check['total'] < 1) {
?>
<?php echo tep_draw_title_top();?>
<h2><?php echo TEXT_PRODUCT_NOT_FOUND; ?></h2>
<?php echo tep_draw_title_bottom();?>

<div class="contentContainer">
  <div class="contentBlock">
      <div class="buttonSet">
        	<span class="fl_right"><?php echo tep_draw_button1_top();?><?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', tep_href_link(FILENAME_DEFAULT)); ?><?php echo tep_draw_button1_bottom();?></span>
      </div>
</div>
</div>
<?php
  } else {
    $product_info_query = tep_db_query("select p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1'
		 
		and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' 
		and pd.products_id = p.products_id 
		and pd.language_id = '" . (int)$languages_id . "'");
		
    $product_info = tep_db_fetch_array($product_info_query);
		$tags_query_raw = tep_db_query("select pt.tag_id, t.tag_text from " . TABLE_TAGS . " t, " . TABLE_PRODUCTS_TAGS . " pt where pt.products_id = '" . (int)$product_info['products_id'] . "' and t.tag_id = pt.tag_id and t.language_id = '" . (int)$languages_id . "'");
		$tags = '';
		$caount = 0;
      while ($tag = tep_db_fetch_array($tags_query_raw)) {
				if ($caount != 0) $tags .= ' ';
				$tags .= '<li><a href="' . tep_href_link(FILENAME_TAG_PRODUCTS) .'?id_tag='.$tag['tag_id'].'">'.$tag['tag_text'].'</a></li>';
				$caount++;
			}
    tep_db_query("update " . TABLE_PRODUCTS_DESCRIPTION . " set products_viewed = products_viewed+1 where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and language_id = '" . (int)$languages_id . "'");

    if ($new_price = tep_get_products_special_price($product_info['products_id'])) {
      $products_price = '<del>' . $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate(					$product_info['products_tax_class_id'])) . '</span>';
			$persent = ($currencies->display_sale_percent($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']), $new_price) * -1);
			$sale_percent = ((REVIEWS_PAGE_SHOW_SPECIALS_LABEL == 'Show') ? '<div class="sale_percent '.$color.'"><strong>'.$persent.'%</strong></div>' : '');
    } else {
      $products_price = '<span class="productSpecialPrice">' . $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';
				$sale_percent = '';
    }
    if (tep_not_null($product_info['products_model'])) {
		
	  
      $products_name = $product_info['products_name'] . '<br /><span class="smallText">[' . $product_info['products_model'] . ']</span>';
    } else {
      $products_name = $product_info['products_name'];
    }
	


	
  	$featured_query = tep_db_query("select status, products_id from " . TABLE_FEATURED . " where products_id = '" . (int)$product_info['products_id'] . "'");
    $featured_product = tep_db_fetch_array($featured_query);
		$p_featured = ((REVIEWS_PAGE_SHOW_FEATURED_LABEL == 'Show' && $featured_product['status']) ? '<div class="featured_label '.$color.'"><strong></strong></div>' : '');
		echo tep_draw_form('cart_quantity', tep_href_link(FILENAME_PRODUCT_INFO, tep_get_all_get_params(array('action')) . 'action=add_product')); 
?>
<div class="contentContainer page_products_info">
  <div class="contentBlock">
      <ol id="product_info">
          			<li class="col_10">
<?php
		if (tep_not_null($product_info['products_image'])) {
			$pi_query = tep_db_query("select image, htmlcontent from " . TABLE_PRODUCTS_IMAGES . " where products_id = '" . (int)$product_info['products_id'] . "' order by sort_order");
					if (tep_db_num_rows($pi_query) > 0) {
?>
				<div id="piGal">
						<ul class="relative">
				<?php 
				$pi_counter = 0;
				while ($pi = tep_db_fetch_array($pi_query)) {
					$pi_counter++;
					$pi_entry = '        <li class="wrapper_pic_div">
											'.$p_featured . "\n".
								'			'.$sale_percent . "\n".
					
					'<a class="product_pic" style="height:'.PROD_INFO_IMAGE_HEIGHT.'px" href="';
					$pi_entry .= tep_href_link(DIR_WS_IMAGES . $pi['image']);
					$pi_entry .= '" target="_blank" data-rel="fancybox" title="' . $pi['htmlcontent'] . '">' . tep_image(DIR_WS_IMAGES . $pi['image'], $pi['htmlcontent'], (PROD_INFO_IMAGE_WIDTH), (PROD_INFO_IMAGE_HEIGHT), '', ' class="img"') . '<div class="pic_zoom" style="width:'.(PROD_INFO_IMAGE_WIDTH).'px;height:'.(PROD_INFO_IMAGE_HEIGHT).'px;"></div></a>';
					$pi_entry .= '</li>';
					echo $pi_entry;
				}
?>
						</ul>
				</div>
<?php
	$coeff =((PROD_INFO_IMAGE_WIDTH != PROD_INFO_IMAGE_HEIGHT) ? PROD_INFO_IMAGE_HEIGHT / PROD_INFO_IMAGE_WIDTH  : 1);
?>      
		<script type="text/javascript">
		$(function(){
	var myWidth = <?php echo (($pi_counter > 1) ? '68' : '0'); ?>;
	var myHeight = myWidth * <?php echo $coeff;?>;
		$('#piGal ul').bxGallery({
			maxwidth: '<?php echo (PROD_INFO_IMAGE_WIDTH); ?>',
			maxheight: '<?php echo (''); ?>',
			thumbwidth: myWidth,
			thumbheight: myHeight,
			thumbcontainer: <?php echo (PROD_INFO_IMAGE_WIDTH + 5); ?>,
			load_image: 'ext/jquery/bxGallery/spinner.gif'
		})
				});
		</script>
<?php
					} else {
				if (tep_not_null($product_info['products_image'])) {
?>
					<div class="hover">
					<?php echo '<div id="piGal" class="wrapper_pic_div fl_left" style="width:'.(PROD_INFO_IMAGE_WIDTH +10).'px;"><a class="product_pic" style="height:'.PROD_INFO_IMAGE_HEIGHT.'px;" href="' . tep_href_link(DIR_WS_IMAGES . $product_info['products_image']) . '" target="_blank" data-rel="fancybox">' . tep_image(DIR_WS_IMAGES . $product_info['products_image'], addslashes($product_info['products_name']), (PROD_INFO_IMAGE_WIDTH), (PROD_INFO_IMAGE_HEIGHT), ''). '<div class="pic_zoom" style="width:'.(PROD_INFO_IMAGE_WIDTH).'px;height:'.(PROD_INFO_IMAGE_HEIGHT).'px;"></div></a></div>'; ?>
					</div>
<?php
				}
		}				  
		echo '<script type="text/javascript">
		$("#piGal a[data-rel^=\'fancybox\']").fancybox({
			cyclic: true
		});
		</script>';
				}
						
?>
							</li>
							<li class="col_11">
									<div class="info">
<?php
	 if ($product_check['total'] >= 1) {
			include (DIR_WS_INCLUDES . 'products_next_previous.php');
	 }
	 if (($oscTemplate->hasBlocks('box_info_page')))	{ $width_ext = PROD_INFO_IMAGE_WIDTH;?><div class="bookmarks fl_right"><?php echo $oscTemplate->getBlocks('box_info_page');?></div><?php	}
?>

<div>
<?php	 
				if ($product_info['products_date_available'] > date('Y-m-d H:i:s')) {
?>
												<div class="data data_padd small_title"><?php echo sprintf(TEXT_DATE_AVAILABLE, tep_date_long($product_info['products_date_available'])); ?></div>
<?php
				}else{
?>		
												<div class="data data_padd small_title"><?php echo sprintf(TEXT_DATE_ADDED, tep_date_long($product_info['products_date_added'])); ?></div>
<?php } ?>
												<h1 class="name"><?php echo $products_name; ?></h1>
												<div class="price"><?php echo '<b>'.PRICE. '</b>'.$products_price; ?></div>
<?php
				$products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'");
				$products_attributes = tep_db_fetch_array($products_attributes_query);


// START: Extra Fields Contribution v2.0b - mintpeel display fix
		  
                      $extra_fields_query = tep_db_query("
SELECT pef.products_extra_fields_order, pef.products_extra_fields_status as status, pef.products_extra_fields_name as name, ptf.products_extra_fields_value as value
FROM ". TABLE_PRODUCTS_EXTRA_FIELDS ." pef
LEFT JOIN ". TABLE_PRODUCTS_TO_PRODUCTS_EXTRA_FIELDS ." ptf
ON ptf.products_extra_fields_id=pef.products_extra_fields_id
WHERE ptf.products_id=". (int)$HTTP_GET_VARS['products_id'] ." and ptf.products_extra_fields_value<>'' and (pef.languages_id='0' or pef.languages_id='".(int)$languages_id."')
ORDER BY pef.products_extra_fields_order");


        echo '
	  <div class="extra_fields options"><ul>';
  while ($extra_fields = tep_db_fetch_array($extra_fields_query)) {
        if (! $extra_fields['status'])  // show only enabled extra field
           continue;
     echo'<li class="main"><b>'.$extra_fields['name'].': </b>' . stripslashes($extra_fields['value']).'</li>';
  }
     echo'</ul></div>'; 
       // END: Extra Fields Contribution - mintpeel display fix
                                       
				
				if ($products_attributes['total'] > 0) {
?>
										<div class="options">
												<p class="options-title"><?php echo TEXT_PRODUCT_OPTIONS; ?></p>
<?php
				
?>												
                                                <ul class="ofh">
<?php
											$products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' order by popt.products_options_name");
											while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {
												$products_options_array = array();
												$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");
												while ($products_options = tep_db_fetch_array($products_options_query)) {
													$products_options_array[] = array('id' => $products_options['products_options_values_id'], 'text' => $products_options['products_options_values_name']);
													if ($products_options['options_values_price'] != '0') {
														$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options['price_prefix'] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) .') ';
													}
												}
												if (is_string($HTTP_GET_VARS['products_id']) && isset($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']])) {
													$selected_attribute = $cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name['products_options_id']];
												} else {
													$selected_attribute = false;
												}
?>
														<li class="fl_left"><label><?php echo $products_options_name['products_options_name'] . ':'; ?></label><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute); ?></li>
<?php
					}
?>
														</ul>
												</div>
<?php } ?>
</div>
									
<?php
				$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and reviews_status = 1");
				$reviews = tep_db_fetch_array($reviews_query);
?>
		
												<div class="buttonSet">
														<span class="buttonAction"><?php echo tep_draw_button1_top();?><?php echo tep_draw_hidden_field('products_id', $product_info['products_id']) . tep_draw_button(IMAGE_BUTTON_IN_CART, 'cart', null, 'primary'); ?><?php echo tep_draw_button1_bottom();?></span>
								
														<div class="fl_left"><?php echo tep_draw_button2_top();?><?php echo tep_draw_button(IMAGE_BUTTON_REVIEWS . (($reviews['count'] > 0) ? ' (' . $reviews['count'] . ')' : ''), 'comment', tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params())); ?><?php echo tep_draw_button2_bottom();?></div>
												</div>
									</div> 
							</li>
		</ol>
        
    </div>
</div></form>


<?php echo tep_draw_content_bottom();?>
<?php
		$title_also_purchas =  false;
		if	(ALSO_PURCHASED_MODUL_DISPLAY == 'Show')	{
				if ((USE_CACHE == 'true') && empty($SID)) {
					echo tep_cache_also_purchased(3600);
				} else {
if (isset($HTTP_GET_VARS['products_id'])) {
	$orders_query2 = 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_ordered2 = tep_db_num_rows($orders_query2);

	if ($num_products_ordered2 >= ALSO_PURCHASED_MODUL_MIN_DISPLAY) {
					
					
			$title_also_purchas =  true;
				}
	}}
		}else{
			$title_also_purchas =  false;
		}
		

/* *********************************** */
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);
			$title_related = ((mysql_num_rows($attribute_query)>0) ? 1 : 0);	
		}
/* *********************************** */			  
	  if (isset($HTTP_GET_VARS['products_id'])) {
        $random2_select_id = " and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'";
      }
		$random2_select_rid = " ORDER BY RAND() limit " . (REVIEWS_BOX_MULTY_MAX_DISPLAY);
		$random2_select = tep_db_query("select r.reviews_id, r.reviews_rating, r.date_added, p.products_id, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price, r.customers_name 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". $random2_select_id . $random2_select_pid . $random2_select_rid);
      
		$random2_product = tep_db_fetch_array($random2_select);
		$title_relviews = (($random2_product) ? true : false);
		
/* *********************************** */		
		if	((ALSO_PURCHASED_MODUL_DISPLAY == 'Show' && $title_also_purchas) || ($title_related) || ($tags != '') || ($product_info['products_description'] != ''))	{
	echo '<div id="tabvanilla_pinfo" 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 ($product_info['products_description'] != '')	{ echo '
		<li><a href="#description"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TEXT_DESCRIPTION.'</strong></strong></a></li>';
		}
		if ($title_also_purchas == true)	{ echo '
				<li><a href="#also_purchase"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TEXT_ALSO_PURCHASED_PRODUCTS.'</strong></strong></a></li>';
		}
		if ($title_related)	{ echo '
				<li><a href="#related"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TEXT_RELATED_PRODUCTS.'</strong></strong></a></li>';
		}
		if ($oscTemplate->hasBlocks('box_toogle_pos_22') && $title_relviews)	{ echo '
				<li><a href="#box_toogle_pos_22"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.MODULE_BOXES_MULTY_REVIEWS_BOX_TOOGLE.'</strong></strong></a></li>';
		}
		if ($tags != '')	{ echo '
				<li><a href="#tags"><strong class="title_wrapper"><strong class="title_inner"><span class="title-icon"></span>'.TAGS_PRODUCT.'</strong></strong></a></li>';
		}																				  
echo '</ul>';
		if ($product_info['products_description'] != '')	{ echo '
		<div id="description"><div class="description">'.stripslashes($product_info['products_description']).'</div></div>';
		}

		if	(ALSO_PURCHASED_MODUL_DISPLAY == 'Show')	{
				if ((USE_CACHE == 'true') && empty($SID)) {
					echo tep_cache_also_purchased(3600);
				} else {
			echo '<div id="also_purchase" class="tabdiv">';			
			echo tep_draw_fpage_content_top();
			include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);
			echo tep_draw_fpage_content_bottom();
			echo '</div>';
				}
		}
//Optional Related Products (ORP)			
		if	($title_related)	{
			echo '<div id="related" class="tabdiv">';
			echo tep_draw_fpage_content_top();
			include(DIR_WS_MODULES . FILENAME_RELATED_PRODUCTS); 
			echo tep_draw_fpage_content_bottom();
			echo '</div>';
		}
//ORP: end	
		if ($oscTemplate->hasBlocks('box_toogle_pos_22') && $title_relviews)	{
			echo '<div id="box_toogle_pos_22" class="tabdiv">'.$oscTemplate->getBlocks('box_toogle_pos_22').'</div>';
		} 
		if	($tags != '')	{
			echo '<div id="tags" class="tabdiv infoBoxContainer">';
			echo tep_draw_fpage_content_top().'<div class="tags"><div class="infoBoxContents"><ul>'.$tags.'</ul></div></div>';
			echo tep_draw_fpage_content_bottom();
			echo '</div>';
		}					
		echo '</div>';	
	}
  }
?>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>