//constants used for the exhibit Artworks array
var THUMBNAIL = 0
var ID = 1
var INFO = 2

var exhibitArtworks

//populates the picture selector under the article
function populateSelector()
{
	var optionList = document.getElementById("picSelector")
	optionList.innerHTML = ""
	
	for (i=0; i < exhibitArtworks.length; i++)
	{
		optionList.innerHTML += "<option value="+i+">"+exhibitArtworks[i][TITLE]+"</option>" 
	}
	
	document.getElementById("picSelector").selectedIndex = 0 //select the first index
	document.getElementById("previewPic").src = exhibitArtworks[0][THUMBNAIL] //display the first picture
	document.getElementById("picLink").href = "viewArtwork.php?id="+exhibitArtworks[0][ID]
}

function populatePics(aLimit)
{
	var picData = ""
	for (i = 0; i < aLimit; i++)
	{
		var currentWork = getRandomWork()
		var thumbnail = currentWork[THUMBNAIL]
		var id = currentWork[ID] 
		var info = currentWork[INFO] 

		picData += "<a href='viewArtwork.php?id="+id+"'><img src='"+thumbnail+"'></a><BR>"
		picData += info+"<BR><BR>"

	}
	document.getElementById('pictureDiv').innerHTML = picData
}

function getRandomWork()
{
	var randomIndex = Math.floor(Math.random() * exhibitArtworks.length)
	var chosenWork = exhibitArtworks[randomIndex]
	exhibitArtworks.splice(randomIndex, 1)

	return chosenWork
}

function viewThumbnail()
{
	if (exhibitArtworks.length > 0)
	{
		var optionList = document.getElementById("picSelector")
		var sIndex = optionList.selectedIndex
		var selectedData = exhibitArtworks[sIndex]

		//display the first picture
		document.getElementById("previewPic").src = exhibitArtworks[sIndex][THUMBNAIL] 
		document.getElementById("picLink").href = "viewArtwork.php?id="+exhibitArtworks[sIndex][ID]
		document.getElementById("previewLabel").innerHTML = exhibitArtworks[sIndex][INFO]
	}
}

function changeBranch(aSelector)
{
	window.location = 'exhibits.php?this_id=' + aSelector.value
}

function initialize(aSelectedIndex) 
{
	var branchSelector = document.getElementById('branchSelector')
	branchSelector.selectedIndex = aSelectedIndex

	//choose random artworks displayed the right side
	if (exhibitArtworks.length >= 5 ) //place 3 works on the right side and the rest in the selector
	{
		populatePics(3)
		populateSelector() //populate the selector at the bottom of the article

		//make the selector at the bottom visible
		document.getElementById('selectorDiv').style.display = 'block'
		viewThumbnail()
	}
	else if (exhibitArtworks.length == 4)
	{
		populatePics(2)
		populateSelector() //populate the selector at the bottom of the article

		//make the selector at the bottom visible
		document.getElementById('selectorDiv').style.display = 'block'
		viewThumbnail()
	}
	else //display everything on the right side and make the selector invisible
	{
		populatePics(exhibitArtworks.length)

		//make the selector at the bottom invisible
		document.getElementById('selectorDiv').style.display = 'none'
	}

	//select the correct index in the exhibit selector
}
