var THUMBS_PER_PAGE;
var THUMBS_ARRAY;
var PHOTOS_ARRAY;
var THUMBS_CURRENT_PAGE;

function setup_thumbs(photos_array, thumbs_array, thumbs_per_page)
{
	PHOTOS_ARRAY = photos_array;
	THUMBS_ARRAY = thumbs_array;
	THUMBS_PER_PAGE = thumbs_per_page;
	THUMBS_CURRENT_PAGE = 0;
	render_thumbs_page(THUMBS_CURRENT_PAGE);
}

function generate_thumb_item(image_src, thumb_src)
{
	//return "<div class=\"thumbitem\"><a href=\"\" onclick=\"esoteric_show_image('" + image_src + "'); return false;\"><img src=\"" + thumb_src + "\"></a></div>";
	return "<a href=\"\" onclick=\"esoteric_show_image('" + image_src + "'); return false;\"><img class=\"thumbnail\" src=\"" + thumb_src + "\"></a>";
}

function generate_thumb_link(link_text, link_url, on_click_function)
{
	var result = "<div style=\"clear:both;\"></div>";
	result += "<div class=\"thumblink\"><a onclick=\"" + on_click_function + "; return false;\" href=\"" + link_url +"\">" + link_text + "</a></div>";
	return result;
}

function show_previous_thumb_set()
{
	THUMBS_CURRENT_PAGE --;
	if (THUMBS_CURRENT_PAGE < 0)
	{
		THUMBS_CURRENT_PAGE = 0;
		return false;
	}
	
	render_thumbs_page(THUMBS_CURRENT_PAGE);
}

function show_next_thumb_set()
{
	if ((THUMBS_CURRENT_PAGE + 1) * THUMBS_PER_PAGE >= THUMBS_ARRAY.length)
	{
		return false;
	}
	
	THUMBS_CURRENT_PAGE ++;	
	render_thumbs_page(THUMBS_CURRENT_PAGE);
}

function render_thumbs_page(page_index)
{
	var base_index = page_index * THUMBS_PER_PAGE;

	if (base_index < 0 || base_index >= THUMBS_ARRAY.length)
	{
		alert("render_thumbs_page error: page_index is invalid: " + page_index);
	}

	var content = "";

	//show the previous link if we're not at the beginning of the set..
	if (base_index > 0)
	{
		//content += generate_thumb_link("PREVIOUS", "", "show_previous_thumb_set()") + "\n";
	}
	else
	{
		//content += "<div style=\"clear:both;\"></div>\n";
		//content += "<div class=\"thumblink\">&nbsp;</div>\n";
	}

	var stop_index = Math.min(base_index + THUMBS_PER_PAGE, THUMBS_ARRAY.length);
	for (var i = base_index; i < stop_index; i++)
	{
		content += generate_thumb_item(PHOTOS_ARRAY[i], THUMBS_ARRAY[i]) + "\n";
	}

	if (base_index + THUMBS_PER_PAGE < THUMBS_ARRAY.length)
	{
		//content += generate_thumb_link("NEXT", "", "show_next_thumb_set()") + "\n";
	}
	
	//alert(content);

	document.getElementById('thumbscontent').innerHTML = content;
	show_or_hide_thumbs_controls();
}

function show_or_hide_thumbs_controls()
{
	if (THUMBS_CURRENT_PAGE == 0)
	{
		showOrHideDiv(false, 'top');
	}
	else
	{
		showOrHideDiv(true, 'top');
	}
	
	if ((THUMBS_CURRENT_PAGE + 1) * THUMBS_PER_PAGE >= THUMBS_ARRAY.length)
	{
		showOrHideDiv(false, 'bottom');
	}
	else
	{
		showOrHideDiv(true, 'bottom');
	}
}

function reset_thumbs_page(thumbs_index)
{
	var page_index = Math.floor(thumbs_index / THUMBS_PER_PAGE);

	if (page_index != THUMBS_CURRENT_PAGE)
	{
		THUMBS_CURRENT_PAGE = page_index;
		render_thumbs_page(THUMBS_CURRENT_PAGE);
	}
}
