// JavaScript Document


// ------------------------------------------------------------------------------------------------------------
//	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;
}




// ------------------------------------------------------------------------------------------------------------
//	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
	if (fotoCount > 0)
	{
		showFoto(document.galleryIndex, (fotoCount + document.fotoIndex - 1) % fotoCount )
	}
}

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

// ------------------------------------------------------------------------------------------------------------
//	preloadNextFoto
// ------------------------------------------------------------------------------------------------------------
function preloadNextFoto()
{
	if (document.galleryIndex >= 0)
	{
		var fotoCount = document.toc.galleries[document.galleryIndex].entries.length
		if (fotoCount > 0)
		{
			var nextImage = new Image
			nextImage.src=document.toc.galleries[document.galleryIndex].entries[(document.fotoIndex + 1) % fotoCount].fotopath
		}
	}
}

// ------------------------------------------------------------------------------------------------------------
//	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()
{
	if (document.toc.galleries[document.galleryIndex].entries.length > 0)
	{
		return document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotopath
	}
	else
	{
		return ""
	}
}

// ------------------------------------------------------------------------------------------------------------
//	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 infoArray = document.toc.galleries[document.galleryIndex].entries[document.fotoIndex].fotoinfo
	var 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()
{
	return document.toc.galleries[document.galleryIndex].name
}

// ------------------------------------------------------------------------------------------------------------
//	"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;
}


// ------------------------------------------------------------------------------------------------------------
//	showContactInfo
// ------------------------------------------------------------------------------------------------------------
function showContactInfo()
{
	// Contact info is a "pseudo gallery" with index = -1.

	if (document.galleryIndex != -1)
	{
		var baseURL 	= document.URL.split("?")[0]
		var newURL 		= baseURL + "?" + String(-1 * 1000)
		
		top.location.href = newURL
	}
}

// ------------------------------------------------------------------------------------------------------------
//	fotoLoaded
// ------------------------------------------------------------------------------------------------------------
function fotoLoaded(inElementId)
{	
	if (document.getElementById)
	{
		var theElement = document.getElementById(inElementId)
		if (theElement.src != "fotoslug.png")
		{
			theElement.style.opacity = 0;
			
			var f = new Fader(theElement, null, 750);
			f.fadeIn();
		
			document.f = f;
		}
		else
		{
			setOpacity(theElement, 0);
		}
	}
	preloadNextFoto()
}

// ------------------------------------------------------------------------------------------------------------
//	handleFotoClick
// ------------------------------------------------------------------------------------------------------------
function handleFotoClick(inEvent)
{
	if (document.galleryIndex >= 0)
	{
		var theEvent	= inEvent ? inEvent:event	
		var element		= theEvent.target ? theEvent.target:theEvent.srcElement
		
		var clickX = 0
		if (inEvent.layerX)
		{
			clickX = inEvent.layerX 
		}
		else if (inEvent.offsetX)
		{
			clickX = inEvent.offsetX 
		}
		inEvent.cancelBubble = true

		if (element.clientWidth/2 - clickX > 0)
		{
			showPreviousFoto()
		}
		else
		{
			showNextFoto()
		}
	}
}

