var curImg = 0;
var timerId = -1;
var secTimerId = -1;
var interval = 5000;
var secInterval = interval / 1000;
var currSec = secInterval;
var copyType = "";
var isPlaying = 1;
var next_slide_text = "Next slide in <<number>> sec";
var loading_text = "Loading image...";
var paused_text = "Slideshow Paused";

function replaceNum() {
  var input = SHOWINGSTRING;
  var output = input;
  var idx = output.indexOf("%slideNum");
  if (idx > -1) {
    output = input.substring(0, idx);
    output += eval(curImg+1);
    output += input.substr(idx+9);
  }
  return output;
}

function changeSpeed() {
  var sidx = document.forms.pg.speedMenu.selectedIndex;
  var speed = document.forms.pg.speedMenu.options[sidx].value;

  interval = speed;
  secInterval = interval / 1000;

  if (timerId != -1) {
    clearTimers();
  }
  begin();
}

function imgLoadNotify() {
  if (isPlaying == 1)
    setTimers();
}

function changeSlide() {
  var width = a_width[curImg];
  var height = a_height[curImg];
  if (timerId != -1) {
    clearTimers();
    dispLoading();
  }

  var html = "<img src=" + a_src[curImg] + " width=" + width +
      " height=" + height + " border=1 onload=\"imgLoadNotify();\" name=\"theImg\" id=\"theImg\" alt=\"" + (a_cap[curImg] != "" || a_cap[curImg] != null ? a_cap[curImg].replace(/<br>/gi,'\n') : "Photo") + "\" title=\"" + (a_cap[curImg] != "" || a_cap[curImg] != null ? a_cap[curImg].replace(/<br>/gi,'\n') : "Photo") + "\">";

  var caption = a_cap[curImg];
  
//  var pnumLine = "<font face=arial size=-1>";
  var pnumLine = "";
  pnumLine += replaceNum();
// pnumLine += "</font>";

  switch(navigator.family) {
    case 'gecko':
	case 'nn5':
      document.getElementById("imgDiv").innerHTML = html;
      document.getElementById("pnumDiv").innerHTML = pnumLine;
      document.getElementById("capDiv").innerHTML = caption;
      break;
    case 'nn4':
      document.layers[1].document.pnumDiv.document.open();
      document.layers[1].document.pnumDiv.document.write(pnumLine);
      document.layers[1].document.pnumDiv.document.close();

      document.layers[0].document.imgDiv.document.open();
      document.layers[0].document.imgDiv.document.write(html);
      document.layers[0].document.imgDiv.document.close();

      document.layers[4].document.capDiv.document.open();
      document.layers[4].document.capDiv.document.write(caption);
      document.layers[4].document.capDiv.document.close();
      break;
    case 'ie4':
	  document.all.pem.innerHTML = pnumLine;
	  document.all.imgp.innerHTML = html;
	  document.all.imgc.innerHTML = caption;
      break;
  }
  
  setIcons();
}

function setIcons() {
/*
  var n = 3;
  for (var i = 1; i <= 5; i++) {
  	n = new Number(curImg-3) + new Number(i);
	if (n >= numImgs) {
		n-=numImgs;
	}
	if (n < 0) {
		n+=numImgs;
	}
	document.getElementById('link_' + i).href = 'javascript:void(moveToImg(\'' + n + '\'))'
//    document.getElementById('icon_' + i).innerHTML = '<a href="javascript:void(moveToImg(\'' + n + '\'))"><img src="/images/templates/photogallery/filmstrip.gif" width="132" height="106" border="0"></a>';
	document.getElementById('icon_' + i).background = a_icon_src[n];
  }
*/
}

function forward() {
  curImg++;
  if (curImg == numImgs) 
    curImg = 0;
  changeSlide();
}

function rewind() {
  curImg--;
  if (curImg < 0) 
      curImg = numImgs - 1;
  changeSlide();
}

function dispSec() {
  var dispSec = ( currSec < 0 ) ? 0 : currSec;
  var secText = "<font face=arial color=gray size=-2>"+(next_slide_text.replace("<<number>>", dispSec))+"</font>";

  switch(navigator.family) {
    case 'gecko':
	case 'nn5':
      document.getElementById("timerDiv").innerHTML = secText;
      break;
    case 'nn4':
      document.layers[2].document.timerDiv.document.open();
      document.layers[2].document.timerDiv.document.write(secText);
      document.layers[2].document.timerDiv.document.close();
      break;
    case 'ie4':
      document.all.timer.innerHTML = secText;
      break;
  }

  currSec--;
}


function dispLoading() {
  var text = "<font face=arial color=gray size=-2>"+loading_text+"</font>";

  switch(navigator.family) {
    case 'gecko':
	case 'nn5':
      document.getElementById("timerDiv").innerHTML = text;
      break;
    case 'nn4':
      document.layers[2].document.timerDiv.document.open();
      document.layers[2].document.timerDiv.document.write(text);
      document.layers[2].document.timerDiv.document.close();
      break;
    case 'ie4':
      document.all.timer.innerHTML = text;
      break;
  }
}

function stop() {
  clearTimers();

  isPlaying = 0;

  var secText = "<font face=arial color=Blue size=-2><B>"+paused_text+"</B></font>";

  switch(navigator.family) {
    case 'gecko':
	case 'nn5':
      document.getElementById("timerDiv").innerHTML = secText;
      break;
    case 'nn4':
      document.layers[2].document.timerDiv.document.open();
      document.layers[2].document.timerDiv.document.write(secText);
      document.layers[2].document.timerDiv.document.close();
      break;
    case 'ie4':
      document.all.timer.innerHTML = secText;
      break;
  }

  document.playbtn.src = buttonImgPfx + buttonOffArray[0];
  document.stopbtn.src = buttonImgPfx + buttonOnArray[1];
}

function begin() {
  setTimers();
  startShow();
}

function startShow() {
  isPlaying = 1;
  document.playbtn.src = buttonImgPfx + buttonOnArray[0];
  document.stopbtn.src = buttonImgPfx + buttonOffArray[1];
}

function setTimers() {
  currSec = secInterval;
  dispSec();
  var regInterval = 1000
  if (navigator.family == 'ie4'){
  	regInterval = 999;
	interval = secInterval * 990;
  }
  if (secTimerId == -1)
    secTimerId = window.setInterval('dispSec();', regInterval);
  if (timerId == -1)
    timerId = window.setInterval('forward();', interval);
}

function clearTimers() {
  window.clearInterval(timerId);
  timerId = -1;
  window.clearInterval(secTimerId);
  secTimerId = -1;
}

function setButton(direction) {
  if (direction == 0) {
    document.prevbtn.src = buttonImgPfx + buttonOnArray[2];
    window.setTimeout("document.prevbtn.src = buttonImgPfx + buttonOffArray[2];", 300);
  } else {
    document.fowdbtn.src = buttonImgPfx + buttonOnArray[3];
    window.setTimeout("document.fowdbtn.src = buttonImgPfx + buttonOffArray[3];", 300);
  }
}

function moveToImg(imgNum) {
/*
	curImg = imgNum;
	changeSlide();
*/
}

