var galleryCurrentIndex = 0;
var cityState = GALLERY_CITY;
var GalleryValues = new Array();
var StoriesPageIndexBack = 1;
var StoriesPageIndex = 6;
var maxRecords = 6;
var recordsPerRow = 2;
var totalPages = 0;
var pagesToShow = 6;
var maxValue = 0;
var firstPageShow = 1;
var lastPageShow = 6;

function calPages(totalRecords, recordsPerPage) {
	pagesToShow = 6;
	lastPageShow = 6;
	totalNoPage = totalRecords / recordsPerPage;
	totalNoPage = parseInt(totalNoPage);
	if ((totalRecords % recordsPerPage) > 0) {
		totalNoPage++;
	}
	if(totalNoPage < pagesToShow) {
		pagesToShow = totalNoPage;
		lastPageShow = totalNoPage;
	}
	return totalNoPage;
}

function displayStoriesPhotoArray(galleryType) {
	galleryCurrentIndex = 1;
	var divTextHtml = "";
	var divGalleryHtml = "";
	
	if (galleryType == 0) {
		var Records = Citydata.split("||");
	} else if (galleryType == 1) {
		var Records = Statedata.split("||");
	}
	maxValue = (Records.length) - 1;
	divGalleryHtml = "<div id='imageDiv'><div id='galleryContainer'><div class='gallery_row'>";
	
	for (var index = 0; index < maxValue; index++) {
		GalleryValues[index] = new Array();
		var thisGallery = Records[index];
		thisGallery = thisGallery.split("~");
		GalleryValues[index][0] = thisGallery[0];
		GalleryValues[index][1] = thisGallery[1];
		GalleryValues[index][2] = thisGallery[2];
		if (index < maxRecords) {
			divGalleryHtml = divGalleryHtml + "<div class='gallery'><div class='gallery_promo'><a href='" + thisGallery[2] + "'><img src='" + thisGallery[0] + "' /></a></div>";
			divGalleryHtml = divGalleryHtml + "<div class='gallery_title'><a href='" + thisGallery[2] + "'>" + thisGallery[1] + "</a></div></div>";
			
			if (((parseInt(index) + 1) % recordsPerRow) == 0 && index != maxValue) {
				divGalleryHtml = divGalleryHtml + "</div><div class='clear'></div><div class='gallery_row'>";
			}
		}
	}
	divGalleryHtml = divGalleryHtml + "</div></div></div><div id=\"textThumbBox\">";

	if (galleryType == 0) {
		var cityText = cityGalleryText;
		var stateText = "<a href=\"javascript:getCityState(1)\">" + StateGalleryText + "</a>";
	} else if (galleryType == 1) {
		var cityText = StateGalleryText;
		var stateText = "<a href=\"javascript:getCityState(0)\">" + cityGalleryText + "</a>";
	}

	divGalleryHtml = divGalleryHtml + "<div id='city-opt-1'><div class='photoarray-photos-head' id='city-txt'>" + cityText + "</div><div id='pagination-n'>";
	divGalleryHtml = divGalleryHtml + "<div class=\"photo-prev-n\"><a href=\"javascript:galleryBackward();\"><img title=\"\" src=\"" + SiteURLforJS + "/resources/images/left-arrow-in.gif\" id=\"left-arrow\"/></a></div><div class=\"photo-paging-n\">";
	firstPageShow = 1;
	totalPages = calPages(maxValue, maxRecords);

	if (pagesToShow < totalPages) {
		lastPageShow = pagesToShow;
	} else {
		lastPageShow = totalPages;
	}

	for (var i = firstPageShow; i <= lastPageShow; i++) {
		if (i == galleryCurrentIndex) {
			var className = "anchor_selected_small";
		} else {
			var className = "next-o-small";
		}
		divGalleryHtml = divGalleryHtml + "<a id=\"number" + i + "\" href=\"javascript:changeDetailsThroughNum(" + i + ")\" class=\"" + className + "\">" + i + "</a>";
	}

	divGalleryHtml = divGalleryHtml + "</div><div class=\"photo-next-n\"><a href=\"javascript:galleryForward();\"><img title=\"\" src=\"" + SiteURLforJS + "/resources/images/right-arrow.gif\" id=\"right-arrow\"/></a></div></div></div>";
	divGalleryHtml = divGalleryHtml + "<div id=\"pixelBarPhotoArray\"><img border=\"0\" src=\"" + SiteURLforJS + "/resources/images/pixelBarPhotoArray.gif\"/></div>";
	divGalleryHtml = divGalleryHtml + "<div id=\"state-opr-1\"><div id=\"state-txt\" class=\"photoarray-head\">" + stateText + "</div></div>";
	divGalleryHtml = divGalleryHtml + "</div>";
	document.getElementById("centerShadowRpt").innerHTML = divGalleryHtml;
}

function getCityState(id) {
	document.getElementById("galleryContainer").innerHTML = "";
	
	if (id == 1) {
		cityState = GALLERY_STATE;
		document.getElementById("cityDestination").style.display = "none";
		document.getElementById("stateDestination").style.display = "block";
	} else {
		cityState = GALLERY_CITY;
		document.getElementById("cityDestination").style.display = "block";
		document.getElementById("stateDestination").style.display = "none";
	}
	slDisplay.GetGalleries("galleryContainer", cityState, A);
}

function changeDetailsThroughNum(selectedIndex) {
	galleryCurrentIndex = selectedIndex;
	changeNumberSelected();
	changeArrow();
	changeGalleryDetails();
}

function changeArrow() {
	if (galleryCurrentIndex <= 1) {
		document.getElementById("left-arrow").src = SiteURLforJS + "/resources/images/left-arrow-in.gif";
	} else {
		document.getElementById("left-arrow").src = SiteURLforJS + "/resources/images/left-arrow.gif";
	}
	if (galleryCurrentIndex == totalPages) {
		document.getElementById("right-arrow").src = SiteURLforJS + "/resources/images/right-arrow-in.gif";
	} else {
		document.getElementById("right-arrow").src = SiteURLforJS + "/resources/images/right-arrow.gif";
	}
}

function changeGalleryDetails() {
	var startIndex = maxRecords * (galleryCurrentIndex - 1);
	var endIndex = maxRecords * galleryCurrentIndex;
	
	if (endIndex >= maxValue) {
		endIndex = maxValue;
	}
	var divGalleryHtml = "<div class='gallery_row'>";
	var index = 0;
	
	for (var i = startIndex, index = 1; i < endIndex; i++, index++) {
		divGalleryHtml = divGalleryHtml + "<div class='gallery'><div class='gallery_promo'><a href='" + GalleryValues[i][2] + "'><img src='" + GalleryValues[i][0] + "' /></a></div>";
		divGalleryHtml = divGalleryHtml + "<div class='gallery_title'><a href='" + GalleryValues[i][2] + "'>" + GalleryValues[i][1] + "</a></div></div>";
		if ((parseInt(index) % recordsPerRow) == 0 && i != maxValue) {
			divGalleryHtml = divGalleryHtml + "</div><div class='clear'></div><div class='gallery_row'>";
		}
	}
	divGalleryHtml = divGalleryHtml + "</div>";
	document.getElementById("galleryContainer").innerHTML = divGalleryHtml;
}

function galleryForward() {
	if (galleryCurrentIndex == totalPages) {
		galleryCurrentIndex = 0;
		firstPageShow = 0;
		lastPageShow = pagesToShow - 1;
	}

	if (galleryCurrentIndex < totalPages) {
		galleryCurrentIndex = galleryCurrentIndex + 1;
		
		if (lastPageShow < totalPages) {
			firstPageShow = firstPageShow + 1;
			lastPageShow = lastPageShow + 1;
		}
		
		if (galleryCurrentIndex <= 1) {
			var divHtml = "<div class=\"photo-prev-n\"><a href=\"javascript:galleryBackward();\"><img id=\"left-arrow\" src=\"" + SiteURLforJS + "/resources/images/left-arrow-in.gif\" title=\"\" /></a></div><div class=\"photo-paging-n\">";
		} else {
			var divHtml = "<div class=\"photo-prev-n\"><a href=\"javascript:galleryBackward();\"><img id=\"left-arrow\" src=\"" + SiteURLforJS + "/resources/images/left-arrow.gif\" title=\"\" /></a></div><div class=\"photo-paging-n\">";
		}

		for (var i = firstPageShow; i <= lastPageShow; i++) {
			if (i == galleryCurrentIndex) {
				var className = "anchor_selected_small";
			} else {
				var className = "next-o-small";
			}
			divHtml = divHtml + "<a id=\"number" + i + "\" href=\"javascript:changeDetailsThroughNum(" + i + ")\" class=\"" + className + "\">" + i + "</a>";
		}

		if (galleryCurrentIndex == totalPages) {
			divHtml = divHtml + "</div><div class=\"photo-next-n\"><a href=\"javascript:galleryForward();\"><img id=\"right-arrow\" src=\"" + SiteURLforJS + "/resources/images/right-arrow-in.gif\" title=\"\" /></a></div>";
		} else {
			divHtml = divHtml + "</div><div class=\"photo-next-n\"><a href=\"javascript:galleryForward();\"><img id=\"right-arrow\" src=\"" + SiteURLforJS + "/resources/images/right-arrow.gif\" title=\"\" /></a></div>";
		}
		document.getElementById("pagination-n").innerHTML = divHtml;
		changeGalleryDetails();
	}	
}

function galleryBackward() {
	if (galleryCurrentIndex == 0) {
		galleryCurrentIndex = totalPages;
		firstPageShow = totalPages - pagesToShow + 1;
		lastPageShow = totalPages;
	}
	
	if (galleryCurrentIndex == 1) {
		galleryCurrentIndex = totalPages + 1;
		firstPageShow = totalPages - pagesToShow + 2;
		lastPageShow = totalPages + 1;
	}
	
	if (galleryCurrentIndex > 1) {
		galleryCurrentIndex = galleryCurrentIndex - 1;
		if (firstPageShow > 1) {
			firstPageShow = firstPageShow - 1;
			lastPageShow = lastPageShow - 1;
		}
		if (galleryCurrentIndex <= 1) {
			var divHtml = "<div class=\"photo-prev-n\"><a href=\"javascript:galleryBackward();\"><img id=\"left-arrow\" src=\"" + SiteURLforJS + "/resources/images/left-arrow-in.gif\" title=\"\" /></a></div><div class=\"photo-paging-n\">";
		} else {
			var divHtml = "<div class=\"photo-prev-n\"><a href=\"javascript:galleryBackward();\"><img id=\"left-arrow\" src=\"" + SiteURLforJS + "/resources/images/left-arrow.gif\" title=\"\" /></a></div><div class=\"photo-paging-n\">";
		}

		for (var i = firstPageShow; i <= lastPageShow; i++) {
			if (i == galleryCurrentIndex) {
				var className = "anchor_selected_small";
			} else {
				var className = "next-o-small";
			}
			divHtml = divHtml + "<a id=\"number" + i + "\" href=\"javascript:changeDetailsThroughNum(" + i + ")\" class=\"" + className + "\">" + i + "</a>";
		}

		if (galleryCurrentIndex == totalPages) {
			divHtml = divHtml + "</div><div class=\"photo-next-n\"><a href=\"javascript:galleryForward();\"><img id=\"right-arrow\" src=\"" + SiteURLforJS + "/resources/images/right-arrow-in.gif\" title=\"\" /></a></div>";
		} else {
			divHtml = divHtml + "</div><div class=\"photo-next-n\"><a href=\"javascript:galleryForward();\"><img id=\"right-arrow\" src=\"" + SiteURLforJS + "/resources/images/right-arrow.gif\" title=\"\" /></a></div>";
		}
		document.getElementById("pagination-n").innerHTML = divHtml;
		changeGalleryDetails();
	}
}

function changeNumberSelected() {
	var divId = "";
	
	for (var i = firstPageShow; i <= lastPageShow; i++) {
		divId = "number" + i;
		imageref = document.getElementById(divId);
		if (galleryCurrentIndex == i) {
			imageref.className = "anchor_selected_small";
		} else {
			imageref.className = "next-o-small";
		}
	}
}
