<?
	header("Content-Type: application/json");
	
	include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	include ('includes/admin-config.php');	//get site options
	
	// the JOIN isn't necessary here, but could be useful in the future
	$parent_cat_result = mysql_query("SELECT 
									c.recordKey, 
									c.categoryName, 
									c.category_type, 
									c.parent_cat_num, 
									p.categoryName as parent_categoryName
							  FROM productCategory c
							  LEFT OUTER JOIN productCategory p
								   on p.recordKey = c.parent_cat_num
							  ORDER BY p.categoryName DESC
							  ");
	
	// recordID->recordID (child->parent)
	$parent_cat_map = array();
	
	// recordID->name
	$cat_names_map = array();
	
	if ($parent_cat_result) {
		while ($myarr = mysql_fetch_array($parent_cat_result)) {
			$parent_cat_map[$myarr[recordKey]] = $myarr[parent_cat_num];
			$cat_names_map[$myarr[recordKey]] = $myarr[categoryName];
		}
	}
	
	$result = mysql_query("select categoryKey, imagePath, thumbnail_path from products  where imagePath <> '' and thumbnail_path <> ''");
	
	$images_by_category = array();
	if ($result) {
		while ($arr = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if (!array_key_exists($arr[categoryKey], $images_by_category)) {
				$images_by_category[$arr[categoryKey]] = array();
			}
			$imagedata = array(
				"normal" => $arr[imagePath],
				"thumb" => $arr[thumbnail_path]
			);
			array_push($images_by_category[$arr[categoryKey]], $imagedata);
		}
	}
	
	$result_data = array(
		"category_parents" => $parent_cat_map,
		"category_names" => $cat_names_map,
		"category_images" => $images_by_category
	);
	
	echo json_encode($result_data);
?>
