package Album;
require 5.004;

use vars qw($VERSION);

$VERSION = do {my @r=(q$Revision: 1.10 $ =~ /\d+/g);sprintf "%d."."%02d" x $#r, @r};

sub new {
	my ($proto,%params) = @_;

  my $class = ref($proto) || $proto;
  my $self = {};

  $self->{ROOTPATH}   = $params{rootpath} || undef;

  bless ($self,$class);
  return $self;
}


sub rootpath {
	my $self = shift;
  if (@_) { $self->{ROOTPATH} = shift ; }
  return $self->{ROOTPATH}
}
	

sub getAllYears {
# Returns all directories matching ([0-9]{4}) in a specific dir
	($self,%params) = @_;
	my $directory_root=$self->{ROOTPATH};
	opendir(DIR, $directory_root) || die "can't opendir $directory_root: $!";
	#@years = grep { /^[0-9]{4}$/  && -d $directory_root/$_ } readdir(DIR);
	my @years = grep { /^[0-9]{4}$/ } readdir(DIR);
  closedir DIR;  
	return @years;
}

sub getAllMonths {
	($self,%params) = @_;
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";
	my $directory_root=$self->{ROOTPATH} . "/$year";

	opendir(DIR,$directory_root) || die "can't opendir $directory_root: $!";
	my @months = grep { /^[0-9]{2}$/ } readdir(DIR);
	closedir DIR;
	return @months;
}

sub getAllDays {
	($self,%params) = @_;
	my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";

	my $directory_root=$self->{ROOTPATH} . "/$year/$month";

	opendir(DIR,$directory_root) || die "can't opendir $directory_root: $!";
  my @days = grep { /^[0-9]{2}$/ } readdir(DIR);
  closedir DIR;
	return @days;
}

sub getListOfImages {
	($self,%params) = @_;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
	my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";

	my $directory_root=$self->{ROOTPATH} . "/$year/$month/$day";

	opendir(DIR,$directory_root) || die "can't opendir $directory_root: $!";
	my @images = grep { /^mP.*\.JPG$/ } readdir(DIR);
	closedir DIR;
	@images = map { s/^m//g ; $_ } @images;
	return @images;
}

sub getMonthImages {
	($self,%params) = @_;
	my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";
	
	my @days=$self->getAllDays(month=>$month,year=>$year);

	my @images;
	foreach $day (@days) {
		@dayimg=$self->getListOfImages(day=>$day,month=>$month,year=>$year);
		foreach (@dayimg) {
			s:^:/$year/$month/$day/:g;
		}
		push @images,@dayimg;
	}
	return @images;
}

sub getYearImages {
	($self,%params) = @_;
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";

	my @months=$self->getAllMonths(year=>$year);

	my @images;
	foreach $month (@months) {
		@monthimg=$self->getMonthImages(year=>$year,month=>$month);
		push @images,@monthimg;
	}
	return @images;
}

sub getNextImage {
	($self,%params) = @_;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
  my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
  my $year = ($self->{YEAR} || $params{year}) || die "No year given!";
	my $image = ($self->{IMAGE} || $params{image}) || die "No image given!";

	my @images = $self->getListOfImages(
		day=> $day,
		month => $month,
		year => $year
	);
	my $nextimage;
	my $count=0;

	foreach (@images) {
		if (/$image/) {
			$count++;
			last;
		}
		$count++;
	}

	if ($count > $#images) {
		$nextimage='';
	} else {
		$nextimage=$images[$count];
	}

	return $nextimage;
}

sub getNextDay {
	($self,%params) = @_;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
  my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
  my $year = ($self->{YEAR} || $params{year}) || die "No year given!";

	my @days = $self->getAllDays(
		month=>$month,
		year=>$year,
	);

	my $nextday;
	my $count=0;
	foreach (@days) {
		last if ($day == $_);
		$count++;
	}	
	
	$count++;
	if ($count > $#days) {
		$nextday=0;
	} else {
		$nextday=$days[$count];
	}

	return $nextday;
}
	

sub getPreviousImage {
	($self,%params) = @_;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
  my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
  my $year = ($self->{YEAR} || $params{year}) || die "No year given!";
	my $image = ($self->{IMAGE} || $params{image}) || die "No image given!";

	my @images = $self->getListOfImages(
		day=> $day,
		month => $month,
		year => $year
	);
	my $previousimage;
	my $count=0;

	foreach (@images) {
		if (/$image/) {
			$count--;
			last;
		}
		$count++;
	}

	if ($count < 0) {
		$previousimage='';
	} else {
		$previousimage=$images[$count];
	}

	return $previousimage;
}

sub getPreviousDay {
	($self,%params) = @_;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
  my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
  my $year = ($self->{YEAR} || $params{year}) || die "No year given!";

	my @days = $self->getAllDays(
		month=>$month,
		year=>$year,
	);

	my $prevday;
	my $count=0;
	foreach (@days) {
		last if ($day == $_);
		$count++;
	}	
	
	$count--;
	if ($count < 0) {
		$prevday=0;
	} else {
		$prevday=$days[$count];
	}

	return $prevday;
}
	

sub makeRaster {
	($self,%params) = @_;
	my $maxim = $params{cols} || 5;
	my $day = ($self->{DAY} || $params{day}) || die "No day given!";
	my $month = ($self->{MONTH} || $params{month}) || die "No month given!";
	my $year = ($self->{YEAR} || $params{year}) || die "No year given!";
	my @i = @{$params{images}};
  my $str="";
	my $c=0;

  foreach my $im (@i) {
    $c++;
    if (($c+($maxim-1))/$maxim == int(($c+($maxim-1))/$maxim)) {
      $str.= '<tr>';
    }
		$str.='<td valign="center" align="middle">';
		$str.="<a href=\"?page=album&yyyy=$year&mm=$month&dd=$day&i=$im\">";
		$str.="<img src=\"$year/$month/$day/t$im\" border=\"0\">";
		$str.='</a>';
		$str.='</td>';
    if ($c/$maxim == int($c/$maxim)) {
      $str.= '</tr>';
    }
  }
  if (($#i+1)/$maxim != int(($#i+1)/$maxim)) {
    while ($c/$maxim != int($c/$maxim)) {
      $c++;
      $str.='<td>&nbsp;</td>';
    }
    $str.= '</tr>';
  }
	return $str;
}

sub makeImgRaster {
	($self,%params) = @_;
	my $maxim = $params{cols} || 5;
	my @i = @{$params{images}};
	my $thumbprefix=$params{thumbpref};
  my $str="";
	my $c=0;

  foreach my $imp (@i) {
		($dummy,$year,$month,$day,$im)=split("/",$imp);
		
    $c++;
    if (($c+($maxim-1))/$maxim == int(($c+($maxim-1))/$maxim)) {
      $str.= '<tr>';
    }
		$str.='<td valign="center" align="middle">';
		$str.="<a href=\"?page=album&yyyy=$year&mm=$month&dd=$day&i=$im\">";
		$str.="<img src=\"$year/$month/$day/$thumbprefix"."$im\" border=\"0\">";
		$str.='</a>';
		$str.='</td>';
    if ($c/$maxim == int($c/$maxim)) {
      $str.= '</tr>';
    }
  }
  if (($#i+1)/$maxim != int(($#i+1)/$maxim)) {
    while ($c/$maxim != int($c/$maxim)) {
      $c++;
      $str.='<td>&nbsp;</td>';
    }
    $str.= '</tr>';
  }
	return $str;
}

1;

