/**
* Popup Before & After Page Interaction Functionality
*
* @author      		Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version        	1.1
*/


popup = new PopupBeforeAfter();
window.onload = new Function("popup.init();");


/**
* Creates a new Popup Before & After object that handles the functionality for the popup gallery page
*
* @author      		Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version        	1.0
*/
function PopupBeforeAfter()
	{
	// Step 1. Define Properties

	this.initialized = false;
	this.imageCache = new Array(0);

	// Import global settings variables
	if( typeof(settings) != "undefined")
	{
	    this.current = settings.current;
	    this.total = settings.total;
	    this.prefix = settings.prefix;
    }

	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Update image
		document.getElementById('image').firstChild.src = this.url(this.current);
	
		// Preload the images
		for (var x = 1; x <= this.total; x++)
			{
			this.imageCache[x] = this.url(x);
			}

		// Set class as initialized
		this.initialized = true;
		}

	
	/**
	* Display the previous image
	*/
	this.prev = function()
		{
		// Update current image number
		this.current--;
		if (this.current < 1)
			{
			this.current = this.total;
			}

		// Update image
		document.getElementById('image').firstChild.src = this.url(this.current);

		// Update Status
		this.updateStatus();
		}


	/**
	* Display the previous image
	*/
	this.next = function()
		{
		// Update current image number
		this.current++;
		if (this.total < this.current)
			{
			this.current = 1;
			}

		// Update image
		document.getElementById('image').firstChild.src = this.url(this.current);

		// Update Status
		this.updateStatus();
		}


	/**
	* Updates the image "x" of "x" text on the page
	*/
	this.updateStatus = function()
		{
		var newText = 'Image ' + this.current + ' of ' + this.total;
		document.getElementById('status').replaceChild( document.createTextNode( newText ), document.getElementById('status').firstChild);
		}


	/**
	* Builds the filename url for the new image
	*
	* @param			num			The new image number
	*/
	this.url = function(num)
		{
		return this.prefix + (num < 100 ? '0' : '') + (num < 10 ? '0' : '') + num + '.jpg';
		}



	// Step 3. Define Private Methods

	/**
	* Preloads a image file
	*
	* @param			url			The url of the image preload
	*/
	function newImage(url)
		{
		if (document.images)
			{
			imgObj = new Image();
			imgObj.src = url;
			return imgObj;
			}
		}
	}
