// The variable "imageArr" will be initialized in prior to calling the Javascript below (see slideshow.js.php)

//Specify the slider's width (in pixels)
var sliderwidth="374px"
//Specify the slider's height
var sliderheight="65px"
//Specify the slider's slide speed (larger is faster 1-10)
var slidespeed=5
//configure background color:
var slidebgcolor="#EAEAEA"

var currImg = 0; //  sets the starting image (hfref#)

//Specify the slider's images
var leftrightslide=new Array();

var bottomSlideOnMouseOver = "this.style.border=\'3px solid #8FB1B2\'; this.style.margin=\'3px 0px -3px 0px\'";
var bottomSlideOnMouseOut  = "this.style.border=\'0px solid #ffffff\'; this.style.borderLeft=\'3px solid #ffffff\'; this.style.borderRight=\'3px solid #ffffff\'; this.style.margin=\'6px 0px 0px 0px\'";

for (var i = 0; i < imageArr.length; i++){
	// preload the images
	if (document.images) {
		imgMain = new Image();
		imgThumb = new Image();
		imgMain.src = imageArr[i]['image'];
		imgThumb.src = imageArr[i]['thumb'];
	}
	
	leftrightslide[i] ='<a id="href'+i+'"  href="javascript:;" onClick="changeMainImg(this.id); return false"><img src="'+imageArr[i]['thumb']+'"  id="thumb'+i+'"  class="bottomSlideCells" onMouseOver="'+bottomSlideOnMouseOver+';" onmouseout="'+bottomSlideOnMouseOut+';"></a>';
}

//Specify gap between each image (use HTML):
var imagegap='';

//Specify pixels gap between each slideshow rotation (use integer):
var slideshowgap=0;

var copyspeed=slidespeed;

leftrightslide='<nobr>'+leftrightslide.join(imagegap)+'</nobr>';

var iedom=document.all||document.getElementById;

if (iedom) document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+leftrightslide+'</span>');
var actualwidth='';
var cross_slide, ns_slide;


function changeMainImg(direction){
	var mainCell = document.getElementById('slideMain');
	if(mainCell.style.opacity != 1) return false;
	
	if(direction == 'up'){
		currImg++;
		if(currImg >= imageArr.length) currImg = 0;
	} else if(direction == 'down') {
		currImg--;
		if(currImg < 0) currImg = imageArr.length - 1;
	} else {
		currImg = parseInt(direction.replace('href', ''));
	}
	
	var hrefObj = document.getElementById('mainImgHref');
	var imgObj = document.getElementById('mainImg');
	
	imgObj.style.visibility = 'hidden';
	imgObj.src = imageArr[currImg]['image']; 
	hrefObj.href = (imageArr[currImg]['url']) ? imageArr[currImg]['url'] : 'javascript:;'; //DWS 20100625; added javascript to prevent empty link

	var textObj = document.getElementById('div_text');
	textObj.innerHTML = imageArr[currImg]['text'];
	
	fade('slideMain');      
}


function fillup(){
	if (iedom){
		cross_slide=document.getElementById? document.getElementById("test2") : document.all.test2
		cross_slide2=document.getElementById? document.getElementById("test3") : document.all.test3
		
		cross_slide.innerHTML=leftrightslide
		cross_slide2.innerHTML=leftrightslide

		actualwidth=document.all? cross_slide.offsetWidth : document.getElementById("temp").offsetWidth
		
		if(!cross_slide.style.left){cross_slide.style.left = -6+"px";}
		if(!cross_slide2.style.left){cross_slide2.style.left = (actualwidth+slideshowgap)+"px";}
	} else if (document.layers){
		ns_slide=document.ns_slidemenu.document.ns_slidemenu2
		ns_slide2=document.ns_slidemenu.document.ns_slidemenu3
		ns_slide.document.write(leftrightslide)
		ns_slide.document.close()
		actualwidth=ns_slide.document.width
		ns_slide2.left=actualwidth+slideshowgap
		ns_slide2.document.write(leftrightslide)
		ns_slide2.document.close()
	}
	
	var lefttime=setInterval("chooseScrollDirection(direction)",30);
}

var direction = '';

var alreadyrunflag=0; //flag to indicate whether target function has already been run

if (document.addEventListener)
	document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; fillup(); setSlideInterval(); fade('slideMain');}, false)
else if (document.all && !window.opera){
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	var contentloadtag=document.getElementById("contentloadtag")
	contentloadtag.onreadystatechange=function(){
		if (this.readyState=="complete"){
			alreadyrunflag=1;
			fillup(); 
			var intVal = setSlideInterval(); 
			fade('slideMain');
		}
	}
}

window.onload=function(){
	setTimeout("if (!alreadyrunflag){fillup(); setSlideInterval(); fade('slideMain');}", 0);
}

function setSlideInterval(){
	intVal = setInterval("changeMainImg('up')",8000);
}

function chooseScrollDirection(direction){
	if(direction == 'left'){
		slideright();
	} 
	else if(direction == 'right'){
		slideleft();
	}
}

function slideleft(){
	if (iedom){
		if (parseInt(cross_slide.style.left)>(actualwidth*(-1)))
			cross_slide.style.left=parseInt(cross_slide.style.left)-copyspeed+"px"
		else
			cross_slide.style.left=parseInt(cross_slide2.style.left)+(actualwidth+slideshowgap-5)+"px"  // the number 5 = the image border width...      
		
		if (parseInt(cross_slide2.style.left)>(actualwidth*(-1)))
			cross_slide2.style.left=parseInt(cross_slide2.style.left)-copyspeed+"px"
		else
			cross_slide2.style.left=parseInt(cross_slide.style.left)+actualwidth+slideshowgap+"px"
	} else if (document.layers){
		if (ns_slide.left>(actualwidth*(-1)))
			ns_slide.left-=copyspeed;
		else
			ns_slide.left=ns_slide2.left+actualwidth+slideshowgap;
	
		if (ns_slide2.left>(actualwidth*(-1)))
			ns_slide2.left-=copyspeed;
		else
			ns_slide2.left=ns_slide.left+actualwidth+slideshowgap;
	}
}

function slideright(){
if (iedom){
	if (parseInt(cross_slide.style.left)<(actualwidth*(1))){
		cross_slide.style.left=parseInt(cross_slide.style.left)+copyspeed+"px"
	}else{
		cross_slide.style.left=parseInt(cross_slide2.style.left)-(actualwidth-5)+"px" // the number 5 = the image border width...
	}
	
	if (parseInt(cross_slide2.style.left)<(actualwidth*(1))){
		cross_slide2.style.left=parseInt(cross_slide2.style.left)+copyspeed+"px"
	}else{
		cross_slide2.style.left=parseInt(cross_slide.style.left)-(actualwidth+slideshowgap+0)+"px"
	}
}
else if (document.layers){
	if (ns_slide.left>(actualwidth*(-1)+8))
		ns_slide.left-=copyspeed
	else
		ns_slide.left=ns_slide2.left+actualwidth+slideshowgap

	if (ns_slide2.left>(actualwidth*(-1)+8))
		ns_slide2.left-=copyspeed
	else
		ns_slide2.left=ns_slide.left+actualwidth+slideshowgap
	}
}

var TimeToFade = 1000.0;


function fade(eid){
	var element = document.getElementById(eid);
	if(element == null) return;
	element.style.opacity = 0;
	element.FadeState = 1;
	element.FadeTimeLeft = TimeToFade;
	setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
}


function animateFade(lastTick, eid){
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	var element = document.getElementById(eid);

	if(element.FadeTimeLeft <= elapsedTicks){
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = '
			+ (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		return;
	}

// this is a hack so that in IE it doesn't start at full opacity for a split sec before the fade kicks in...
	document.getElementById('mainImg').style.visibility = 'visible';

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	if(element.FadeState == 1) newOpVal = 1 - newOpVal;
	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}
