﻿var totalThumbnailGroups;
var currentGroup = 1;
var scrollingSemaphore = true;

function getThumbnailGroupCount()
{
	if (totalThumbnailGroups === undefined || totalThumbnailGroups === null)
	{
		totalThumbnailGroups = 0;
		var totalThumbnails = 0;
	
		var ulChildren = $('galleryThumbs').childNodes;
		for (var i = 0; i < ulChildren.length; i++)
		{
			if (ulChildren[i].nodeName == "LI")
			{
				totalThumbnails++;
				if (totalThumbnails % 5 == 1)
				{
					totalThumbnailGroups++;
				}
			}
		}
		
		$('galleryThumbs').style.width = (totalThumbnailGroups * 540) + "px";
	}
	
	return totalThumbnailGroups;
}

function advanceThumbnailGroup() 
{
	if (currentGroup < getThumbnailGroupCount() && scrollingSemaphore) 
	{
		scrollingSemaphore = false;
		currentGroup++;    			

		$('backButton').style.display = "block";
		$('backButtonDisabled').style.display = "none";
		if (currentGroup == getThumbnailGroupCount())
		{
			$('forwardButton').style.display = "none";
			$('forwardButtonDisabled').style.display = "block";
		}

		new Effect.Move($('galleryThumbs'), { x: -540, y: 0, mode: 'relative', afterFinish: function(){scrollingSemaphore = true} });
	}
}

function retreatThumbnailGroup() 
{
	if (currentGroup != 1 && scrollingSemaphore) 
	{
		scrollingSemaphore = false;
		currentGroup--;
		
		$('forwardButton').style.display = "block";
		$('forwardButtonDisabled').style.display = "none";
		if (currentGroup == 1)
		{
			$('backButton').style.display = "none";
			$('backButtonDisabled').style.display = "block";
		}
		
		new Effect.Move($('galleryThumbs'), { x: 540, y: 0, mode: 'relative', afterFinish: function(){scrollingSemaphore = true} });
	}
}

function embedFlashPlayer(linkButton) {
    var flashvars = {
        movie: linkButton.getAttribute("movie"),
        img: linkButton.getAttribute("largeImage")
    };
    swfobject.embedSWF(linkButton.getAttribute("player"), "flashPlayer", "640", "360", "8.0.0", "expressInstall.swf", flashvars);
}

function changeMainImage(linkButton)
{
	selectThumbnail(linkButton);

	var largeImage = linkButton.getAttribute("largeImage");
	var caption = linkButton.getAttribute("alt");
	var isVideo = linkButton.getAttribute("isVideo") == "True";

	if (isVideo) {
	    embedFlashPlayer(linkButton);
	    $('mainImage').hide();
	    $('flashPlayer').show();
	}
	else {
	    $('flashPlayer').hide();
	    $('mainImage').src = largeImage;
	    $('mainImage').alt = caption;
	    $('mainImage').show();
	    $('imageCaption').innerHTML = caption == "" ? "&nbsp;" : caption;
	    $('mainImage').focus();
	}
}

function selectThumbnail(linkButton)
{
	var ulChildren = $('galleryThumbs').childNodes;
	for (var i = 0; i < ulChildren.length; i++) 
	{
		if (ulChildren[i].nodeName == "LI") 
		{
			var liChildren = ulChildren[i].childNodes;
			for (var j = 0; j < liChildren.length ; j++)
			{
				if (liChildren[j].nodeName == "DIV")
				{
					liChildren[j].setAttribute("class", "thumbnail");
					liChildren[j].setAttribute("className", "thumbnail");
				}
				
				/*
				if (liChildren[j].nodeName == "A")
				{
					var aChildren = liChildren[j].childNodes;
					for (k = 0; k < aChildren.length; k++)
					{
						if (aChildren[k].nodeName == "IMG") 
						{
							aChildren[k].setAttribute("class", "");
							aChildren[k].setAttribute("className", "");
						}
					}
				}
				*/
			}
		}
	}

	linkButton.parentNode.setAttribute("class", "thumbnail selected");
	linkButton.parentNode.setAttribute("className", "thumbnail selected");

	/*
	for (var i = 0; i < linkButton.childNodes.length; i++)
	{
		if (linkButton.childNodes[i].nodeName == "IMG")
		{
    		linkButton.childNodes[i].setAttribute("class", "selected");
			linkButton.childNodes[i].setAttribute("className", "selected");
		}
	}
	*/
}