var hTimer     = false;
var hSpeed     = 10;

var hCurrItem  = 0;
var hNextItem  = 1;
var imgArray   = false;
var btnArray   = false;

var zIndex1    = 900;
var zIndex2    = 800;
var zIndex3    = 700;
var btn1       = "<img src='/_assets/images/ui/buttons/button_photo_dot_on.png' alt='@' />";
var btn2       = "<img src='/_assets/images/ui/buttons/button_photo_dot_off.png' alt='O' />";


function startSlideShow()
{
	imgArray = document.getElementById('photos').getElementsByTagName('img');
	btnArray = document.getElementById('photo_buttons').getElementsByTagName('span');

	if(imgArray.length > 1 && !hTimer)
	{
		hTimer = setInterval("nextSlide()", hSpeed * 500);
	}
}

function nextSlide()
{
	for(var i=0; i<imgArray.length; ++i)
	{
		if(i == hCurrItem)      { imgArray[i].style.zIndex = zIndex1; }
		else if(i == hNextItem) { imgArray[i].style.zIndex = zIndex2; }
		else                    { imgArray[i].style.zIndex = zIndex3; }
	}

	fadeIt(imgArray[hCurrItem], 100, 0, 3);

	hCurrItem = hNextItem;
	if(hNextItem < imgArray.length-1) { ++hNextItem } else { hNextItem = 0; }

	for(var i=0; i<btnArray.length; ++i)
	{
		if(i == hCurrItem)      { btnArray[i].innerHTML = btn1; }
		else                    { btnArray[i].innerHTML = btn2; }
	}

}

function setNext(num)
{
	hNextItem = num-1;
	clearInterval(hTimer);
	hTimer = false;
	nextSlide();
	hTimer = setInterval("nextSlide()", hSpeed * 500);

}


/**********************************************************
 **                                                      **
 **     Element Fader Script                             **
 **                                                      **
 **     Created by: Roger Soucy                          **
 **                 rsoucy@crystalforge.com              **
 **                                                      **
 **                 Copyright 2007                       **
 **                                                      **
 **  --------------------------------------------------  **
 **                                                      **
 **  Usage: fadeIt(elem, start, stop, speed)             **
 **  --------------------------------------------------  **
 **  elem:  Document element to be faded, can be called  **
 **         using the "this" keyword.                    **
 **                                                      **
 **  start: 0-100 integer value of the starting opacity  **
 **         of the element.                              **
 **                                                      **
 **  stop:  0-100 integer value of the ending opacity    **
 **         of the element.                              **
 **                                                      **
 **  speed: 1-25+ integer value of the rate of fade for  **
 **         the element. 1 is a slow change and 25 is    **
 **         almost instantaneous, higher values are not  **
 **         significant. Floating point numbers may be   **
 **         used for smaller degrees of change.          **
 **                                                      **
 **  example:                                            **
 **         <div id="el_1"                               **
 **              onmouseover="fadeIt(this,5,100,4)"      **
 **              onmouseout="fadeIt(this,100,5,2)">      **
 **         </div>                                       **
 **                                                      **
 **  Note:  You must specify the opacity of an element   **
 **         within the css for the page. The necessary   **
 **         css code is:                                 **
 **         filter: alpha(opacity: 5); opacity: 0.05;    **
 **         where alpha(opacity: #) is for MSIE and      **
 **         opacity: #/100 is for Firefox/Opera.         **
 **                                                      **
 **********************************************************/


/**********************************************************
 *** GLOBAL VARIABLES:                                  ***
 **********************************************************/
var faders = new Array();			// element fader stack
var interval = false;					// variable to hold interval


/**********************************************************
 *** PRIMARY FUNCTION: fadeIt()                         ***
 *** -------------------------------------------------- ***
 *** Adds opacity properties to the element and starts  ***
 *** the fade() function on a 1 millisecond timer.      ***
 **********************************************************/
function fadeIt(el, start_opacity, stop_opacity, speed) {

	// add the start opacity to the element as an object property
	el.start_op = start_opacity;

	// add the stop opacity to the element as an object property
	el.stop_op = stop_opacity;

	// create a current opacity property unless it already exists
	el.curr_op = start_opacity;

	// set the speed of the fade with a default of 4
	el.speed = (speed) ? speed : 4;

	// add this element to the fader stack
	faders.push(el);

	// if no interval exists, start one to execute fade()
	if(!interval) {
		interval = setInterval("fade()", 1);
	}
}


/**********************************************************
 *** SECONDARY FUNCTION: fade()                         ***
 *** -------------------------------------------------- ***
 *** Cycles through the faders stack and adjusts each   ***
 *** element's curr_op property, then calls the         ***
 *** setOpacity() function to display the change. This  ***
 *** function also removes elements from the faders     ***
 *** stack when complete and cancels the timer when the ***
 *** faders stack is empty.                             ***
 **********************************************************/
function fade() {

	// Check to see if faders holds any elements
	if(faders.length > 0) {

		// iterate through each of the elements in faders
		for(i = 0; i < faders.length; i++) {

			// if the current opacity is lower than the stop opacity...
			if(faders[i].curr_op < faders[i].stop_op) {

				// add the speed to the current opacity
				faders[i].curr_op += faders[i].speed;

				// if the current opacity exceeds the stop opacity...
				if(faders[i].curr_op > faders[i].stop_op) {
					// set the current opacity to the stop opacity
					faders[i].curr_op = faders[i].stop_op;
				}

				// call the setOpacity function to display the change
				setOpacity(faders[i]);

			// if the current opacity is higher than the stop opacity...
			} else if(faders[i].curr_op > faders[i].stop_op) {

				// subtract the speed from the current opacity
				faders[i].curr_op -= faders[i].speed;

				// if the current opacity exceeds the stop opacity...
				if(faders[i].curr_op < faders[i].stop_op) {
					// set the current opacity to the stop opacity
					faders[i].curr_op = faders[i].stop_op;
				}

				// call the setOpacity function to display the change
				setOpacity(faders[i]);

			// in any other case...
			} else {
				// remove the element from the faders stack
				faders[i].style.zIndex = zIndex3;
				faders[i].curr_op = faders[i].start_op;
				setOpacity(faders[i]);
				faders.splice(i,1);
			}

		}

	// if faders is empty
	} else {
		clearInterval(interval);	// clear the interval and
		interval = false;					// then set interval to false
	}
}

/**********************************************************
 *** TERTIARY FUNCTION: setOpacity()                    ***
 *** -------------------------------------------------- ***
 *** Sets the opacity CSS for the element for various   ***
 *** browsers.
 **********************************************************/
function setOpacity(el) {

	// if the current opacity is 100, set to 99.999, otherwise
	// leave it alone.  This is a browser bug fix.
  opacity = (el.curr_op == 100)?99.999:el.curr_op;

  // Internet Explorer on Windows (proprietary style: filter)
  el.style.filter = "alpha(opacity:"+opacity+")";

  // Safari < 1.2 & Konqueror (proprietary style: KHTMLOpacity)
  el.style.KHTMLOpacity = opacity/100;

  // Older Mozilla & Firefox (proprietary style: MozOpacity)
  el.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  el.style.opacity = opacity/100;
}

