<?php
do
{
	if (!$vbulletin->userinfo['userid'])
	{
		// No guests
		break;
	}

	if ($thread['postuserid'] != $vbulletin->userinfo['userid'])
	{
		// No guests
		break;
	}

	$itemIds = array();
	foreach (VBSHOP::$cache['item'] as $itemid => $item)
	{
		if (!$item['active'])
		{
			// Skip this
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $item['permissions']))
		{
			// This item isn't to be displayed
			continue;
		}

		if (!$category = VBSHOP::$cache['category'][$item['categoryid']])
		{
			// This category isn't to be displayed
			continue;
		}
		
		if (!$category['active'])
		{
			// This category isn't to be displayed
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $category['permissions']))
		{
			// This category isn't to be displayed
			continue;
		}

		if ($item['threadpage'])
		{
			// We can buy this on the thread page
			$itemIds[$itemid] = $itemid;
		}
	}

	if (!count($itemIds))
	{
		// No items
		break;
	}

	$purchases = VBSHOP::$db->fetchAll('
		SELECT featureid, configuration
		FROM $dbtech_vbshop_purchase
		WHERE feature = ?
			AND userid = ?
	', array(
		'item',
		$vbulletin->userinfo['userid'],
	));

	$itemsByType = array();
	$items = VBSHOP::$db->fetchAll('
		SELECT item.itemid, item.categoryid, shopinventory.*
		FROM $dbtech_vbshop_shopinventory AS shopinventory
		LEFT JOIN $dbtech_vbshop_item AS item ON(item.itemid = shopinventory.itemid)
		LEFT JOIN $dbtech_vbshop_category AS category ON(category.categoryid = item.categoryid)
		WHERE shopinventory.itemid :itemIds
			AND shopinventory.active = \'1\'
		ORDER BY category.displayorder ASC, item.displayorder ASC
	', array(
		':itemIds' 		=> VBSHOP::$db->queryList($itemIds),
	));
	foreach ($items as $item)
	{
		if (!$shop = VBSHOP::$cache['shop'][$item['shopid']])
		{
			// Inactive shop
			continue;
		}

		if (!$shop['active'])
		{
			// Inactive shop
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $shop['permissions']))
		{
			// This isn't to be displayed
			continue;
		}

		// Store the category array
		$itemsByType[VBSHOP::$cache['item'][$item['itemid']]['itemtypeid']][$item['itemid']] = array_merge(VBSHOP::$cache['item'][$item['itemid']], $item);
	}

	// Init this
	$threadItems = $threadItemPopups = '';

	foreach ($itemsByType as $itemtypeid => $items)
	{
		$itemList = '';
		foreach ($items as $itemid => $item)
		{
			// Init this
			$disabled = '';

			// Shorthand
			$item['purchasetext'] = $vbphrase['dbtech_vbshop_purchase'];
			
			if (!VBSHOP::check_price($item, $vbulletin->userinfo))
			{
				// Couldn't afford
				$disabled = $vbphrase['dbtech_vbshop_insufficient_funds'];
			}
			
			if ($item['stock'] == 0)
			{
				// Out of stock
				$disabled = $vbphrase['dbtech_vbshop_out_of_stock'];
			}

			foreach ($purchases as $purchase)
			{
				if ($purchase['featureid'] != $itemid)
				{
					// Skip this
					continue;
				}

				if ($item['uniqueitem'])
				{
					// Already bought it
					$disabled = $vbphrase['dbtech_vbshop_already_owned_bought'];
				}
				else
				{
					// Make sure this is an array
					$purchase['configuration'] = @unserialize($purchase['configuration']);
					$purchase['configuration'] = is_array($purchase['configuration']) ? $purchase['configuration'] : array();

					if (isset($purchase['configuration']['contentid']) AND $thread['threadid'] == $purchase['configuration']['contentid'])
					{
						// This purchase applied to this thread
						$item['purchasetext'] = '<img src="' . $vbulletin->options['bburl'] . '/dbtech/vbshop/images/purchased.png" /> ' . $vbphrase['dbtech_vbshop_purchased'];
					}
				}
			}
			
			// Ensure this is set
			$item['icon'] 		= ($item['icon'] 		? $item['icon'] 	: 'unknown.gif');
			$item['shopicon'] 	= ($item['shopicon'] 	? $item['shopicon'] : 'unknown_shop.gif');
			
			// Ensure this is set
			$item['currency'] 		= VBSHOP::$cache['currency'][$item['currencyid']]['title'];
			$item['owner'] 			= ($item['ownerid'] 	? $userids[$item['ownerid']] 	: 'N/A');				
			$item['giftable'] 		= ($item['giftable'] 	? $vbphrase['yes'] 				: $vbphrase['no']);				
			$item['unique'] 		= ($item['uniqueitem'] 	? $vbphrase['yes'] 				: $vbphrase['no']);				
			$item['duration'] 		= ($item['duration'] 	? $item['duration'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['buybacktime'] 	= ($item['buybacktime'] ? $item['buybacktime'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['maxstock'] 		= ($item['stock'] > -1 	? $item['maxstock'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['stock'] 			= ($item['stock'] > -1 	? $item['stock'] 				: $vbphrase['dbtech_vbshop_unlimited']);	
			$item['maxstock'] 		= ($item['stock'] 		? $item['maxstock'] 			: 0);						
			$item['description'] 	= nl2br(htmlspecialchars_uni($item['description']));

			// Do item bits only if it's in the currently viewing shop
			$templater = vB_Template::create('dbtech_vbshop_threadpage_itembit');
				$templater->register('item', 		$item);
				$templater->register('currency', 	VBSHOP::$cache['currency'][$item['currencyid']]);
				$templater->register('disabled', 	$disabled);
				$templater->register('threadid', 	$threadid);
			$itemList .= $templater->render();

			// Do item bits only if it's in the currently viewing shop
			$templater = vB_Template::create('dbtech_vbshop_itempopup');
				$templater->register('item', 		$item);
			$threadItemPopups .= $templater->render();
		}

		// Template output
		$templater = vB_Template::create('dbtech_vbshop_threadpage_itemgroup');
			$templater->register('phrase', VBSHOP::$cache['itemtype'][$itemtypeid]['title']);
			$templater->register('items', $itemList);
		$threadItems .= $templater->render();
	}

	// Now create wrapper
	$templater = vB_Template::create('dbtech_vbshop_threadpage_wrapper');
		$templater->register('items', $threadItems);

	// Before posts
	if (intval($vbulletin->versionnumber) == 3)
	{
		$poll .= $templater->render();
	}
	else
	{
		$template_hook['showthread_above_posts'] .= $templater->render();
	}

	$footer .= $threadItemPopups . '<script type="text/javascript" src="' . VBSHOP::jQueryPath() . '"></script>' . VBSHOP::js('.hover', true, false);
}
while (false);
?>