#!/usr/bin/perl
###########################################################################
eval {
	($0=~ m,(.*)/[^/]+,)   && unshift (@INC, "$1"); 
	($0=~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");
	require "config.pl";
	push(@INC, $script_path);
	require "database.pl";
	require "display.pl";
	require "html.pl";
	require "library.pl";
	require "mojoscripts.pl";
	require "parse_template.pl";
	require "english.lng";
	use CGI qw(:standard);
	use CGI::Carp qw(fatalsToBrowser);
   &main;
};
if ($@) { 
	print "content-type:text/html\n\n";
	print "Error Including configuration file, Reason: $@";
	exit;
}
&main;
###########################################################################
sub main{
	$|++;							
	&ParseForm;
	&LoadDefaultValues;

	if($FORM{cat}){	&DisplayCategory;	}
	elsif($FORM{ID}){	&DisplayQuestion;	}
	else{					&DisplayHome;		}
}
###########################################################################
###########################################################################
sub DisplayHome{
	$MOJOTAG{cats} = &BuildSubcategories($category_path);
#	$MOJOTAG{media} = &BuildCategoryFiles($category_path);
	&PrintTemplate($TEMPLATE{home});
}
###########################################################################
sub DisplayCategory{
	my(%CAT, $cat, @cats, $count, $data, @html, $half,
	 $line, @lines, $file, @files, $name, $template);
	local($dir, $new, $link);
	
	&CheckCategory($category_path);
	$MOJOTAG{cats} = &BuildSubcategories($category_path);
	($MOJOTAG{questions},$MOJOTAG{answers}) = &BuildCategoryFiles($category_path);
	&PrintTemplate((-f "$category_path/category.html")?"$category_path/category.html":$TEMPLATE{category});
}
###########################################################################
sub DisplayQuestion{
	my($data, $file, $line, @lines, $media, %MEDIA, $next, $prev, $slideshow,$template);
#	&CheckMedia;$$category_files_db
	$line = &FileReadArray($category_files_db);
	@lines = @$line;
	$FORM{total} = @lines;
	$MOJOTAG{slideshow} = &BuildSlideShow(\@lines);
	($MOJOTAG{prev_file}, $MOJOTAG{next_file}) = &BuildNextPrevFiles(\@lines);
	for(my $i=0; $i <@lines; $i++){
		$media = &RetrieveMediaDatabase($lines[$i]);
		%MEDIA = %$media;
		if($MEDIA{ID} eq $FORM{ID} or $MEDIA{filename} eq $FORM{filename}){
###if match, update view
			$MEDIA{hits}++;
			$lines[$i] = &SaveMediaDatabase($category_files_db, \%MEDIA, 1);
			&FileWrite($category_files_db, \@lines);
			$FORM{file} = $i + 1;
			$template = (-f "$category_path/media.html")?"$category_path/media.html":$TEMPLATE{media};
			if($CONFIG{slideshow_direction} eq "backward"){		$slideshow = ($i)?$i:$#lines;	}
			else{				$slideshow = ($i+1 < @lines)?$i+1:0;			}
			$prev = ($i>0)?$lines[$i-1]:0;
			$next = ($i<$#lines)?$lines[$i+1]:0;
			$MOJOTAG{user_options} = &BuildUserOptions($lines[$slideshow],$next, $prev);
			&PrintTemplate(&ParseMediaTemplate($template, \%MEDIA, $category_path));
		}
	}
### No such ID found
	if($FORM{ID}){
		&PrintError("Invalid ID <b>$FORM{ID}</b> entered", 
				"The ID you have entered is not valid. This maybe a result if you have an old bookmark <br>$media_file..@lines");
	}else{
		&PrintError("Invalid fielname<b>$FORM{filename}</b>", 
				"The filename you have entered does not exist. The media may have been removed, changed, or you have an old bookmark");
	}
}
###########################################################################
sub CheckCategory{
	unless(-d "$data_path/$FORM{cat}"){
		&PrintError("Invalid category <b>$FORM{cat}</b>", 
				"The category you have requested is not valid. Please hit back and try again");
	}
}
###########################################################################

###########################################################################
return 1;