////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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 defViewerWidth     = 640;
var defViewerHeight    = 640;
var defPanoramicScroll = true;
var defPanoramicRatio  = 1.8;

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;

// Filtering Variables
var defExts         = "jpg,jpeg,gif";
var defIDs          = "rpIgnore";

var iFilter  = (typeof(includeFilter) != "undefined") ? includeFilter : "";
var eFilter  = (typeof(excludeFilter) != "undefined") ? excludeFilter : "";
var extText  = (typeof(extensions) != "undefined") ? extensions.replace(/ /g, "") : defExts;
var idText   = (typeof(excludeIDs) != "undefined") ? excludeIDs.replace(/ /g, "") : defIDs;
var extArray = extText.split(",");
var idArray  = idText.split(",");

// Border Variables
var defBorderSize = 0
var defBorderColor = ""
var defBorderColorLight = ""
var defBorderColorDark = ""
var tableStart = ""
var tableEnd = ""

// *********************************************************************************************
function InitViewer() {
    defViewerWidth     = (typeof(viewerWidth)     != "undefined") ? viewerWidth     : defViewerWidth;
    defViewerHeight    = (typeof(viewerHeight)    != "undefined") ? viewerHeight    : defViewerHeight;
    defPanoramicScroll = (typeof(panoramicScroll) != "undefined") ? panoramicScroll : defPanoramicScroll;
    defPanoramicRatio  = (typeof(panoramicRatio)  != "undefined") ? panoramicRatio  : defPanoramicRatio;

    var bs = (typeof(borderSize)       != "undefined") ? borderSize       : defBorderSize;
    var bc = (typeof(borderColor)      != "undefined") ? borderColor      : defBorderColor;
    var bl = (typeof(borderColorLight) != "undefined") ? borderColorLight : defBorderColorLight;
    var bd = (typeof(borderColorDark)  != "undefined") ? borderColorDark  : defBorderColorDark;
    
    if (bs > 0) {
        tableStart = "<table cellpadding=0 cellspacing=0 border=" + bs;
        if (bc.length > 0) tableStart += " BORDERCOLOR='"      + bc + "'";
        if (bl.length > 0) tableStart += " BORDERCOLORLIGHT='" + bl + "'";
        if (bd.length > 0) tableStart += " BORDERCOLORDARK='"  + bd + "'";
        tableStart += "><tr><td>";
        tableEnd = "</td></tr></table>";
    }

    CountPics();
}

// *********************************************************************************************
function FilterPic( thisLink ) {
	var linkText = thisLink.getAttribute("href");
	var linkID = thisLink.id.toLowerCase();
	var dot = linkText.lastIndexOf(".");

	foundPic = false;
	foundID = false;

	if( dot >= 0 ) {
		extension = linkText.substr(dot + 1,linkText.length);
		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;
	}
	
    return foundPic;
}

// *********************************************************************************************
function CountPics() {
	picCount = 0;

	for (i = 0; i < document.links.length; i++) {
	    thisLink = document.links[i];
	    
	    if (FilterPic( thisLink )) {
			picCount++;
			thisLink.id = "p" + picCount;
			thisLink.onclick=new Function("return rpView(this)");
			thisLink.innerHTML = tableStart + thisLink.innerHTML + tableEnd;
	    }
	}

	// 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', defViewerWidth, defViewerHeight + 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.toLowerCase())
		{
			return(true);
		}
	}

	return( false );
}
// *********************************************************************************************

InitViewer();

