/**
* Assign the view handler
*/

viewHandler = Installations;

/**
* Creates a new object with methods used by the Installations page
*
* @author				Matt Gifford
* @copyright			2008 Timeshifting Interactive Limited
*/
function Installations()
	{
	// Step 1. Define Properties

	var _instance = this;
	this.currentThumbnail = 0;
	this.totalThumbnails = 0;
	this.precache = [];



	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Call generic page init method
		this.base.init.call(this);

		// Add event handlers to thumbnails
		var thumbnails = document.getElementById('installationsThumbnails').getElementsByTagName('div');
		for (var x = 0; x < thumbnails.length; x++)
			{
			// Add event handler
			thumbnails[x].onclick = xhtml.displayPhoto;

			// Cache photo
			this.precache[this.precache.length] = new Image();
			this.precache[this.precache.length-1].src = thumbnails[x].getElementsByTagName('img')[0].src.replace('thumbnail','fullsize');
			}
		if (0 < thumbnails.length)
			{
			thumbnails[0].className += ' active';
			this.totalThumbnails = thumbnails.length;
			}
		}



	/**
	* Displays the thumbnail clicked on
	*/
	this.displayPhoto = function()
		{
		// Update the current thumbnail
		var thumbnails = document.getElementById('installationsThumbnails').getElementsByTagName('div');
		for (var x = 0; x < thumbnails.length; x++)
			{
			thumbnails[x].className = thumbnails[x].className.replace(/\s?active/g, '');
			if (thumbnails[x] == this)
				{
				thumbnails[x].className += ' active';
				xhtml.currentThumbnail = x;
				}
			}

		// Update the fullsize image
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('img')[0].src = thumbnails[xhtml.currentThumbnail].getElementsByTagName('img')[0].src.replace('thumbnail','fullsize');

		// Update the description
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('p')[0].innerHTML = '<span>' + (thumbnails[xhtml.currentThumbnail].getElementsByTagName('a')[0].innerHTML).replace(/<([^>]+)>/ig, ' ') + '</span>';
		}


	/**
	* Displays the previous image
	*/
	this.previousPhoto = function()
		{
		// Decreament the current index
		xhtml.currentThumbnail--;
		if (xhtml.currentThumbnail < 0)
			{
			xhtml.currentThumbnail = xhtml.totalThumbnails - 1;
			}
		
		// Update the current thumbnail
		var thumbnails = document.getElementById('installationsThumbnails').getElementsByTagName('div');
		for (var x = 0; x < thumbnails.length; x++)
			{
			thumbnails[x].className = thumbnails[x].className.replace(/\s?active/g, '');
			if (x == xhtml.currentThumbnail)
				{
				thumbnails[x].className += ' active';
				}
			}

		// Update the fullsize image
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('img')[0].src = thumbnails[xhtml.currentThumbnail].getElementsByTagName('img')[0].src.replace('thumbnail','fullsize');

		// Update the description
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('p')[0].innerHTML = '<span>' + (thumbnails[xhtml.currentThumbnail].getElementsByTagName('a')[0].innerHTML).replace(/<([^>]+)>/ig, ' ') + '</span>';
		}


	/**
	* Displays the previous image
	*/
	this.nextPhoto = function()
		{
		// Decreament the current index
		xhtml.currentThumbnail++;
		if (xhtml.currentThumbnail == xhtml.totalThumbnails)
			{
			xhtml.currentThumbnail = 0;
			}
		
		// Update the current thumbnail
		var thumbnails = document.getElementById('installationsThumbnails').getElementsByTagName('div');
		for (var x = 0; x < thumbnails.length; x++)
			{
			thumbnails[x].className = thumbnails[x].className.replace(/\s?active/g, '');
			if (x == xhtml.currentThumbnail)
				{
				thumbnails[x].className += ' active';
				}
			}

		// Update the fullsize image
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('img')[0].src = thumbnails[xhtml.currentThumbnail].getElementsByTagName('img')[0].src.replace('thumbnail','fullsize');

		// Update the description
		document.getElementById('installationsPhotoFullsize').getElementsByTagName('p')[0].innerHTML = '<span>' + (thumbnails[xhtml.currentThumbnail].getElementsByTagName('a')[0].innerHTML).replace(/<([^>]+)>/ig, ' ') + '</span>';
		}
	}

