/**
 * @author Sheldon
 */
/* Create array for user variables */
var slideParams = new Array();

/* ENTER USER SLIDE PARAMETERS HERE!!!!........ */

/* Copy the line below (do not include comment markers /* * /) for each slide,
 * for example:
 * slideParams.push(['images/picture.jpg','Some Picture','Picture of something','Tooltip info about something']);

 */
/* slideParams.push(['src','alt','caption','info']); */

slideParams.push(['apples/P1050987-2010.gif', 'Silkey Gardens Apples', 'Silkey Garden Apples']);
slideParams.push(['apples/P1070581crop2010.gif', 'Apple Blossom', 'Silkey Garden Apple Blossoms']);
slideParams.push(['apples/P1050990-2010.gif', 'Apples', 'Silkey Garden Apples']);
slideParams.push(['apples/P1070664applesblossom2010.gif', 'Muliple Apple Blossoms', 'Muliple Apple Blossoms']);
slideParams.push(['apples/P1080410apple-fruit.gif', 'Apple', 'Apple']);
slideParams.push(['apples/P1080441apples-green.gif', 'Apples', 'Silkey Gardens Apples']);
slideParams.push(['apples/P1080446apples-green.gif', 'Silkey Garden Apple', 'Silkey Garden Apple']);
slideParams.push(['apples/P1070653crop2010keeper.gif', 'Multiple Blossoms ', 'Silkey Gardens Apple Blossoms']);
slideParams.push(['apples/P1070651appleblossom2010.gif', '', 'Full bloom Apples']);
slideParams.push(['apples/P1080428apples-green.gif', '', 'Silkey Garden Green Apple']);

/* slideDelay sets the delay between image rotations (in milliseconds) including the transition time */
var slideDelay=4000;

/* transitionspeed is the length of time from beginning to end of the fade-in/fade-out effect in milliseconds */
var transitionTime = 550;

/* transitionSteps is the number of individual 'frames' used for the transition effect. It affects the 
smoothness of the effect. (Movies are typically 30 frames/sec); transitionTime/33 should work well in most cases */
var transitionSteps = 20;

/* END OF USER PARAMETERS ..*/

/* for readability, define some constants for indexing */
var SRC = 0;
var ALT = 1;
var CAPTION = 2;
var INFO = 3;

/* calculate timeout for transition function */
var transitionTimeout = Math.floor(transitionTime/transitionSteps) ;

/* Create array for storing image objects */
var slideImages = new Array();

/* Create globals for container and slide-width increment */
var capSpot;
var slideIncrement;

/* Initialize counter for slideshow */
var slideIndex = 1;

/* Set img_1 to first img in sequence to display while other images are loading */

var dImg;

var altImg;

/* function to be called when body loaded */
function initShow(){
    capSpot = document.getElementById('caption');
    dImg = document.getElementById('img_1');
    dImg.src = slideParams[0][SRC];
    dImg.width = 260;
    dImg.style.left = 0;
    altImg = document.getElementById('img_2');
    altImg.width = 0;    
    loadSlides();
    /* lock the height of the first image */
    dImg.height = dImg.height;
    capSpot.firstChild.nodeValue = slideParams[0][CAPTION];
    showSlides(dImg, altImg);
}

/* The slideshow main loop */
function showSlides(oldImg, newImg){
  if(slideImages[slideIndex].complete)
  {
    newImg.src = slideImages[slideIndex].src;
    slideIncrement = Math.ceil(slideImages[slideIndex].width/transitionSteps);
//    alert(slideIncrement);
    newImg.style.left = 0;
    newImg.width = 0;
    newImg.height = slideImages[slideIndex].height;
    capSpot.style.display = 'none';
    transitionSlides(oldImg, newImg);
  }
  else
  /* if the picture hasn't loaded yet, try again in 1/5 seconds */
  {
    setTimeout(function(){showSlides(oldImg,newImg)},200);
  }
}

/* transition slides inner loop */
function transitionSlides(oldImg, newImg){
    oldImg.width = oldImg.width - slideIncrement;
    var left = oldImg.style.left.replace("px","");
    oldImg.style.left = parseInt(left) + slideIncrement + "px";
    newImg.width += slideIncrement;
    if (newImg.width >= slideImages[slideIndex].width) {
    /* this is the last frame of the transition; complete the process */
        newImg.width = slideImages[slideIndex].width;
        oldImg.width = 0;
        capSpot.firstChild.nodeValue = slideParams[slideIndex][CAPTION];
        capSpot.style.top = newImg.height + "px";
        capSpot.style.display = 'block';
        slideIndex ++;
        if (slideIndex == slideParams.length)
        {
          slideIndex = 0;
        }
        /* set timer for next slide */       
        setTimeout(function(){showSlides(newImg, oldImg)}, slideDelay);
    }
    else {
    /* the transition is not finished; set time for next step */
        setTimeout(function(){transitionSlides(oldImg, newImg)}, transitionTimeout);
    }
}

/* Function for preloading images */
function loadSlides(){
    for (var i = 0; i < slideParams.length; i++) {
        slideImages[i] = new Image();
        slideImages[i].src = slideParams[i][SRC];
    }
}



