/**************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: scripts.js                                           *
 *        Copyright: (C) 2004 Daniel Janesch                              *
 *            Email: admin@the-deejay.com                                 *
 *              Web: http://www.the-deejay.com                            *
 *    Scriptversion: 1.2                                                  *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) f槀 weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/

function setCurrent (manualSlide) {
  if (len > 1) {
    curOrder = Number(document.forms["slideshow"].order.value);
    current += (manualSlide) ? manualSlide - curOrder : curOrder;

    if (manualSlide == 0 || manualSlide == len-1) {
      current = manualSlide;
    } // if
    else if (curOrder == 0) {
      current = Math.round(Math.random() * (len_random - 1));
    }
      
    // Check if we are below 0 or greater the maximum length of the array index:
    if (current < 0)
      current = len-1;
    else if (current >= len)
      current = 0;
  }
} // setCurrent

function startStopSlideshow (kind) {
  newround = kind;
  if (document.getElementById("startstop").firstChild.nodeValue == lang_pause_slide) {
    document.getElementById("startstop").firstChild.nodeValue = lang_play_slide;
    window.clearInterval(hndl);
  }
  else {
    document.getElementById("startstop").firstChild.nodeValue = lang_pause_slide;
    next();
  }
} // startStopSlideshow

function next(manualSlide) {
  // Check if we are below 0 or greater the maximum length of the array index:
  curOrder = Number(document.forms["slideshow"].order.value);

  // Order ascending and last picture reached:
  if (current == 0 && newround < 1 && curOrder == 1 && loopprompt == 1) {
    if (manualSlide != 1) {
      startStopSlideshow (2);
    }
    alert (lang_last_pic_reached);
  }
  // Order descending and first picture reached:
  else if (current == (len - 1) && newround < 1 && curOrder == -1 && loopprompt == 1) {
    if (manualSlide != 1) {
      startStopSlideshow (2);
    }
    alert (lang_first_pic_reached);
  }
  // Random and all pictures shown:
  else if (len_random == 0 && newround < 1 && curOrder == 0) {
    if (manualSlide != 1 && loopprompt == 1) {
      startStopSlideshow (2);
    }
    if (loopprompt == 1) {
      alert (lang_all_pics_viewed);
    }
    theimage_random = theimage;
    len_random = theimage_random.length;
  }

  if (newround != 2) {
    newround = 0;

    var siObj = document.images["slideimage"];
    var interval = document.forms["slideshow"].interval.value;
    
    // Transision script for make picture visible:
    if(document.all && dotrans == 1 && !window.opera)
      eval("siObj.filters." + transtype + ".apply()");
  
    var cur_img_nbr = current + 1;
    if (curOrder == 0) {
      siObj.src = theimage_random[current][1];
      cur_img_nbr = theimage_random[current][0];
      temp1 = theimage_random.slice(0,current);
      temp2 = theimage_random.slice(current+1, len_random);
      theimage_random = temp1.concat(temp2);
      len_random = theimage_random.length;
    } // if
    else {
      siObj.src = theimage[current][1];
    } // else
    document.forms["slideshow"].number.value = cur_img_nbr + "/" + len;
  
    // Transision script for make picture invisible:
    if(document.all && dotrans == 1 && !window.opera)
      eval("siObj.filters." + transtype + ".play()");

    setCurrent ();

    if (!window.opera) {
      nextpic.src = theimage[current][1];
      nextpic.onload=function() {
        if (document.getElementById("startstop").firstChild.nodeValue == lang_pause_slide) {
          hndl = clearTimeout(hndl);
          hndl = window.setTimeout("next()", document.forms["slideshow"].interval.value * 1000);
        } // if slide is playing
      } // onload
    } // if not opera
    else {
      if (document.getElementById("startstop").firstChild.nodeValue == lang_pause_slide) {
        hndl = clearTimeout(hndl);
        hndl = window.setTimeout("next()", document.forms["slideshow"].interval.value * 1000);
      } // if slide is playing
    } // else opera
  } // if newround != 2
  else {
    newround = 1;
  }
} // next
//-----------------------------------------------------
//--- Slideshow changes -------------------------------
//-----------------------------------------------------
function openslideshow(url,window_width,window_height,new_window) {
  if (new_window == 2 || new_window == 1) {
    var width_avail = screen.availWidth-15;
    var height_avail = screen.availHeight-60;
    var width_full = screen.width;
    var height_full = screen.height;

    // Callculate the spaces from left and top to center the new window:
    var left_space = ((width_avail - window_width) > 0) ? parseInt((width_avail - window_width) / 2) : 0;
    var top_space = ((height_avail - window_height) > 0) ? parseInt((height_avail - window_height) / 2) : 0;
    if (new_window == 2) {
      window_width = width_avail;
      window_height = height_avail;
      left_space = 0;
      top_space = 0;
      url += "&maw="+(window_width-97)+"&mah="+(window_height-194)+"&msw="+width_full+"&msh="+height_full;
    } // if complete picture (whole window)

    slideshow_window = window.open(url,"slideshowwindow","resizable=yes,dependent=yes,hotkeys=no,location=no,menubar=no,scrollbars=yes,status=yes,toolbar=no,width="+window_width+",height="+window_height+",left="+left_space+",top="+top_space+"");
  } // if new_window
  else {
    window.location.href=url;
  } // else new_window
} // openslideshow
