var icn = "imgcontainer";	// image container name
var icc = 0;				// number of image containers
var iclist  = new Array();

var gin = "gallery";		// gallery image name
var gic = 0;				// gallery image count

var gcn = "comment";		// gallery comment class
var gcc = 0;				// gallery comment count
var htmlc = true;			// html comments on/of

var gnn = "navbar";			// gallery navigation name
var gnc = 0;				// gallery navigation count
var swapnav = true			// swap navigation order
var gnSep = "|";
var gnFirst = "First";
var gnPrev = "Prev";
var gnNext = "Next";
var gnLast = "Last";

var pnSep = "|";
var pnFirst = "First page";
var pnPrev = "Previous page";
var pnNext = "Next page";
var pnLast = "Last page";

var tcn = "thumbs";			// thumbnail container name
var tcc = 0;				// number of thumbnail containers
hideatc=false;
var tclist = new Array();	// list of thumbnail containers
var tcmin = new Array();
var tcmax = new Array();
var hidetc = new Array();
var multipage=false;		// multipage layout
var page=0;					// page to start at

var galleryList = new Array();
var thumbsList = new Array();
var commentList = new Array();
var current;				// number of the current image

var reverse=true;			// reverse image list order

var noreload=true;			// reload pages or not.

var preloadedImg = new Array();		// array of preloaded images

var linkto = 1;				// images should link to what
							// 0 : no thumbs, direct image
							// 1 : next image

// add the images <images> with thumbnail <thumbnail> and comment <comment> to the gallery list
function list(images , thumbnail , comment)
{
	galleryList[galleryList.length] = images;
	commentList[commentList.length] = comment;
	thumbsList[thumbsList.length] = thumbnail;
}


function batchList(images , thumbnail , comment , start , stop)
{
	for (var i = 1 ; i<images.length ; i++)
	{


	}
}


function expandNum(n,l)
{
	while (n.length < l)
		n = "0" + n;

	return n;
}

// do everything
function automate()
{
	getParamTable();
	parseParameters();
	if (gic > 0) addNavigation();
	updateGallery();
}

// automate from url uri
function update(uri)
{
	for (var i=0;i<tclist.length;i++)
		if (tclist[i].hasChildNodes())
			removeChildren(tclist[i]);

	for (var i=0;i<iclist.length;i++)
		if (iclist[i].hasChildNodes())
			removeChildren(iclist[i]);

	parseParamString(getParamString(uri));
	parseParameters();
	if (gic > 0) addNavigation();
	updateGallery();

	preloadImages();

}

// parse parameter array
function parseParameters()
{
	for (var i = 0 ; i< keyw.length ; i++)
	{

		if (keyw[i]=="image")
			addImage(param[i]);

		if (keyw[i]=="comment")
			addComment(param[i]);

		if (keyw[i]=="show")
			show(param[i]);

		if (keyw[i]=="nothumbs")
				hideatc=true;

		if (keyw[i]=="page")
			setPage(param[i]);
	}
}

// sets the currently shown page
function setPage(num)
{
	for (var j=0;j<hidetc.length;j++)
	{
		if(num==j)
			hidetc[j]=false;
		else
			hidetc[j]=true;
	}

	page=num;
}

// show gallery list item <i>
function show(i)
{
	current = i*1;
	var nr=(reverse?galleryList.length-i-1:i);

	if(multipage)
	{
		while (nr>=tcmax[page])
			setPage(page+1);

		while (nr<tcmin[page])
			setPage(page-1);
	}
	var nr=i;


	if (typeof galleryList[nr] != "undefined" && galleryList[nr] != null)
		for (var j = 0 ; j<galleryList[nr].length ; j++)
			if (typeof galleryList[nr][j] != "undefined" && galleryList[nr][j] != null)
				addImage(galleryList[nr][j]);


	if (typeof commentList[nr] != "undefined" && commentList[nr] != null)
		addComment(commentList[nr]);
}

// creates a div wherein thumbnail lists are displayed
// you can create as much as you want
function insertGallery(min, max)
{
	tcmin[tcc] = min;
	tcmax[tcc] = max;
	document.write('<div name="' + tcn + '" id="' + tcn + tcc + '" class="' + tcn + '"></div>');
	tclist[tcc]=document.getElementById(tcn+tcc);
	hidetc[tcc]=false;
	if (tcc > 0 && multipage)
		hidetc[tcc]=true;

	tcc++;
}

// recreate the gallery lists
function updateGallery()
{
	for (var i=0;i<tclist.length;i++)
	{

		if (tclist[i].hasChildNodes())
			removeChildren(tclist[i])

		tclist[i].appendChild( createGallery(i));

		if (hidetc[i] || hideatc)
			tclist[i].style.display="none";

	}
}

// create the gallery content
function createGallery(num)
{
	var newDiv = document.createElement("DIV");

	if (!hidetc[num] && !hideatc)
	{
		if (multipage)
			newDiv.appendChild(createPageNavigation());

		newDiv.setAttribute("class",tcn);
		var min = tcmin[num];
		var max = tcmax[num];
		if (min==null || typeof min == "undefined") min = 0;
		if (max==null || typeof max == "undefined") max = galleryList.length;
		for (var i = min ; i< max ; i++)
		{
			var nr=(reverse?galleryList.length-i-1:i);

			var newA = document.createElement("A");
			newA.setAttribute("class",tcn);
			var uri = getBaseUrl() + pb + "show=" + nr;
			createLink(newA , uri);
//			newA.setAttribute("href",uri);

			if (thumbsList[nr]!=null)
			{
				var newImg = document.createElement("IMG");
				newImg.setAttribute("src",thumbsList[nr]);
				newImg.setAttribute("class",tcn);
				newImg.setAttribute("id",tcn);
				newA.appendChild(newImg);
			}
			else
			{
				var newSpan = document.createElement("SPAN");
				newSpan.setAttribute("class",tcn);
				newSpan.setAttribute("id",tcn);
				newSpan.appendChild(document.createTextNode(" " + (nr+1) + " "));
				newA.appendChild(newSpan);
			}

			newDiv.appendChild(newA);
		}

		if (multipage)
			newDiv.appendChild(createPageNavigation());
	}
	return newDiv;
}

// create the navigation bar to be added to the gallery containers
function createPageNavigation() {
	var newDiv = document.createElement("DIV");
	newDiv.setAttribute("class",gnn);
	newDiv.setAttribute("id",gnn);

	if (swapnav) {
		newDiv.appendChild(createPageNavLink(0 , pnFirst , "First" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(page-1 , pnPrev , "Previous" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(page+1 , pnNext , "Next" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(hidetc.length-1 , pnLast , "Last" , "page"));
	}

	else  {
		newDiv.appendChild(createPageNavLink(hidetc.length-1 , gnFirst , "First" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(page+1 , pnPrev , "Previous" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(page-1 , pnNext , "Next" , "page"));
		newDiv.appendChild(createSeparator(pnSep));
		newDiv.appendChild(createPageNavLink(0 , pnLast , "Last" , "page"));
	}

	return newDiv;
}

// create the navigation link with url <link> caption <caption> and class <name>
function createPageNavLink(link,caption,name,type)
{
	if (link < 0) link = 0;
	if (link > hidetc.length-1) link = hidetc.length-1;

	var uri = getBaseUrl() + pb;

	if (hideatc) uri = uri + "nothumbs" + ps;

	uri = uri + type + pa + link;

	var newText= document.createTextNode(caption);

	var newSpan = document.createElement("SPAN");
	newSpan.setAttribute("class",gnn + name);

	var newA = document.createElement("A");
	createLink(newA , uri);
//	newA.setAttribute("href",uri);
	newA.appendChild(newText);
	newSpan.appendChild(newA);

	return newSpan;
}

// creates a div wherein images are displayed
// you can create as much as you want
function insertImages()
{
	document.write('<div name="' + icn + '" class="' + icn + '" id="' + icn + icc + '" + style="display:none;"></div>');
	iclist[icc]=document.getElementById(icn+icc);
	icc++;
}

// adds the image at <uri> to all image containers
function addImage(uri)
{
	addNavigation();

	var img = createImage(uri);
	for (var i=0;i<iclist.length;i++)
	{
			iclist[i].appendChild(img);
			iclist[i].style.display="block";
	}
}

// create the image to be added to the gallery containers
function createImage(uri)
{
	var newA = document.createElement("A");
	var luri;

	if (linkto ==0)
	{
		if (!hideatc)
			luri = getBaseUrl() + pb + "nothumbs" + ps + getParamString(document.URL);
		else
			luri = uri;
	}
	if (linkto == 1)
	{
		luri = getBaseUrl() + pb + "show" + pa + (current+1);
	}


	createLink(newA , luri);
//	newA.setAttribute("href",luri);

	var newImg = document.createElement("IMG");
	newImg.setAttribute("src",uri);
	newImg.setAttribute("class",gin);
	newImg.setAttribute("name",gin);
	newImg.setAttribute("id",gin);
	gic++;

	newA.appendChild(newImg);

	return newA;
}

// adds the comment <comment> to all image containers
function addComment(comment) {
	addNavigation();

	var div = createComment(comment);
	for (var i=0;i<iclist.length;i++)
			iclist[i].appendChild(div);
}

// create the comment to be added to the gallery containers
function createComment(comment) {
	var newDiv = document.createElement("DIV");
	newDiv.setAttribute("class",gcn);
	newDiv.setAttribute("name",gcn);
	newDiv.setAttribute("id",gcn);

	if (htmlc) {
		newDiv.innerHTML = '';
		newDiv.innerHTML = comment;
	}
	else {
		var newText= document.createTextNode(comment)
		newDiv.appendChild(newText);
	}

	return newDiv;
}

// add the navigation bar to all gallery containers
function addNavigation() {

	var newDiv = createNavigation();
	for (var i=0;i<iclist.length;i++)
	{
			iclist[i].appendChild(newDiv);
	}
}

// create the navigation bar to be added to the gallery containers
function createNavigation() {
	var newDiv = document.createElement("DIV");
	newDiv.setAttribute("class",gnn);
	newDiv.setAttribute("id",gnn);

	if (swapnav) {
		newDiv.appendChild(createNavLink(0 , gnFirst , "First","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(current-1 , gnPrev , "Previous","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(current+1 , gnNext , "Next","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(galleryList.length-1 , gnLast , "Last","show"));
	}

	else  {
		newDiv.appendChild(createNavLink(galleryList.length-1 , gnFirst , "First","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(current+1 , gnPrev , "Previous","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(current-1 , gnNext , "Next","show"));
		newDiv.appendChild(createSeparator(gnSep));
		newDiv.appendChild(createNavLink(0 , gnLast , "Last","show"));
	}

	return newDiv;
}

// create separators in the navigation bar
function createSeparator(sep) {
	var newSpan = document.createElement("SPAN");
	newSpan.setAttribute("class",gnn + "Separator");
	var newText= document.createTextNode(sep);
	newSpan.appendChild(newText);
	return newSpan;
}

// create the navigation link with url <link> caption <caption> and class <name>
function createNavLink(link,caption,name,type)
{
	if (link < 0) link = 0;
	if (link > galleryList.length-1) link = galleryList.length-1;

	var uri = getBaseUrl() + pb;

	if (hideatc) uri = uri + "nothumbs" + ps;

	uri = uri + type + pa + link;

	var newText= document.createTextNode(caption);

	var newSpan = document.createElement("SPAN");
	newSpan.setAttribute("class",gnn + name);

	var newA = document.createElement("A");
	createLink(newA, uri);

	newA.appendChild(newText);
	newSpan.appendChild(newA);

	return newSpan;
}


function createLink(node,uri)
{
	if (noreload)
	{
		addEvent(node , "onclick" , 'update("' + uri +  '")');
		node.setAttribute("href","#");
	}
	else
		node.setAttribute("href",uri);
}

function addEvent(node,handler,addFunction){

   if ((!document.all)&&(document.getElementById)){
       node.setAttribute(handler,addFunction);
   }
   //workaround for IE 5.x
   if ((document.all)&&(document.getElementById)){
       node[handler]=new Function(addFunction);
   }
}

function removeChildren(node)
{
	while (node.childNodes.length > 0)
		node.removeChild(node.firstChild);
}

window.onload = function(e) {
preloadImages();
}

function preloadImages()
{
	if (typeof galleryList[current + 1] != "undefined" && galleryList[current+1] != null)
		for (var j = 0 ; j<galleryList[current+1].length ; j++)
			if (typeof galleryList[current+1][j] != "undefined" && galleryList[current+1][j] != null)
			{
				preloadedImg[j] = new Image();
				preloadedImg[j].src = galleryList[current + 1][j];
			}

}

