// slideshow.js Beta Version 0.2
// Copyright (c) Gary Swift, Design Matrix, 2006-2008. All rights reserved.
// http://www.designmatrix.com

var loading = new Image(60,60);
loading.src = '../graphics/loading.gif';
var preload = new Array();
function preloadImgArr () {
  for (var idx = 0; idx < imgarr.length; idx++) {
    preload[idx] = new Image();
  }
  preload[0].src = imgarr[0]['imgpath'];
  preload[1].src = imgarr[1]['imgpath'];
}
preloadImgArr();
var nextimg = 0;
function preLoadImg() {
  if (preload[nextimg].src == '') {
    window.status='Preloading next: ' +imgarr[nextimg]['imgpath'];
    preload[nextimg].src = imgarr[nextimg]['imgpath'];
  }
  if (preload[nextimg].complete) {
    if (thumbs == true && nextimg < maxthumbs) {
      thumbidx = 'thumb' +nextimg;
      thisthumb = document.images[thumbidx];
      thisthumb.src = imgarr[nextimg]['imgpath'];
      thisthumb.imgarridx = nextimg;
      imgarr[nextimg]['thumb'] = nextimg;
      if (nextimg == 0 && !coverimage) {
	thisthumb.style.border = "2px solid #000000";
      } else {
	thisthumb.style.border = "1px solid #000000";
      }
    }
    if (nextimg == imgarr.length -1 ) { 
      clearInterval(interval_id);
      window.status="Slide show loaded.";
    } else {
      nextimg = ++nextimg;
      preLoadImg();
    }
  }
}
  var interval_id = setInterval ("preLoadImg()", 500);
var maxthumbs;
var thumbs;
var thumbx;
var thumby;
function writethumbs(thumbcnt) {
  if (thumbcnt == 0) return;
  if (thumbcnt == undefined || thumbcnt > imgarr.length) thumbcnt = imgarr.length;
  maxthumbs = thumbcnt;
  thumbs = true;
  var thumbwidth = "";
  var thumbheight = "";
  if (thumby == undefined && thumbx == undefined) thumbx = 60;
  if (thumbx != undefined) thumbwidth = 'WIDTH="' +thumbx +'"';
  if (thumby != undefined) thumbheight = 'HEIGHT="' +thumby +'"';
  var count = 0;
  while (count < thumbcnt) {
    document.write('\
    <SPAN STYLE="width: 66px; height: 66px;"> \
    <IMG SRC = ' +loading.src +'\
    STYLE="border: 1px solid #000000; background: #FFFFFF; cursor: pointer; margin: 1px;" \
    ID="thumb' +count +'"' +thumbwidth +thumbheight
    +'ALT="view slide image" \
    ALIGN="center" \
    TITLE="view slide image" \
    onclick="thumbidx = this.imgarridx; btnaction(\'thumbclick\');" \
    onmouseover="window.status=\'view slide image\';" \
    onmouseout="window.status=\'\';"> \
    </SPAN>');
    count++;
  }
}
var coverimage;
var slideidx;
if (coverimage) {
  slideidx = -1;
} else {
  slideidx = 0;
}
var slideidxlast;
var slideborder;
if (slideborder == undefined) slideborder = 3;
var maxslidewidth;
if (maxslidewidth == undefined) maxslidewidth = 580 - (2 * slideborder);
var maxslideheight;
if (maxslideheight == undefined) maxslideheight = 260 - (2 * slideborder);
function chgslide() {
  if (preload[slideidx].src == '') {
    return;
  }
  if (preload[slideidx].complete) {
    newwidth = preload[slideidx].width;
    newheight = preload[slideidx].height;
    newaspect = newwidth / newheight;
    if (newwidth > maxslidewidth && (maxslidewidth / newaspect) <= maxslideheight) {
      newwidth = maxslidewidth;
      newheight = (maxslidewidth) / newaspect;
    }
    if (newheight > maxslideheight && (maxslideheight * newaspect) <= maxslidewidth) {
      newwidth = maxslideheight * newaspect;
      newheight = maxslideheight;
    }
    document.images['slideimg'].src = imgarr[slideidx]['imgpath'];
    document.images['slideimg'].width = newwidth;
    document.images['slideimg'].height = newheight;
    document.images['slideimg'].style.border = slideborder +"px solid #000000";
    newurl = imgarr[slideidx]['url'];
    if (newurl !== undefined) {
    document.getElementById('URL').href = imgarr[slideidx]['url'];
    document.getElementById('URL').firstChild.data = imgarr[slideidx]['urlname'];
    }
    var newcaption = imgarr[slideidx]['caption'];
    if (newcaption !== undefined) {
      document.getElementById('slidecaption').firstChild.data = newcaption;
    }
    if (thumbs == true) {
      imgthumb = imgarr[slideidx]['thumb'];
      if (imgthumb != undefined || imgthumb != null) {
	thumbidx = 'thumb' +imgthumb;
	document.images[thumbidx].style.border = "2px solid #000000";
      }
      if (slideidxlast >= 0 && (imgarr[slideidxlast]['thumb'] != null)) {
	lastthumb = 'thumb' +imgarr[slideidxlast]['thumb'];
	document.images[lastthumb].style.border = "1px solid #000000";
      }
    }  
  }
}
function slideshow() {
    slideidxlast = slideidx;
    slideidx = (slideidx == imgarr.length -1) ? 0 : ++slideidx;
    chgslide();
}
var slideshow_id;
function playstop(text) {
    document.buttonform.playbutton.value = (text == "Stop") ? "Play" : "Stop";
    if (document.buttonform.playbutton.value == "Play") {
      clearInterval(slideshow_id);
    } else {
      slideidxlast = slideidx;
      switch (slideidx) {
	  case -1: 
	      slideidx = 0;
	      break;
	  case imgarr.length -1:
	      slideidx = 0;
	      break;
	  default:
	      slideidx = ++slideidx;
	      break;
      }
      chgslide();
      slideshow_id = setInterval ("slideshow()", 5000);
    }
}
function btnaction(action) {
    clearInterval(slideshow_id);
    document.buttonform.playbutton.value = "Play";
    slideidxcurrent = slideidx;
    switch(action) {
	case 'first':
	  slideidx = 0;
	  break;
	case 'previous':
	  if (slideidx <= 0) {
	    slideidx = imgarr.length -1;
	  } else --slideidx;
	  break;
	case 'next':
	  if (slideidx == imgarr.length -1) {
	    slideidx = 0;
	  } else slideidx = ++slideidx;
	  break;
	case 'last':
	  slideidx = imgarr.length -1;
	  break;
	case 'thumbclick':
	  slideidx = thumbidx;
	  break;
	default:
	  alert(name +'Unknown action: ' +action);
	  break;
    }
    if (slideidx != slideidxcurrent) {
      slideidxlast = slideidxcurrent;
      chgslide();
    }
}
var imgarridx;
function chgthumbs(action) {
  var thisthumb = 0;
  var thumbidx;
  while (thisthumb < maxthumbs) {
    thumbidx = 'thumb' +thisthumb;
    imgarridx = document.images[thumbidx].imgarridx;
    switch(action) {
    case 'first':
      if (thisthumb == imgarridx) {
	return;
      }
      if (imgarridx > thisthumb) {
	imgarr[imgarridx]['thumb'] = null;
      }
      imgarridx = document.images[thumbidx].imgarridx = thisthumb;
      break;
    case 'previous':
      if (thisthumb == maxthumbs -1) {
	imgarr[imgarridx]['thumb'] = null;
      }
      if (imgarridx <= 0) {
	imgarridx = document.images[thumbidx].imgarridx = imgarr.length -1;
      } else {
	imgarridx = --document.images[thumbidx].imgarridx;
      }
      break;
    case 'next':
      if (thisthumb == 0) {
	imgarr[imgarridx]['thumb'] = null;
      }
      if (imgarridx == (imgarr.length -1)) {
	imgarridx = document.images[thumbidx].imgarridx = 0;
      } else {
	imgarridx = ++document.images[thumbidx].imgarridx;
      }
      break;
    case 'last':
      lastthumb = 'thumb' +(maxthumbs -1);
      if (document.images[lastthumb].imgarridx == imgarr.length -1) {
	return;
      }
      if (imgarridx < imgarr.length - maxthumbs) {
	imgarr[imgarridx]['thumb'] = null;
      }
      newimg = imgarr.length - maxthumbs + thisthumb;
      imgarridx = document.images[thumbidx].imgarridx = newimg;
      break;
    default:
      alert(name +'Unknown action: ' +action);
      return;
      break;
    }
    document.images[thumbidx].src = imgarr[imgarridx]['imgpath'];
    if (imgarridx == slideidx) {
      document.images[thumbidx].style.border = "2px solid #000000";
    } else {
      document.images[thumbidx].style.border = "1px solid #000000";
    }
    imgarr[imgarridx]['thumb'] = thisthumb;
    ++thisthumb;
  }
}
function gotopage(target) {
  if (target == undefined) { 
    window.location = document.getElementById('URL').href;
  } else  window.open(document.getElementById('URL').href, target);
}
function imgover(title) {
  if (slideidx != -1) {
    if (title == "clear") {
      document.body.style.cursor = 'default';
      window.status = '';
      document.images['slideimg'].title = "";
    } else {
      document.body.style.cursor = 'pointer';
      document.images['slideimg'].alt = 'Go to ' +imgarr[slideidx]['urlname'];
      document.images['slideimg'].title = 'Go to ' +imgarr[slideidx]['urlname'];
      window.status = document.getElementById('URL').href;
    }
  }
}
function newshow(whichshow) {
  clearInterval(slideshow_id);
  document.buttonform.playbutton.value = "Play";
  imgarr = whichshow;
  nextimg = 0;
  preloadImgArr();
  slideidx = 0;
  chgslide();
  interval_id = setInterval ("preLoadImg()", 500);
}
function printslide (target){
  var target;
  if (target == '') { 
    win = this.window;
  } else win =  window.open('', target);
  win.document.write (
  '<HTML>\n<HEAD>\n</HEAD>\n<BODY onLoad="self.focus()">\n<CENTER>\n<H2>\n'
  +imgarr[slideidx]['urlname']
  +'\n</H2>\n<H4>\n'
  +imgarr[slideidx]['caption']
  +'\n</H4>\n'
  +'<IMG SRC="' +imgarr[slideidx]['imgpath'] +'" BORDER="4">'
  +'\n</CENTER>\n</BODY>\n</HTML>');
  win.focus();
  win.document.close ();
}
