/* ================================
    thunder::tech Setup JavaScript
   ================================ */

/* Adds a contains function to Array
   Credit: http://www.roseindia.net/java/javascript-array/javascript-array-contains.shtml */

Array.prototype.contains = function (element)
{
	for (var i = 0; i < this.length; i++)
	{
		if (this[i] == element)
		{
			return true;
		}
	}
	return false;
};

function src_to(target_element, new_src)
{
	/* Changes the src attribute of an element
	   ex: <img onmouseover="src_to(this, 'images/thundertech.gif')"
	   onmouseout="src_to(this, '')" /> */
	target_element.src = new_src;
}

function preload_image(image)
{
	var nImage = new Image;
	myImages[image] = nImage;
	nImage.src = image;
}

// Legacy version as of 1/6/08, used for compatibility with this site
function random_image_by_number(target_element, filebase, maxcount, fileext)
{
	var pickone=Math.floor(Math.random()*maxcount)+1;
	selectedimgurl = filebase + pickone + fileext;
	src_to(document.getElementById(target_element), selectedimgurl);
}

/* Replaces the innerHTML of a container with a thumbnail image gallery using the following properties of its settingsObject parameter:

   filenameStart, filenameEnd, lastImage, and excludeList are the same as in randomImageByNumber
   thumbFilenameStart and thumbFilenameEnd are filenameStart and filenameEnd for the thumbnails
   aAttributes, imgAttributes - Optional, additional attributes to place in the a and img tags.
   thumbColumns - The number of columns (div-based)
   columnClass - The class of column divs if thumbColumns >= 1
   galleryName - Affects the IDs of the images.  Necessary to set if there is more than one gallery on a page; default is thunderGallery
   popupAreaID - The ID string of the area that receives the images
   containerTarget - the DOM element to replace the innerHTML of, preferably a DIV tag.
   autoShowFirst - Optional, specify True to automatically populates the container specified with popupAreaID with the first image
   previousLink - Optional, text or HTML code that makes a previous link.  Default hides the link.
   nextLink - Optional, text or HTML code that makes a next link.  Default hides the link.
   useThickBox - Optional, specify True to set the class to thickbox, uses galleryName for the previous-next feature if available, and
      disregards popupAreaID.  Apply thickbox separately in order for this feature to work.
	  Initialize with TB_init after calling generateThumbGallery; doing so before will fail to add the correct javascript event to
	  elements of class "thickbox."  Makes the following properties irrelevent: nextLink, previousLink, autoShowFirst, popupAreaID.
	  Don't apply a class to the a tag using aAttributes because the a tag's class is thickbox.

   You cannot exclude 1 or the last number in the list or the previous/next will fail to work.
   
   Example using popupAreaID:
   generateThumbGallery({nextLink:"Next &gt;", previousLink:"&lt; Previous", autoShowFirst: true, popupAreaID: "test3", galleryName: "myGallery", filenameStart: "test/p", filenameEnd:".jpg", thumbFilenameStart: "test/sm_p", thumbFilenameEnd:"_t.jpg", lastImage: 4, containerTarget: document.getElementById("test2"), thumbColumns: 2, columnClass: "proof3", imgAttributes: " class=\"proof1\"", aAttributes: " class=\"proof2\"", excludeList: [2] });
   
   Example using thickbox:
   generateThumbGallery({useThickBox: true, galleryName: "myGallery", filenameStart: "test/p", filenameEnd:".jpg", thumbFilenameStart: "test/sm_p", thumbFilenameEnd:"_t.jpg", lastImage: 4, containerTarget: document.getElementById("test2"), thumbColumns: 2, columnClass: "proof3", excludeList: [2] });
   
   */

function generateThumbGallery(settingsObject)
{
	if(settingsObject.containerTarget)
	{
		var excludeList;
		var finalInnerHTML = "";
		var imgCounter;
		var currentImgURL;
		var colCount = 0;
		var colMax;
		var colClass;
		if(settingsObject.excludeList)
		{
			excludeList = settingsObject.excludeList;
		}
		else
		{
			excludeList = new Array;
		}
		if(settingsObject.aAttributes)
		{
			settingsObject.aAttributes = " " + settingsObject.aAttributes;
		}
		if(!(settingsObject.galleryName))
		{
			settingsObject.galleryName = "thunderGallery";
		}
		if(settingsObject.useThickBox)
		{
			settingsObject.aAttributes = settingsObject.aAttributes + " rel=\"" + settingsObject.galleryName + "\"";
		}
		if(settingsObject.imgAttributes)
		{
			settingsObject.imgAttributes = " " + settingsObject.imgAttributes;
		}
		if(!(settingsObject.previousLink))
		{
			settingsObject.previousLink = "";
		}
		if(!(settingsObject.nextLink))
		{
			settingsObject.nextLink = "";
		}
		if(settingsObject.thumbColumns)
		{
			colMax = settingsObject.thumbColumns;
			if(settingsObject.columnClass)
			{
				colClass = " class=\""+settingsObject.columnClass+"\"";
			}
			else
			{
				colClass = "";
			}
			finalInnerHTML = "<div"+colClass+">"
		}
		for(imgCounter = 1; imgCounter <= settingsObject.lastImage; imgCounter++)
		{
			if(excludeList.contains(imgCounter)==false)
			{
				if(settingsObject.autoShowFirst == true)
				{
					settingsObject.autoShowFirst = false;
					popupGalleryImage(imgCounter, settingsObject.lastImage, settingsObject.excludeList, settingsObject.filenameStart, settingsObject.filenameEnd, settingsObject.previousLink, settingsObject.nextLink, settingsObject.popupAreaID);
				}
				currentImgURL = settingsObject.thumbFilenameStart + imgCounter + settingsObject.thumbFilenameEnd;
				if(settingsObject.useThickBox)
				{
					finalInnerHTML = finalInnerHTML + "<a href=\"" + settingsObject.filenameStart + imgCounter + settingsObject.filenameEnd + "\"" + settingsObject.aAttributes + " class=\"thickbox\" "+"><img src=\""+currentImgURL+"\""+settingsObject.imgAttributes+" id=\""+settingsObject.galleryName+imgCounter+"\" /></a>";
				}
				else
				{
					finalInnerHTML = finalInnerHTML + "<a"+settingsObject.aAttributes+" "+genPopupHref(settingsObject, imgCounter)+"><img src=\""+currentImgURL+"\""+settingsObject.imgAttributes+" id=\""+settingsObject.galleryName+imgCounter+"\" /></a>";
				}
				if(colMax > 0)
				{
					colCount ++;
					if((colCount >= colMax)&&(imgCounter<settingsObject.lastImage))
					{
						colCount = 0;
						finalInnerHTML = finalInnerHTML + "</div><div"+colClass+">";
					}
				}
			}
		}
		if(colMax > 0)
		{
			finalInnerHTML = finalInnerHTML + "</div>"
		}
		settingsObject.containerTarget.innerHTML = finalInnerHTML;
	}
}

/* Internal:
   Generates a href="" with a javascript function call to popupGalleryImage for:
   - the Previous/Next links generated by popupGalleryImage
   - the images generated by generateThumbGallery
   Requires an image index and either generateThumbGallery's settingsObject or a scaled down version
   with at least previousLink, nextLink, lastImage, excludeList, filenameStart, filenameEnd, and popupAreaID.
*/

function genPopupHref(settingsObject, imgCounter)
{
	var thisHTMLCode = "href=\"javascript:popupGalleryImage("+imgCounter+", " + settingsObject.lastImage+", ";
	thisHTMLCode = thisHTMLCode + "[" + settingsObject.excludeList.toString() + "], ";
	thisHTMLCode = thisHTMLCode + "'" + settingsObject.filenameStart + "', ";
	thisHTMLCode = thisHTMLCode + "'" + settingsObject.filenameEnd + "', ";
	thisHTMLCode = thisHTMLCode + "'" + settingsObject.previousLink + "', ";
	thisHTMLCode = thisHTMLCode + "'" + settingsObject.nextLink + "', ";
	thisHTMLCode = thisHTMLCode + "'" + settingsObject.popupAreaID+"');\"";
	return thisHTMLCode;
}


/* Internal:
   Shows a popup image from a link generated by either itself or generateThumbGallery.
*/
function popupGalleryImage(imgCounter, lastImage, excludeList, filenameStart, filenameEnd, previousLink, nextLink, myAreaID)
{
	var myURL = filenameStart + imgCounter + filenameEnd;
	var finalInnerHTML = "<img src=\"" + myURL + "\" />";
	var prevInt = exclusiveIntSearch(imgCounter, -1, excludeList, lastImage, 1, lastImage);
	var nextInt = exclusiveIntSearch(imgCounter, 1, excludeList, lastImage, 1, 1);
	if(previousLink!="")
	{
		finalInnerHTML = finalInnerHTML + "<a style=\"float: left; clear: left;\" " + genPopupHref({previousLink: previousLink, nextLink:nextLink, filenameStart: filenameStart, filenameEnd: filenameEnd, popupAreaID: myAreaID, excludeList: excludeList, lastImage: lastImage}, prevInt) + ">"+previousLink+"</a>";
	}
	if(nextLink!="")
	{
		finalInnerHTML = finalInnerHTML + "<a style=\"float: right; clear: right;\" " + genPopupHref({previousLink: previousLink, nextLink:nextLink, filenameStart: filenameStart, filenameEnd: filenameEnd, popupAreaID: myAreaID, excludeList: excludeList, lastImage: lastImage}, nextInt) + ">"+nextLink+"</a>";
	}
	document.getElementById(myAreaID).innerHTML = finalInnerHTML;
}

/* Internal:
   Searches for an integer by an interval with a minimum, maximum, and exlude list.  Returns fail if minimum or maximum are reached.
   Used in popupGalleryImage to get the next and previous images in the gallery.
*/

function exclusiveIntSearch(presentInt, incInt, excludeList, maxInt, minInt, failInt)
{
	var thisInt = presentInt;
	do
	{
		thisInt = thisInt + incInt;
		if((thisInt > maxInt)||(thisInt < minInt))
		{
			//alert("failed and returned "+ failInt);
			return failInt;
		}
	}
	while(excludeList.contains(thisInt)!=false)
	//alert("succeeded and returned "+ failInt);
	return thisInt;
}