// Copyright Guenter Hildebrandt 2008, all rigths reservered

// ### make an java object
var pictures = new Array();
var icon = new Array();
var picTitle = new Array();
var picturePath =  new Array();
var picturLoaded = new Array();
// number of pictures
var numberPictures = 0;
// current pictur index
var currentPicture = 0;
// run dia show
var Run= 1;
// period for dia show
var TimePeriod = 5000;
// timer for dia show
var activeTimer = 0;
// picture to load
var LoadedPicture = 0;
// the timer for loading
var loadTimerHandle = 0;
// is one if the picture is not yet loaded
var PictureLoadDeferred = 0;
// the timer that checks if the picture is loaded
var checkTimer = 0;

// add a pictur to the show
function addPicture(picture,iconPicture,title) {
  // load the first two pictures, the first is shown immediately
  // the secons is used if the diashow starts (pre-loaded)
  // store the other parameters
  picturePath[numberPictures] = picture;
  picTitle[numberPictures] = title;
  picturLoaded[numberPictures] = 0;
  // load the icon
  icon[numberPictures] = new Image();
  icon[numberPictures].src = iconPicture;
  numberPictures += 1;
  if (numberPictures == 1) {
      LoadTimer(0);
  }
} // addPicture

// this timer runs to the pictures one after the other
// if all pictures are loaded the timer stops
function LoadTimer(number) {
  var i;
  number = ValidateNumber(number);
  if (loadTimerHandle != 0) {
    window.clearTimeout(loadTimerHandle);
    loadTimerHandle = 0;
  }
  for (i=0; i<numberPictures; i++) {
    if (IsPictureLoaded(number) == 0) {
      LoadedPicture = number;
      LoadPicture(LoadedPicture);
      loadTimerHandle = window.setTimeout("LoadTimer(LoadedPicture)", 200);
      break;
    }
    number++;
    number = ValidateNumber(number);
  }
} // LoadTimer

// returns a valid number
// wrap arround
function ValidateNumber(number) {
  // make sure the number is in a valid range
  if (number >= numberPictures) {
    number = 0;
  }
  if (number < 0) {
    number = numberPictures - 1;
  }
  return number;
}

// returns 1 if the picture with the index is loaded from
// server
function IsPictureLoaded(number) {
  number = ValidateNumber(number);
  if (picturLoaded[number] == 1 &&  pictures[number].complete == true) {
    return 1;
  } else {
    return 0;
  }
}

// load a picture from the server
// after the function returns the picture is created
// and the load process has been started
function LoadPicture(number) {
  number = ValidateNumber(number);
  // if it is not yet loaded, load it
  if (picturLoaded[number] == 0) {
    picturLoaded[number] = 1;
    pictures[number] = new Image();
    pictures[number].src = picturePath[number];
  }
}

// set a new picture and the related text
// call this function again, if the picture is not yet loaded
// return 1 if the picture s loaded
// if it returns 0 the
function switchPicture (increment) {
  if (checkTimer != 0) {
    window.clearTimeout(checkTimer);
    checkTimer = 0;
  }
  currentPicture +=increment;
  currentPicture = ValidateNumber(currentPicture);
  LoadTimer(currentPicture);
  if (pictures[currentPicture].complete == true) {
    document.images["mainPicture"].src = pictures[currentPicture].src;
    document.getElementById("pictureTitle").firstChild.nodeValue = picTitle[currentPicture];
    if (PictureLoadDeferred == 1) {
      PictureLoadDeferred = 0;
          newPictureTimer();
    }
    return 1;
  } else {
    // set picture titel to "Lade Bild" and call this function again
    document.getElementById("pictureTitle").firstChild.nodeValue = "Lade Bild ...";
    checkTimer = window.setTimeout("switchPicture(0)", 200);
    PictureLoadDeferred = 1;
    return 0;
  }
}

// is called from the icon view by user interaction
function ShowPicture(index) {
  Run = 0;
  currentPicture = index;
  switchPicture(0);
  // make the picture visable
  window.location.href = "#mainPictureAnchor";
}

// create HTML code for the icons
function PrintIcons(rows,IconX,IconY) {
        var i;
  document.writeln('<a name="IconAnchor">');
        document.writeln('<table border="3" cellpadding="0" cellspacing="5">');
        document.writeln("<tr> ");
        for (i = 0; i < numberPictures; i++) {
                document.write('<td align="center"> ');
                document.writeln('<a href="javascript:ShowPicture(' + i + ')" title="'+picTitle[i]+'"><img src="'+ icon[i].src + '" height="'+IconY+'" border="0"></a>');
                document.writeln("<\/td> ");
                if (i % rows == rows - 1) {
                  document.writeln("<\/tr><tr> ");
                }
  }
        document.writeln("<\/tr> ");
        document.writeln("<\/table>");
        document.writeln('</a>');
}

// create HTML code for the main pictur and the controls
function PrintPicture(width,heigth,withIcon) {
        document.writeln('<div align="center">');
        if (numberPictures >= 1) {
                document.writeln('<a name="mainPictureAnchor">');
                document.writeln('<table border="5" cellpadding="0" cellspacing="0"><tr><td>');
                document.writeln('<img src="' + pictures[0].src + '" name="mainPicture" border="0" vspace="0" height="'+heigth+'">');
                document.writeln('</td></tr></table>');
                document.writeln('</a>');
                document.writeln('<p><div id="pictureTitle">' + picTitle[0] + '&nbsp;</div></p>');
        } else {
                document.writeln('<p>Kein Bild geladen</p>');
                Run = 0;
        }
        document.writeln('<p><table border="0" cellpadding="2" cellspacing="0" ><tr>');
        if (withIcon != 0) {
          document.writeln('<td valign="top"><a href="javascript:ShowIcons()" title="Icons zeigen"><img src="http://home.arcor-online.de/guenter22/pic/icons.png" border="0"></a></td>');
        }
        document.writeln('<td valign="top"><a href="javascript:Last()" title="vorheriges Bild"><img src="http://home.arcor-online.de/guenter22/pic/last.png" border="0"></a></td>');
        document.writeln('<td valign="top"><a href="javascript:Play()" title="Bilder abspielen"><img src="http://home.arcor-online.de/guenter22/pic/play.png" border="0"></a></td>');
        document.writeln('<td valign="top"><a href="javascript:Stop()" title="Pause"><img src="http://home.arcor-online.de/guenter22/pic/pause.png" border="0"></a></td>');
        document.writeln('<td valign="top"><a href="javascript:Next()" title="nächstes Bild"><img src="http://home.arcor-online.de/guenter22/pic/next.png" border="0"></a></td>');
        document.writeln('<td valign="top"><form action="">');
        document.writeln('    <select name="TimeSelect" title="Zeitintervall" size="1" onchange="SetTimePeriod(this.form.TimeSelect.options[this.form.TimeSelect.selectedIndex].value)">');
        document.writeln('      <option value="1000">1 Sekunde</option>');
        document.writeln('      <option value="2000">2 Sekunden</option>');
        document.writeln('      <option value="3000">3 Sekunden</option>');
        document.writeln('      <option value="5000" selected>5 Sekunden</option>');
        document.writeln('      <option value="10000">10 Sekunden</option>');
        document.writeln('    </select>');
        document.writeln('</form></td>');
        document.writeln('</tr></table></p></div>');
}

function newPictureTimer() {
  if (activeTimer != 0) {
    window.clearTimeout(activeTimer);
    activeTimer = 0;
  }
  if (Run == 1) {
    activeTimer = window.setTimeout("PictureTimer()", TimePeriod);
  }
}

// the timer function for the dia show
function PictureTimer() {
  if (activeTimer != 0) {
    window.clearTimeout(activeTimer);
    activeTimer = 0;
  }
  if (Run == 1) {
    if (1==switchPicture(1)) {
      activeTimer = window.setTimeout("PictureTimer()", TimePeriod);
    }
  }
}

// a function to change the timer period
function SetTimePeriod(tp) {
        TimePeriod = tp;
        newPictureTimer();
}

// start the dia show
function Play() {
        Run = 1;
        PictureTimer();
}

// stop the dia show
function Stop() {
  Run = 0;
}

// manual switch to next picture
function Next() {
  Run = 0;
  switchPicture(1);
}

// manually switch to last picture
function Last() {
  Run = 0;
  switchPicture(-1);
}

function ShowIcons() {
  Run = 0;
  // make the icons visable
  window.location.href = "#IconAnchor";
}
