<?php
$lcheck = "b";
$z = "b";

global $setting;

if(!isset($_GET['p'])){
    $page = 1;
} else {
    $page = $_GET['p'];
}

$from = (($page * $setting[images_per_page]) - $setting[images_per_page]);

eval (" ?>" . headera() . " <?php ");

if ($_GET['id'] == "") {
if ($_GET['title']) {
$title = str_replace("-", " ", $_GET['title']);
echo "<title>".$title."</title>";
}
echo "<table align='center' border='0' cellpadding='3' cellspacing='2' width='98%'><tr>";
if ($_GET['s'] == "") {
$query = "SELECT * FROM ".$pre."albums ORDER BY `id` DESC";
} else {
$query = "SELECT * FROM ".$pre."albums WHERE systems = '".$_GET['s']."' ORDER BY `id` DESC";
}

$query .= " LIMIT $from, $setting[images_per_page]";

$query2 = mysql_query($query);
$iy = mysql_num_rows($query2);
for($i = 0; $row = mysql_fetch_assoc($query2); $i++) {
$icon = mysql_fetch_row(mysql_query("SELECT name,icon FROM ".$pre."systems WHERE id = '".$row[systems]."'"));
$fetchn = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."images WHERE album = '".$row[id]."' ORDER BY `id` DESC"));

	$val[0] = "a_images/nopreview.jpg";
	$val[1] = "";
	$val[2] = "ss2";

	if ($fetchn == "0") {
    $fetch7 = implode(",", $val);
	$fetch = explode(",", $fetch7);
	} else {
	$fetch = mysql_fetch_row(mysql_query("SELECT name,caption,type2 FROM ".$pre."images WHERE album = '".$row[id]."' ORDER BY `id` DESC LIMIT 1"));
	}

	$f1[0] = "{image}";
	$f1[1] = "{thumb}";
	$f1[2] = "{caption}";
	$f1[3] = "{name}";
	$f1[4] = "{url}";
	$f1[5] = "{system}";
	$f1[6] = "{icon}";
	$f1[7] = "{imagenum}";
	if ($fetch[2] == "ss2") {
	$r1[0] = $fetch[0];
	} else {
	$r1[0] = $setting[images]."/".$fetch[0];
	}
	if ($fetch[2] == "ss2") {
	$r1[1] = $fetch[0]."' width='".$setting[thumbnail_width]."' height='".$setting[thumbnail_height];
	} else {
	$r1[1] = $setting[images]."/".$fetch[0]."' width='".$setting[thumbnail_width]."' height='".$setting[thumbnail_height];
	}
	$r1[2] = stripslashes($fetch[1]);
	$r1[3] = stripslashes($row[name]);
	$r1[4] = $gpart1.$row[id].fthrough($row[name], "").$gpart2;
	if ($row[systems]) {
	$r1[5] = stripslashes($icon[0]);
	$r1[6] = "<img src='".stripslashes($icon[1])."'>";
	} else {
	$r1[5] = "";
	$r1[6] = "";
	}
	$r1[7] = $fetchn;
	
	if (!$i == "0") {
	if (($i % $setting[images_per_row]) === 0) {
		print $setting[gallery_html_between];
	}
	}
	echo str_replace($f1, $r1, $setting[gallery_template]);
}
echo "</tr></table>";
if ($_GET['s'] == "") {
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."albums"),0);
} else {
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."albums WHERE systems = '".$_GET['s']."'"),0);
}

$total_pages = ceil($total_results / $setting[images_per_page]);

if ($total_pages > "1") {
echo "<br><br><center>Page:<br>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
	if ($_GET['s'] == "") {
    echo "<a href=\"gallery.php?p=$prev\"><<Previous</a>&nbsp;";
	} else {
	echo "<a href=\"gallery.php?s=".$_GET['s']."&p=$i\">$i</a>&nbsp;";
	}
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
			if ($_GET['s'] == "") {
            echo "<a href=\"gallery.php?p=$i\">$i</a>&nbsp;";
			} else {
			echo "<a href=\"gallery.php?s=".$_GET['s']."&p=$i\">$i</a>&nbsp;";
			}if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
	if ($_GET['s'] == "") {
    echo "<a href=\"gallery.php?p=$next\">Next>></a>";
	} else {
	echo "<a href=\"gallery.php?s=".$_GET['s']."&p=$next\">Next>></a>";
	}
}
echo "</center>";
}
}

if ($_GET['id']) {
if (is_numeric($_GET['id'])) {
$id = $_GET['id'];
$galleryid = $_GET['id'];
} else {
echo "ID number invalid";
die;
}

echo '<script type="text/javascript" src="a_images/lightbox/lightbox.js"></script><link rel="stylesheet" type="text/css" href="a_images/lightbox/lightbox.css" />';

$imagenum = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."images WHERE album = '".$id."'"));
$title = mysql_fetch_row(mysql_query("SELECT name,views,systems FROM ".$pre."albums WHERE id = '".$id."'"));

$query = mysql_query("SELECT name,icon FROM ".$pre."systems WHERE id = '".$title[2]."'");
$systems = mysql_fetch_row($query);

if ($title[1] == "") {
$views = "1";
} else {
$views = $title[1] + 1;
}

mysql_query("UPDATE ".$pre."albums SET views = ".$views." WHERE id = '".$id."'");

$newviews = $title[1] + 1;

$alb21[0] = "/{title}/";
$alb21[1] = "/{imagenum}/";
$alb21[2] = "/{views}/";
$alb21[3] = "/{system}/";
$alb21[4] = "/{icon}/";
$alb21[5] = "/{slide}/";
$alb22[0] = $title[0];
$alb22[1] = $imagenum;
$alb22[2] = $newviews;
if ($title[2]) {
$alb22[3] = stripslashes($systems[0]);
$alb22[4] = "<img src='".stripslashes($systems[1])."'>";
} else {
$alb22[3] = "";
$alb22[4] = "";
}
$alb22[5] = "javascript:awindow(\"slideshow.php?id=$id\", \"\", \"width=".$swidth.",scroll=yes\")";

eval (" ?>" . preg_replace($alb21, $alb22, $setting[album_template]) . " <?php ");

$query = "SELECT * FROM ".$pre."images WHERE album = '".$id."' ORDER BY `id`";

if (($albpage) && ($albpages == "No")) {
	$query .= " LIMIT ".$albpage;
}
if ($albpages == "Yes") {
    $query .= " LIMIT $from, $setting[images_per_page]";
}

$query2 = mysql_query($query);
$iy = mysql_num_rows($query2);
for($i = 0; $row = mysql_fetch_assoc($query2); $i++) {

	$f1[0] = "{image}";
	$f1[1] = "{thumb}";
	$f1[2] = "{caption}";
	$f1[3] = "{slide}";
	$f1[4] = "{date}";
	$f1[5] = "{game}";
	$f1[6] = "{endgame}";
	$f1[7] = "{content}";
	$f1[8] = "{endcontent}";
	$f1[9] = "{views}";

	if ($row[type2] == "ss2") {
	$r1[0] = $row[name];
	} else {
	$r1[0] = $setting[images]."/".$row[name];
	}

	if ($row[type2] == "ss2") {
	$r1[1] = $row[name]."' width='".$setting[thumbnail_width]."' height='".$setting[thumbnail_height];
	} else {
	$r1[1] = $setting[images]."/".$row[name]."' width='".$setting[thumbnail_width]."' height='".$setting[thumbnail_height];
	}

	$r1[2] = stripslashes($row[caption]);
	$r1[3] = "javascript:awindow(\"slideshow.php?id=$row[album]\", \"\", \"width=".$swidth.",height=".$sheight.",scroll=yes\")";
	$r1[4] = date($dformat, $row[date]);

	$sql = mysql_fetch_row(mysql_query("SELECT id,name FROM ".$pre."games WHERE album = '".$id."'"));
	$sql_id = mysql_fetch_row(mysql_query("SELECT id2 FROM ".$pre."fielddata WHERE name = 'games' AND data = '".$sql[0]."'"));
	$sqn = mysql_fetch_row(mysql_query("SELECT id,name FROM ".$pre."content WHERE id = '".$sql_id[0]."'"));

	$r1[5] = "<a href='".$gamepart1.$sql[0].fthrough($sql[1], "").$gamepart2."'>";
	$r1[6] = "</a>";
	$r1[7] = "<a href='".$part1.$sqn[0].fthrough($sqn[1], "").$part2."'>";
	$r1[8] = "</a>";
	$r1[9] = $row[views];
    
	if (!$i == "0") {
	if (($i % $setting[images_per_row]) === 0) {
		print $setting[gallery_html_between];
	}
	}

	echo str_replace($f1, $r1, $albtemplate);
}
eval (" ?>" . preg_replace($alb21, $alb22, $setting[gallery_footer]) . " <?php ");

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."images WHERE album = '".$id."'"),0);

$total_pages = ceil($total_results / $setting[images_per_page]);

if ($total_pages > "1") {

echo "<br><br><center>Page:<br>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"gallery.php?id=".$galleryid."&p=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"gallery.php?id=".$galleryid."&p=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"gallery.php?id=".$galleryid."&p=$next\">Next>></a>";
}
echo "</center>";
}
}
eval (" ?>" . footera() . " <?php ");
?>