var szGalleryName = '';
var szGalleryDesc = '';
var szGalleryID = '';
var nPageSize = 12;
var nSetSize = 3;
var nCurPage = 0;
var nCurPic = 0;
var aPicLabels = new Array();
var aHasFullSize = null;

function ProcessCaption(strCaption)
{
	var aCaption = strCaption.split('#');
	if (aCaption.length > 1)
	{
		var strNewCaption = '';
		for (var i = 0; i < aCaption.length; i += 3)
		{
			strNewCaption += aCaption[i];
			if (i + 2 < aCaption.length)
				strNewCaption +=
					'<a href="JavaScript:;" onclick="OpenBubble(\'<p>' +
					aCaption[i + 2] + '</p>\', this)">' +
					aCaption[i + 1] + '</a>';
		}
		return strNewCaption;
	}
	else return strCaption;
}

function ReloadThumbnails()
{
	var nMaxLabel = aPicLabels.length;
	var pElement = null; var nCurIndex;
	for (var i = 0; i < 12; ++i)
	{
		pElement = getElement('id_gallery_thumb_' + i);
		nCurIndex = ((nCurPage * nPageSize) + i);
		if (nCurIndex < nMaxLabel)
		{
			pElement.style.display = '';
			pElement.src = 'skin/null.gif';
			pElement.src = 'images/gallery_' + szGalleryID + '/thumbs/image_' +
				nCurIndex + '.jpeg';
			getElement('id_gallery_capt_' + i).innerHTML =
				ProcessCaption(aPicLabels[nCurIndex]);
		}
		else
		{
			pElement.style.display = 'none';
			pElement.src = 'skin/null.gif';
			getElement('id_gallery_capt_' + i).innerHTML = '';
		}
	}
	getElement('id_gallery_page_counter').innerHTML = 'Strana: ' + (1 +
		nCurPage) + ' / ' + (1 + Math.floor(aPicLabels.length / nPageSize));
}

function ReloadSet()
{
	var nMaxLabel = aPicLabels.length;
	var pElement = null; var nCurIndex;
	for (var i = 0; i < 5; ++i)
	{
		pElement = getElement('id_gallery_set_' + i);
		nCurIndex = nCurPic + i - 2;
		if ((nCurIndex >= 0) && (nCurIndex < nMaxLabel))
		{
			pElement.style.display = '';
			pElement.src = 'skin/null.gif';
			pElement.src = 'images/gallery_' + szGalleryID + '/thumbs/image_' +
				nCurIndex + '.jpeg';
			getElement('id_gallery_set_capt_' + i).innerHTML =
				ProcessCaption(aPicLabels[nCurIndex]);
		}
		else
		{
			pElement.style.display = 'none';
			pElement.src = 'skin/null.gif';
			getElement('id_gallery_set_capt_' + i).innerHTML = '';
		}
	}
}

function ActivateGalleryItem() { ActivateMenuItem_NoLoad('galeria'); }

function OpenGallery(galleryID)
{
	CloseBubble();

	if (bSaveToHistory)
	{
		aNavigationHistory[++nCurrentHistory] =
			'PopActiveItem();OpenGallery(\'' + galleryID + '\')';
		nMaxHistory = nCurrentHistory;
	}

	LoadContent('id_page_content', 'files/gallery.html', false);

	szGalleryName = szGalleryDesc = '';
	nCurPage = nCurPic = 0;
	szGalleryID = galleryID;
	aPicLabels = new Array();
	aHasFullSize = null;
	hideItem('id_full_size_element');

	var pGalleryLabels = GetContent('images/gallery_' +
		szGalleryID + '/gallery_setup.js');

	eval(pGalleryLabels);
	getElement('id_gallery_name').innerHTML = szGalleryName;
	if (true === szGalleryDesc)
	{
		pGalleryLabels = GetContent('content/gallery_' +
			szGalleryID + '.html');
		getElement('id_gallery_description').innerHTML = pGalleryLabels;
	}
	else
		getElement('id_gallery_description').innerHTML = '<p>' +
			szGalleryDesc.replace('<br />', '</p><p>') + '</p>';

	ReloadThumbnails();
	ReloadSet();
	document.title = szMainTitle + ' – ' + szGalleryName;

	SetDirectLink('gal=' + galleryID);
}

function NavigatePicture(navCommand)
{
	CloseBubble();

	if (!isNaN(parseInt(navCommand)))
	{
		nCurPic = parseInt(navCommand);
		if (nCurPic >= aPicLabels.length) nCurPic = aPicLabels.length - 1;
		if (nCurPic < 0) nCurPic = 0;
	}
	else switch (navCommand)
	{
	case 'first_pic': nCurPic = 0; break;
	case 'last_pic': nCurPic = aPicLabels.length - 1; break;
	case 'next_page':
		nCurPic = nPageSize * Math.floor(nCurPic / nPageSize);
		if ((nCurPic += nPageSize) >= aPicLabels.length) nCurPic = 0;
		break;
	case 'prev_page':
		nCurPic = nPageSize * Math.floor(nCurPic / nPageSize);
		if ((nCurPic -= nPageSize) < 0) nCurPic =
			nPageSize * Math.floor(aPicLabels.length / nPageSize);
		break;
	case 'next_set':
		if ((nCurPic += nSetSize) >= aPicLabels.length)
			nCurPic -= aPicLabels.length;
		break;
	case 'prev_set':
		if ((nCurPic -= nSetSize) < 0) nCurPic += aPicLabels.length;
		break;
	case 'next_pic':
		if (++nCurPic >= aPicLabels.length) nCurPic = 0;
		break;
	case 'prev_pic':
		if (nCurPic-- <= 0) nCurPic = aPicLabels.length - 1;
		break;
	}

	var nNewPage = Math.floor(nCurPic / nPageSize);
	if (nNewPage != nCurPage)
	{
		nCurPage = nNewPage;
		ReloadThumbnails();
	}
	ReloadSet();

	getElement('id_gallery_image').src = 'skin/null.gif';
	getElement('id_gallery_image').src = 'images/gallery_' +
		szGalleryID + '/image_' + nCurPic + '.jpeg';

	if ((null != aHasFullSize) && (null != aHasFullSize[nCurPic]) &&
		aHasFullSize[nCurPic])
	{
		getElement('id_full_size').href = 'fullsize-image.html?gal=' +
			szGalleryID + '&img=' + nCurPic;
		showItem('id_full_size_element');
	}
	else
	{
		getElement('id_full_size').href = '';
		hideItem('id_full_size_element');
	}

	getElement('id_gallery_caption').innerHTML =
		ProcessCaption(aPicLabels[nCurPic]);
	getElement('id_gallery_pic_counter').innerHTML = 'Obrázok: ' + (1 +
		nCurPic) + ' / ' + aPicLabels.length;
}

function ZoomPicture(picNumber)
{
	changeVisibility('id_gallery_zoom', 'id_gallery_thumbs');
	NavigatePicture((nCurPage * nPageSize) + picNumber);
}

function ZoomSet(picNumber) { NavigatePicture(nCurPic + picNumber - 2); }

function BrowseGallery()
{
	CloseBubble();
	changeVisibility('id_gallery_thumbs', 'id_gallery_zoom');
}


/*
function captureKeyboard(evnt)
{
	if (!evnt)
	{
		// if the browser did not pass the event information to the
		// function, we will have to obtain it from the event register
		if (window.event)
		{
			// IExplore
			evnt = window.event;
		}
		else
		{
			// total failure, we have no way of referencing the event
			return false;
		}
	}

	var keyCode;

	if (typeof(evnt.keyCode) == 'number')
	{
		// DOM
		keyCode = evnt.keyCode;
	}
	else if (typeof(evnt.which) == 'number')
	{
		// NS 4 compatible
		keyCode = evnt.which;
	}
	else if (typeof(evnt.charCode) == 'number')
	{
		// also NS 6+, Mozilla 0.9+
		keyCode = evnt.charCode;
	}
	else
	{
		// total failure, we have no way of obtaining the key code
		return false;
	}

	// alert(keyCode);

	if ((37 == keyCode) || (33 == keyCode) ||
		(27 == keyCode) || (80 == keyCode))
	{
		NavigatePicture('prev_pic');
		NavigatePicture('prev_pic');
		NavigatePicture('prev_pic');
		NavigatePicture('prev_pic');
		NavigatePicture('prev_pic');
		NavigatePicture('prev_pic');
		return false;
	}
	if ((39 == keyCode) || (34 == keyCode) ||
		(32 == keyCode) || (78 == keyCode))
	{
		NavigatePicture('next_pic');
		NavigatePicture('next_pic');
		NavigatePicture('next_pic');
		NavigatePicture('next_pic');
		NavigatePicture('next_pic');
		NavigatePicture('next_pic');
		return false;
	}


	return true;
}

if (document.captureEvents && Event.keydown) { document.captureEvents(Event.keydown); }
document.onkeydown = captureKeyboard;
*/
