// Pager Functions

function page_left(imageList, hrefList, captionList, heightList, widthList, galleryFormat, customVar1, customVar2) {

	var currentPage = globalCurrentPage;

	if (currentPage != 0) {
		globalCurrentPage = currentPage - 1;
		redraw_page(imageList, hrefList, captionList, heightList, widthList, galleryFormat, customVar1, customVar2);

	}
}


function page_right(imageList, hrefList, captionList, heightList, widthList, galleryFormat, customVar1, customVar2) {
	var maxPages;
	var currentPage = globalCurrentPage;
	
//	alert("pageRight:currentPage:"+currentPage);
	
	if (galleryFormat == "grid-paged") {
		pageLength = customVar1 * customVar2;

//		alert("pageRight:pageLength:"+pageLength);

		var fractionalResult = imageList.length / pageLength;
		var maxPages = Math.round(fractionalResult);
		if (maxPages < fractionalResult) {
			maxPages++;
		}

//		alert("pageRight:maxPages:"+maxPages);
		
		if (currentPage != (maxPages - 1)) {
			globalCurrentPage = currentPage + 1;
			redraw_page(imageList, hrefList, captionList, heightList, widthList, galleryFormat, customVar1, customVar2);
		}
	}
}



function redraw_page(imageList, hrefList, captionList, heightList, widthList, galleryFormat, customVar1, customVar2) {
	var pageLength;
	var firstIndex;

	var testImageUrl = imageList[0];
	if (testImageUrl) {} else testImageUrl = '';
	var hostUrl = testImageUrl.replace(/^(http[s]?:\/\/\w+\.[\w\.]+\w+[\:\d]*\/).*$/, "$1");
	
	var currentPage = globalCurrentPage;
//	alert("redrawPage:currentPage:" + currentPage);
		
	if (galleryFormat == "grid-paged") {
		pageLength = customVar1 * customVar2;
		firstIndex = pageLength * currentPage;
	}

//	alert("redrawPage:pageLength:"+pageLength);
//	alert("redrawPage:firstIndex:"+firstIndex);


//	alert("starting WHILE loop.");
	
	var i = 0;
	while (i < pageLength) {
		var indexMarker = i + (pageLength * currentPage);
		
//		alert("WHILE:indexMarker:"+indexMarker);
//		alert("WHILE:i:"+i);

		if (indexMarker < imageList.length) {

			var img = get_image_by_name("img"+i);
			img.src = imageList[indexMarker];
			img.height = heightList[indexMarker];
			img.width = widthList[indexMarker];
			
//			alert("assigned img");
			
			linkReferences[i] = hrefList[indexMarker];
	
//			alert("assigned anchor");
	
			if (document.getElementById) {
				if (mydiv = document.getElementById("caption"+i)) {
					mydiv.innerHTML = captionList[indexMarker];
				}
			}
			

		} else {
			var img = get_image_by_name("img"+i);
			if (img) img.src = hostUrl + "/dashboard/core/gallerytool/graphics/blank.gif";
			
			linkReferences[i] = "none";
			
			if (document.getElementById) {
				if (mydiv = document.getElementById("caption"+i)) {
					mydiv.innerHTML = "";
				}
			}
		}

	
		i++;
		
	}
}


function get_image_by_name(name) {
	var imgs = document.images;
	
	if (imgs) {
		for (var i=0; i < imgs.length; i++) {
			if (imgs[i].name == name) {
				return imgs[i];
			}
		}
	}
	
	return null;
}
