//----------------------------------------------------------------------------------------
//	fotosite-utils
//
// 	<http://www.muratnkonar.com/otherstuff/fotosite>
//
//	(C) 2006 murat n konar
//
//----------------------------------------------------------------------------------------


// ------------------------------------------------------------------------------------------------------------
//	UnEncodeHTML
// ------------------------------------------------------------------------------------------------------------
function UnEncodeHTML(htmlString)
{
	var unencodedString = new String("");
	var open = false;
	var charCode = 0;
	
	for (var index = 0; index < htmlString.length; index++) 
	{
		var c = htmlString.charAt(index);
	
		if (!open && c=="&" && htmlString.charAt(index+1) == "#")
		{
			open = true
			charCode = ""
			continue
		}
		else if (open && c==";")
		{
			open = false
			unencodedString += String.fromCharCode(Number(charCode))
			continue
		}
		
		if (open)
		{
			switch (c)
			{
				case "x":
					charCode += "0x"
					break;
				
				case "#":
					// skip
					break;
					
				default:
					charCode += c
			}
		}
		else
		{
			unencodedString += c
		}
	}
		
	return unencodedString;
}

// ------------------------------------------------------------------------------------------------------------
//	showBio
// ------------------------------------------------------------------------------------------------------------
function showBio()
{
	if (document.galleryIndex != -2000)
	{
		var baseURL 	= document.URL.split("?")[0]
		var newURL 		= baseURL + "?" + String(-2000)
		
		top.location.href = newURL
	}
}


// ------------------------------------------------------------------------------------------------------------
//	showFoto
// ------------------------------------------------------------------------------------------------------------
function showFoto(inGalleryIndex, inFotoIndex)
{
	if (document.galleryIndex != inGalleryIndex || document.fotoIndex != inFotoIndex)
	{
		var baseURL 	= document.URL.split("?")[0]
		var newURL 		= baseURL + "?" + String(inGalleryIndex * 1000 + inFotoIndex)
		
		top.location.href = newURL
	}
}

// ------------------------------------------------------------------------------------------------------------
//	showPreviousFoto
// ------------------------------------------------------------------------------------------------------------
function showPreviousFoto()
{
	var fotoCount = document.toc.galleries[document.galleryIndex].entries.length
	showFoto(document.galleryIndex, (fotoCount + document.fotoIndex - 1) % fotoCount )
}

// ------------------------------------------------------------------------------------------------------------
//	showNextFoto
// ------------------------------------------------------------------------------------------------------------
function showNextFoto()
{
	var fotoCount = document.toc.galleries[document.galleryIndex].entries.length
	showFoto(document.galleryIndex, (document.fotoIndex + 1) % fotoCount )
}


// ------------------------------------------------------------------------------------------------------------
//	galleryIndexFromURL
// ------------------------------------------------------------------------------------------------------------
function galleryIndexFromURL(inURL)
{
	var theGalleryIndex = 0;
	var indexString = inURL.split("?")[1]
	
	if (indexString)
	{
		theGalleryIndex = Math.floor(Number(indexString)/1000) 
	}
	
	return theGalleryIndex;
}

// ------------------------------------------------------------------------------------------------------------
//	fotoIndexFromURL
// ------------------------------------------------------------------------------------------------------------
function fotoIndexFromURL(inURL)
{
	var theFotoIndex = 0;
	var indexString = inURL.split("?")[1]
	
	if (indexString)
	{
		theFotoIndex = Number(indexString)%1000;
	}
	
	return theFotoIndex;
}

// ------------------------------------------------------------------------------------------------------------
//	fotoPath
// ------------------------------------------------------------------------------------------------------------
function fotoPath()
{
	return document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotopath
}

// ------------------------------------------------------------------------------------------------------------
//	fotoHeight
// ------------------------------------------------------------------------------------------------------------
function fotoHeight()
{
	return document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoheight
}

// ------------------------------------------------------------------------------------------------------------
//	fotoWidth
// ------------------------------------------------------------------------------------------------------------
function fotoWidth()
{
	return document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotowidth
}

// ------------------------------------------------------------------------------------------------------------
//	fotoAspectRatio
// ------------------------------------------------------------------------------------------------------------
function fotoAspectRatio()
{
	return fotoWidth()/fotoHeight
}


// ------------------------------------------------------------------------------------------------------------
//	fotoInfo
// ------------------------------------------------------------------------------------------------------------
function fotoInfo()
{
	/* fotoInfo() returns an unordered list of foto info in the form:
			
			<ul class="fotoinfo-list">
			
				<li class="fotoinfo-item">
					<ul>
						<li class="fotoinfo-tag">tag text</li>
						<li class="fotoinfo-data">data text</li>
					</ul>
				</li>
				
				<li class="fotoinfo-item">
					<ul>
						<li class="fotoinfo-tag">tag text</li>
						<li class="fotoinfo-data">data text</li>
					</ul>
				</li>
				
				<li class="fotoinfo-item">
					<ul>
						<li class="fotoinfo-tag">tag text</li>
						<li class="fotoinfo-data">data text</li>
					</ul>
				</li>
			</ul>
	*/
	

	var infoString = ""
	var infoArray = document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoinfo

	if (infoArray.length > 0)
	{

		infoString = '<ul class="fotoinfo-list">'

		for(var i = 0; i < infoArray.length; i++)
		{
			var infoTag = document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoinfo[i].infotag
			var infoData = document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoinfo[i].infodata
			var infoLink = document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoinfo[i].infolink
		
			if (infoLink.length)
			{
				if (infoLink.indexOf("@") > 0)
				{
					// infoLink looks like an email address.
					infoLink = "mailto:"+infoLink
				}

				infoString += '<li class="fotoinfo-item"><ul><li class="fotoinfo-tag">' + infoTag + '</li><li class="fotoinfo-data"><a href="' + infoLink + '" target="_blank">' + infoData + '</a></li></ul></li>'
			}
			else
			{
				infoString += '<li class="fotoinfo-item"><ul><li class="fotoinfo-tag">' + infoTag + '</li><li class="fotoinfo-data">' + infoData + '</li></ul></li>'
			}
		}
	
		infoString += '</ul>'
	}
	return infoString
}

// ------------------------------------------------------------------------------------------------------------
//	currentGalleryName
// ------------------------------------------------------------------------------------------------------------
function currentGalleryName()
{
	var currentGalleryName;
		
	if (document.galleryIndex == -2)
	{
		currentGalleryName = document.siteDetails.bioTitle;
	}
	else
	{
		currentGalleryName = document.toc.galleries[document.galleryIndex].name;
	}
	return currentGalleryName
}

// ------------------------------------------------------------------------------------------------------------
//	"main"
// ------------------------------------------------------------------------------------------------------------
document.toc = new Toc();
document.siteDetails = new SiteDetails();

document.galleryCount 	= document.toc.galleries.length;

document.galleryIndex 	= galleryIndexFromURL(document.URL);
document.fotoIndex 		= fotoIndexFromURL(document.URL);

document.fotoCount	 	= new Array();

for(var g = 0; g < document.galleryCount; g++)
{
	document.fotoCount[g] = document.toc.galleries[g].entries.length;
}