/***********************************************
	Slide Show IV
	Version 1.0
	Last Revision: 09.25.2004
	steve@slayeroffice.com
	
	PLEASE LEAVE THIS NOTICE IN TACT!

	Should you modify/improve upon this code
	please let me know so that i may update the 
	version hosted at slayeroffice

***********************************************/


//window.onload = init;		// set up the initialization event
var d=document;				// shortcut reference to the document object
var imageObjArray;			// this array will hold all the info we need for the images found in the imageContainer div
var imgArray;
var currentImg = 0;			// the index of the current image
var primaryObj,containerObj,captionObj,creditObj,navObj;// references to div elements
var dims;					// array to hold the dimension transitions
var wIndex=0;				// the index in the width subset of the array
var hIndex=0;				// the index in the height subset of the array
var zInterval = null;		// the animation interval
var INCREMENT = 100;		// the rate at which we move through the dimension tranisition array. lower to slow it down, raise it to speed up.
var bbWidth = 1;			// Width of the border under the selected image's thumbnail
//var maxWidth = 0;

function init() {
	if(!d.getElementById)return;		//bail out if this is an older browser

	imageObjArray = getImageDimensions(d.getElementById("imageContainer"));	// get all the data we need on the images
	if ( imageObjArray.length > 0 ) {
		createImageNav(imageObjArray);						// create the navigation elements
	
		creditObj = d.getElementById("mContainer").appendChild(d.createElement("div"));	//create the credit object
		creditObj.id = "credit";		// set the credit to the first image's credit attribute
		creditObj.innerHTML = imageObjArray[0][3];			// set the credit to the first image's credit attribute
		creditObj.style.textTransform = "uppercase";
		
		captionObj = d.getElementById("mContainer").appendChild(d.createElement("div"));	//create the caption object
		captionObj.id = "caption";
		captionObj.innerHTML = imageObjArray[0][0].getAttribute("alt");			// set the caption to the first image's alt attribute
	
		imageObjArray[0][0].style.display = "block";					// show the first image and set its opacity to near 100%
		resetOpacity(imageObjArray[0][0],99);							// mozilla browsers on Win32 give an ugly flash if you set the object opacity to 100%, so we use 99 to avoid that
		var w = imageObjArray[0][1];
		var h = imageObjArray[0][2];
	
		containerObj = d.getElementById("imageContainer");				// create a reference to the imageContainer element
		containerObj.style.width = imageObjArray[0][1] + "px";		// and set its dimensions the same as the first image.
		containerObj.style.height = imageObjArray[0][2] + "px";
		
		primaryObj = d.getElementById("primaryContainer");			// create a reference to the primaryContainer element
		primaryObj.style.width = imageObjArray[0][1] - 0 + 2;
	
		d.getElementById("inlineBox").style.width = imageObjArray[0][1] - 0 + 2;
	}
}

// following function sets the object's (argument obj) opacity to argument val
// and covers all three methods for the different browsers capable of the effect
function resetOpacity(obj,val) {
	//if(window.opera)return;
	obj.style.MozOpacity = val/10;
	obj.style.opacity = val/10;
	obj.style.filter = "alpha(opacity="+val*10+")";
}

// creates an array of image objects and the data we need for them.
// only looks at the images found in the parentObj argument.
function getImageDimensions(parentObj) {
	imgArray = parentObj.getElementsByTagName("img");
	nImage = new Array();
	for(i=0;i<imgArray.length;i++) {
		nImage[i] = new Array();
		nImage[i][0] = imgArray[i];
		// MSIE returns "0" for an object's dimensions when said object's display style is 'none'. we use bogus
		// attributes to get around this annoying flaw.
		nImage[i][1] = imgArray[i].getAttribute("xwidth");
		
		if (maxWidth < nImage[i][1]) { maxWidth = nImage[i][1]; }
		
		nImage[i][2] = imgArray[i].getAttribute("xheight");
		nImage[i][3] = imgArray[i].getAttribute("credit");
		//alert("WIDTH:"+imgArray[i].width+"\nHEIGHT"+imgArray[i].height);
		
	}
	return nImage;
}

//this function simply creates the navigational elements for the slide show based on the length of
// the array passed to it.
function createImageNav(imgArray) {
	if ( imgArray.length < 2 ) return;
	n = d.getElementById("primaryContainer").appendChild(d.createElement("div"));
	n.id = "navContainer";
	for(i=0;i<imgArray.length;i++) {
		a = n.appendChild(d.createElement("a"));
		a.href = "javascript:showImage(" + i + ");";
		a.id = "a"+i;
		//a.innerHTML = i+1;
		a.innerHTML = "<img src='"+imageObjArray[i][0].getAttribute("src")+"' height='40' border='1' alt='"+imageObjArray[i][0].getAttribute("alt")+"'/>";
	}
	navObj = n;
	//d.getElementById("a0").style.borderBottomWidth = bbWidth;
	resetOpacity(d.getElementById("a0"),5);
}


function showImage(imgIndex) {
	if(zInterval != null || imgIndex == currentImg) return;		// return if we are already animating or the user is clicking on the active image
	d.getElementById("caption").innerHTML = "";					// reset the caption element to a blank string
	imageObjArray[currentImg][0].style.display = "none";		// hide the current image
	
	//d.getElementById("a"+currentImg).style.borderBottomWidth = 0;// get rid of the border on the navigation element and then give it to the now active navigation element
	resetOpacity(d.getElementById("a"+currentImg),99);
	//d.getElementById("a"+imgIndex).style.borderBottomWidth = bbWidth;
	resetOpacity(d.getElementById("a"+imgIndex),5);
	
	resetOpacity(imageObjArray[currentImg][0],0);				// reset the opacity of the new current image to 0 and then figure out its dimension transitions
	dims = calculateDimensionTransition(imageObjArray[currentImg][1],imageObjArray[currentImg][2],imageObjArray[imgIndex][1],imageObjArray[imgIndex][2]);
	currentImg = imgIndex;										// set the current image to the image passed in
	zInterval = setInterval("resizeContainer()",10);			// begin the fade-in interval
}

function resizeContainer() {
	if(wIndex<dims[0].length)containerObj.style.width = dims[0][wIndex] + "px";	// if we havent exceeded the length of widths and heights, set the container object to the new dimensions
	if(hIndex<dims[1].length)containerObj.style.height = dims[1][hIndex] + "px";		

	wIndex+=INCREMENT; hIndex+=INCREMENT;		// add INCREMENT to the index's. Incrementing or decrementing here will speed up/slow down the animation
	try {
		containerObj.style.top = (375-dims[1][hIndex])/2 + "px"; // IE can flip out here if we go out of range a little, so we catch the error.
	} catch(err) { }

	if(wIndex>=dims[0].length && hIndex >= dims[1].length) { // we've reached the width and height transition. clean it up and get ready for the next round
		clearInterval(zInterval);
		wIndex=0;
		hIndex=0;
		imageObjArray[currentImg][0].style.display = "block";
		containerObj.style.width = imageObjArray[currentImg][1] + "px";
		containerObj.style.height = imageObjArray[currentImg][2] + "px";
		containerObj.style.left = "0px";
		containerObj.style.top = "0px";
		//primaryObj.style.width = imageObjArray[currentImg][1] - 0 + 10;
		primaryObj.style.width = imageObjArray[currentImg][1] - 0  + 2;
		
		//alert(navObj.style.width+"\n"+navObj.style.height);
		
		curOpacity = 0;
		zInterval = setInterval("fadeImage()",10);
	}
}

// this function fades the object in until it is 0.9 (90%) opaque
function fadeImage() {
	curOpacity++;
	resetOpacity(imageObjArray[currentImg][0],curOpacity);
	if(curOpacity/10 == 0.9) {
		resetOpacity(imageObjArray[currentImg][0],9.9);
		captionObj.innerHTML = imageObjArray[currentImg][0].getAttribute("alt");
		creditObj.innerHTML = imageObjArray[currentImg][3];
		clearInterval(zInterval);
		zInterval = null;
		curOpacity=0;
	}
}

// calculates the transitions required for a 200x100 element to become a 315x60 element.
// the returned array is then used in resizeContainer for the animation effect.

function calculateDimensionTransition(startW,startH,endW,endH) {
	dimensions = new Array();
	dimensions[0] = new Array(); dimensions[1] = new Array();
	nW = startW;
	nH = startH;
	if(endW>startW) {
		while(nW<endW) {
			nW++;
			dimensions[0][dimensions[0].length] = nW;
		}
	} else {
		while(nW>endW) { 
			nW--;
			dimensions[0][dimensions[0].length] = nW--;
		}
	}

	if(endH>startH) {
		while(nH<endH) {
			nH++;
			dimensions[1][dimensions[1].length] = nH;
		}
	} else {
		while(nH>endH) {
			nH--;
			dimensions[1][dimensions[1].length] = nH;
		}
	}

	return dimensions;
}