/*
 * 
 * NEEDED ELEMENTS:
 * fileGallery (div)
 * galleryLocation (div)
 * galleryPrev (div)
 * galleryNext (div)
 * 
 */

var gallery = new Object();
var galleryMaxCountPerPage = 5;
var galleryCurrentStartRecord = 0;
var currentProfilePath;

// we get the gallery recordset and pass it off to display content
function getGallery( siteId, profilePath, publicOnly ) {
	currentProfilePath = profilePath;
	
	$.getJSON( proxyPath + "?method=remote&process=getGallery&publicOnly=" + publicOnly + "&siteId=" + siteId + "&profile=" + currentProfilePath,
		function( data ) {
			gallery = data;
			
			// render gallery
			renderGallery( 0, galleryMaxCountPerPage );
		});
}

// render gallery
function renderGallery( startAt, stopAt ) {
	
	var _file = 0;
	
	onGalleryRenderStart( gallery );
	
	// if gallery items are found continue
	if ( gallery.galleryfound == "true" && startAt <= gallery.gallery.recordcount ) {

		
		// if stopAt is greater than gallery count then set it to the end
		if ( stopAt > gallery.gallery.recordcount ) {
			stopAt = gallery.gallery.recordcount;
		}
		
		// clear out current gallery
		$("#fileGallery").empty();
		
		// start building the gallery
		for ( _file=startAt; _file < stopAt; _file++) {
			// call external handler
			if (gallery.galleryfound == "true") {
				onGalleryItemRender( gallery.gallery.data, _file );
			}
		} 
		
		// show position in gallery
		$("#galleryLocation").empty();
		$("#galleryLocation").html( (startAt+1) + " to " + stopAt + " of " + gallery.gallery.recordcount );
		/*
		$("<a/>").html("reload gallery").click(
				function() {
					getGallery( siteId, currentProfilePath );
				})
				.appendTo("#galleryLocation");
		*/
		// create prev/next buttons
		// prev
		$("#galleryPrev").empty();
		if ( startAt >= galleryMaxCountPerPage )
		{
			// only show if we are not at the beginning
			if ( startAt != 0 ) {
				$("<a/>").html("&laquo; Prev").click(
					function() {
						renderGallery( startAt-galleryMaxCountPerPage, startAt );
					})
					.appendTo("#galleryPrev");
			}
		}
		
		// next
		$("#galleryNext").empty();
		if ( stopAt < gallery.gallery.recordcount )
		{
			// only show if we are not at the end of the recordcount
			if ( stopAt != gallery.gallery.recordcount ) {
				$("<a/>").html("Next &raquo;").click(
					function() {
						renderGallery( startAt+galleryMaxCountPerPage, stopAt+galleryMaxCountPerPage );
					})
					.appendTo("#galleryNext");
			}
		}
		
	}
	
	onGalleryRenderEnd( gallery );
}

function onGalleryImageClick( id ) {
	return false;
}
function onGalleryRenderStart() {
	return false;
}
function onGalleryRenderEnd() {
	return false;
}
