<?php

include 'php/classes/page.php';
include 'functions/func_filesystem.php';
$page = new Page();

# gather album data and which album we want to display
$albums_xml_path = $DOCROOT.'/site/data/photosandvideos/albums.xml';
$albums = new SimpleXMLElement(file_get_contents($albums_xml_path));
$requested_album = null;
$album_data = null;
$first_photo = null;
$subalbums = array();
$thumbs = array();
if(isset($_GET['which']))
{
	$requested_album = $_GET['which'];
	# find the data for the requested album
	foreach($albums->album as $album)
	{
		if((int)$album['id'] == $requested_album)
		{
			$album_data = $album;
			break;
		}
	}
	
	# if album_data is still empty then we never found a matching album!
	if(is_null($album_data))
		$page->addError('Album does not exist!');
	else
	{
		# get the sub albums for this album
		foreach($albums->album as $album)
		{
			if((int)$album->subalbumof == $requested_album)
				array_push($subalbums,$album);
		}

		# get the thumbnails for this albums
		$thumbs_path = $DOCROOT.'/site/data/photosandvideos/albums/'.$album_data->imagefolder;
		$thumbs = filesys_get_wanted_children($thumbs_path,'folders','/^thumb_.*\.(jpg|JPG)$/');

		# get the first photo that is initially displayed in the album window
		$first_photo = str_replace('thumb_','small_',reset($thumbs));
	}
}
else
{
	$page->addError('Album does not exist.');
}

$page->attr('title','Discipling the Nations Album: '.$album->name);
$page->content('album.php');

$page->display('header',false);
$page->display('footer',false);
$page->display('menu',false);

$page->useCSSFile('album.css');
$page->useJSFile('photosandvideos.js');

# pass data to template
$page->data('album',$album_data);
$page->data('subalbums',$subalbums);
$page->data('thumbs',$thumbs);
$page->data('first_photo',$first_photo);
$page->data('num_photos',count($thumbs));

$page->process();

?>