// Penguin Gallery http://www.penguintutor.com/penguingallery 
// Copyright 2009 Stewart Watkiss
/*  
	This file is a part of Penguin Gallery

	Penguin Gallery is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Penguin Gallery is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Penguin Gallery.  If not, see <http://www.gnu.org/licenses/>.
*/

/* The following are variables provided to the JavaScript code by embedding into the php page
* imgscript				// URL for image php file
* navscript				// URL for navigation php file
* slideshowseconds		// Time to display image for before moving to next image in seconds
* slideshowautorepeat	// Whether repeat from start when reach end (true / false)

*/

// Position in slideshow
slideshowimgnum = 1;
slideshowrunning = false;

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

// running is an optional parameter (if true then we set play image accordingly - ie pause - if not we stop slideshow)
function changeimg(imgnum, running) {
// Check image is in range
// If it's too small set to first img
if (imgnum < 1) {imgnum = 1;}
// If it's too big set to last img
if (imgnum > numimages) {imgnum = numimages;}

// We set slideshowimgnum - so any slideshow will go from current location
// This could be an option - whether always start from 0
slideshowimgnum = imgnum;

// Handle whether running
if ((running == null) || (running == false)) { slideshowrunning = false;}
if (slideshowrunning == false) {runningStatus = "no";}
else {runningStatus = "yes";}

  // code to change image
  var request = getHTTPObject();
  if (request) {
    request.onreadystatechange = function() {
      replaceImage(request);
    };
    request.open("GET", imgscript+'?img='+imgnum+'&gallery='+galleryconfigfile, true);
    request.send(null);
  }
  // code to change navigation
  var request2 = getHTTPObject();
  if (request2) {
    request2.onreadystatechange = function() {
      replaceNav(request2);
    };
    request2.open("GET", navscript+'?img='+imgnum+'&gallery='+galleryconfigfile+'&running='+runningStatus, true);
    request2.send(null);
  }
}


function replaceImage(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      var details = document.getElementById("gallerylargeimgdiv");
      details.innerHTML = request.responseText;
    }
  }
}

function replaceNav(request2) {
  if (request2.readyState == 4) {
    if (request2.status == 200 || request2.status == 304) {
      var details2 = document.getElementById("gallerynavigationdiv");
      details2.innerHTML = request2.responseText;
    }
  }
}


function slideshowRefresh() {
	// First check to see if this is still running if not then we cancel
	// by setting to false we can cancel
	if (slideshowrunning == false) {return;}
	// Increment image number - check to see if it's after end if so do we restart?
	slideshowimgnum ++;
	
	//If we reach end of slideshow
	if (slideshowimgnum > numimages) 
	{
		// Go back to 0 or just stop
		if (slideshowautorepeat == true) {slideshowimgnum = 0;}
		else {slideshowimgnum = numimages; return;} 
	}
	
	// Change image
	changeimg(slideshowimgnum, true);
	// After time delay has passed calls this again to keep slideshow running
	setTimeout("slideshowRefresh();", slideshowseconds * 1000);
}

// Used by pause to refresh navigation without changing image
function updateNavigation() {
	
	// Handle whether running
if (slideshowrunning == false) {runningStatus = "no";}
else {runningStatus = "yes";}
	
	
  // code to change navigation
  var request = getHTTPObject();
  if (request) {
    request.onreadystatechange = function() {
      replaceNav(request);
    };
    request.open("GET", navscript+'?img='+slideshowimgnum+'&gallery='+galleryconfigfile+'&running='+runningStatus, true);
    request.send(null);
  }
}
