// ---- START INIT --------
var currentImgsSet = '';
var crossFadeDuration = 3;
var Pic = new Array();
var PicLink = new Array();
var PicTitle = new Array()
var preLoad = new Array();
var i = 0;
var j = 0;
var p = 0;
var theTimer;
var started=0;
// ---- END INIT --------
function toggleStartStop() {
  if (started == 0){
    startAutoRun();
    document.startstop.src="/img/gallery/pausa.gif"
    started=1;
  }else{
    document.startstop.src="/img/gallery/autoplay.gif"
    stopAutoRun();
    started=0;
  }
}

function startAutoRun() {
  runSlideShow(currentImgsSet,'next');
  theTimer = setTimeout("startAutoRun()",2500);
}
function stopAutoRun() {
  clearTimeout(theTimer);
}
function runSlideShow(imgsSet,versus) {
  stopAutoRun();// non si sa mai;
  if (imgsSet != currentImgsSet) {
    loadImgsSet(imgsSet);
    currentImgsSet = imgsSet;
    imgsPreload(imgsSet,1); //all'inizio precarico solo la prima
  }
  if (versus != 'next' &&  versus != 'prev') {
    j = versus;
  }
  if (versus == 'next') {
    j = j + 1;
    if (j > (p - 1)) {
      j = 0;
    }
  }
  if (versus == 'prev') {
    j = j - 1;
    if (j < 0) {
      j = p - 1;
    }
  }
  var prev_idx = j;
  var next_idx = j;
  prev_idx = prev_idx - 1;
  if (prev_idx < 0) {
    prev_idx = p - 1;
  }
  next_idx = next_idx + 1;
  if (next_idx > (p - 1)) {
    next_idx = 0;
  }
  if (document.all) {
    //document.images['SlideShow_'+imgsSet].style.filter="blendTrans(duration=2)";
    //document.images['SlideShow_'+imgsSet].style.filter="blendTrans(duration=crossFadeDuration)";
    //document.images['SlideShow_'+imgsSet].filters.blendTrans.Apply();
  }
  if (preLoad[j] == null) {
    preLoad[j] = new Image();
    preLoad[j].src = Pic[j];
  }
  if (preLoad[prev_idx] == null) {
    preLoad[prev_idx] = new Image();
    preLoad[prev_idx].src = Pic[prev_idx];
  }
  if (preLoad[next_idx] == null) {
    preLoad[next_idx] = new Image();
    preLoad[next_idx].src = Pic[next_idx];
  }
  document.images['SlideShow_'+imgsSet].src = preLoad[j].src;
  if (document.getElementById('PicLink_'+imgsSet)) {
    document.getElementById('PicLink_'+imgsSet).href = PicLink[j];
    document.getElementById('PicLink_'+imgsSet).rel = 'lightbox';
  }
  if (document.getElementById('PicTitle_'+imgsSet)) {
    document.getElementById('PicTitle_'+imgsSet).innerHTML = PicTitle[j];
  }
  if (document.all) {
    //document.images['SlideShow_'+imgsSet].filters.blendTrans.Play();
  }
}
function imgsPreload(numImgsToPreload) {
  p = Pic.length;
  var limit = p;
  if (numImgsToPreload > 0 && numImgsToPreload < p) {
    limit = numImgsToPreload;
  }
  preLoad = new Array();
  for (i=0; i<limit; i++) {
    preLoad[i] = new Image();
    preLoad[i].src = Pic[i];
  }
}
function loadImgsSet(imgsSet) {
  eval( 'Pic = imgsSet_'+imgsSet );
  eval( 'PicLink = imgsLink_'+imgsSet );
  eval( 'PicTitle = imgsTitle_'+imgsSet );
}

