/*######################################################*\
||                                                      ||
||                  S N E L S I T E                     ||
||                                                      ||
||       Media Design Content Management System         ||
|| Copyright 2002-2004 Media Design  v. ##CMS_VERSION## ||
\*######################################################*/



// removeChildren() - thanks to Mark Wubben
function removeChildren(oNode){
	var oChild = oNode.firstChild;
	var nextChild;
	if (!oChild)
	{
		return;
	}
	while (oChild)
	{
		nextChild = oChild.nextSibling;
		oChild.parentNode.removeChild(oChild);
		oChild =  nextChild;
	}
}


// showPicture()
function showPicture(path, filename, i, j)
{
	var viewdiv = document.getElementById('viewpic');
	removeChildren(viewdiv);

	var picture = document.createElementNS('http://www.w3.org/1999/xhtml', 'img');
	picture.setAttribute('src', "/gpic.php?n=" + realGallery[i][j] + "&path=" + path);
	picture.setAttribute('alt', realGallery[i][j]);

	if (fotoTexts[realGallery[i][j]] != undefined)
	{
		picture.setAttribute('title', fotoTexts[realGallery[i][j]]);

		var description = document.createElement('p');
		var descText = document.createTextNode(fotoTexts[realGallery[i][j]]);

		description.appendChild(descText);

		viewdiv.appendChild(description);
		viewdiv.appendChild(picture);
	}
	else
	{
		viewdiv.appendChild(picture);
	}
	return false;
}

// writePageThumbs()
function writePageThumbs(path, totalpics, pageno, perpage)
{
	var thumbsdiv = document.getElementById('thumbs');
	removeChildren(thumbsdiv);

	if (currentPage != null)
	{
		currentPage.className = "";
	}
	currentPage = document.getElementById('page' + pageno);
	currentPage.className = "selectedpage";

	var thumbs = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');

	var j = 0;

	for (var i in realGallery)
	{
		if (i == pageno)
		{
			for (j=0; j < perpage; j++)
			{
				if (realGallery[i][j] != undefined)
				{
					var thumbIMG = document.createElement('img');
					thumbIMG.setAttribute('class', "gthumb");
					thumbIMG.setAttribute('alt', realGallery[i][j]);
					thumbIMG.setAttribute('src', "/gpic.php?y=" + realGallery[i][j] + "&path=" + path);
					thumbIMG.setAttribute('title', fotoTexts[realGallery[i][j]]);

					var thumbA = document.createElement('a');
					thumbA.setAttribute('href', "/gpic.php?n=" + realGallery[i][j] + "&path=" + path);
					
					// Yes, it's nasty to set an event as an attribute, but we found no other way to make it work. :(
					eval("thumbA.onclick = function () { showPicture('" + path + "', '" + realGallery[i][j] + "', " + i + ", " + j + "); return false; };");

					thumbA.appendChild(thumbIMG);

					thumbs.appendChild(thumbA);
				}
			}
		}
	}
	thumbsdiv.appendChild(thumbs);
	return false;
}

// writePagelist()
function writePagelist(path, totalpics, pageno, perpage, pagepath)
{
	var pages = (totalpics / perpage) + 1;
	var i = 1;

	var pagelist = document.createElementNS('http://www.w3.org/1999/xhtml', 'ul');
	pagelist.setAttribute("id", "pagelist");

	var firstlistitem = document.createElement('li');
	var firstlistitemcontent = document.createTextNode(phrasePage);

	firstlistitem.appendChild(firstlistitemcontent);

	pagelist.appendChild(firstlistitem);


	for (i=1; i < pages; i++)
	{
		var newli = document.createElement('li');
		newli.setAttribute('id', "page" + i);
		newli.setAttribute('class', "");

		var newliLink = document.createElement('a');
		newliLink.setAttribute('href', pagepath + i);
		//newliLink.setAttribute('onclick', "writePageThumbs('" + path + "', " + totalpics + ", " + i + ", " + perpage + "); return false;");
		newliLink.onclick = function () { writePageThumbs(path, totalpics, i, perpage); return false; };

		var newliLinkcontent = document.createTextNode(i);

		newliLink.appendChild(newliLinkcontent);
		newli.appendChild(newliLink);

		pagelist.appendChild(newli);
	}

	var pagelistdiv = document.getElementById('pagelistdiv');
	pagelistdiv.appendChild(pagelist);
}

/*######################################################*\
||                   S N E L S I T E                    ||
||                   ##CMS_VERSION##                    ||
||           Copyright 2002-2004 Media Design           ||
\*######################################################*/