var resizeTO = false;
var scrollTO = false;
var slideshowTO = false;
var screenW = 1024;
var screenH = 768;

var botH = 100;
var topH = 100;
var fadeColor = '#eaeaea';
var fadeTo = 0.5;
var slideshowWait = 3000;

var curNum = -1;
var lastNum = -1;
var endX = 0;
var curX = 0;
var totW = 0;
var fadeW = 0;
var nDims = new Array();
var loadedImages = 0;
var isPreloading = false;

var oDims = new Array();
var oImageSrc = new Array();
var oImages = new Array();


function preloadImages() {
 for (iC=loadedImages;iC<loadedImages + 6 && iC<oDims.length;iC++) {
  oImages[iC] = new Image();
  oImages[iC].src = oImageSrc[iC];
  oImages[iC].nm = iC;
  if (oImages[iC].complete)
   loadedPic(oImages[iC].nm);
  else
   oImages[iC].onload = function() { loadedPic(this.nm); };
 }
 isPreloading = false;
}

var ranInit = false;

function loadedPic(nm) {
 loadedImages++;
 document.getElementById('galImg'+nm).src = oImages[nm].src;
 document.getElementById('galImg'+nm).style.background = '';

 if (loadedImages == 1)
  initImages();

 if (loadedImages % 6 == 0 && loadedImages < oDims.length)
  preloadImages();
}

function initImages() {
 if (window.innerWidth) {
  screenW = window.innerWidth;
  screenH = window.innerHeight;
 } else if (document.body.offsetWidth) {
  screenW = document.body.offsetWidth;
  screenH = document.body.offsetHeight;
 }

 tmpH = screenH - botH - topH;

 o = document.getElementById('lHover');
 o.width = Math.floor(screenW / 2);
 o.height = screenH - botH - topH;
 o.style.top = topH + 'px';

 o = document.getElementById('rHover');
 o.width = Math.floor(screenW / 2);
 o.height = screenH - botH - topH;
 tmpLeft = Math.round(screenW / 2 + 1);
 o.style.left = tmpLeft + 'px';
 o.style.top = topH + 'px';


 tmpX = 0;
 totW = 0;
 for (i=0;i<oDims.length;i++) {
  scale = (screenH - botH - topH) / oDims[i][1];

/*----- RETURN TO LATER
  if (screenW / oDims[i][0] < scale)
   scale = screenW / oDims[i][0];
-------------------*/

  nDims[i] = new Array(Math.round(scale * oDims[i][0]), Math.round(scale * oDims[i][1]));
  o = document.getElementById('galImg'+i);
  o.width = nDims[i][0];
  o.height = nDims[i][1];
  nDims[i][2] = tmpX;
  tmpX += nDims[i][0] + imgSpace;
  if (i > 3 && i <= oDims.length - 3)
   totW += nDims[i][0] + imgSpace;
 }

 o = document.getElementById('imgHolder');
 o.style.width = screenW + 'px';
 o.style.height = screenH + 'px';
 o.style.top = topH + 'px';
 o.style.left = '0px';
 document.getElementById('imgTable').style.visibility = 'visible';

 o = document.getElementById('footer');
 tmpTop = screenH - botH;
 o.style.top = tmpTop + 'px';
 o.width = screenW;
 o.style.visibility = 'visible';

 if (curNum < 0 || lastNum < 0) {
  curNum = 2;
  lastNum = 2;
  showImage(3);
 } else {
  showImage(curNum);
 }
}

function doResize() {
 clearTimeout(resizeTO);
 resizeTO = setTimeout('initImages()', 200);
}




function showImage(nm) {
 tmpNum = nm - 2;
 if (tmpNum < 1)
  tmpNum += totalImages;
 else if (tmpNum > totalImages)
  tmpNum -= totalImages;
 document.getElementById('imgCount').innerHTML = tmpNum;

 endX = nDims[nm][2] * -1 + (screenW - nDims[nm][0]) / 2;
 lastNum = curNum;
 curNum = nm;

 fadeW = Math.abs(endX - curX);

 clearTimeout(scrollTO);
 scrollTO = setTimeout('scrollImages()', 50);
}

function scrollImages() {
 o = document.getElementById('imgTable');
 curX = Math.round((curX * 4 + endX) / 5);

 if (curX < -1 * totW) {
  curX += totW;
  endX += totW;
  curNum -= totalImages;
  lastNum -= totalImages;
 } else if (curX > 0) {
  curX -= totW;
  endX -= totW;
  curNum += totalImages;
  lastNum += totalImages;
 }
 o.style.left = curX + 'px';

//----- OPACITY 
 if (curNum != lastNum) {
 tmpOpac = Math.abs(endX - curX) / fadeW * (1 - fadeTo);
 o = document.getElementById('alphaN');
 o.style.height = nDims[curNum][1];
 o.style.width = nDims[curNum][0];
 o.style.top = topH + 'px';
 o.style.opacity = tmpOpac;
 ieOpac = tmpOpac * 100;
 o.style.filter = 'alpha(opacity='+ieOpac+')';
 tmpL = curX + nDims[curNum][2];
 o.style.left = tmpL + 'px';


 tmpOpac = fadeTo - tmpOpac;
 o = document.getElementById('alphaO');
 o.style.height = nDims[lastNum][1];
 o.style.width = nDims[lastNum][0];
 o.style.top = topH + 'px';
 o.style.opacity = tmpOpac;
 ieOpac = tmpOpac * 100;
 o.style.filter = 'alpha(opacity='+ieOpac+')';
 tmpL = curX + nDims[lastNum][2];
 o.style.left = tmpL + 'px';
 } else {
  document.getElementById('alphaN').style.opacity = 0;
  document.getElementById('alphaN').style.filter = 'alpha(opacity=0)';
  document.getElementById('alphaO').style.opacity = 0;
  document.getElementById('alphaO').style.filter = 'alpha(opacity=0)';
 }


 o = document.getElementById('alphaL');
 o.height = screenH - topH - botH;
 if (curX < endX)
  tmpNum = curNum;
 else
  tmpNum = lastNum;
 tmpW = nDims[tmpNum][2];
 o.width = nDims[tmpNum][2];
 o.style.top = topH + 'px';
 o.style.left = curX + 'px';
 o.style.opacity = fadeTo;
 ieOpac = fadeTo * 100;
 o.style.filter = 'alpha(opacity='+ieOpac+')';

 o = document.getElementById('alphaR');
 o.height = screenH - topH - botH;
 if (curX > endX)
  tmpNum = curNum;
 else
  tmpNum = lastNum;
 tmpL = curX + nDims[tmpNum][2] + nDims[tmpNum][0];
 tmpW = nDims[nDims.length-1][2] - tmpL;
 o.width = tmpW;
 o.style.top = topH + 'px';
 o.style.left = tmpL + 'px';
 o.style.opacity = fadeTo;
 ieOpac = fadeTo * 100;
 o.style.filter = 'alpha(opacity='+ieOpac+')';

 


 if (Math.abs(endX - curX) > 2) {
  clearTimeout(scrollTO);
  scrollTO = setTimeout('scrollImages()', 40);
 } else {
  clearTimeout(scrollTO);
 }
}


function toggleSlideshow() {
 if (!slideshowTO) {
  document.getElementById('slideshowImg').src = 'images/pause.gif';
  doSlideshow();
 } else {
  clearTimeout(slideshowTO);
  slideshowTO = false;
  document.getElementById('slideshowImg').src = 'images/slideshow.gif';
 }
}

function doSlideshow() {
 showNext();
 clearTimeout(slideshowTO);
 slideshowTO = setTimeout('doSlideshow()', slideshowWait);
}




function activeArrExists(nm) {
 for (i=0;i<activeArr.length;i++) {
  if (activeArr[i][0] == nm)
   return true;
 }
 return false;
}

function showLast() {
 showImage(getLastNum(curNum));
}
function showNext() {
 showImage(getNextNum(curNum));
}

function getLastNum(nm) {
 nm--;
 if (nm < 0)
  nm = oDims.length - 1;

 return nm;
}

function getNextNum(nm) {
 nm++;
 if (nm >= oDims.length)
  nm = 0;

 return(nm);
}

function stopSelect(evt) {
 evt.preventDefault();
}
