#!/usr/bin/perl 

BEGIN {
	use FindBin;
  	use lib ("$FindBin::Bin", "$FindBin::Bin/libs", $ENV{WEBSPACEROOT}{lib}, $ENV{WEBSPACEROOT}{libs}, $ENV{WEBSPACEROOT}{/};
	$ENV{TMPDIR} = $ENV{TEMP} || "";
};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer =:-)

use strict;
use warnings;

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

use CGI::Simple;

use lib::Global;
use lib::Authen;
use lib::News;
use lib::Press;
use lib::Profil;
use lib::Services;
use lib::Refs;
use lib::Contact;
use lib::Imprint;


# main call
&main;

sub handler
{
	my ($obj, $tpl, $section) = @_;

	my $sid = $obj->{-cgi}->param('sid');
	if($sid)
	{
		lib::Global::redirect($obj, "./run.pl") unless lib::Authen::check_sid($sid);
	}

	if($section eq 'login') { handler_login($obj, $tpl); }
	elsif($section eq 'profil') { handler_profil($obj, $tpl); }
	elsif($section eq 'services') { handler_services($obj, $tpl); }
	elsif($section eq 'press') { handler_press($obj, $tpl); }
	elsif($section eq 'refs') { handler_refs($obj, $tpl); }
	elsif($section eq 'showref') { handler_showref($obj, $tpl); }
	elsif($section eq 'refwnd_after') { handler_refwnd($obj, $tpl); }
	elsif($section eq 'refwnd_before') { handler_refwnd($obj, $tpl); }
	elsif($section eq 'contact') { handler_contact($obj, $tpl); }
	elsif($section eq 'imprint') { handler_imprint($obj, $tpl); }
}

sub handler_profil
{
	my ($obj, $tpl) = @_;

	my $text = lib::Profil::get_text($obj);

	$tpl->param('CONTENT' => lib::Global::format_text($obj, $text));
}

sub handler_services
{
	my ($obj, $tpl) = @_;

	lib::Services::overview($obj, $tpl);
}

sub handler_press
{
	my ($obj, $tpl) = @_;

	lib::Press::overview($obj, $tpl);
}

sub handler_refs
{
	my ($obj, $tpl) = @_;

	lib::Refs::overview($obj, $tpl);
}

sub handler_refwnd
{
	my ($obj, $tpl) = @_;

	my $id = $obj->{-cgi}->param('id');
	my $pic = $obj->{-cgi}->param('pic');
	my $what = $obj->{-cgi}->param('what');
	my $entry = lib::Refs::get_entry($obj, $id);

	my $prefix = (lc($what) eq "after" ? "nach_img" : "vor_img") . $pic;

	$tpl->param(PICID => $pic);
	$tpl->param(ID => $id);
	$tpl->param(PICSRC => $entry->{$prefix});
}

sub handler_showref
{
	my ($obj, $tpl) = @_;

	my $id = $obj->{-cgi}->param('id');
	my $entry = lib::Refs::get_entry($obj, $id);

	my @tdclasses = qw/tol tom tor/;
	my @imclasses = qw/oleft omiddle oright/;
	my @tdwidth = qw/196 184 190/;
	my @images  = qw/nach_img1 nach_img2 nach_img3/;
	my @pics = ();
	for my $i (0 .. $#images)
	{
		if(defined $entry->{$images[$i]})
		{
			my %data = ();

			$data{ANKER} = $id;
			$data{PICID} = $i+1;
			$data{PCISRC} = $entry->{$images[$i]};
			$data{TDWIDTH} = $tdwidth[$i];
			$data{TDCLASS} = $tdclasses[$i];
			$data{IMGCLASS} = $imclasses[$i];

			push @pics, \%data;
		}
	}

	$tpl->param(REFPICTURES => \@pics);

	$tpl->param('ANKER' => $id);
	foreach (keys %{$entry})
	{
		$tpl->param(uc($_) => $entry->{$_});
	}
}

sub handler_contact
{
	my ($obj, $tpl) = @_;

	my ($email, $address) = lib::Contact::get_address_and_email($obj);

	$tpl->param('ADDRESS' => lib::Global::format_text($obj, $address));
	$tpl->param('EMAIL' => $email);

	my $result = lib::Contact::send($obj, $email);
	if(defined $result) {
		my $msg = "Nachricht wurde erfolreich zugestellt.";
		$msg = "Es ist ein Fehler beim Versand der Nachrricht aufgetreten." unless $result;

		$tpl->param('ERROR' => $msg);
	}
}

sub handler_imprint
{
	my ($obj, $tpl) = @_;

	my $text = lib::Imprint::get_text($obj);

	$tpl->param('CONTENT' => lib::Global::format_text($obj, $text));
}

sub handler_login
{
	my ($obj, $tpl) = @_;

	if($obj->{-cgi}->param('submit'))
	{
		my $user = $obj->{-cgi}->param('user');
		my $pass = $obj->{-cgi}->param('password');

		if(my $usertype = lib::Authen::login($user, $pass))
		{
			my $sid = lib::Authen::create_sid($usertype);
			lib::Global::redirect($obj, "./run.pl?sid=$sid");
		}
		else
		{
			lib::Global::set_error($obj, $tpl, 'login');
		}
	}

	1;
}


sub main
{
	my $obj = {};

	$obj->{-cgi} = CGI::Simple->new();
	$obj->{-cgi}->parse_query_string();


	# create template
	my $section = lc($obj->{-cgi}->param('section')) || 'profil';

	my $tpl = lib::Global::tpl_create($obj, $section);

	# output

	handler($obj, $tpl, $section);

	lib::News::overview($obj, $tpl);
	lib::Global::write_standardhttp_header($obj);
	lib::Global::tpl_defaultset($obj, $tpl);

	print $tpl->output;

	return 1;
}
