<?php

// load user
$url = _INT_PAGE_URL;
$urlParts = explode('/', $url);
array_shift($urlParts);
$subCategory = trim(current($urlParts));
$pageTitle = t("browse_page_name", "Browse");
$categoryId = null;
$categoryListing = false;
$activeTab = 'browse-images';
if((strlen($subCategory)) && ($subCategory == 'category'))
{
	// get category name
	array_shift($urlParts);
	$subCategoryName = trim(current($urlParts));
	if(strlen($subCategoryName))
	{
		// check category is valid
		$foundCat = $db->getRow('SELECT id, label FROM plugin_imageviewer_category WHERE `key`='.$db->quote($subCategoryName).' LIMIT 1');
		if($foundCat)
		{
			$pageTitle = t("browse_page_name", "Browse").' - '.$foundCat['label'];
			$categoryId = $foundCat['id'];
			$categoryListing = true;
		}
	}
	$activeTab = 'browse-categories';
	define("SELECTED_NAVIGATION_LINK", "category");
}
elseif((strlen($subCategory)) && ($subCategory == 'album'))
{
	$activeTab = 'browse-albums';
	define("SELECTED_NAVIGATION_LINK", "album");
}
else
{
	define("SELECTED_NAVIGATION_LINK", "browse");
}

// setup page
define("PAGE_NAME", $pageTitle);
define("PAGE_DESCRIPTION", t("browse_meta_description", "Browse Images"));
define("PAGE_KEYWORDS", t("browse_meta_keywords", "browser, images, file, your, interface, upload, download, site"));
define("SELECTED_NAVIGATION_LINK", "browse");

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header_file_manager.inc.php');

?>

<script>
var categoriesLoaded = false;
var recentLoaded = false;
var albumsLoaded = false;
$( document ).ready(function() {
	<?php
	switch($activeTab)
	{
		case 'browse-albums':
			echo "localLoadBrowsePageAlbums();";
			break;
		case 'browse-categories':
			echo "localLoadBrowsePageCategories();";
			break;
		case 'browse-images':
		default:
			echo "localLoadBrowsePageRecentImages();";
			break;
	}
	?>
});

function localLoadBrowsePageRecentImages()
{
	if(recentLoaded == false)
	{
		loadBrowsePageRecentImages();
	}
	recentLoaded = true;
}

function localLoadBrowsePageAlbums()
{
	if(albumsLoaded == false)
	{
		loadBrowsePageAlbums();
	}
	albumsLoaded = true;
}

function localLoadBrowsePageCategories()
{
	if(categoriesLoaded == false)
	{
		<?php if($categoryListing == true): ?>
		loadBrowsePageCategoryImages(<?php echo (int)$categoryId; ?>);
		<?php else: ?>
		loadBrowsePageCategories();
		<?php endif; ?>
	}
	categoriesLoaded = true;
}
</script>

<div id="browse-page" class="main-content layer base-slide browse-page">
	<div class="row">
		<div class="col-md-12">
			<ul class="nav nav-pills">
				<!-- available classes "bordered", "right-aligned" --> 
				<li<?php echo $activeTab=='browse-images'?' class="active"':''; ?>> <a href="#browse-images" data-toggle="tab" onClick="localLoadBrowsePageRecentImages(); return false;"> <i class="entypo-camera"></i> <span class="hidden-xs"><?php echo t('browse_images', 'Browse Images'); ?></span> </a> </li>
				<li<?php echo $activeTab=='browse-albums'?' class="active"':''; ?>> <a href="#browse-albums" data-toggle="tab" onClick="localLoadBrowsePageAlbums(); return false;"> <i class="entypo-picture"></i> <span class="hidden-xs"><?php echo t('navigation_albums', 'Albums'); ?></span> </a> </li>
				<li<?php echo $activeTab=='browse-categories'?' class="active"':''; ?>> <a href="#browse-categories" data-toggle="tab" onClick="localLoadBrowsePageCategories(); return false;"> <i class="entypo-archive"></i> <span class="hidden-xs"><?php echo t('navigation_categories', 'Categories'); ?></span> </a> </li>
			</ul>
			<div class="tab-content">
				<div class="tab-pane <?php echo $activeTab=='browse-albums'?'active':''; ?>" id="browse-albums">
					<div class="ajax-loading-image"><!-- --></div>
				</div>
				<div class="tab-pane <?php echo $activeTab=='browse-images'?'active':''; ?>" id="browse-images">
					<div class="ajax-loading-image"><!-- --></div>
				</div>
				<div class="tab-pane <?php echo $activeTab=='browse-categories'?'active':''; ?>" id="browse-categories">
					<div class="ajax-loading-image"><!-- --></div>
				</div>
			</div>
		</div>
	</div>
</div>

<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer_file_manager.inc.php');
?>
