// JavaScript Document

var xmlDoc;
var leftmenu;
var szPrevFoto;
var szDescription;
var szNextFoto;
var nrImages;
var nrCurrent;

function GetWidth()
{
	var x = 0;
	if (self.innerHeight)
		{ x = self.innerWidth; }
	else if (document.documentElement && document.documentElement.clientHeight)
		{ x = document.documentElement.clientWidth; }
	else if (document.body)
		{ x = document.body.clientWidth; }
	return x;
}
 
function GetHeight()
{
    var y = 0;
	if (self.innerHeight)
		{ y = self.innerHeight; }
	else if (document.documentElement && document.documentElement.clientHeight)
		{ y = document.documentElement.clientHeight; }
	else if (document.body)
		{ y = document.body.clientHeight; }
	return y;
}

function GetNodeValue (Node)
{
	var strResult = "";
	
	for (m = 0; m < Node.childNodes.length; m++)
	{
		if (Node.childNodes[m].nodeType == 3)
			{ strResult += Node.childNodes[m].nodeValue; }
	}
	return strResult;
}

function GetData (Rubriek, szFoto)
{
	var images = Rubriek.getElementsByTagName ('Image');
	var n, m, t;
	var bFound = 0;

	nrCurrent = 1;
	szPrevFoto = '';
	szNextFoto = '';
	for (n = 0; n < images.length; n++)
	{
		var source = "";
//		szDescription = GetNodeValue (images[n]);
		for (m = 0; m < images[n].attributes.length; m++)
		{
			if (images[n].attributes[m].nodeName == "filename")
				{ source = images[n].attributes[m].nodeValue; }
		}
		if (source == szFoto)
		{
			var Titles = images[n].getElementsByTagName ('Title');
			if (Titles.length > 0)
				szDescription = GetNodeValue (Titles[0]);
//			szDescription = GetNodeValue (images[n]);
			if ((n+1) < images.length)
			{
				for (m = 0; m < images[n+1].attributes.length; m++)
				{
					if (images[n+1].attributes[m].nodeName == "filename")
						{ szNextFoto = images[n+1].attributes[m].nodeValue; }
				}
			}
			bFound = 1;
		}
		else if (!bFound)
		{
			szPrevFoto = source;
			nrCurrent += 1;
		}
	}
}

function GetInfo (szAlbum, szSub, szFoto)
{
	var Rubrieken = xmlDoc.getElementsByTagName ('Rubriek001');
	var nRubriek = -1;
	var nSub     = -1;
	var Sub;
	
	szDescription = '';
	szPrevFoto = '';
	szNextFoto = '';
	for (n = 0; n < Rubrieken.length; n++)
	{
		var szKnop = "";
		var Knop   = Rubrieken[n].getElementsByTagName ('Button');
		if (Knop.length > 0)
			{ szKnop = GetNodeValue (Knop[0]); }
		if (szKnop == szAlbum)
		{
			nRubriek = n;
			if (szSub !== '')
			{
				var Subs = Rubrieken[n].getElementsByTagName ('Rubriek002');
				var nSubs = Subs.length;
				for (k = 0; k < nSubs; k++)
				{
					szKnop = "";
					Knop = Subs[k].getElementsByTagName ('Button');
					if (Knop.length > 0)
						{ szKnop = GetNodeValue (Knop[0]); }
					if (szKnop == szSub)
					{
						nRubriek = n;
						nSub     = k;
						Sub      = Subs;
					}
				}
			}
		}
	}
	if (nRubriek == -1)
	{
		alert ('Kan de foto\'s uit album \"' + szAlbum + '\" niet vinden');
		return ;
	}
	else if (nSub == -1)
		{ GetData (Rubrieken[nRubriek], szFoto); }
	else
		{ GetData (Sub[nSub], szFoto); }
}

function loadDoc ()
{
	var  Rubrieken = xmlDoc.getElementsByTagName ('Rubriek001');
	var nRubrieken = Rubrieken.length;
	
	if (!Content)
		return ;
		
	var divMenu = document.getElementById ("leftmenu");
	if (divMenu)
		Content.removeChild (divMenu);
	divMenu = document.createElement ('div');
	divMenu.setAttribute ('id', 'leftmenu');
	Content.appendChild (divMenu);
	var ul = document.createElement ('ul');
	ul.setAttribute ('id', 'sddm');
	divMenu.appendChild (ul);

	for (n = 0; n < nRubrieken; n++)
	{
		var  Sub    = Rubrieken[n].getElementsByTagName ('Rubriek002');
		var nSub    = Sub.length;
		var szKnop  = "";
		var szTitel = "";
		var Knop = Rubrieken[n].getElementsByTagName ('Button');
		
		if (Knop.length > 0)
			{ szKnop = GetNodeValue (Knop[0]); }
		var Titel = Rubrieken[n].getElementsByTagName ('Title');
		if (Titel.length > 0)
			{ szTitel = GetNodeValue (Titel[0]); }
		var li = document.createElement ('li');
		ul.appendChild (li);
		var a = document.createElement ('a');
		li.appendChild (a);
		a.setAttribute ('title', szTitel);
		a.innerHTML = szKnop;
		if (nSub === 0)				// het zijn rechtstreeks foto's
			{ a.setAttribute ('href', 'javascript:album (\'' + szKnop + '\', \'\')'); }
		else											// er zijn onderliggende rubrieken
		{
			var szRubriek = szKnop;
			a.setAttribute ('href', 'javascript:mopen(\'m' + n + '\', ' + n + ')');
			a.onmouseover = new Function('mopen(\'m' + n + '\', ' + n + ')');
			a.onmouseout = new Function('mclosetime()');
			
			var div = document.createElement ('div');
			div.setAttribute ('id', 'm' + n);
			div.setAttribute ('class', 'subje');
			div.onmouseover = new Function ('mcancelclosetime()');
        	div.onmouseout = new Function ('mclosetime()');
			ul.appendChild (div);
        	for (k = 0; k < nSub; k++)
        	{
				Knop = Sub[k].getElementsByTagName ('Button');
				if (Knop.length > 0)
					{ szKnop = GetNodeValue (Knop[0]); }
				Titel = Sub[k].getElementsByTagName ('Title');
				if (Titel.length > 0)
					{ szTitel = GetNodeValue (Titel[0]); }
	        	a = document.createElement ('a');
				a.setAttribute ('href', 'javascript:album (\'' + szRubriek + '\', \'' + szKnop + '\')');
				a.setAttribute ('title', szTitel);
				a.innerHTML = szKnop;
				div.appendChild (a);
	        }
        }
	}
}


function getFotos ()
{
	var szFile = 'fotos/HNFotos.xml';
	
	Content = document.getElementById ('content');
	if (Content)
	{
		if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.onload = loadDoc;
		}
		else if (window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc.readyState == 4) { loadDoc(); }
			};
		}
		else
		{
			alert('Your browser can\'t handle this script');
			return;
		}
		try
		{
			xmlDoc.load(szFile);
		}
		catch (e)
		{
			try //Google Chrome
			{
				var xmlhttp = new window.XMLHttpRequest();
				xmlhttp.open("GET",szFile,false);
				xmlhttp.send(null);
				xmlDoc = xmlhttp.responseXML.documentElement;
				loadDoc();
			}
			catch(e)
			{
				alert (e.message);
			}
		}
	}
}

function loadImages (Rubriek, szDirectory, szAlbum, szSub)
{
	var images = Rubriek.getElementsByTagName ('Image');
	var n, m;
	var content = document.getElementById ('fotomenu');
	if (!content)
		{ return; }
	nrImages = 0;
	var Knop = Rubriek.getElementsByTagName ('Button');
	if (Knop.length > 0)
	{
		var szKnop = GetNodeValue (Knop[0]);
		var h2 = document.createElement ('h2');
		h2.innerHTML = szKnop;
		content.appendChild (h2);
	}

	for (n = 0; n < images.length; n++)
	{
		var source = "";
		var description = GetNodeValue (images[n]);
		for (m = 0; m < images[n].attributes.length; m++)
		{
			if (images[n].attributes[m].nodeName == "filename")
				{ source = images[n].attributes[m].nodeValue; }
		}
		var a = document.createElement ('a');
		a.setAttribute ('href', 'javascript:showFoto (\'' + source + '\', \'' + szDirectory + '\', \'' + szAlbum + '\', \'' + szSub + '\')');
		a.setAttribute ('title', description);
		var img = document.createElement('img');
		img.setAttribute ('src', 'fotos/' + szDirectory + '/_thumbs/' + source);
		a.appendChild (img);
		content.appendChild (a);
		nrImages += 1;
	}
}

function album (szAlbum, szSub)
{
	
	var image = document.getElementById ('image');
	if (image)
	{
		var overlay = document.getElementById ('overlay');
		if (!overlay)
		{
			overlay = document.createElement ('div');
			overlay.setAttribute ('id', 'overlay');
			image.appendChild (overlay);
		}
	}
	var balk2 = document.getElementById ('balk2');
	if (balk2)
	{
		var content = document.getElementById ('fotomenu');
		if (content)
			{ balk2.removeChild (content); }
		var div = document.createElement('div');
		div.setAttribute ('id', 'fotomenu');
		balk2.appendChild (div);
		
		var Sub;
		var Rubrieken = xmlDoc.getElementsByTagName ('Rubriek001');
		var nRubriek = -1;
		var nSub     = -1;
		var szDirectory = "";
		
		for (n = 0; n < Rubrieken.length; n++)
		{
			var szKnop = "";
			var Knop   = Rubrieken[n].getElementsByTagName ('Button');
			if (Knop.length > 0)
				{ szKnop = GetNodeValue (Knop[0]); }
			if (szKnop == szAlbum)
			{
				nRubriek = n;
				for (m = 0; m < Rubrieken[n].attributes.length; m++)
				{
					if (Rubrieken[n].attributes[m].nodeName == "directory")
						{ szDirectory = Rubrieken[n].attributes[m].nodeValue; }
				}
				var Subs = Rubrieken[n].getElementsByTagName ('Rubriek002');
				var nSubs = Subs.length;
				if (szSub !== '')
				{
					for (k = 0; k < nSubs; k++)
					{
						szKnop = "";
						Knop = Subs[k].getElementsByTagName ('Button');
						if (Knop.length > 0)
							{ szKnop = GetNodeValue (Knop[0]); }
						if (szKnop == szSub)
						{
							for (m = 0; m < Subs[k].attributes.length; m++)
							{
								if (Subs[k].attributes[m].nodeName == "directory")
								{
									if (szDirectory != "")
										szDirectory += "/";
									szDirectory += Subs[k].attributes[m].nodeValue;
								}
							}
							nRubriek = n;
							nSub     = k;
							Sub      = Subs;
						}
					}
				}
			}
		}
		if (nRubriek == -1)
		{
			alert ('Kan de foto\'s uit album \"' + szAlbum + '\" niet vinden');
			return ;
		}
		else if (nSub == -1)
			{ loadImages (Rubrieken[nRubriek], szDirectory, szAlbum, ''); }
		else
			{ loadImages (Sub[nSub], szDirectory, szAlbum, szSub); }
	}
}

function showFoto (szFoto, szDirectory, szAlbum, szSub)
{
	
	var overlay = document.getElementById ('doc_over');
	if (!overlay)
	{
		overlay = document.createElement ('div');
		overlay.setAttribute ('id', 'doc_over');
		document.body.appendChild (overlay);
	}
	var foto = document.getElementById ('grotefoto');
	if (foto)
		{ document.body.removeChild (foto); }
	foto = document.createElement ('img');
	foto.setAttribute ('id', 'grotefoto');
	foto.style.visibility = 'hidden';
	foto.style.position = 'absolute';
	document.body.appendChild (foto);
		
	foto.onload = function ()
	{
		var Text    = document.getElementById ('description');
		var nHeight = parseInt (foto.height, 10);
		var nLeft   = parseInt (foto.width, 10);
		var nTop    = nHeight;
		var nWidth  = nLeft;
		
		szDescription = '';
		szPrevFoto = '';
		szNextFoto = '';
		
		GetInfo (szAlbum, szSub, szFoto);
		
		nLeft  = GetWidth() - nLeft;
		nTop   = GetHeight() - nTop;
		nTop  /= 2;
		nLeft /= 2;
		foto.style.top  = nTop + 'px';
		foto.style.left = nLeft + 'px';
		foto.style.visibility='visible';
		if (Text)
		{
			Text.style.left = nLeft + 'px';
			nTop += nHeight;
			Text.style.top = nTop + 'px';
			nTop -= nHeight;
		}
		
		nTop += 50;
		var a   = document.getElementById ('vorige-a');
		var img = document.getElementById ('vorige-i');
		if (szPrevFoto !== '')
		{
			if (!a)
			{
				a = document.createElement ('a');
				a.setAttribute ('id', 'vorige-a');
				document.body.appendChild (a);
			}
			a.setAttribute ('href', 'javascript:showFoto (\'' + szPrevFoto + '\', \'' + szDirectory + '\', \'' + szAlbum + '\', \'' + szSub + '\')');
			a.setAttribute ('title', 'vorige foto');
			if (!img)
			{
				img = document.createElement ('img');
				img.setAttribute ('id', 'vorige-i');
				a.appendChild (img);
			}
			img.setAttribute ('src', 'images/left.gif');
			img.setAttribute ('border', '0');
			a.style.top = nTop + 'px';
			nLeft -= 39;
			a.style.left = nLeft + 'px';
			nLeft += 39;
			a.style.position = 'absolute';
		}
		else if (a)
			{ document.body.removeChild (a); }

		a   = 0;
		a   = document.getElementById ('volgende-a');
		img = 0;
		img = document.getElementById ('volgende-i');
		if (szNextFoto !== '')
		{
			if (!a)
			{
				a = document.createElement ('a');
				a.setAttribute ('id', 'volgende-a');
				document.body.appendChild (a);
			}
			a.setAttribute ('href', 'javascript:showFoto (\'' + szNextFoto + '\', \'' + szDirectory + '\', \'' + szAlbum + '\', \'' + szSub + '\')');
			a.setAttribute ('title', 'volgende foto');
			if (!img)
			{
				img = document.createElement ('img');
				img.setAttribute ('id', 'volgende-i');
				a.appendChild (img);
			}
			img.setAttribute ('src', 'images/right.gif');
			img.setAttribute ('border', '0');
			a.style.top = nTop + 'px';
			nLeft += 7;
			nLeft += nWidth;
			a.style.left = nLeft + 'px';
			a.style.position = 'absolute';
			nLeft -= 7;
			nLeft -= nWidth;
		}
		else if (a)
			{ document.body.removeChild (a); }
			
		nTop -= 50;
		nTop += nHeight;
		
		if (!Text)
		{
			Text = document.createElement ('div');
			Text.setAttribute ('id', 'description');
			Text.style.position = 'absolute';
			document.body.appendChild (Text);
		}
		if (Text)
		{
			Text.style.left = nLeft + 'px';
			Text.style.width = (nWidth-80) + 'px';
			Text.style.top = nTop + 'px';
			var szHtml = 'foto ' + nrCurrent + '/' + nrImages;
			if (szDescription)
				{ szHtml += '<br />' + szDescription; }
			Text.innerHTML = szHtml;
		}
		
		a = document.getElementById ('close');
		if (!a)
		{
			a = document.createElement ('a');
			a.setAttribute ('id', 'close');
			a.setAttribute ('href', 'javascript:clearFoto ()');
			a.innerHTML = 'sluiten';
			a.style.position = 'absolute';
			a.setAttribute ('title', 'sluiten');
			a.style.width = 'auto';
			a.style.height = 'auto';
			document.body.appendChild (a);
		}
		a.style.top   = (nTop +2) + 'px';
		a.style.right = (nLeft+2) + 'px';
	};
	foto.setAttribute ('src', 'fotos/' + szDirectory + '/_images/' + szFoto);
}

function RemoveFromBody (szName)
{
	
	var element = document.getElementById (szName);
	if (element)
		{ document.body.removeChild (element); }
}

function clearFoto ()
{
	RemoveFromBody ('doc_over');
	RemoveFromBody ('grotefoto');
	RemoveFromBody ('description');
	RemoveFromBody ('volgende-a');
	RemoveFromBody ('vorige-a');
	RemoveFromBody ('close');
}

