/**
  * get products based on filter (family, main element, symbol, color, and brand)
  * @param Int $product_id Products ID
  * @return Array Array of products ID
  */
 function getProductsCrossSelling($product_id)
 {
     use_class('products_brand');
     use_class('styles_php4');
     use_class('products_minierp');
     use_class('products_family_php4');
     $class_s = new styles();
     $class_pb = new products_brand();
     $class_pm = new products_minierp();
     $max_products = 50;
     $max_products_by_family = 20;
     $max_products_by_main_element = 10;
     $max_products_by_symbol = 30;
     //            $max_products_by_color = 5;
     //            $max_products_by_brand = 5;
     $max_products_by_category = 40;
     $pids = array();
     $p_data = $class_pm->retrieveDetail($product_id, 'p,cat');
     //FAMILY
     $family_id = $p_data['p']['family_id'];
     $obj_family = new products_family(null, $product_id);
     $pid_family = $obj_family->retrieveMembers();
     $pid_family_exclude_kill_prod = $class_pm->filterExcludeKilledProducts($pid_family);
     $pid_similar = $obj_family->getSimilarProducts($product_id, false, false);
     $pids = array_merge($pid_family_exclude_kill_prod, $pid_similar);
     $key = array_search($product_id, $pids);
     unset($pids[$key]);
     $pids = array_slice($pids, 0, 20);
     //MAIN ELEMENT
     $pid_family_filter = implode(",", $pids);
     if ($pid_family_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_family_filters = "AND pue.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_family_filters = "AND pue.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_family_filters = "AND pue.products_id NOT IN ({$pid_family_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_family_filters = "AND pue.products_id NOT IN ({$pid_family_filter}, {$product_id})";
         }
     }
     $main_element_id = $class_pm->getMainElement($product_id);
     if ($main_element_id != NULL) {
         $pid_main_element = $class_pm->getProductsByMainElement($main_element_id, false, $pid_family_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_main_element}");
         $pids = array_merge($pids, $pid_main_element);
     }
     //SYMBOL
     $symbol_id = $class_s->getProductStyle($product_id);
     $pid_main_element_filter = implode(",", $pids);
     if ($pid_main_element_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$pid_main_element_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$pid_main_element_filter}, {$product_id})";
         }
     }
     if ($symbol_id['Symbol'] != '') {
         $pid_symbol = array_keys($class_s->retrieveProducts($symbol_id['Symbol'], false, $pid_main_element_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_symbol}"));
         $pids = array_merge($pids, $pid_symbol);
     }
     //COLOR
     //            $color_id = $class_pm->loadColorPattern($product_id);
     //            if ($color_id) {
     //                $pids_symbol_filter = implode(",", $pids);
     //                if ($pids_symbol_filter == '') {
     //                    $pid_symbol_filters = "AND ptcp.products_id NOT IN ($product_id)";
     //                } else {
     //                    $pid_symbol_filters = "AND ptcp.products_id NOT IN ($pids_symbol_filter, $product_id)";
     //                }
     //                $pid_color = $class_pm->getProductsByColor(
     //                        $color_id[0],
     //                        false,
     //                        $pid_symbol_filters,
     //                        "ORDER BY pl.sold_monthly_3 DESC LIMIT $max_products_by_color");
     //                $pids = array_merge($pids, $pid_color);
     //            }
     //BRAND
     //            $brand_id = $p_data['p']['products_brand_id'];
     //            if ($brand_id) {
     //                $pids_color_filter = implode(",", $pids);
     //                if ($pids_color_filter == '') {
     //                    $pid_color_filters = "AND p.products_id NOT IN ($product_id)";
     //                } else {
     //                    $pid_color_filters = "AND p.products_id NOT IN ($pids_color_filter, $product_id)";
     //                }
     //                $pid_brand = $class_pb->getProducts(
     //                        $brand_id,
     //                        false,
     //                        $pid_color_filters,
     //                        "ORDER BY pl.sold_monthly_3 DESC LIMIT $max_products_by_brand");
     //                $pids = array_merge($pids, $pid_brand);
     //            }
     //CATEGORY
     $cat_id = $p_data['categories_id'];
     $pids_color_filter = implode(",", $pids);
     if ($pids_color_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_color_filters = "AND p.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_color_filters = "AND p.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_color_filters = "AND p.products_id NOT IN ({$pids_color_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_color_filters = "AND p.products_id NOT IN ({$pids_color_filter}, {$product_id})";
         }
     }
     $pid_cat = $class_pm->getProductsByCategory($cat_id, false, $pid_color_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_category}");
     $pids = array_merge($pids, $pid_cat);
     //LIMIT ALL PRODUCTS BY 50
     $pids = array_slice($pids, 0, $max_products);
     $pids_implode = implode(",", $pids);
     if ($p_data['p']['products_brand_id'] == 14) {
         $pids = $class_pb->getProducts($p_data['p']['products_brand_id'], false, "AND p.products_id IN ({$pids_implode})", "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products}");
     }
     return $pids;
 }
    if ($family_id != '') {
        $q .= " AND (p.family_id != '$family_id'  OR IFNULL(p.family_id,'') = '')";
    }
    $dbq = tep_db_query($q);
    while ($row = tep_db_fetch_array($dbq)) {
        $p_fid = $row['family_id'];                    
        $pid_similar = $row['products_id'];
        if ($p_fid == '') {
            $similar_products[] = "#box-pids-similar|$pid_similar";
        } else {
            $similar_products[] = "#box-pids-similar|$p_fid|$pid_similar";
        }
    }
    * 
    */
 $get_similar_products = $obj_family->getSimilarProducts($pid, true);
 foreach ($get_similar_products as $sp_fid => $sp_pids) {
     foreach ($sp_pids as $sp_pid) {
         if ($sp_fid == '') {
             $similar_products[] = "#box-pids-similar|{$sp_pid}";
         } else {
             $similar_products[] = "#box-pids-similar|{$sp_fid}|{$sp_pid}";
         }
     }
 }
 /*DISABLED THIS, instead of construct search key on the fly, we decide to safe it to table to reduce query time and process*/
 //                $elements_used = $class_pm->retrieveElementsUsage($pid);
 //                ksort($elements_used);
 //
 //                $group_of_ref_elements = array();
 //                $composition_keys = array();