// JavaScript Document


/************************************************************************************************************
Ajax dynamic content
Copyright (C) November, 2005  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com


************************************************************************************************************/	

/************************************************************************************************************
Script has been modifed to support captions for the images. Of course derivitive works are 

************************************************************************************************************/	

var ss_noFading = false;
var ss_timeBetweenSlides = 10000;	// Amount of time between each image(1000 = 1 second)
var ss_fadingSpeed = 1;	// Speed of fading	(Lower value = faster)


var ss_stats = new Array();

var ss_slideIndex = new Array();	// Index of current image shown
var ss_slideIndexNext = new Array();	// Index of next image shown
var ss_slideElementDivs = new Array();	// Array of image divs(Created dynamically)
var ss_currentOpacity = new Array();	// Initial opacity
var ss_sildesInShow = new Array();	// Number of images in gallery
var Opera = navigator.userAgent.indexOf('Opera')>=0?true:false;
function createParentDivs(imageIndex,divId)
{
	// if the index is the same as te one put in the array
	if(imageIndex==ss_sildesInShow[divId]){	
	
		// end Case of recursive function where all of them have been labeled.
		showGallery(divId);
	
	
	}else{
		// Get the image Object
		var imgObj = document.getElementById(divId + '_' + imageIndex);	
		
		// if opers set the image to static
		if(Opera)imgObj.style.position = 'static';
		
		// if 2d Array slideElementDivs are an array of images sotred for each DivID
		if(!ss_slideElementDivs[divId])ss_slideElementDivs[divId] = new Array();
		
		
		// This addes the object to the end of the array
		ss_slideElementDivs[divId][ss_slideElementDivs[divId].length] =  imgObj;

		// Hides the object but allows it to occupy the same space
		imgObj.style.visibility = 'hidden';	
		imageIndex++;
		
		// Recursive Function
		createParentDivs(imageIndex,divId);	
	}		
}

function showGallery(divId)
{
	if(ss_slideIndex[divId]==-1)ss_slideIndex[divId]=0; else ss_slideIndex[divId]++;	// Index of next image to show
	
	// After incrementing the current slide show index
	
	//If the new current is equal to the length of the Array you should rotate the index to 0
	if(ss_slideIndex[divId]==ss_slideElementDivs[divId].length)ss_slideIndex[divId]=0;
	
	// The next one is incremented
	ss_slideIndexNext[divId] = ss_slideIndex[divId]+1;	// Index of the next next image
	// If next is the lenght of the Array that should go to 1
	if(ss_slideIndexNext[divId]==ss_slideElementDivs[divId].length)ss_slideIndexNext[divId] = 0;


	// The initial paccity for that set of imaegs
	ss_currentOpacity[divId]=100;	// Reset current opacity

	// Set the new current Image to visible
	ss_slideElementDivs[divId][ss_slideIndex[divId]].style.visibility = 'visible';
	
	// If Opera make it inline and visibale
	if(Opera)ss_slideElementDivs[divId][ss_slideIndex[divId]].style.display = 'inline';
	if(navigator.userAgent.indexOf('Opera')<0){
		ss_slideElementDivs[divId][ss_slideIndexNext[divId]].style.visibility = 'visible';
	}
	
	// next become small  and current become full opacity. This is the inital setup required before the next one can be revield
	if(document.all){	// IE rules
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.filter = 'alpha(opacity=100)';
		ss_slideElementDivs[divId][ss_slideIndexNext[divId]].style.filter = 'alpha(opacity=1)';
	}else{
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.opacity = 0.99;	// Can't use 1 and 0 because of screen flickering in FF
		ss_slideElementDivs[divId][ss_slideIndexNext[divId]].style.opacity = 0.01;
	}		
	

	setTimeout('revealImage("' + divId + '")',ss_timeBetweenSlides);		
}

// This function is called based on the timeout.
function revealImage(divId)
{

	// no fading Make the current one hidden
	if(ss_noFading){
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.visibility = 'hidden';
		if(Opera)ss_slideElementDivs[divId][ss_slideIndex[divId]].style.display = 'none';
		
		showGallery(divId);
		return;
	}
	
	// Decrement current opacity
	ss_currentOpacity[divId]--;
	if(document.all){
		// IE Uses 100 based scale for opacity
		// Fade out
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.filter = 'alpha(opacity='+ss_currentOpacity[divId]+')';
		// Fade in
		ss_slideElementDivs[divId][ss_slideIndexNext[divId]].style.filter = 'alpha(opacity='+(100-ss_currentOpacity[divId])+')';
	}else{
		
		//All others
		
		// Fading out
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.opacity = Math.max(0.01,ss_currentOpacity[divId]/100);	
		// Can't use 1 and 0 because of screen flickering in FF
		// Fading as curr
		ss_slideElementDivs[divId][ss_slideIndexNext[divId]].style.opacity = Math.min(0.99,(1 - (ss_currentOpacity[divId]/100)));
	}
	
	
	// of Current opacity is not 0
	if(ss_currentOpacity[divId]>0){
		
		// call this function again to start fading
		setTimeout('revealImage("' + divId + '")',ss_fadingSpeed);
		
	}else{
		
		// When they are done the curent on is set to hidden because
		ss_slideElementDivs[divId][ss_slideIndex[divId]].style.visibility = 'hidden';	
		if(Opera)ss_slideElementDivs[divId][ss_slideIndex[divId]].style.display = 'none';	
		
		showGallery(divId);
	}
}

function initImageGallery(divId)
{
	var ss_galleryContainer = document.getElementById(divId);
	
	
	ss_slideIndex[divId] = -1;
	ss_slideIndexNext[divId] = false;
	
	var galleryImgArray = ss_galleryContainer.getElementsByTagName('div');
	for(var no=0;no<galleryImgArray.length;no++){
		galleryImgArray[no].id = divId + '_' + no;
	}
	
	// This defines how how many images there are to go through
	ss_sildesInShow[divId] = galleryImgArray.length;
	createParentDivs(0,divId);		
	
}

