
/* FUNCTION: make popup windows */
function makePopup()
{
	if (document.getElementById('thumbnails'))
	{
		var thumbs = document.getElementById('thumbnails');
		var links = thumbs.getElementsByTagName('a');

		for (var i=0; i<links.length; i++)
		{
			links[i].onclick = function()
			{
				if (document.getElementById('usepopup').checked)
				{
					var url = this.getAttribute('href');
				
					// RegEx replace gallery url with popup url
				
					// When Mod_Rewrite enabled
					re = /\/([0-9]+)\/([0-9]+)/gi;
					url = url.replace(re, '/$1/$2popup');
				
					var opts = "width=600, height=470, left=0, top=0, scrollbars=yes, resizable=yes";
					var gallery = window.open(url, 'Gallery', opts);
					gallery.window.focus();
					return false;
				}
			}
		}
	}
}

/* FUNCTION: add the show popups toggle form */
function addToggle()
{
	if (document.getElementById('togglepopup'))
	{
		var tog = document.getElementById('togglepopup');

		var oForm = document.createElement('form');
		var oFieldset = document.createElement('fieldset');
		var oLegend = document.createElement('legend');
		var oPara = document.createElement('p');
		var oInput = document.createElement('input');
		var oLabel = document.createElement('label');
		var oLegendTxt = document.createTextNode('Toggle Popup Window');
		var oLabelTxt = document.createTextNode('Tick (select) the box to open photos in a popup window.');
		
		oForm.setAttribute('id', 'toggleform');
		oInput.setAttribute('type', 'checkbox');
		oInput.setAttribute('name', 'usepopup');
		oInput.setAttribute('id', 'usepopup');
		oInput.setAttribute('value', 1);
		oLabel.setAttribute('for', 'usepopup');

		oLegend.appendChild(oLegendTxt);
		oLabel.appendChild(oLabelTxt);
		oPara.appendChild(oInput);
		oPara.appendChild(oLabel);
		oFieldset.appendChild(oLegend);
		oFieldset.appendChild(oPara);
		oForm.appendChild(oFieldset);

		tog.appendChild(oForm);

		oInput.onclick = function()
		{
			var change = oLabel.firstChild;

			if (oInput.checked)
			{
				change.nodeValue = "Untick (deselect) the box to open photos in the current window (no popups)";
			}
			else
			{
				change.nodeValue = "Tick (select) the box to open photos in a popup window.";
			}
		}
	}
}

// FUNCTION: enable keyboard navigation through albums
function albumNav(key)
{
	var links = document.getElementsByTagName('a');

	for (var i=0; i<links.length; i++)
	{
		if (links[i].getAttribute('accesskey') == "1")
		{
			var first = links[i].getAttribute('href');
		}

		if (links[i].getAttribute('accesskey') == "2")
		{
			var prev = links[i].getAttribute('href');
		}

		if (links[i].getAttribute('accesskey') == "3")
		{
			var next = links[i].getAttribute('href');
		}

		if (links[i].getAttribute('accesskey') == "4")
		{
			var last = links[i].getAttribute('href');
		}

		if (links[i].getAttribute('accesskey') == "5")
		{
			var close = 1;
		}

		if (links[i].getAttribute('accesskey') == "6")
		{
			var viewalbums = links[i].getAttribute('href');
		}

		if (links[i].getAttribute('accesskey') == "7")
		{
			var viewthumbs = links[i].getAttribute('href');
		}
	}

	if (!key)
	{
		key = event;
		key.which = key.keyCode;
	}

	if (key.which == 49) {
	/* Number 1 ("first") - keycode 49 */
		if(first)
		{
			document.location.href = first;
		}
	}

	if (key.which == 50) {
	/* Number 2 ("previous") - keycode 50 */
		if(prev)
		{
			document.location.href = prev;
		}
	}

	if (key.which == 51) {
	/* Number 3 ("next") - keycode 51 */
		if(next) {
			document.location.href = next;
		}
	}

	if (key.which == 52) {
	/* Number 4 ("last") - keycode 52 */
		if(last)
		{
			document.location.href = last;
		}
	}

	if (key.which == 53) {
	/* Number 5 ("close popup") - keycode 53  */
		if(close)
		{
			window.close();
		}
	}

	if (key.which == 54) {
	/* Number 6 ("all albums") - keycode 54  */
		if(viewalbums)
		{
			document.location.href = viewalbums;
		}
	}

	if (key.which == 55) {
	/* Number 7 ("thumbnails") - keycode 55  */
		if(viewthumbs)
		{
			document.location.href = viewthumbs;
		}
	}
}

function initialise() {
	if (!document.getElementById || !document.getElementsByTagName ) return;

	makePopup(); // Make the popup windows
	addToggle(); // Add the popup toggle
	document.onkeyup = albumNav; // Enable keyboard navigation
}

// Run script when window has finished loading.
window.onload = function()
{
	initialise();
}
