// TODO:
// Switch for fullsize
// JS file.
var n = 0;
var width = 1024;
var height = 768;
var iwidth = 800;
var iheight = 600;
var iaspect = 1.33;
var timeout = null;
var docpannable = null;
var docpicture = null;
var doctitle = null;
var docftitle = null;
var docfenabled = null;
function setsize() {
  if (document.all != null) {
    width = document.body.clientWidth - 25;
    height = document.body.clientHeight - 100;
    // document.write('This is IE!<br>');
  } else if (navigator.appName == 'Netscape') {
    width = window.innerWidth - 50;
    height = window.innerHeight - 250;
    // document.write('This is NS!<br>');
  } else {
    // document.write('This is neither!<br>');
  }
  if (width > 1200) {
    iwidth = 1200;
  } else if (width > 1000) {
    iwidth = 1000;
  } else if (width > 800) {
    iwidth = 800;
  } else if (width > 600) {
    iwidth = 600;
  } else if (width > 400) {
    iwidth = 400;
  } else if (width > 200) {
    iwidth = 200;
  }
  if (height > 1000) {
    iheight = 1000;
  } else if (height > 800) {
    iheight = 800;
  } else if (height > 600) {
    iheight = 600;
  } else if (height > 400) {
    iheight = 400;
  } else if (height > 200) {
    iheight = 200;
  } else if (height > 100) {
    iheight = 100;
  }
  iaspect = iwidth / iheight;
  //window.status = 'Window: ' + width + 'x' + height + ' --> Size: ' + iwidth + 'x' + iheight;
  docpannable = document.all? document.all.pannable  : document.getElementById? document.getElementById('pannable')  : null;
  docpicture  = document.picture;
  doctitle    = document.all? document.all.titletext : document.getElementById? document.getElementById('titletext') : null;
  docftitle   = document.imgform? document.imgform.imgtitle : null;
  docfenabled = document.imgform? document.imgform.enabled : null;
  //if (window.location.search) {
  //  var run = window.location.search.indexOf("run=");
  //  if (run) {
  //    window.alert('Found run= at ' + run);
  //    delay = parseInt(window.location.search.substring(run+4,100));
  //  }
  //}
  setimage();
}
var img;
var imgname;
var imgtitle;
var imgpan = 0;
function loaded() {
  //window.alert('Loaded...' + imgname);
  //docpicture.src = imgname;
  //return;
  if (docpicture) {
    if (resizemode) {
      var origwidth = img.width;
      var origheight = img.height;
      var origaspect = origwidth / origheight;
      if (imgpan) {
        if (img.height > iheight) {
          docpicture.src = 'Icons/1px.gif';
          docpicture.width = iheight*origaspect;
          docpicture.height = iheight;
          //window.status = 'Window: ' + width + 'x' + height + ', Img: ' + img.width + 'x' + img.height + ' --> Size: ' + iwidth + 'x' + iheight + ' --> Pan ' + (iheight*origaspect) + 'x' + iheight;
        } else {
          docpicture.src = 'Icons/1px.gif';
          docpicture.width = img.width;
          docpicture.height = img.height;
          //window.status = 'Window: ' + width + 'x' + height + ', Img: ' + img.width + 'x' + img.height + ' --> Size: ' + iwidth + 'x' + iheight + ' --> Pan ' + (iheight*origaspect) + 'x' + iheight;
        }
      } else if (origaspect < iaspect && img.height > iheight) {
          // Portrait.
        docpicture.src = 'Icons/1px.gif';
        docpicture.width = iheight*origaspect;
        docpicture.height = iheight;
        //window.status = 'Window: ' + width + 'x' + height + ', Img: ' + img.width + 'x' + img.height + ' --> Size: ' + iwidth + 'x' + iheight + ' --> Portrait ' + (iheight*origaspect) + 'x' + iheight;
      } else if (origaspect > iaspect && img.width > iwidth) {
        // Landscape.
        docpicture.src = 'Icons/1px.gif';
        docpicture.width = iwidth;
        docpicture.height = iwidth/origaspect;
        //window.status = 'Window: ' + width + 'x' + height + ', Img: ' + img.width + 'x' + img.height + ' --> Size: ' + iwidth + 'x' + iheight + ' --> Landscape ' + iwidth + 'x' + (iwidth/origaspect);
      } else {
        docpicture.width = img.width;
        docpicture.height = img.height;
        //window.status = 'Window: ' + width + 'x' + height + ', Img: ' + img.width + 'x' + img.height + ' --> Size: ' + iwidth + 'x' + iheight + ' --> Natural ' + img.width + 'x' + img.height;
      }
    }
    if (docpannable) { docpannable.style.left = 0; }
    docpicture.src = imgname;
    if (doctitle) {
      doctitle.innerHTML = '<font color=white face="arial,helvetica">' + imgtitle + '</font>';
    }
    if (docftitle) {
      docftitle.value = list[n].t? list[n].t : '';
    }
    if (docfenabled) {
      docfenabled.checked = list[n].e;
    }
  }
  if (imgpan) {
    pan_image();
  } else if (delay) {
    if (timeout) { clearTimeout(timeout) };
    timeout = setTimeout(nextimage, delay);
  }
}
var clientwidth = 0;
var scrollwidth = 0;
var scrolllimit = 0;
var scrollpos = 0;
function pan_image() {
  scrollpos = 0;
  if (docpannable) {
    if (timeout) { clearTimeout(timeout) };
    timeout = setTimeout("pan()", 500);
  }
}
function pan() {
  clientwidth = document.body.clientWidth;
  scrollwidth = document.body.scrollWidth;
  scrolllimit = scrollwidth - clientwidth;
  scrollpos += imgpan;
  //window.status = 'Scrollpos = ' + Math.round((scrollpos * 100) / scrolllimit) + '%';
  if (scrollpos < (scrollwidth - clientwidth)) {
     docpannable.style.left = -scrollpos;
     //window.scroll(scrollpos, 0);
     if (timeout) { clearTimeout(timeout) };
     timeout = setTimeout("pan()", 5);
  } else if (delay) {
    if (timeout) { clearTimeout(timeout) };
    timeout = setTimeout(nextimage, 500);
  }
}
function notloaded() {
  window.alert('NOT Loaded: ' + imgname);
  //return;
  nextimage();
}
function setimage() {
  // window.alert('setimage');
  if (list.length == 0) { return; }
  img = null;
  img = new Image();
  img.onload = loaded;
  img.onerror = notloaded;
  imgname = basedir + '/' + subdir + '/' + list[n].i;
  imgtitle = list[n].t?list[n].t:list[n].i;
  imgpan = list[n].p? list[n].p : 0;
  img.src = imgname;
}
function setimageno(i) {
  n = i;
  setimage();
  window.focus();
}
function start() {
  stop();
  if (n >= list.length-1) { n = 0; }
  delay = 2000;
  setimage();
}
function startfast() {
  stop();
  if (n >= list.length-1) { n = 0; }
  delay = 100;
  setimage();
}
function stop() {
  delay = 0;
  clearTimeout(timeout);
  timeout = null;
}
function firstimage() {
  n = 0;
  setimage();
}
function previmage() {
  if (n <= 0) { return; }
  n = n-1;
  setimage();
}
function nextimage() {
  if (n >= list.length-1) { stop(); return; }
  n = n+1;
  setimage();
}
function lastimage() {
  n = list.length-1;
  setimage();
}
var fullwindow = null;
function showfull() {
  if (fullwindow == null || fullwindow.closed) {
    fullwindow = window.open('', 'FullImage', 'scrollbars=yes,resizable=yes');
  }
  fullwindow.location = list[n].i;
  fullwindow.focus();
}
var px = new Image();
px.src = 'Icons/1px.gif';
var nav = null;
function createnavwindow() {
  if (nav == null || nav.closed) {
    var s = '<html><head>\n' +
    '<title>Slideshow Navigation</title>\n' +
    '<' + 'script' + '>' + '\nfunction setimagen(i) { window.opener.setimageno(i); }\n' + '<' + '/script' + '>' +
    '</head><body bgcolor=black style="margin: 2px 2px 2px 2px;">\n' +
    '<table width=400 cellpadding=1 cellspacing=0>' +
    '<tr><td width=400 colspan=2><font size=2 color=white><b>Slideshow Navigation</b></font></td></tr>\n';
    for (i = 0; i < (list.length); i++) {
      var li = list[i];
      var ttl = li.t?li.t:li.i;
      s += '<tr><td width=20><font size=-2 color=white>' + (i+1) + '</font></td>' +
           '<td width=380><font size=-2 color=white><a href="javascript:setimagen('+i+');">' + (ttl?ttl:fnm) + '</a></font></td></tr>\n';
    }
    s += '</table>' +
         '</body></html>';
    var ht = 762;
    if (screen) { ht = screen.height-30; }
    nav = window.open('', 'SlideshowNavigation', 'position=yes,scrollbars=yes,resizable=yes,width=200,height='+ht+',left=0,top=0');
    nav.document.write(s);
    nav.document.close();
  }
  nav.focus();
}
function closenavwindow() {
  if (nav != null && nav.open) { nav.close(); }
}
//setsize();
document.writeln('<title>' + title + '</title>');

