// Postmode Simulacrum Switching Service Script //
// Author: John Nicely
// Last Modified Date: 09/29/2007
// Version: 1.0.0
// Copyright 2007+ John Nicely
/* Description: *********************************//*
This script takes a number of required input arguments
and an infinitely long list of image names/locations and
can be used in an onClick attribute to switch between
the images specified (going either to "next" or "previous"
images). */

function pm_3s() {
	// Expected Arguments: //
	// Any number of image paths/locations/filenames
	// Note: The first element in this array MUST be the original picture file.

	var imgObject = new Array();
	
	for( i = 0; i < arguments.length; i++ ) {
		imgObject[i] = arguments[i];
	}
	
	return imgObject;
}

function findImgInList( img, imgObject ) {
	var firstPart = "img searching for: " + img + "\n";
	for( i = 0; i < imgObject.length; i++ ) {
		//alert( firstPart + "imgObject[i]: " + imgObject[i] + " - result = " + imgObject[i].indexOf( img ) );
		if( imgObject[i].indexOf( img ) >= 0 ) {
			return i;
		};
	}
	
	for( i = 0; i < imgObject.length; i++ ) {
		firstPart = "imgObject[" + i + "] searching for: " + imgObject[i] + "\n";
		//alert( firstPart + "img: " + img + " - result = " + img.indexOf( imgObject[i] ) );
		if( img.indexOf( imgObject[i] ) >= 0 ) {
			return i;
		};
	}
	
	return -1;
}

function changeListPointer( method, imgObject, imgLocationInList ) {
	var newLocationInList = imgLocationInList;
	
	switch( method ) {
		case "increase":
			//alert( "Increase Step 1: newLocationInList: " + newLocationInList );
			//alert( "Increase Step 1: imgLocationInList: " + imgLocationInList );
			newLocationInList++;
			//alert( "Increase Step 2: newLocationInList: " + newLocationInList );
			if( newLocationInList > (imgObject.length - 1) ) {
				newLocationInList = 0;
			};
			
			break;
		case "decrease":
			//alert( "Increase Step 1: newLocationInList: " + newLocationInList );
			//alert( "Increase Step 1: imgLocationInList: " + imgLocationInList );
			newLocationInList--;
			//alert( "Increase Step 2: newLocationInList: " + newLocationInList );
			if( newLocationInList < 0 ) {
				newLocationInList = imgObject.length - 1;
			}
			break;
	}
	
	return newLocationInList;
}

function simulacrumSwitcher(imgObject, direction, imgID) {
	var imgSrc = document.images[imgID].src;
	
	switch( direction ) {
		case "preceding":
			var imgLocationInList = findImgInList( imgSrc, imgObject );
			var newLocationInList = changeListPointer( "decrease", imgObject, imgLocationInList );
			document.images[imgID].src = imgObject[newLocationInList];
			//alert ( "Switcher Results: imgLocationInList: " + imgLocationInList );
			//alert ( "Switcher Results: newLocationInList: " + newLocationInList );
			//alert( "Switcher Results: imgID.src: " + document.images[imgID].src );
			break;
		case "succeeding":
			var imgLocationInList = findImgInList( imgSrc, imgObject );
			var newLocationInList = changeListPointer( "increase", imgObject, imgLocationInList );
			document.images[imgID].src = imgObject[newLocationInList];
			//alert ( "Switcher Results: imgLocationInList: " + imgLocationInList );
			//alert ( "Switcher Results: newLocationInList: " + newLocationInList );
			//alert( "Switcher Results: imgID.src: " + document.images[imgID].src );
			break;
	}
}