/*
JAVASCRIPT FUNCTIONALITY FOR:
	PhotoWallScroller.asp
	IFramed file for Photo Gallery
	Works in conjunction with: PhotoWallPhoto.asp and incPhotoWall.js

*/

var nScrollPos = 0;
var nCurIndex = 0;
var nCurPhotoID = 0;
var nTotalPhotos = 0;
var nPhotoWidth = 72;
var nIFrameWidth =577;
var bAddIEHover = false;

// Called OnLoad
function funInit() {
	funScrollToPhoto();
	try {
		top.bGalleryLoaded = true;
	}
	catch(e) {
		// Don't Do Nothin'
	}
	if (bAddIEHover) sfHover();
}

// Displays this Image
function funClickPhoto(nPhotoID, nIndex, evt) {
	if (nCurPhotoID != nPhotoID) {
		funSetCurPhoto(nPhotoID, nIndex);
		top.funShowPhoto(nPhotoID);
	}
	return gfunCancelEvent(evt);
}

// Highlight the Current Selected Photo
function funSetCurPhoto(nPhotoID, nIndex) {
	var objImage;
	if (nCurPhotoID != nPhotoID) {
		objImage = g_getRawObject("Photo" + nCurPhotoID);
		objImage.className = "toggleOpacity";
		funSetMaxSize(objImage, 54);
	}
	objImage = g_getRawObject("Photo" + nPhotoID);
	objImage.className = "imageOn";
	funSetMaxSize(objImage, 54);

	nCurPhotoID = nPhotoID;
	if (nIndex != null) {
		if (isNaN(nIndex)) {
			nIndex = (nIndex.toLowerCase() == "prev") ? (nCurIndex - 1) : (nCurIndex + 1);
		}
		nCurIndex = nIndex;
	}

	funScrollToPhoto(nIndex, nPhotoID);
}

// Shows the image at the correct size
function funSetMaxSize(objImage, nMaxSize) {
	if (objImage.width == objImage.height) {
		objImage.width = nMaxSize;
		objImage.height = nMaxSize;
	}
	else if (objImage.width > objImage.height) {
		objImage.height = Math.round( objImage.height / (objImage.width / nMaxSize));
		objImage.width = nMaxSize;
	}
	else {
		objImage.width = Math.round( objImage.width / (objImage.height / nMaxSize));
		objImage.height = nMaxSize;
	}
}

// Scroll to the Selected Photo
function funScrollToPhoto(nIndex, nPhotoID) {
	if (nIndex == null && nPhotoID == null) nIndex = nCurIndex;
	if (nIndex != null) {
		var nPixels;
		if (nIndex > 3) {
			nPixels = (nIndex * nPhotoWidth) ;
			if (nPixels > funMaxScrollValue()) nPixels = funMaxScrollValue();
		}
		else {
			nPixels = 0;
		}
		
		// Move the Page
		funScrollToPos(nPixels, nIndex);
	}
	else if (nPhotoID != null) {
		var objPhoto = g_getRawObject("Photo" + nPhotoID);
		if (objPhoto) if (objPhoto.scrollIntoView) objPhoto.scrollIntoView();
		if (gfunGetScrollValueY() != null) nScrollPos = gfunGetScrollValueY();
		else nScrollPos = gfunGetObjectLeft(objPhoto);
	}
}

// Scroll Left or Right One Picture
function funScroll(sDir) {
	var nPixels = (sDir.toLowerCase() == "left") ? (0 - nPhotoWidth) : (nPhotoWidth);
	nPixels += nScrollPos;
	if (nPixels < 0) nPixels = 0;
	else if (nPixels > funMaxScrollValue()) nPixels = funMaxScrollValue();
	
	funScrollToPos(nPixels);
	
	if (nPixels == 0 || nPixels == funMaxScrollValue()) return false;
	else return true;
}

// Scroll the Window to a certain Y Position
function funScrollToPos(nPixels, nIndex) {
	nScrollPos = nPixels;

	if (window.scroll) {
		window.scroll(nPixels, 0);
	}
	else if (window.scrollTo) {
		window.scrollTo(nPixels, 0);
	}
	else {
		if (nIndex == null) nIndex = Math.floor(nPixels / nPhotoWidth);
		var objCell = g_getRawObject("Cell" + (nIndex - 3));
		if (objCell) if (objCell.scrollIntoView) objCell.scrollIntoView();
	}
}

// Get the Maximum Scroll Postion
function funMaxScrollValue() {
	return (nTotalPhotos * nPhotoWidth) - nIFrameWidth;
}

// Set Data For Top document
function funSetTopIndexes() {
	try {
		top.nCurrentIndex = nCurIndex+1;
		top.nPreviousPhotoID = (nCurIndex == 0) ? 0 : aryPhotos[nCurIndex-1];
		top.nNextPhotoID = (nCurIndex == aryPhotos.length-1) ? 0 : aryPhotos[nCurIndex+1];
	}
	catch(e) {
		// Don't Do Anything
	}
}

