////////////////////////////////////////////////////////////////////////////////////////////////
//
// rpViewer - Popup JavaScript Image Viewer
//
// made by:
//   Russ Prince 01-11-2004
//
// Feel free to use as you see fit.
//
////////////////////////////////////////////////////////////////////////////////////////////////

// Location of the 'rpViewer.htm' file.  This path should be relative to the root of your website.
// If you installed this package correctly then this should not need to be changed, but I left this
// here in case people did not want to install this at the root of their website.

var viewer = "/rpViewer/rpViewer.htm"

////////////////////////////////////////////////////////////////////////////////////////////////
// Do not alter anything beyond here
////////////////////////////////////////////////////////////////////////////////////////////////

// Default width & height of the images.  If you set alwaysFit to true all images regardless of
// their size will be resized to fit within the window.  If you set it to false (recommended)
// the code will try to intelligently fit the pictures in the window.  Panoramic images will show
// with a scrollbar so you can view them better.  The current settings are the best overall settings
// and should not need to be changed.  If you want to change these it is best to do it in the HTML
// code with a small block of script code at the bottom of your html.  See readme.txt for details

var viewerWidth     = 640;
var viewerHeight    = 640;
var panoramicScroll = false;
var panoramicRatio  = 1.8;

var defExts         = "jpg,jpeg,gif,png";
var defIDs          = "rpIgnore";
var stretchToFit    = true;
var slideshow       = false;
var random          = false;
var toolbar         = true;
var picNum          = 1;
var picCount        = 0;
var slideshowDelay  = 10;
var zoomLevel       = 1.0;
var randArray;
var slideshowTimer;

var pic_loaded = false;
var but_loaded = false;
var wrk_loaded = false;

// *********************************************************************************************

function CountPics() {
	var extension = "";
	var dot;
	var linkText;
	var linkID;
	var thisLink;
	var iFilter = (typeof(includeFilter) != "undefined") ? includeFilter : "";
	var eFilter = (typeof(excludeFilter) != "undefined") ? excludeFilter : "";

	var extText = (typeof(extensions) != "undefined") ? extensions.replace(/ /g, "") : defExts.replace(/ /g, "");
	var extArray = extText.split(",");

	var idText = (typeof(excludeIDs) != "undefined") ? excludeIDs.replace(/ /g, "") : defIDs.replace(/ /g, "");
	var idArray = idText.split(",");

	picCount = 0;

	for (i = 0; i < document.links.length; i++) {
		thisLink = document.links[i];
		linkText = thisLink.getAttribute("href");
		linkID = thisLink.id.toLowerCase();
		dot = linkText.lastIndexOf(".");

		foundPic = false;
		foundID = false;

		if( dot >= 0 ) {
			extension = linkText.substr(dot + 1,linkText.length).toLowerCase();

			foundPic = Contains(extArray, extension) && !Contains(idArray, linkID);

			// filter
			foundPic = (foundPic && iFilter.length > 0) ? (linkText.indexOf(iFilter) >= 0) : foundPic;
			foundPic = (foundPic && eFilter.length > 0) ? (linkText.indexOf(eFilter) <  0) : foundPic;

			if (foundPic) {
				picCount++;
				thisLink.id = "p" + picCount;
				thisLink.onclick=new Function("return rpView(this)");
			}
		}
	}

	// create array for random pics  
	randArray = new Array(picCount);
	ResetRandArray();
}
// *********************************************************************************************
function Next() { SetPicNum( picNum < picCount        ? picNum + 1    : 1);         }
function Prev() { SetPicNum( picNum > 1               ? picNum - 1    : picCount ); }
function Home() { SetPicNum( 1 );                                                   }
function Ends() { SetPicNum( picCount );                                            }
function PgUp() { SetPicNum( picNum > 10              ? (picNum - 10) : 1        ); }
function PgDn() { SetPicNum( picNum < (picCount - 10) ? (picNum + 10) : picCount ); }
// *********************************************************************************************
function GetPicNum() {
	return((random) ? randArray[picNum - 1] : picNum);
}
// *********************************************************************************************
function GetPic() {
	return(document.getElementById( "p" + ((random) ? randArray[picNum - 1] : picNum) ).href);
}
// *********************************************************************************************
function SetPicNum(num) {
	if (document.getElementById( "p" + num )) {
		picNum = num;
	}
}
// *********************************************************************************************
function ChangeRandState() {
	random = !random;
	
	if (random) {
		// Reshuffle the array
		ResetRandArray();
		JumpToPictureIndexInRandomArray( picNum );
	} else {
		// random was turned off so make sure the current index is set to the pic we were just looking at
		SetPicNum(randArray[picNum - 1]);
	}
}
// *********************************************************************************************
function ResetRandArray() {
	var found = true;
	var thisPic = 0;
	
	for (i = 0; i < picCount; i++) {
		found = true;
		
		while (found) {
			found = false;
			thisPic = Math.round( (picCount - 1) * Math.random() ) + 1;
			
			for (j = 0; j < i && !found; j++) {
				if (randArray[j] == thisPic) {
					found = true;
				}
			}
		}
		
		randArray[i] = thisPic;
	}
}
// *********************************************************************************************
function JumpToPictureIndexInRandomArray( idx ) {
	for (i = 0; i < picCount; i++) {
		if (randArray[i] == idx) {
			SetPicNum(i + 1);
			return;
		}
	}
}
// *********************************************************************************************
function rpView( picLink ) {
	var clickedPictureNumber = parseInt(picLink.id.substring(1, picLink.id.length));
	
	if (random) {
		JumpToPictureIndexInRandomArray( clickedPictureNumber );
	} else {
		SetPicNum( clickedPictureNumber );
	}
	
	OpenWindow(viewer, 'rpViewer', viewerWidth, viewerHeight + 35);
	
	// this must return false so that the links to the picutes do not click
	return false;
}
// *********************************************************************************************
function OpenWindow(url, name, width, height) {
	window.open(url, name, "resizable=yes,scrollbars=yes,width=" + width + ",height=" + height + ",left=" + (screen.availWidth / 2 - (width / 2)) + ",top=" + (screen.availHeight / 2 - (height / 2)));
}
// *********************************************************************************************
function Contains( thisArray, val ) {
	for (j = 0; j < thisArray.length; j++) {
		if (thisArray[j].toLowerCase() == val)
		{
			return(true);
		}
	}

	return( false );
}
// *********************************************************************************************

CountPics();
