// Copyright 2009 J.W. Friedman, All Rights Reserved.

/**
 * Setup function for the page.
 */
function initPage() {
	galleryData = [
		{name: "Research", uriStrand: "research"},
		{name: "Held", uriStrand: "held"},
		{name: "Leaves", uriStrand: "leaves"}
	];
	
	Event.observe($('header'), 'click', function() { loadHTMLContent('about', 'content_body'); });
	Event.observe($('nav-about'), 'click', function() { loadHTMLContent('about', 'content_body'); });
	Event.observe($('nav-images'), 'click', function () { loadGalleryContent(); });
	Event.observe($('nav-exhibitions'), 'click', function () { loadHTMLContent('exhibitions', 'content_body'); });
	
	seriesNav = $('series_nav');
	galleryView = $('series-view');
	contentView = $('content_body');
	imageView = $('image-view');
	galleriesPopulated = false;
	randomizeSidebar();
	galleryView.hide();
	loadHTMLContent('about', 'content_body');
};

/**
 * Loads content into the main section of the page.
 *
 * @param {string} contentURI The content's identifier.
 * @param {string} contentTarget The element to load the content into.
 */
function loadHTMLContent(contentId, contentTarget) {
	seriesNav.hide();
	galleryView.hide();
	imageView.hide();
	$(contentTarget).show();
	var url = 'partials/'+contentId+'.html';
	var target = contentTarget;
	var ajaxCall = new Ajax.Updater(target, url, {method: 'get'});	
};

/**
 * Calls the php script that constructs a thumbnail gallery
 * of images and returns the HTML.
 *
 * @param {string} opt_seriesId The ID that is used to identify the
 *     series of images.  If empty, randomizes.
 */
function loadGalleryContent(opt_seriesId) {
	contentView.hide();
	imageView.hide();
	showSeriesNav();
	galleryView.show();

	if (opt_seriesId == undefined) {
		var opt_seriesId = Math.ceil(Math.random() * galleryData.length) - 1;
	}
	var url = 'php/gallery.php';
	var params = {
		title: galleryData[opt_seriesId].name,
		uri: 'gallery/' + galleryData[opt_seriesId].uriStrand,
		id: opt_seriesId
	};
	var ajaxRequest = new Ajax.Request(url,
	    {
			method: 'post',
			parameters: params,
			onSuccess: function(response) {
				galleryView.update(response.responseText);	
			},
			onFailure: function() {
				alert('error');
			}
		});
};

/**
 * Constructs the series nav bar if required and shows it regardless.
 */
function showSeriesNav() {
	seriesNav.show();

	if (!galleriesPopulated) {
		for (var i = 0, series; series = galleryData[i]; i++) {
			var newItem = new Element('li');
			var linkTarg = 'javascript: loadGalleryContent('+i+')';
			var newLink = new Element('a',
				{
					href: linkTarg
				});
			newLink.addClassName('series_link');
			newLink.update(series.name);
			newItem.appendChild(newLink);
			$('series_nav_list').appendChild(newItem);
		}
		galleriesPopulated = true;
	}
};

function displayImage(imagePath, id) {
	galleryView.hide();
	contentView.hide();
	imageView.show();
	var newImg = new Element('img',
		{
			src: imagePath
		}
	);
	$('image-view-content').update(newImg);
	var backLink = new Element('a',
		{
			href: 'javascript: loadGalleryContent('+id+')'
		}).update('< '+galleryData[id].name);
	backLink.addClassName('series_link');
	$('image-view-back').update(backLink);
};

function randomizeSidebar() {
	var ajaxRequest = new Ajax.Request('php/sidebar_randomizer.php',
	{
		method: 'get',
		onSuccess: function(response) {
			var newImg = new Element('img',
				{
					src: response.responseText
				});
			$('random-img').update(newImg);
		},
		onFailure: function() {
			alert('error');
		}
	});	
};

Event.observe(window, 'load', function() { initPage() });