/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 Sunday, February 25, 2007 8:58:04 PM
 HAPedit 3.1.11.111
 Author: Jesus J. Ramirez
 Website URL: http://www.memories2treasure.net
 program name: Slideshow
 Script name: slide1-5.js
 Script version: 1.5;
 Script Description: This script will take a list of images and display them in
       a slideshow. The images do not have to be preloaded because it loads the
       images using the "onload()" method which allows java to wait for the image
       to finish loading before applying the delay and loading the next image.
       Pre-loading images for this purpose is not a practical use as it takes a
       very long time to load many images to an array. This script can be incorporated
       with other scripting languages to produce a dynamic slideshow.
 Script Licence: This script is freeware but you must leave intact this comment.
       DO NOT DELETE OR ERASE ANYTHING IN THIS COMMENT BLOCK COMMENT or you will
       be breaking the licensing agreement. You may use this script in your website
       but you must have a link to this site to give credit to the programmer. If you
       use this script you will not hold the programmer, or anyone else associated
       or not associated to the organization which programmed the script, liable for
       damages directly or indirectly caused by the use of this software.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */






//--------------SlideShow Template Object---------------------

function SlideShowTemplate(){

  var files = new Array();               // Image names fed from external source via SetFilenames() method
  var imageList = new Array();           // Holds indexes of images viewed randomly
  var currentImage = new Image();        // Holds a copy of the current image
  var timeCounter;                       // Used for setTimedelay
  var curImageIndex = 0;                 // Holds current array index used with previous and next functions

  // Boolean flags initialization

  var finished = false;                  // Used to stop show
  var skipTimeDelay = false;             // Used to skip time delay when using "previous" or "next"
  var firstTime = true;                  // Used to allow execution of code only once.
  var running = false;                   // true: running   false: idle or stopped
  var randomShow = true;                 // Set slideshow to random
  var zoomed = false;                    // Determines if a photo has been zoomed in or out
  var manualControl = false;            // Used to control previous and next photo navigation
  var adjustIndex = false;
  var useImageScaling = true;            // Use automaic image scaling. Set to "true" to enable or "false" to disable
  var initFade = false;
  
  // Initial settings

  var imageWidth=250;                    // Initial image width. also used to reset
  var imageHeight=250;                   // Initial image height. also used to reset 
  var curImgPer = 0.30;                  // Image reduction ratio if image less than "imageHeight"
  var zoomFactor = 0.10;                 // Zoom ration for images
  var RANDOM = 0;                        // Constant used in switch for random numbers
  var SEQUENTIAL = 1;                    // Constant used in switch for sequential numbers.
  var timeDelay = 5;                     // keeps track of slideshow delay
  var totalImages                        // Holds total image count
  var sourcePath = "slideshow";          // Default photo path for slideshow. Change according to your path of slideshow.
  var fadeMode = 1;                      // Fade modes: 0=fade out 1=fade in
  var imgOpacity = 100;
  var imgStatus = "visible";

  // Form names for form objects or elements

  var frmName = 'picture';               // Form name
  var frmList = 'list';                  // Form file list element name
  var frmCat = 'category';               // Form picture category element name
  var frmDelay = 'delay';                // Form delay element name
  var showType = SEQUENTIAL;             // Form show type element name
  var showStatus = 'status';              // Form status text display box
  var frmPhoto = 'photo';                // Form photo name
  var frmScaled = 'scaled';              // Form text box for scaled resolution
  var frmOriginal = 'original';          // Form text box for original resolution


  // Methods declarations

  this.Stop = Stop;                             // Method used to stop slideshow with externally call
  this.Play = Play;                             // Method used to start show with external call
  this.Run = Run;                               // Method invoked by image.onload method
  this.ZoomIn = ZoomIn;                         // Method used to zoom in on a picture
  this.ZoomOut = ZoomOut;                       // Method used to zoom out on a picture
  this.SetFileNames = SetFileNames;             // Method used to hold image file names and image text data
  this.GetPicture = GetPicture;                 //
  this.GetSelectedPicture = GetSelectedPicture; // 
  this.Start = Start;                           // Method used to call the Run method 
  this.SetPath = SetPath;                       // Method used to set slideshow path
  this.SetMode = SetMode;                       // Method used to set mode: random or sequential
  this.SetDelay = SetDelay;                     // Method used to set slideshow time delay
  this.SetImageScaling = SetImageScaling;       // Method used to enable or disable image scaling
  this.SetImageSize = SetImageSize;             // Method used to set image height and width
  this.FadeIn = FadeIn;
  this.FadeOut = FadeOut;

  //----------------Method Defintions------------------

  function Run(){
    if(firstTime){
      firstTime = false;
      running = true;
      manualControl = false;
      skipTimeDelay = false;
      initialTimeSkip = true;      
      totalImages = files.length;
      document[showStatus].src = sourcePath + "/icons/status-on.jpg"; 
    }  
    if(!finished){  
      if(initialTimeSkip){
        initialTimeSkip = false;
        Play();
      }else{
        (manualControl) ? skipTimeDelay = true : skipTimeDelay = false; 
        if(!skipTimeDelay){ timeCounter = setTimeout("slideShow.Play()", timeDelay*1000);}
      } 
    }else{
      alert("An error might have occured");
    }
  }
  

  function SetPath(path){  
    sourcePath = path;
  }


  function SetMode(mode){
   
    switch(mode) {
      case 0 : showType = RANDOM;
               break;
      case 1 : showType = SEQUENTIAL;
               break;
      default: showType = "SEQUENTIAL";
               alert("Invalid mode supplied. Show type set to SEQUENTIAL");
    }
  }


  function SetDelay(delay){
    if(delay>0 && delay<16){   
      timeDelay = delay;
    }else{
      timeDelay = 5;
      alert("Time delay too large or invalid. Time delay set to 5 seconds");
   }
  }


  function SetImageScaling(choice){  
    switch(choice) {
      case 0 : useImageScaling = false;
               break;
      case 1 : useImageScaling = true;      
               break;
      default: useImageScaling = false;
               alert("Invalid mode supplied. Show type set to SEQUENTIAL");
    }    
  }

  function SetImageSize(imgX,imgY){
    imageWidth = imgX; 
    imageHeight = imgY;
  }

 


  function Start(){  
     finished = false;
     skipTimeDelay = false;
     manualControl = false;
     running = true;
     document[showStatus].src = sourcePath + "/icons/status-on.jpg";
     Run();
  }


  function SetFileNames(fileList){
     files =  fileList;
  }


  function Play(){

     switch(showType){
        case     RANDOM : alert("RANDOM feature not supported at this time!");
                         //GenerateRandom();
                          break;
        case SEQUENTIAL : ProcessSequentialPhoto('next');
                          break;
        default         : alert("no show type obtained");
                          finished = true;
     }
     
     if(!finished){
       ProcessPhoto();
     }else{           
       Stop();
       alert("Unable to Get Show type: sequential or random.\n" +"Unable to Start!");
     }
  }


  function Stop(){
    document[showStatus].src = sourcePath + "/icons/status-off.jpg"; 
    if(running){
      clearTimeout(timeCounter);
      running = false;
    }
  }


  function GenerateRandom(){
   var foundIndex = false;
   var totalImages;
   var num;

   totalImages = document.forms[frmName].elements[frmList].options.length;
   num = Math.floor(Math.random()*totalImages);
   indexFound = CheckIndex(num,totalImages);
   if(!foundIndex){
      imageList.push(num);
      curImageIndex = imageList.length-1;
   }
   return(num);
  }


  function CheckIndex(number,totalIndexes){
    var indexNumber;
    var indexFound = false;
    var index;

    for(index=0; index<totalIndexes; index++){
       if(imageList[index] == number){
         indexNumber = index;
         indexFound = true;
         break;
       }
    }
    if(indexFound){
       totalIndexes = imageList.length;
       for(i=indexNumber; i<totalIndexes; i++){
          imageList[i] = imageList[i+1];
       }
       imageList.length = imageList.length-1;
    }
    return(indexFound);
  }



  function GetPicture(navChoice){
    if(running) Stop();
    manualControl = true;
    switch(showType){
       case     RANDOM : alert("RANDOM feature not supported at this time!");
                         //    ProcessRandomPhoto(navChoice);
                         break;
       case SEQUENTIAL : ProcessSequentialPhoto(navChoice);
                         break;
       default         : alert("no show type obtained");
                         number = 0;
    }
    ProcessPhoto();
  }


  function ProcessPhoto(){
     currentImage.onload = ProcessPhotoIndex;
     currentImage.src = files[curImageIndex].imgFilename;
  }


  function ProcessError(){
   Stop();
   alert("An error occured. Show stopped!" + "\n" +
          "total images: " + totalImages + "\n" +
          "current index: " + curImageIndex + "\n" +
          "current record: " + files[curImageIndex]['imgFilename'] + "\n"
          
         );
  }

  function ProcessRandomPhoto(choice){
    var number;

    switch(choice){
       case     'next': number = ProcessRandomNext();
                        break;
       case 'previous': number = ProcessRandomPrevious();
                        break;
       default        : alert("unable to find next or previous");
                        number = 0;
    }
    return(number);
  }


  function ProcessRandomNext(){
     var number;
     var tmpIndex;
     tmpIndex = imageList.length-1;
     if(curImageIndex < tmpIndex){
         curImageIndex++;
         number = imageList[curImageIndex];
     }else{
       number = GenerateRandom();
     }
     return(number);
  }


  function ProcessSequentialPhoto(choice){
    switch(choice){
       case     'next': curImageIndex++;
                        if(curImageIndex > totalImages-1) curImageIndex = 0;
                        break;
       case 'previous': curImageIndex--;
                        if(curImageIndex < 0) curImageIndex = totalImages-1;
                        break;
       default        : alert("unable to find next or previous");
                        curImageIndex = 0;
    }   
  }


  function ProcessRandomPrevious(){
     var number;
     var imageCount;

     imageCount = imageList.length;
     curImageIndex--;
     if(curImageIndex<0) curImageIndex = imageCount-1;
     number = imageList[curImageIndex];
     return(number);
  }



  function ProcessPhotoIndex(){
    
    if(zoomed) ResetSize();
    if(initFade){
        FadeOut(100);
    }else{
        initFade=true;
    }
    if(useImageScaling) {
        ScaleImage();
    }else {
      document[frmPhoto].src = currentImage.src; 
    }     
    SwitchText();
    FadeIn(0);
    Run();
  } 


 function SetOpacity(imgOpacity, status){
    document[frmPhoto].style.filter = "alpha(opacity:"+imgOpacity+")";
    document[frmPhoto].style.opacity = imgOpacity/100;
    document[frmPhoto].style.visibility = status;
 }


 function FadeIn(count){
    
    document[frmPhoto].style.filter = "alpha(opacity:"+count+")";
    document[frmPhoto].style.opacity = count/100;
    document[frmPhoto].style.visibility = 'visible';
    count += 20;
    if(count <= 100) setTimeout("slideShow.FadeIn(" + count + ")", 100);
 }


 function FadeOut(count){
    
    document[frmPhoto].style.filter = "alpha(opacity:"+count+")";
    document[frmPhoto].style.opacity = count/100;
    document[frmPhoto].style.visibility = 'visible';
    count -= 20;
    if(count >= 0) setTimeout("slideShow.FadeOut(" + count + ")", 100);
 }


  function ResetSize(){
    zoomed = false;
    document[frmPhoto].width = imageWidth;
    document[frmPhoto].height = imageHeight;
  }


  function ZoomIn(){
    if(running) Stop();
    zoomed = true;
    imageW = document[frmPhoto].width;
    imageH =  document[frmPhoto].height;
    imageH += Math.floor(zoomFactor*imageH);
    imageW += Math.floor(zoomFactor*imageW);
    document[frmPhoto].width = imageW;
    document[frmPhoto].height = imageH;
  }


  function ZoomOut(){
    if(running) Stop();
    zoomed = true;
    imageW = document[frmPhoto].width;
    imageH =  document[frmPhoto].height;
    imageH -= Math.floor(zoomFactor*imageH);
    imageW -= Math.floor(zoomFactor*imageW);
    document[frmPhoto].width = imageW;
    document[frmPhoto].height = imageH;
  }


  function ScaleImage(image){
    var imageW;    // imageH: Image height holder
    var imageH;    // imageW: Image width holder
    var rationH;   //Holds image percentage with respect to set image height "imageHight"
    var rationW;   //Holds image percentage with respect to set image width "imageWidth"
    var shrinkImage;
    
    imgH = currentImage.height;
    if(imgH >= imageHeight) {
       percentage = imageHeight/imgH;
       shrinkImage = true;
    }else {
       percentage = imgH/imageHeight;
       shrinkImage = false;
    }

    if(shrinkImage){
       imageH = Math.floor(percentage*currentImage.height);
       imageW = Math.floor(percentage*currentImage.width);
    }else{
       imageH = Math.floor((1+percentage)*currentImage.height);
       imageW = Math.floor((1+percentage)*currentImage.width);
    }
    document[frmPhoto].width = imageW;
    document[frmPhoto].height = imageH;
    document[frmPhoto].src = currentImage.src;  
  }



  function GetSelectedPicture(number){
    Stop();
    finished = false;
    skipTimeDelay = true;
    running = true;
    DisplayShowStatus("Select Mode");
    ProcessPhotoIndex(number);
  }



  function SwitchText(){
    var htmlTextObj;
    htmlTextObj = document.getElementById("message");
    if(document.all) {              // IE and Opera;    
      htmlTextObj.innerHTML = files[curImageIndex].imgTextData;
     // htmlTextObj.innerText = files[index].imgTextData;
     }else if(htmlTextObj.innerHTML){  // Firefox
    //}else if(htmlTextObj.textContent){  // Firefox
    //  htmlTextObj.textContent = files[index].imgTextData;    
      htmlTextObj.innerHTML = files[curImageIndex].imgTextData;
    } else { 
      alert("Your browser doesn't support text retrieval.");
    }
}




}


// Switches slideshow images on mouseover event 

  function changeHoverImage(name){

    if(document.images) { 
       switch(name){
         case 'previous' : document[name].src = slideshowPath + "/icons/previous-hover.jpg";
                           break;
         case 'stop'     : document[name].src = slideshowPath + "/icons/stop-hover.jpg";
                           break;
         case 'play'     : document[name].src = slideshowPath + "/icons/play-hover.jpg";
                           break;
         case 'next'     : document[name].src = slideshowPath + "/icons/next-hover.jpg";
                           break;
         case 'zoomin'   : document[name].src = slideshowPath + "/icons/zoomin-hover.jpg";
                           break;
         case 'zoomout'  : document[name].src = slideshowPath + "/icons/zoomout-hover.jpg";
                           break;
       }
    }
  }


// Restores slideshow control buttons

  function restoreImage(name) {
 
    if(document.images) { 
       switch(name){
         case 'previous' : document[name].src = slideshowPath + "/icons/previous.jpg";
                           break;
         case 'stop'     : document[name].src = slideshowPath + "/icons/stop.jpg";
                           break;
         case 'play'     : document[name].src = slideshowPath + "/icons/play.jpg";
                           break;
         case 'next'     : document[name].src = slideshowPath + "/icons/next.jpg";
                           break;
         case 'zoomin'   : document[name].src = slideshowPath + "/icons/zoomin.jpg";
                           break;
         case 'zoomout'  : document[name].src = slideshowPath + "/icons/zoomout.jpg";
                           break;
       }
    }

  }


// Sets image file names and image text data

function AddFileAndText(picName,picText){
  this.imgFilename = picName;
  this.imgTextData = picText;
}
