<?php

include './core.php';

if( isset( $_REQUEST['imgID'] ) )
{
    $screenshot = $MySQL->query_first( "SELECT * FROM screenshots WHERE screenshotID = ".intval( $_REQUEST['imgID'] ) );
    $Smarty->assign( 'screenshot', $screenshot );

    $next_screen = $MySQL->query_first( "SELECT * FROM screenshots WHERE timestamp < ".$screenshot['timestamp']." && categoryID = ".$screenshot['categoryID']." ORDER BY timestamp DESC LIMIT 0,1" );
    if( $next_screen['screenshotID'] > 0 )
    	$Smarty->assign( 'next_screen', $next_screen );

    $last_screen = $MySQL->query_first( "SELECT * FROM screenshots WHERE timestamp > ".$screenshot['timestamp']." && categoryID = ".$screenshot['categoryID']." ORDER BY timestamp DESC LIMIT 0,1" );
    if( $last_screen['screenshotID'] > 0 )
    	$Smarty->assign( 'last_screen', $last_screen );

    $imgurl = combine_path( $settings['screenshots_dir'], 'image'.$screenshot['screenshotID'].'.jpg' );
    $imgsize = @getimagesize( $imgurl );

    if( $imgsize[0] <= 200 )
    	$imgsize[0] = 250;

    $Smarty->assign( 'imgurl', $imgurl );
    $Smarty->assign( 'imgsize', $imgsize );

    $Smarty->display( 'screenshot_item.tpl' );

    die();
}

$catlist = $MySQL->fetch_complete( "SELECT categoryID, name, pictures_per_page, pictures_per_row FROM screenshot_categories ORDER BY `order`" );
$Smarty->assign( 'catlist', $catlist );

if( isset( $_REQUEST['category'] ) )
	$cat = $MySQL->query_first( "SELECT categoryID, name, pictures_per_page, pictures_per_row FROM screenshot_categories WHERE categoryID=".intval( $_REQUEST['category'] ) );
else
	$cat = $catlist[0];
$Smarty->assign( 'cat', $cat );
$Smarty->assign( 'ppr', $cat['pictures_per_row'] );

// start page navigation

if( !isset( $_REQUEST['page_num'] ) || $_REQUEST['page_num'] < 1 )
	$page_num = 1;
else
	$page_num = intval( $_REQUEST['page_num'] );

$Smarty->assign( 'page_num', $page_num );

list( $screenshot_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM screenshots WHERE categoryID = ".$cat['categoryID'] );
if(intval( $cat['pictures_per_page'] ) > 0)
	$pages = ceil( $screenshot_count / intval( $cat['pictures_per_page'] ) );
else
	$pages = 1;

$Smarty->assign( 'pages', $pages );

$page_navigation = make_page_navigation( "screenshots.php?category=".$cat['categoryID']."&amp;".SID, $page_num, $pages, 2 );
$Smarty->assign( 'page_navigation', $page_navigation );

// end page navigation

$screenshots_res = $MySQL->query( "SELECT * FROM screenshots WHERE categoryID=".$cat['categoryID']." ORDER BY `timestamp` DESC LIMIT ".( intval( $cat['pictures_per_page'] ) * ( $page_num - 1 ) ).", ".$cat['pictures_per_page'] );
$screenshots = array();
while( $row = $MySQL->fetch_array( $screenshots_res ) )
{
    $row['thumburl'] = combine_path( $settings['screenshots_dir'], 'thumb'.$row['screenshotID'].'.gif' );
    $row['imgurl'] = combine_path( $settings['screenshots_dir'], 'image'.$row['screenshotID'].'.jpg' );
    $row['imgsize'] = @getimagesize( $row['imgurl'] );

    if( $row['imgsize'][0] <= 200 )
    	$row['imgsize'][0] = 250;

    $row['thumbsize'] = @getimagesize( $row['thumburl'] );

    $screenshots[] = $row;
}
$Smarty->assign( 'screenshots', $screenshots );

$page['content'] = $Smarty->fetch( 'screenshots.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>