/*

	Author: 
		Justin Emond
	
	Date:
		20080819 - case 11865
		
	Description:
		Widget gallery tool. Installation instructions on wiki.

*/

// internal settings
var nImages = 0;
var nCurrentImage;
var fScroll = true; // pause control

function startMeUp()
{
	// number of images
	$("#Photos > img").each(function(){
		nImages++;
	});
	
	$("#Photo p.image img").bind("click", function(){
		
	});
	
	// get start image
	if(fStartRandom)
		nCurrentImage=getRandomInt(1,nImages);
	else
		nCurrentImage=1;
	
	// setup image base
	if(fControls)
		$("#Photo p.controls").html(getControlHtml());	
	else
		$("#Photo p.controls").remove();				
	loadImage(nCurrentImage);
	
	// start the scroll if it's enabled
	if(fImageScroll)
		window.setTimeout("loopImages()",nScrollSpeed*1000);	
}

function getRandomInt(start,finish)
{
	var n;
	n = Math.floor(Math.random()*(finish));
	return n+1;
}

// load next image
function loadImage(n)
{
	if(n > nImages)
		n = 1;
		
	if(n < 1)
		n = nImages;
		
	nCurrentImage=n;

	// set random
	var count = 1;
	$("#Photos img").each(function(){
		if(count == nCurrentImage)
		{
			var sSrc = $(this).attr("src");
			var sAlt = $(this).attr("alt");
			var sUrl = $(this).attr("class");
			
			$("#Photo p.image img").attr("src",sSrc).attr("alt",sAlt).attr("title",sAlt);
			
			if(fCaption) {
				if(sUrl != "")
					$("#Photo p.caption").html("<a href='"+sUrl+"'>"+sAlt+"</a>");
				else
					$("#Photo p.caption").html(sAlt);
			}
			else
				$("#Photo p.caption").hide();
			
			if(fNumbers)
				$("#Photo p.numbers").html(getPhotoList(nCurrentImage));
			else
				$("#Photo p.numbers").hide();

			$("#Photo p a img, #Photo p.caption").hide().fadeIn();
		}
		count++;
	});
}

function getPhotoList(n)
{
	var s = "";
	for(i=1;i<=nImages;i++)
	{
		if(i == nCurrentImage)
			s = s + " " + i;
		else
			s = s + " <a href=\"javascript:moveImage("+i+")\">"+i+"</a>";
	}
	
	// pause control
	if(fImageScroll)
	{
		if(fScroll)
			sPauseWord = "Pause";
		else
			sPauseWord = "Unpause";
			
		s = s + " <p class=\"pause\"><a href=\"javascript:pauseScroll();\">"+sPauseWord+"</a></p>";
	}
	
	return s;
}

function getControlHtml()
{
	return '<a href="javascript:previousImage();">Previous</a> <a href="javascript:nextImage();">Next</a>';
}

function nextImage()
{
	moveImage(nCurrentImage+1);	
}

function previousImage()
{
	moveImage(nCurrentImage-1);
}

function pauseScroll()
{
	if(fScroll)
	{
		fScroll = false;
		$("#Photo .pause > a").html("Unpause");	
	}
	else
	{
		fScroll = true;
		$("#Photo .pause > a").html("Pause");	
		loopImages();	
	}
}			

function moveImage(n)
{
	$("#Photo p a img, #Photo p.caption").fadeOut(function(){
		loadImage(n);
	});	
}

function loopImages()
{
	if(fScroll)
	{
		nextImage();
		window.setTimeout("loopImages()",nScrollSpeed*1000);
	}
}