/************************************
	Slide Show V
	version 1.0
	last revision: 01.25.2005
	steve@slayeroffice.com

	should you modify or improve upon
	this code, let me know so that I can
	update the version hosted at slayeroffice

	PLEASE LEAVE THIS NOTICE IN TACT!

************************************/
// set up the onload event handler
//window.onload = init; 	
// shorcut reference var to the document object	
var d = document;			
var zInterval = null;		// timer interval
var swapped = false;		// boolean to tell us if we've swapped the old image for the new one yet

var mImages = new Array();	// reference array to the image elements in the container element

var MASK_SPEED = 8

var activeTransition = false;		// are we currently transitioning?

var currentContent="SubNav1";

function init() {
	if(!d.getElementById) return;					// older browser, do nothing.
	mObj = d.getElementById("mContainer");			// reference to the container element
	maskObj = mObj.appendChild(d.createElement("div"));		// create the mask element
	maskObj.style.left = "-800px";				// put the mask outside of viewable range. Dont define this in the CSS or Opera will refuse to move the DIV
	maskObj.id = "mask";					// the objects ID

	

/*	mImages = mObj.getElementsByTagName("img");		// reference array for the images in the container element

	// create navigational elements
	navObj = d.getElementById("navContainer");			// reference to the navigation container
	for(i=0;i<mImages.length;i++) {				// loop over the length of images we have and create anchor elements for each
		a = navObj.appendChild(d.createElement("a"));
		a.setAttribute("href","#");
		a.xid = i;						// xid references the index of the image in the mImages array
		a.onclick = function() { initSlideMask(this.xid); }		// set up the onclick event
		a.appendChild(d.createTextNode(i+1));		// put the text in the anchor element
	}

	setCaptionText(0);						// set the caption for the 0th image in the array
	*/
}

function getTransitionType() {					// gets the currently selected radio button and returns the index 
	if(!d.getElementById("controls")) return 0;			// so we know which transition effect to use
	btns = d.forms[0].transition_type;
	for(i=0;i<btns.length;i++) {
		if(btns[i].checked)return i;
	}
}

function setCaptionText(imgIndex) {					// sets the caption elements text as the image elements alt text
	captionObj = d.getElementById("caption");
	removeTextNodes(captionObj);
	captionObj.appendChild(d.createTextNode(mImages[imgIndex].getAttribute("alt")));
}

function initSlideMask(nxtImage) {
	// initialize the transition
	// already transitioning, bail out.
	if(zInterval != null) return;
	
	//check if we're not just clciking on the same link...
	if(currentContent==nxtImage)
	{
		return;
	}
	
	// set the mask elements starting left
	startPoint = new Array(-500,-500,250,250);
	

	
	d.getElementById("mask").style.left = startPoint[getTransitionType()]+"px";
	
	
	
	// define the interval function
	fn = function() { slideMask(nxtImage); }			
	// start the interval
	zInterval = setInterval(fn,10);
}

function slideMask(nxtImage) {					// the function we iterate over to do the transitions
	x = parseInt(d.getElementById("mask").style.left);			// get the current left of the mask DIV
	if(!activeTransition) {
		// prevents the user from changing the transition type mid-transition
		tType = getTransitionType();		
		activeTransition = true;
	}
	if(tType == 0) {
		x+=MASK_SPEED;
		if(x>=-150 && !swapped) swapImage(nxtImage);
	} else if (tType == 1) {
		if(x<-150 && !swapped) {
			x+=MASK_SPEED;
		} else {
			x-=MASK_SPEED;
			if(!swapped) swapImage(nxtImage);
		}
	} else if (tType == 2) {
		if(x>=-150 && !swapped) {
			x-=MASK_SPEED;
		} else {
			x+=MASK_SPEED;
			if(!swapped) swapImage(nxtImage);
		}
	} else if (tType == 3) {
		x-=MASK_SPEED;;
		if(x<=-150 && !swapped) swapImage(nxtImage);
	}

	// put the mask div where it needs to be
	d.getElementById("mask").style.left = x+"px";

	clr = false;
	// different transitions have different stopping points. check for them here.
	if(tType == 0 || tType == 2) {
		if(x>=588) clr = true;
	} else if (tType == 1 || tType == 3) {
		if(x<=-438) clr = true;
	}
	
	if(clr) { 
		// transition has completed. clear out the interval and reset "swapped" to false
		clearInterval(zInterval);
		zInterval = null;
		swapped = false;
		activeTransition = false;
	}
}

function swapImage(nxtImage) {
	swapped=true;
//	src = mImages[nxtImage].getAttribute("src");
//	setCaptionText(nxtImage);
//	d.getElementById("mContainer").style.backgroundImage = "url(" + src + ")";
	d.getElementById(currentContent).style.display="none";
	d.getElementById(nxtImage).style.display="block";
	currentContent=nxtImage;

}

// utitlity function to clear the text of an object
function removeTextNodes(parentObj) {
	_i=0;
	while(parentObj.childNodes[_i]) {
		if(parentObj.childNodes[_i].nodeType == 3) parentObj.removeChild(parentObj.childNodes[_i]);
		_i++;
	}
}
