#!/usr/bin/perl -I/usr/home/btshv/lib
require "EW.pm";
require "std_lib.pl";
require "Getent.pm";
require "BtsData6DBR.pl";
require "idx-lib.pl";
use Template;
require "template_lib.pl";

use XML::Simple;
use CGI;

my $q = new CGI;

my $config = XMLin('/usr/home/btshv/conf/buzzardsbayboatshop/photogallery/config.xml');
my $TT = $config;
$CI = $config->{ERS};
%FORM = $q->Vars;

my $DBRCI = &BtsData6DBR::get_db_read_info();
my %D = ('table_name' => $TT->{index_table},
		 'dbci' => $DBRCI,
		 'keepalive' => 0);
$D = \%D;

#$|=1;&ptext;

%l2_verify = ('' => '',
			  'list' => '');

if($FORM{type} eq 'list' or $FORM{type} eq '') { &verify_l2($FORM{type}); &list_pages();}
else{ &EW::report($CI, "Bad Type", "Invalid type in $0 [$FORM{type}]", '11', '1007', undef, undef, 'ssl', 'error'); }

exit;

sub list_pages {

my $sw = 'id,name,mode,categ';
my $mr = '1=1';
my $sorty = qq{categ, name};
my ($list, undef, $RET) = idx_list_sort($sw, $mr, $sorty, $D);
if ($RET->{failure}) { &EW::report($CI, $RET->{user_message}, $RET->{detail}, $RET->{categ}, $RET->{sub_categ}, undef, $RET->{severity}, undef, 'error'); }

my ($col_groups, $num_groups, $list) = &organize_list($list);


&phtml;
		&print_template3({
			template_path	=> '',
			file		=> $TT->{page_list_head},
			vars		=> {}
		});

my %SEEN_CATEG = ();
my $num_categs = 0;
my $break = 0;
print "<table cellpadding=\"4\" width=\"500\"><tr><td valign=\"top\">\n";
#print "$col_groups, $num_groups\n";
foreach $e (@$list) {
	next if $e->{mode} != 1;

		#print "nc: $num_categs\n";



	unless($SEEN_CATEG{$e->{categ}}) { #if we haven't seen this categ before, total it up
		#print "counting:", $e->{categ}, "\n";
		$num_categs++;
	}

	if ($num_categs == $col_groups + 1 and $break == 0) {
		$break = 1;
		print "</td><td valign=\"top\">\n";
	}

	print <<EO_HTML;
	<li><font size="2" face="Arial"><b><a href="page-show.pl?type=show&id=$e->{id}">$e->{name}</a></b></font><br>
EO_HTML
	$SEEN_CATEG{$e->{categ}}++;

}#end foreach @$list

print "</td></tr></table><br>\n";

		&print_template3({
			template_path	=> '',
			file		=> $TT->{page_list_foot},
			vars		=> {}
		});

}#end list_pages


sub organize_list {
	my ($list) = @_;

	my $num_categs = 0;
	my %CATEGS = ();

	foreach $e (@$list) {
		unless($CATEG{$e->{categ}}) { #if we haven't seen this categ before, total it up
			$num_categs++;
		}
		$CATEG{$e->{categ}}++; #now record it
	}

	return (ceil($num_categs / 2), $num_categs, $list);

}#end organize_list



