
// rss_thumbpop thumbnail image gallery plugin
// by Rob Sable
// http://www.wilshireone.com
// Version 0.1 - 12/07/2004 - Initial Release
// Version 0.2 - 12/08/2004 - Added paging support with use of limit, showpagelinks, backlabel and nextlabel properties.
// Version 0.3 - 1/10/2005 - Added articlethumbs property to automatically show a gallery named "article-" + the current article ID.
// Version 0.4 - 1/10/2005 - Added orderby, pagepop using thumbpop.php, padheight, padwidth.
// Version 0.5 - 5/1/2005 - Added divpop, jpop, showblank, blankimg, clickonly, usearticle, catfield, addpermlink, fullontop, allcats, showalt, orderby alt + random, thumbpop.php is no longer needed.
// Version 0.6 - 1/4/2006 - Added orderby namedesc, fixed validation errors, fixed paging links
// Version 0.6.1 - 1/10/2006 - Fixed paging again.  Added showfullcaption for jpop.
// Version 0.7 - 8/2/2006 - Added showfullalt for jpop.  (Sponsored by mrdale)

function rss_thumbpop($atts) {
global $img_dir,$txpcfg,$pretext,$thisarticle;
extract($atts);
extract($pretext);

$mode = isset($mode) ? $mode: "table";
$cols = isset($cols) ? $cols: "3";
$label = isset($label) ? $label: "";
$showcaption = isset($showcaption) ? $showcaption: "1";
$showalt = isset($showalt) ? $showalt: "0";
$nopop = isset($nopop) ? $nopop: "0";
$category = isset($category) ? doSlash($category) : $c ;
$limit= isset($limit) ? $limit: "99999";
$offset= isset($offset) ? $offset: "0";
$showpagelinks= isset($showpagelinks) ? $showpagelinks: "1";
$backlabel= isset($backlabel) ? $backlabel: "Back";
$nextlabel= isset($nextlabel) ? $nextlabel: "Next";
$articlethumbs= isset($articlethumbs) ? $articlethumbs: "0";
$orderby = isset($orderby) ? $orderby: "";
$pagepop = isset($pagepop) ? $pagepop: "0";
$padheight = isset($padheight) ? $padheight: "20";
$padwidth = isset($padwidth) ? $padwidth: "20";
$divpop = isset($divpop) ? $divpop: "0";
$fullontop = isset($fullontop) ? $fullontop: "1";
$showfullcaption = isset($showfullcaption) ? $showfullcaption: "0";
$showfullalt = isset($showfullalt) ? $showfullalt: "0";
$allcats = isset($allcats) ? $allcats: "0";
$jpop = isset($jpop) ? $jpop: "0";
$showblank = isset($showblank) ? $showblank: "0";
$blankimg = isset($blankimg) ? $blankimg: "/img/blankphoto.jpg";
$clickonly = isset($clickonly) ? $clickonly: "0";
$usearticle = isset($usearticle) ? $usearticle: "0";
$catfield = isset($catfield) ? $catfield: "custom_1";
$addpermlink = isset($addpermlink) ? $addpermlink: "1";
$artcat = "";
$divimg = "";
$i=0;

if ($usearticle) {
	$id = $thisarticle["thisid"];
	$res = safe_rows("*, unix_timestamp(Posted) as uPosted", "textpattern", "id = ".$id);

	if ($res) {
		foreach($res as $a) {
			extract($a);
			populateArticleData($a);
			$artcat = " AND (category = '" . doSlash($a[$catfield]) . "') ";
			$artlink = permlinkurl($a);
		}
	}
}

foreach (explode(',', $category) as $category) {
	$catsql[] = ($category) ? " (category = '" . doSlash($category) . "') " : "";
}
$categories = (!$allcats && $category) ? '(' . join(' OR ', $catsql) . ')' : '1=1 ';


if  ($articlethumbs) {
	$categories = " (category = '" . doSlash('article-' . $thisarticle['thisid']) . "') ";
}

switch ($orderby) {
case "name":
   $obfield=" ORDER BY name";
   break;
case "namedesc":
   $obfield=" ORDER BY name desc";
   break;
case "caption":
   $obfield=" ORDER BY caption";
   break;
case "alt":
   $obfield=" ORDER BY alt";
   break;
case "dateasc":
   $obfield=" ORDER BY date asc";
   break;
case "datedesc":
   $obfield=" ORDER BY date desc";
   break;
case "random":
   $obfield=" ORDER BY rand()";
   break;
default:
	$obfield="";
}

if ($limit && $showpagelinks) {


	// DO PAGING
	$where = $categories.$artcat.$obfield.' LIMIT '.$offset.',99999';
	$results = safe_rows('*', 'txp_image', $where);
	$numthum = count($results);

	$numPages = ($numthum > 0 && $limit) ? ceil($numthum/$limit) : 1;
	$thumbpage = (!gps('tpg')) ? 1 : gps('tpg');
	$offset = ($thumbpage - 1) * $limit;
	$results = safe_rows('*', 'txp_image', $categories.$obfield.' LIMIT '.$offset.','.$limit);

} else {

	//NO PAGING
	$numPages = 0;
	$where = $categories.$artcat.$obfield.' LIMIT '.$offset.','.$limit;
	$results = safe_rows('*', 'txp_image', $where);
	$numthum = count($results);

	if ($numthum == 0 && $articlethumbs) {
		return;
	}

}

if ($mode=="table") {
	$head = '<table class="rssThumbs">'.n;
	$head.= ($label) ? '<tr><th class="rssThumbLabel" colspan="' . $cols . '">'. $label .'</th></tr>'.n : '';
	$head.= '<tr>'.n;
} else 	if ($mode == "float") {
	$head = '<div class="rssThumbs">';
	$head.= ($label) ? '<div class="rssThumbLabel">'. $label .'</div>'.n : '';
}

if($results){
	foreach($results as $image){
		extract($image);
		$thumb =  hu.$img_dir.'/' . $id . 't' . $ext;
		$full = hu.$img_dir.'/' . $id . $ext;

		$divimg = (gps("img") && ($id == gps("img")) && $divpop) ? '<div class="rssFull"><img src="'  . $full . '" alt="'.$alt.'" /></div>'.n : $divimg;

		$img = (isset($artlink) && $addpermlink) ? href('<img class="rssThumb" src="'  . $thumb . '" alt="'.$alt.'" title="'.$caption.'" />',$artlink) : '<img class="rssThumb" src="'  . $thumb . '" alt="'.$alt.'" title="'.$caption.'" />';

		if (!$nopop ) {
			if ($pagepop) {
				$page = hu . "index.php?file=thumbpop&amp;pic=" . $id;
				$imgtag = join("",array(
					" <a target=\"_blank\" href=\"".$page."\"",
					" onclick=\"","window.open(this.href, 'popupwindow', 'width=",$w+$padwidth,",height=",$h+$padheight,
					",resizable'); return false;\">",
					$img,"</a>"));
			} else if ($divpop) {
				$imgtag = '<a href="?img='.$id.'">'.$img.'</a>';
			} else if ($jpop) {
				$events = ($clickonly) ? 'onclick="return showPic(this)"' : 'onmouseover="return showPic(this)" onclick="return showPic(this)"';
				$imgtit = ($showfullalt) ? $alt."|".$caption : $caption;
				$imgtag = '<a '.$events.' href="'.$full.'" title="'.$imgtit.'">'.$img.'</a>';
				$blankimg = ($showblank) ? $blankimg : $full;
				$divimg = '<div id="fullsize"><div id="desc">Mouse over a thumbnail to view</div>';
				$divimg .= ($showfullalt) ? '<div id="jpopalt">'.$alt.'</div>' : '';
				$divimg .= '<img id="placeholder" src="'.$blankimg.'" alt="" />';
				$divimg .= ($showfullcaption) ? '<div id="jpopcap">'.$caption.'</div>' : '';
				$divimg .= '</div>';
			} else {
				$imgtag = join("",array(
					" <a target=\"_blank\" href=\"".$full."\"",
					" onclick=\"","window.open(this.href, 'popupwindow', 'width=",$w+$padwidth,",height=",$h+$padheight,
					",resizable'); return false;\">",
					$img,"</a>"));
			}
		} else {
			$imgtag = $img;
		}

		$numthum--;

		if ($mode == "table") {
			$output[] =  '<td valign="top"><div class="rssImg">';
			$output[] = $imgtag.'</div>';
			$output[] = ($showalt && $alt) ? n.'<span class="cap">'.$alt.'</span>'.n : '';
			$output[] = ($showcaption && $caption) ? '<span class="cap">'.$caption.'</span>'.n : '';
			$output[] = '</td>'.n;
			if($i++ % $cols == ($cols - 1) && $numthum > 0) $output[]= '</tr>' .n . '<tr>'.n;
		} else	if ($mode == "float") {
			$output [] =  '<div class="rssThumbFloat">'.n;
			$output[] = $imgtag;
			$output[] = ($showalt && $alt) ? n.'<span class="cap">'.$alt.'</span>'.n : '';
			$output[] = ($showcaption && $caption) ? '<span class="cap">'.$caption.'</span>'.n : '';
			$output[] = '</div>'.n;
			if($i++ % $cols == ($cols - 1) && $numthum > 0) $output[]= '<br style="clear: both;" />'.n;
		}
	}
} else {
$output[]= ($mode == "table") ? '<td class="rssImg"  colspan="' . $cols . '">No Images Found</td>'.n : 'No Images Found';
}
$output[] = ($mode == "table") ? n.'</tr>'.n.'</table>'.n : '<br style="clear: both;" /></div>'.n;

if ($numPages > 1 && $showpagelinks) {
	$next = ($numPages > 1 && $thumbpage != $numPages);
	$back = ($numPages > 1 && $thumbpage > 1);
	$output[] = '<ul id="rssThumbNav">'.n;
	$output[] = ($back) ? '<li class="left"><a href="?tpg='.($thumbpage-1).'">'.$backlabel.'</a></li>'.n : '';
	$output[] = ($next) ? '<li class="right"><a href="?tpg='.($thumbpage+1).'">'.$nextlabel.'</a></li>'.n : '';
	$output[] = '</ul>'.n;
}

$jpop = ($jpop) ? jsPop() : '';

return ($fullontop) ? $divimg.$head.implode('', $output).$jpop : $head.implode('', $output).$divimg.$jpop;
}

function jsPop() {
return <<<EOF
<script language="JavaScript" type="text/javascript">
<!--
	// Javascript Image Gallery by Jeremy Keith
	// http://adactio.com/articles/display.php/javascript_image_gallery
	function showPic (whichpic) {
		if (document.getElementById) {
			document.getElementById('placeholder').src = whichpic.href;
			if (whichpic.title) {
				 var rssText = whichpic.title;
				 txtt = rssText.substring(rssText.indexOf('|') + 1, rssText.length);
				 altt = rssText.substring(0,rssText.indexOf('|'));
				 if (document.getElementById('jpopcap')) document.getElementById('jpopcap').childNodes[0].nodeValue = txtt;
				 if (document.getElementById('jpopalt')) document.getElementById('jpopalt').childNodes[0].nodeValue = altt;
			} else {
				 if (document.getElementById('jpopcap')) document.getElementById('jpopcap').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
				 if (document.getElementById('jpopalt')) document.getElementById('jpopalt').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
			}
			return false;
		} else {
			return true;
		}
	}
	-->
	</script>
EOF;
}

if (gps('file') == 'thumbpop') {
$id = $_GET["pic"];
if (!$id) { return; }
global $pfr,$img_dir;
$get = safe_row("*","txp_image","id='$id'");
extract($get);

echo <<<EOF

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>
EOF;
			echo $alt;
echo <<<EOF
		</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<style type="text/css" media="screen">
			<!--
			p { font: .8em Verdana, sans-serif; text-align: center; }
			img { padding: 2px; border: 1px solid #555; }
			-->
		</style>
  </head>
  <body>
EOF;
		echo '<img src="'  . $pfr.$img_dir.'/' . $id . $ext . '" alt="'.$alt.'" /><p>' . $caption .'</p>';
echo <<<EOF
  </body>
</html>

EOF;
exit;
}
