<?php
include("funcs.php");

//The PROD_TAG_PP gets pre-pended to every tag,
//in order to avoid accidentally using an existing
//CSS class name for a tag
$PROD_TAG_PP = "prod_tag_";


//Get a valid category, or else use the default
unset($prod_cat_id);
$test_c = $_REQUEST['c'];
if (is_numeric($test_c) && $test_c>0 && $test_c<9999) {
    //Lookup test_c to see if it's a real category
    $test_name=get_category_name($test_c);
    if ($test_name) {
        $prod_cat_id = $test_c;
        $prod_cat_name = $test_name;
    }

}
if (!$prod_cat_id) {
    //default to Eurorack modules
    $prod_cat_id = 5;
    $prod_cat_name = get_category_name($prod_cat_id);
}

$q="SELECT parent_id, link_url FROM product_categories WHERE id='$prod_cat_id'";
$r=db_query($q);
$s=db_fetch_array($r);
if (!$s['parent_id']) {
	header("Location: ".$s['link_url']);

}

$page="4ms: $prod_cat_name";
include("header.inc");

$q="SELECT overview_text FROM product_categories WHERE id='$prod_cat_id'";
$s = db_fetch_array(db_query($q));
$overview_text = $s['overview_text'];

//Extract product info and put it in $products array
$products = [];
$q="SELECT pp.job_t_id, pp.tags,
        pd.product_long, pd.product_abbr,
        ppics.pic_link,
        job_template.hi_price
        FROM product_placements pp
        JOIN product_descriptions pd ON pd.job_t_id=pp.job_t_id
        JOIN product_pics ppics ON ppics.job_t_id=pp.job_t_id AND display=1 AND img_order=1
        JOIN job_template ON job_template.id = pp.job_t_id
        WHERE pp.prod_cat_id='$prod_cat_id'
        ORDER BY pp.priority>0 DESC, pp.priority";
$r=db_query($q);
$all_unique_tags=array();
while ($s=db_fetch_array($r)){
    if ($s['job_t_id']>0){

        $name=$s['product_long'];
        $abbr=$s['product_abbr'];

        $products[$s['job_t_id']] = [];
        $products[$s['job_t_id']]['name'] = $name;

        //Pre-pend $PROD_TAG_PP to every tag
        $prod_tags = explode(" ", trim($s['tags']));
        $this_product_tags = "";
        foreach ($prod_tags as $t) {
            $tag = $PROD_TAG_PP.$t;
            $this_product_tags.=$tag." ";

            //Add unique tags to $tags array
            if (!in_array($tag, $all_unique_tags, TRUE))
                array_push($all_unique_tags, $tag);
        }
        $products[$s['job_t_id']]['product_abbr'] = "[ $abbr ]";
        $products[$s['job_t_id']]['tags'] = $this_product_tags;
        $products[$s['job_t_id']]['pic_link'] = $s['pic_link']."-420.jpg";
		if ($s['hi_price'] != round($s['hi_price'],0))
			$products[$s['job_t_id']]['price'] = pretty_number(round($s['hi_price'],2));
		else
			$products[$s['job_t_id']]['price'] = round($s['hi_price'],0);
    }
}


//Separate unique tags into numbered tags, and unnumbered tags.
//Order the former by number (alphabetical)
//Then combine them back into one array
$numbered_tags=array();
$unnumbered_tags=array();
foreach ($all_unique_tags as $prod_tag){
    $tag = substr($prod_tag,strlen($PROD_TAG_PP));

    if ((strpos($tag,"_")!==FALSE) && is_numeric(substr($tag,0,strpos($tag,"_"))))
        array_push($numbered_tags, $prod_tag);
    else
        array_push($unnumbered_tags, $prod_tag);
}
sort($numbered_tags);
$all_unique_tags = array_merge($numbered_tags, $unnumbered_tags);
?>

<body>

<div id="content">


<div class="gallerytitle1"><?php echo $prod_cat_name;?>:</div>

	<div class="side-nav-wrapper">
        <ul>
            <li class="gallerytitle"><div class="gallerytitle"><span class="pgtitle"><?php echo $prod_cat_name;?>:</span></div></li>
            <?php
            foreach ($all_unique_tags as $prod_tag){
                $tag = substr($prod_tag,strlen($PROD_TAG_PP));
                if (is_numeric(substr($tag,0,strpos($tag,"_"))))
                    $tag=substr($tag,strpos($tag,"_")+1);
                ?>
                <li><span class="submenu"><a data-target="<?php echo $prod_tag;?>"><?php echo str_replace('_',' ',$tag);?></a></span></li>
                <?php
            }
            ?>
            <li><span class="submenu"><a data-target="gallery_item_base">All <?php echo $prod_cat_name;?></a></span></li>

        </ul>
		<script>
        $('.side-nav-wrapper ul li a').click(function() {
          var target = $(this).data('target');
          $('.gallery_item_base').hide();
          $('.'+target).show();
		  if (target=='gallery_item_base')
		  	$('.galleryinfo').show();
		  else $('.galleryinfo').hide();
        });
        </script>
	</div>






<div class="galleryspace"><span class="caption">
<?php echo $overview_text;?>
</span></div>

<div class="productgallery">
	<div class="tab-content-wrapper">
        <div class="gallery">
        <?php

        if (is_array($products)){
            foreach ($products as $product_id=>$product){
                ?>
                <div class="wide gallery_item_base <?php echo $product['tags'];?>">
                        <a href="p.php?p=<?php echo $product_id;?>&c=<?php echo $prod_cat_id;?>">
                            <img src="<?php echo $product['pic_link'];?>" >
                            <br>
                            <span class="product"><?php echo $product['name'];?></span>
                            <br>
                            <span class="galprice1"><?php echo $product['product_abbr'];?></span>
                            <br>
                            <span class="galprice">$<?php echo $product['price'];?></span>
                        </a>
                            </p>

                </div>
                <?php
            }
        }
        ?>
        </div>
	</div>
</div>

<br class="clear-both" />

	<a id="homebutton">&#8722;&nbsp;&nbsp;&nbsp;</a>
    <script>
        var btn = $('#homebutton');
        $(window).scroll(function() {
          if ($(window).scrollTop() > 300) {
            btn.addClass('show');
          } else {
            btn.removeClass('show');
          }
        });
        btn.on('click', function(e) {
          e.preventDefault();
          $('html, body').animate({scrollTop:0}, '300');
        });
    </script>

</div>







<?php include("footer.inc");?>



</body>
