package MainIndex;
use Exporter;
@ISA = qw(Exporter);
use strict;

# the module is of code taken out of mainIndex.pl 
# to allow display of main index by pageFormatter.pl without code duplication
# calling sequence
# my $indexText = MainIndex->mainContentLayout() ;

# to create a new index entry
# 1. enter the name of the package into the use list in mainIndex.pl
# 2. enter the name of the group into the @groupNameAry for use by pathInfo validation
# 3. use group and package names to create new elsif stmt for getting the sideIndex data
# 4. add thumb and url data for the group to the @linkAry


my $newFormatUrl    = "http://yourWebsiteDotCom/cgi-bin/gnu/";

    my @linkAry    = (
        {
            rowType            => "rowOfOne",
            rowComponents     =>[
                {
                    groupTitle            => "subject Group 1: PayPal and plain text",
                    groupId               => "subjectGroup1",
                    firstPageOfGroupUrl   => "group1TitledPictureAndPayPal.pl",
                    groupThumbUrl         => "bellasDreamIndexThumb.jpg",
                    thumbWidth            => "512",
                    thumbHeight           => "86",
                    thumbAlt              => "to subjectGroup1",
                },
            ],
        },
        {
            rowType            => "rowOfThree",
            rowComponents     =>[
                {
                    groupTitle             => "subject Group 2: unordered list",
                    groupId                => "subjectGroup2",
                    firstPageOfGroupUrl    => "group2UnorderedList.pl",
                    groupThumbUrl          => "bearThumb.jpg",
                    thumbWidth             => "143",
                    thumbHeight            => "100",
                    thumbAlt               => "Link to subjectGroup2",
                },
                {
                    groupTitle             => "subject Group 3: pre-formatted text",
                    groupId                => "subjectGroup3",
                    firstPageOfGroupUrl    => "group3PreFormattedText.pl",
                    groupThumbUrl          => "anteaterThumb.jpg",
                    thumbWidth             => "138",
                    thumbHeight            => "100",
                    thumbAlt               => "Link to subject Group 3",
                },
                {
                    groupTitle             => "subject Group 4: Use non-default CSS",
                    groupId                => "subjectGroup4",
                    firstPageOfGroupUrl    => "group4ChangeCss.pl",
                    groupThumbUrl          => "gibbonThumb.jpg",
                    thumbWidth             => "147",
                    thumbHeight            => "100",
                    thumbAlt               => "link to page that uses different CSS",
                },
            ],
        },
        {
            rowType            => "rowOfThree",
            rowComponents     =>[
                {
                    groupTitle             => "subject Group 5: list of software components",
                    groupId                => "subjectGroup5",
                    firstPageOfGroupUrl    => "group5listSoftwareComponents.pl",
                    groupThumbUrl          => "lucyAndSealThumb.jpg",
                    thumbWidth             => "155",
                    thumbHeight            => "100",
                    thumbAlt               => "link to list software components",
                },
            ],
        },
        
    ); # end of my @linkAry


sub mainContentLayout {

    # we implement the rows of the index as a sequence of divs to avoid problems with ul variations under ie.
    # Each of the index entries within a row is implemented as a div
    # three element and one element rows are distinguished by class
    
    my $text    = "";
    $text        .= "<h3 class='mainIndex'>MAIN INDEX</h3>\n";
    $text        .= "<table border='1' width='534' cellspacing='0' cellpadding='6'>\n";
    $text        .= "<tr><td> ";
    $text        .= "<table border='0' width='528' cellspacing='0' cellpadding='0'>\n";
    foreach my $rowHashRef (@linkAry) {
    
        $text    .= "<tr>\n";
        foreach  my $elemHashRef (@{ $rowHashRef->{rowComponents} }) {
            if ($rowHashRef->{rowType} eq "rowOfThree") {
            # then there are three elements, possibly empty
                $text    .= imageTdWide176($elemHashRef);
            } else {
                $text    .= imageTdWide528($elemHashRef);
            } # end if
        } # end foreach
        $text    .= "</tr><tr>\n";
        foreach  my $elemHashRef (@{ $rowHashRef->{rowComponents} }) {
            if ($rowHashRef->{rowType} eq "rowOfThree") {
            # then there are three elements, possibly empty
                $text    .= headerTdWide176($elemHashRef);
            } else {
                $text    .= headerTdWide528($elemHashRef);
            } # end if
        } # end foreach
        $text    .= "</tr>\n";
    
    } # end foreach
    $text    .= "</table> ";
    $text    .= "</td></tr></table>\n";
            
    

    return $text;

} # end sub mainContentLayout 


sub imageTdWide176 {
my $hashRef    = shift;
    my $text    = "";
    
    $text        .= "<td  width='176' align='center'> ";
    if ( exists ($hashRef->{groupTitle}) &&  defined ($hashRef->{groupThumbUrl})) {
    
        my $fileIdentifier    = $hashRef->{firstPageOfGroupUrl};
        my $url = $newFormatUrl . $hashRef->{groupId} . '/' . $fileIdentifier;

        $text    .= <a href="'. $url  . '">';
    
        $text    .= "<img src='http://yourWebsiteDotCom/gnuPictures";
        $text    .= "/". $hashRef->{groupId} ;
        $text    .= "/". $hashRef->{groupThumbUrl} ."'";
        $text    .= " class='imgMainIndex'";
        $text    .= " width='". $hashRef->{thumbWidth} ."'";
        $text    .= " height='". $hashRef->{thumbHeight} ."'";
        $text    .= " border='1'";
        $text    .= " alt='". $hashRef->{thumbAlt} ."'";
        $text    .= " />";
        $text    .= "</a>";
    } else {
        $text    .= " ";
    } # end if
    $text        .= "</td>\n";
    
    return $text;
    
} # end imageTdWide176

sub imageTdWide528 {
my $hashRef    = shift;
    my $text    = "";
    
    $text        .= "<td colspan='3' width='528' align='center'> ";
    
    
    my $fileIdentifier    = $hashRef->{firstPageOfGroupUrl};
    my $url = $newFormatUrl . $hashRef->{groupId} . '/' . $fileIdentifier;

    $text    .= '<a href="'. $url  . '">';
    
    $text    .= "<img src='http://yourWebsiteDotCom/gnuPictures";
    $text    .= "/". $hashRef->{groupId} ;
    $text    .= "/". $hashRef->{groupThumbUrl} ."'";
    $text    .= " class='imgMainIndex'";
    $text    .= " width='". $hashRef->{thumbWidth} ."'";
    $text    .= " height='". $hashRef->{thumbHeight} ."'";
    $text    .= " border='1'";
    $text    .= " alt='". $hashRef->{thumbAlt} ."'";
    $text    .= " />";
    $text    .= "</a>";
    $text    .= "</td>\n";
    
    return $text;
    
} # end imageTdWide528


sub headerTdWide176 {
my $hashRef     = shift;
    my $text    = "";
    
    $text        .= "<td  width='176' align='center' valign='top'> ";
    if ( exists ( $hashRef->{groupTitle} ) ) {
        $text    .= "<h3> ";
        
        my $fileIdentifier    = $hashRef->{firstPageOfGroupUrl};
        my $url = $newFormatUrl . $hashRef->{groupId} . '/' . $fileIdentifier;

        $text    .= <a href="'. $url  . '">';
    
        $text    .= $hashRef->{groupTitle} ;
        $text    .= "</a>";
        $text    .= "</h3>";
    } else {
        $text    .= " ";
    } # end if
    $text        .= "</td>\n";
    
    return $text;
    
} # end headerTdWide176

sub headerTdWide528 {
my $hashRef    = shift;
    my $text   = "";
    
    $text     .= "<td colspan='3' width='528' align='center' valign='top'> ";
    
    $text     .= "<h3> ";
    
    my $fileIdentifier    = $hashRef->{firstPageOfGroupUrl};
    my $url = $newFormatUrl . $hashRef->{groupId} . '/' . $fileIdentifier;

    $text    .= '<a href="'. $url  . '">';
    
    $text    .= $hashRef->{groupTitle};
    $text    .= "</a>";
    $text    .= "</h3>";
        
    $text    .= "</td>\n";
    
    return $text;
    
} # end headerTdWide528


sub sendErrorEmail{
my $cgiObj                = shift;
my $errorLocation        = shift;
my $pathInfoObjError    = shift;

# for details on sending mail see Guelich, Gundavaram and Birznieks, CGI Programming with Perl, O'Reilly, 2000, pp.222-225
    
    my $httpUserAgent  = $ENV{HTTP_USER_AGENT};
    my $httpReferer    = $ENV{HTTP_REFERER};
    my $newBrowser     = "browser: " . $httpUserAgent;
    
    my $remoteAddress    = $ENV{REMOTE_ADDR};
    
    # create the subject string
    my $subjectStr       = "Error in MainIndex->mainContentLayout()";
    
    
    my $queryString    = $cgiObj->param('pageQueryType') . "," . 
                         $cgiObj->param('queryState') . "," . 
                         $cgiObj->param('pageType') . "," . 
                         $cgiObj->param('groupId') . "," . 
                         $cgiObj->param('pictureNameRoot') . "," . 
                         $cgiObj->param('textName');
        
    my $pathInfo       = $cgiObj->path_info;
    my @pathElemAry    = split "/", $pathInfo;
    my $pictureName    = pop @pathElemAry;

    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)            = localtime;
    my $timeStampStr     = "$year:$mon:$hour:$min:$sec";
    
    

	open MAIL, "| /usr/lib/sendmail -t -i -f'gnuReturnedMailAddr_yourWebsiteDotCom'" or return undef;
	print MAIL <<END_OF_MESSAGE;
To: gnuDiagnosticMailAddr_yourWebsiteDotCom
From: gnuErrorMailAddr_yourWebsiteDotCom
Reply-To: gnuErrorHandlerMailAddr_yourWebsiteDotCom
Subject: gnuError: $subjectStr

Error occurred in MainIndex.pm in  free software
TimeStamp : $timeStampStr

errorLocation : $errorLocation
pathInfoObjError : $pathInfoObjError

remoteAddress : $remoteAddress
httpUserAgent : $httpUserAgent
httpReferer : $httpReferer
pathInfo: $pathInfo
queryString:$queryString
browser: $newBrowser
pictureName: $pictureName
END_OF_MESSAGE
     
    
    print MAIL "\n";
    
    
    close MAIL or die "Error closing sendmail in mainIndex: $!";



} # end emailVisitorLogInfo




sub subjectGroupExists {
# called from PathInfo
my $subjectGroup    = shift;
    foreach my $rowHashRef (@linkAry) {
        foreach  my $elemHashRef (@{ $rowHashRef->{rowComponents} }) {
            if ($elemHashRef->{groupId} eq $subjectGroup) {
                return 1;
            } # end if
        } # end foreach
    
    } # end foreach

    return undef;

} # end subjectGroupExists













1;