/* Addtional Images Functionality
 * Preloader for all images, and also navigation functions.
 * This script required the following arrays to be initialized so that it can function properly
 *
 * img[]
 */

function AdditionalImages()
{
	this.completed = 0;
	this.currentPage = 0;
	this.loader = new aiPage('ai_preloader','none');
	this.pager = new aiPage('ai_pager','none');
	this.imgs = [];
	this.pages = [];
	this.PreLoad = aiPreLoad;
	this.CheckLoad = aiCheckLoad;
	this.Next = aiNext;
	this.Prev = aiPrevious;
}

function aiPreLoad(instance)
{
	if (document.images)
	{
		/* Switch to preloader page */
		
		
		/* Start the preloading process if 1 or more images. */
		if (img.length > 0)
		{
			this.loader.div.style.display = "block";
			for (i=0; i<img.length; i++)
			{
				this.completed = 0;
				this.imgs[i] = [];
				this.imgs[i][0] = new Image();
				this.imgs[i][0].src = img[i];
				this.imgs[i][1] = setInterval(instance + ".CheckLoad(" + i + ")", 500);
				this.pages[i] = new aiPage('ai_p_' + i, 'none');
			}
		}
	}
	else
	{
		/* We are not preloading images. */
		alert('Your browser does not support JavaScript, or JavaScript is disabled, please enable it if possible.');
	}
}

function aiCheckLoad(index)
{
	if(this.imgs[index][0].complete)
	{
		clearInterval(this.imgs[index][1]);
		this.completed++;
		/* If all images complete, show pager.. */
		if (this.completed == this.imgs.length)
		{
			this.loader.div.style.display = "none";
			if (this.completed > 1)
				this.pager.div.style.display = "block";
			this.pages[this.currentPage].div.style.display = "block";
		}
	}
}

function aiPage(id, startstate)
{
	cur_div = document.getElementById(id);
	if (cur_div != null)
	{
		this.div = cur_div;
		this.div.style.display = startstate;
	}
}

function aiNext()
{
	this.currentPage = ((this.currentPage + 1) % this.pages.length);
	for(var i=0; i<this.pages.length; i++)
	{
		this.pages[i].div.style.display = "none";
	}
	this.pages[this.currentPage].div.style.display = "block";
}

function aiPrevious()
{
	this.currentPage = (((this.pages.length + this.currentPage) - 1) % this.pages.length);
	for(var i=0; i<this.pages.length; i++)
	{
		this.pages[i].div.style.display = "none";
	}
	this.pages[this.currentPage].div.style.display = "block";
}
