ImageList = new Array (); 
TitleList = new Array ();
TextList = new Array ();
URLList = new Array ();

var ImgNum; 
var ImgTitleId;
var ImgTextId;
var delay = 3000;//Time delay between Slides in milliseconds
var lock = false;
var run;
var maxW = 600;
var ImgDivId;

function init_slides(divid, titleid,textid,maxwidth){
	ImgTitleId = titleid;
	ImgTextId = textid;
	ImgDivId = divid;
	//maxW = typeof(maxwidth) != 'undefined' ? maxwidth : 0;
	setmaxwidth(maxwidth);
	ImgNum = 0;
}

function setmaxwidth(maxwidth){
	maxW = typeof(maxwidth) != 'undefined' ? maxwidth : 0; 
}

function addimage(filename, ttl, txt, theurl)
{	if (filename == '') filename = "/apps/slideshow/blank.gif";
	if (filename.indexOf('.jpg') > 0 || filename.indexOf('.JPG') > 0 || filename.indexOf('.gif') > 0  || filename.indexOf('.GIF') > 0
			|| filename.indexOf('.png') > 0  || filename.indexOf('.PNG') > 0 )
	{
		ImageList.push(filename);
		var fileAndPath = filename;
		//find the index of the last "/" replace with \\ for windows
		var lastPathDelimiter = fileAndPath.lastIndexOf("/");
		//get everything after the last "/"
		var fileNameOnly = fileAndPath.substring(lastPathDelimiter+1);
		if (ttl == '') {TitleList.push(fileNameOnly);} else {TitleList.push(ttl);}
		if (txt == '') {TextList.push('*');} else {TextList.push(txt);}
		if (theurl == '') {URLList.push('*');} else {URLList.push(theurl);}
	}
}

function chgImg(direction) {
	if (document.images && ImageList.length > 0) {
		ImgLength = ImageList.length - 1;
		ImgNum = ImgNum + direction;
		if (ImgNum > ImgLength) {ImgNum = 0;}
		if (ImgNum < 0) { ImgNum = ImgLength;}
		var n = ImgNum + 1;
		var imgStyle;
		//imgStyle = document.images['slideshow'].style;
		
		if (document.getElementById(ImgTitleId)) {
			if (URLList[ImgNum] != '*') { 
				document.getElementById(ImgTitleId).innerHTML ='<a href="' + URLList[ImgNum] + '" target="_blank">' + TitleList[ImgNum] +'</a> [' +n +']';
			}
			else { document.getElementById(ImgTitleId).innerHTML = TitleList[ImgNum] +' [' + n + ']';}
		}
		if (document.getElementById(ImgTextId) ) {
			if ( TextList[ImgNum] != '*' ) 
				{ document.getElementById(ImgTextId).innerHTML = TextList[ImgNum];}
				else {document.getElementById(ImgTextId).innerHTML = '';}
		}
		
		imgStyle = document.images[ImgDivId].style;
		//imgStyle.visibility = 'hidden';		
		var newImg = new Image();
		newImg.src = ImageList[ImgNum];
		imgWidth = newImg.width;
		document.images[ImgDivId].src = ImageList[ImgNum]; 
		if (maxW > 0) 
			document.images[ImgDivId].width = (imgWidth > maxW) ? maxW : imgWidth;
		else
			document.images[ImgDivId].width =  imgWidth;
		//if (document.images[ImgDivId].complete) {imgStyle.visibility = 'visible';}
		imgStyle.visibility = 'visible';
	}
}

function firstImg(){
	showImg(0);
}

function lastImg(){
	showImg(ImageList.length - 1);
}

function showImg(num){
	if (ImageList.length > 0) {	ImgNum = num; chgImg(0);}
}

function auto() {
	if (ImageList.length > 0){
		if (lock == true) {
		lock = false;
		window.clearInterval(run);
		}
		else if (lock == false) {
		lock = true;
		run = setInterval("chgImg(1)", delay);
	   }
	}
}

function slidehandleOver(img,maxw){ 
	var magSelect = document.getElementById('mag');
	var x = magSelect.options[magSelect.selectedIndex].value; 
	var newImg = new Image(); 
	newImg.src = img.src;
	if (x != 1) {
		if (newImg.width >= maxw) {img.width = maxw * x;} else {img.width = newImg.width * x;}
	}
}

function slidehandleOnLoad(img,maxw){ 
	var newImg = new Image(); 
	newImg.src = img.src;
	if (newImg.width < maxw) {img.width = newImg.width;} else {img.width = maxw;}
}

function slidehandleOut(img, maxw){ slidehandleOnLoad(img, maxw);}

function handleselectChange(slct){ 
	var Select = slct; document.getElementById('showlist');
	var url = Select.options[Select.selectedIndex].value; 
	if (url != '') window.location.href = url;
}

