<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$ccolor = "#FFFFFF"; //#FFFFFF

global $siteConfig;

echo "<script type='text/javascript' src='".$siteConfig['site_url']."/inc/javascript/jquery/plugins/lightbox/js/jquery.lightbox.js'></script>\n";
echo "<link rel='stylesheet' href='".$siteConfig['site_url']."/inc/javascript/jquery/plugins/lightbox/css/jquery.lightbox.css' type='text/css'>\n";

?>
	<script type="text/javascript">
		$(function() {
			$('a.lightbox').lightBox({
				imageLoading: 'inc/javascript/jquery/plugins/lightbox/images/lightbox-ico-loading.gif',
				imageBtnClose: 'inc/javascript/jquery/plugins/lightbox/images/lightbox-btn-close.gif',
				imageBtnPrev: 'inc/javascript/jquery/plugins/lightbox/images/lightbox-btn-prev.gif',
				imageBtnNext: 'inc/javascript/jquery/plugins/lightbox/images/lightbox-btn-next.gif',
				imageBlank: 'inc/javascript/jquery/plugins/lightbox/images/lightbox-blank.gif'
			});
		});
    </script>
<?php

function galleryMain() {
	global $dblink;
	
	$a = 0;
	echo "<table align='center' border='0'>\n";
	$result = $dblink->get_list("SELECT * FROM memht_gallery_categories WHERE enabled=1 ORDER BY name");
	foreach ($result as $row) {
		$cid = intval($row['id']);
		$cname = outCode($row['name']);
		$cimgfile = outCode($row['imgfile']);
		$cdescription = outCode($row['description']);

		$num_img = $dblink->get_num("SELECT * FROM memht_gallery_images WHERE category='$cid' AND enabled=1");
		
		if ($a==0) { echo "<tr>"; }
			echo "<td valign='top'><table style='margin: 10px;'>";
				echo "<tr><td colspan='2'><a href='index.php?page=gallery&op=showGallery&id=$cid&title=".mem_urlencode($cname)."' title='$cname'><b>$cname</b></a> ($num_img)</td></tr>\n";
				echo "<tr><td valign='top' width='1'><a href='index.php?page=gallery&op=showGallery&id=$cid&title=".mem_urlencode($cname)."' title='$cname'><img src='pages/gallery/images/categories/$cimgfile' border='0' title='header=[$cname] body=[<b>"._DESCRIPTION_."</b>:<br>$cdescription]\' alt='$cname'></a></td><td valign='top'>";
				echo "</td></tr>\n";
		echo "</table></td>";
		if ($a>=3) { echo "</tr>\n"; $a = 0; } else { $a++; }
	}
	echo "</table>";
}

function showGallery($id,$ofsbgn,$ofsppg,$ccolor) {
	global $dblink,$pg,$columns;
	$id = intval($id);
	
	$n = 0;	
	$amt = 1;
	
	$row = $dblink->get_row("SELECT name FROM memht_gallery_categories WHERE id=$id");
	$catname = outCode($row['name']);

	$query = "SELECT * FROM memht_gallery_images WHERE category='$id' AND enabled=1 ORDER BY id DESC LIMIT $ofsbgn,$ofsppg";
	if ($dblink->get_num($query)>0) {
		echo "<div class='box' align='center'><a href='index.php?page=gallery' title='"._MAIN_."'>"._MAIN_."</a> > <b>$catname</b></div>";
		echo "<table border='0' cellspacing='0' cellpadding='0' align='center'>\n";
		$result = $dblink->get_list($query);
		foreach ($result as $row) {
			$iid = intval($row['id']);
			$ititle = outCode($row['title']);
			$iimgfile = outCode($row['imgfile']);
			$ithumb = outCode($row['thumb']);
			$iattachfile = outCode($row['attachfile']);
			$idescription = outCode($row['description']);
			
			$imagesize=@getimagesize("pages/gallery/gallery/".$iimgfile);
			$ix = ceil($imagesize[0]);
			$iy = ceil($imagesize[1]);
			
			if ($idescription!="") { $mydesc = "<br><b>"._DESCRIPTION_."</b>: $idescription"; } else { $mydesc = ""; }
			
			if ($amt==1) { echo "<tr>\n"; }
			echo "<td align='center' valign='top'><div style='margin:8px;'><table style='padding: 2px; background-color:{$ccolor};'><tr><td><a href='pages/gallery/gallery/$iimgfile' title=\"$ititle\" class='lightbox'><img src='pages/gallery/gallery/thumb/$ithumb' border='0' title=\"header=[$ititle] body=[<b>"._SIZE_."</b>: $ix x $iy (".strSize(filesize(htmlspecialchars("pages/gallery/gallery/$iimgfile"))).")$mydesc]\" alt=\"$ititle\"></a></td></tr></table>";		
				if ($iattachfile!="") {
					echo "<div><a href=\"$iattachfile\" title=\"header=["._ATTACHMENT_."] body=[$iattachfile]\"><img src='pages/gallery/images/attachment.gif' border='0' alt='Attachment'></a></div>";
				}
			echo "</div></td>\n";
			if ($amt==$columns) { echo "</tr>\n"; $amt = 1; } else { $amt++; }
		}
		echo "</table>\n";
		
		//Pages
		include_once("inc/class/paginationSystem.class.php");
		$ps = new paginationSystem();
		$ps->items = $ofsppg;
		$ps->actpg = $pg;
		$ps->query = "SELECT id FROM memht_gallery_images WHERE category=$id AND enabled=1";
		$ps->url = "index.php?page=gallery&op=showGallery&id=$id&title=".mem_urlencode($catname)."&pg={{N}}";
		$ps->show();
	} else {
		echo "<div class='box' align='center'><a href='index.php?page=gallery' title='"._MAIN_."'>"._MAIN_."</a> > <b>$catname</b></div>";
		echo "<div align='center' align='center' id='errorText'><b>"._EMPTY_."</b></div><br>\n";
	}
}

function showRandomImages($columns,$ccolor) {
	global $dblink;
	$columns = intval($columns);
	
	echo "<div class='box' align='center' style='margin-top:5px;'><b>"._RANDOMIMAGES_."</b></div>";
	
	$a = 1;
	$result = $dblink->get_list("SELECT * FROM memht_gallery_images WHERE enabled=1");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$gi[$a] = $id;
		$a++;
	}
	if ($a>1) {
		echo "<table border='0' cellspacing='0' cellpadding='0' align='center'><tr>";
		for ($i=0;$i<$columns;$i++) {
			$rand = rand(1,($a-1));
			$id = $gi[$rand];
			
			$row = $dblink->get_row("SELECT * FROM memht_gallery_images WHERE id='$id' AND enabled=1");
			$ititle = outCode($row['title']);
			$iimgfile = outCode($row['imgfile']);
			$ithumb = outCode($row['thumb']);
			$iattachfile = outCode($row['attachfile']);
			$idescription = outCode($row['description']);
			
			$imagesize=@getimagesize("pages/gallery/gallery/".$iimgfile);
			$ix = ceil($imagesize[0]);
			$iy = ceil($imagesize[1]);
					
			$mydesc = ($idescription!="") ? "<br><b>"._DESCRIPTION_."</b>: $idescription" : "" ;
			
			echo "<td valign='top'><div style='margin:8px;'><table style='padding: 2px; background-color:{$ccolor};'><tr><td><a href='pages/gallery/gallery/$iimgfile' title=\"$ititle\" class='lightbox'><img src='pages/gallery/gallery/thumb/$ithumb' border='0' title=\"header=[$ititle] body=[<b>"._SIZE_."</b>: $ix x $iy (".strSize(filesize(htmlspecialchars("pages/gallery/gallery/$iimgfile"))).")$mydesc]\" alt=\"$ititle\"></a></td></tr></table>";		
			if ($iattachfile!="") {
				echo "<div align='center'><a href=\"$iattachfile\" title=\"header=["._ATTACHMENT_."] body=[$iattachfile]\"><img src='pages/gallery/images/attachment.gif' border='0' alt='Attachment'></a></div>\n";
			}
			echo "</div></td>\n";
		}
		echo "</tr></table>\n";
	} else {
		echo "<div id='errorText' align='center'><b>"._EMPTY_."</b></div>";
	}
}

if (isSet($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isSet($_GET['id'])) { $id = inCode($_GET['id']); } else { $id = ""; }

if (isSet($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }

$pagerow = $dblink->get_row("SELECT * FROM memht_pagine WHERE nome='$page'");
$fullScreen = intval($pagerow['fullscreen']);

$cfgrow = $dblink->get_row("SELECT * FROM memht_gallery_config");
$rows = intval($cfgrow['rows']);
$columns = intval($cfgrow['cols']);

$ofsppg = $columns*$rows; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

require_once("inc/inc_header.php");
	if (isset($_GET['page'])) { page_title($page); }
	openTable();
	
	switch($op) {
		case "showGallery":
			showGallery($id,$ofsbgn,$ofsppg,$ccolor);
			showRandomImages($columns,$ccolor);
		break;

		default:
			galleryMain();
			showRandomImages($columns,$ccolor);
		break;
	}
	
	closeTable();
require_once("inc/inc_footer.php");

?>