# -------------------------------------------------------------------------------------
# flo::Template::Sibling
# -------------------------------------------------------------------------------------
#
#         Author : Jean-Michel Hiver (jhiver@cisedi.com)        (c) MKDoc Holdings Ltd.
#    Description : Provides the structure for the bread
#                  crumb trail loop.
#
#    <% sibling set="Sibling" %>
#    <% var name="Sibling:Previous:URI" %>
#    <% var name="Sibling:Previous:Title" %>
#    <% var name="Sibling:Next:URI" %>
#    <% var name="Sibling:Next:Title" %>
#
# -------------------------------------------------------------------------------------
package flo::Template::Sibling;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use flo::Standard;
use strict;
use vars qw /@ISA/;
@ISA = qw /lib::Template::AbstractTag/;


sub main
{
    my $self = shift;
    my $result = $self->result;
    my $tokens = $self->tokens;
    my ($token_name, $token_hash) = $self->parse_tag (shift @{$tokens});
    
    my $symbol = $token_hash->{set} or
        throw (new lib::Exception ( code => 'VARIABLE_NOT_SET',
                                    info => "$token_name should have a 'symbol' attribute" ) );
    
    my $document_table = table ('Document');
    my $document = $document_table->get ( Full_Path => path_info );
    
    # if this document is the root document, then @siblings is the document itself
    my @siblings = ();
    if ($document->Parent)
    {
	@siblings = $document->Parent->Children;
    }
    else
    {
	@siblings = $document;
    }
    
    # this hash will hold Previous and Next values
    my $hash = {
	Previous => undef,
	Next     => undef,
    };
    
    for (my $i=0; $i < @siblings; $i++)
    {
	if ($siblings[$i]->ID == $document->ID)
	{
	    if ($i > 0)         { $hash->{Previous} = $siblings[$i - 1] }
	    if ($i < @siblings) { $hash->{Next}     = $siblings[$i + 1] } 
	    last;
	}
    }
    
    $lib::Template::STASH->{$symbol} = $hash;
}


1;
