/*
 * Photo gallery navigation
 * This JS provides functionality that is lacking in the server side tools.
 * makeNumList() based on the previous function logic written by Øyvind Bjerkvik
 * 
 * Andreas Søvik - 2008
 */

var PhotoGallery = new Class( {
	
	/* 
	 * 	Output navigator for gallery overview. Includes previous/next button logic
	 */
	
	makeArchiveNavigator:function(count, current, category, offset, container) {
		str = '<ul>';
		var cap = 10*offset;
		
		if (current == 0) 
			str = str + '<li class="prev"><img src="/gui/graphics/gallery/btn_nnav_prev_off.gif" alt="Forrige" title="Forrige">';
		else 
			str = str + '<li class="prev"><a href="/apps/pbcs.dll/section?Category=' + category + '&Template=GalleryArchive&Start=' + (current - offset) + '"><img src="/gui/graphics/btn_nnav_prev.gif" alt="Forrige" title="Forrige"></a></li>';
		
		str = str + '<li class="numbers archive"><ul>';
		for (i = 0; i < count && i <= cap; i += offset) {
			if (current >= i && current < (i + offset)) 
				str = str + '<li class="current">';
			else 
				str = str + '<li>';
			str = str + '<a href="/apps/pbcs.dll/section?Category=' + category + '&Template=GalleryArchive&Start=' + i + '">' + (i + 1) + ' - ' + (i + offset) + '</a> </li>';
		}
		str = str + '</li></ul>';
		
		if ((current + offset) < count) 
			str = str + '<li class="next"><a href="/apps/pbcs.dll/section?Category=' + category + '&Template=GalleryArchive&Start=' + (current + offset) + '"><img src="/gui/graphics/btn_nnav_next.gif" alt="Neste" title="Neste"></a></li>';
		else 
			str = str + '<li class="next"><img src="/gui/graphics/gallery/btn_nnav_next_off.gif" alt="Neste" title="Neste"></li>';
		
		str = str + '</ul>';
		$(container).set("html",str);
	},
	
	/*
	 *  Output numeric photo listing for a gallery being viewed. Previous/next button logic is done server side
	 */
	
	makeNumList:function(count, curr, url, maxnum, container) {
		str = '';
		base = '';
		stop = count;
		next = false;
		base = url.replace(/&Params=Itemnr=\d+/g, '');
		
		if (curr >= 90) maxnum = Math.round(maxnum / 2);
		start = maxnum * Math.floor(curr / maxnum) + 1;
		
		if (count < start + maxnum) {
			stop = count;
			next = false;
		} else {
			stop = start + (maxnum - 1);
			next = true;
		}
		
		if (start != 1) {
			start -= 1;
			str = str + '<li>...</li>';
		}
		
		for (i = start; i <= stop; i++) {
			url = base + '&Params=Itemnr=' + i;
			if (i == curr) {
				str = str + '<li class="current">' + i + '</li>';
			}
			else {
				str = str + '<li><a href="' + url + '">' + i + '</a></li>';
			}
		}
		
		if (next) 
			str = str + '<li>...</li>';
		
		$(container).set("html" ,str);
	}
	
} );
