<?php
include_once('config.inc.php');
include_once('classes/core/Site.class.php');


// obtain a new instance of the easy-CMS site object
$site = &Site::getInstance(); // constructor checks the $_GET, $_POST and $_SESSION arrays
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<style type="text/css">
@import url("includes/html/styles/styles_dia.css");
</style>

<script language="JavaScript">
var PLAY = 1;
var PAUSE = 0;

var diaDelay = 3;
var diaIndex = 0;

var diapoState = PLAY;
var timeOut;

var onMenu = false;

var picturesList	= new Array();
var imagesList		= new Array();

function cancelBubble(newEvent) {
    if (document.all) {
        window.event.cancelBubble = true;
    } else {
        newEvent.cancelBubble = true;
    }
}

function gotoPrevious() {
	--diaIndex;
	if(diaIndex < 0) {
		gotoLast();
	}
	showPicture();
}

function gotoNext() {
	++diaIndex;
	if(diaIndex >= picturesList.length) {
		gotoFirst();
	}
	showPicture();
}

function gotoFirst() {
	diaIndex = 0;
	showPicture();
}

function gotoLast() {
	diaIndex = picturesList.length - 1;
	showPicture();
}

function playOrPause () {
	if(diapoState == PLAY) {
		diapoState = PAUSE;
		document.getElementById('dia_play').src = "includes/images/dia_play.gif";
	}
	else {
		diapoState = PLAY;
		document.getElementById('dia_play').src = "includes/images/dia_pause.gif";
	}
}

function increaseDelay() {
	if(diaDelay < 30) {
		++diaDelay;
	}
	document.getElementById('inputDelay').value = diaDelay;
}

function decreaseDelay() {
	if(diaDelay > 1) {
		--diaDelay;
	}
	document.getElementById('inputDelay').value = diaDelay;
}

function showPicture() {
	document.images.SlideShow.src = imagesList[diaIndex].src;
}

function hideMenu() {
	document.getElementById('dia_menu').style.display = 'none';
}

function showMenu() {
	onMenu = true;
	document.getElementById('dia_menu').style.display = 'block';
}

function autoHideMenu() {
	if(!onMenu) {
		setTimeout('hideMenu()', 1000);
 	}
 	else {
 		onMenu = false;
 	}
}

function mainLoop() {
	if(diapoState == PLAY) {
		if (timeOut > 1){
			timeOut -= 1;
		}
		else{
			gotoNext();
			timeOut = diaDelay;
		}
	}
	gameLoopTimerId = setTimeout("mainLoop()", 1000);
}

function keyPressedHandler(evnt) {
	var key;

	if(evnt) {
	  key = evnt.keyCode;
	}
	else{
	  key = window.event.keyCode;
	}

	gotoNext();

}
<?php

function &getSubPagesList(&$dbConnection, $pid) {
	$pagesList = "";
	$result = &$dbConnection->getRecord(PAGES_TABLE, $pid, '', 'parent_id');
	while($row = &$dbConnection->fetchArray($result)){
		$subPagesList = getSubPagesList($dbConnection, $row['id']);
		$pagesList .= $row['id'].',';
		if(strlen($subPagesList) > 0) {
			$pagesList .= $subPagesList.',';
		}
	}
	return $pagesList;
}

$diaIndex = 0;

// get dia_mode
if(( $mode = $site->getSubmittedVar("dia_mode", false))) {
	$imagesList = "";
	$i = 0;
	$dbConnection = &$site->getDBManipulator();
	$requestedDiaContentIndex = $site->getSubmittedVar("index", 0);

	$langId = $site->getCurrentLangId();
	list($type, $id) = split("_", $mode);
	switch($type) {
		case "ctn":
			$result = &$dbConnection->getRecord(CONTAINERS_TABLE, $id);
			if($row = &$dbConnection->fetchArray($result)){
				$containerRepository .= $row["repository"];
			}

			$result = &$dbConnection->getRecord(CONTAINER_CONTENTS_TABLE, $id, '', 'container_id');
			while($row = &$dbConnection->fetchArray($result)){
				$imagesList .= $row["content_id"].',';
			}
			$imagesList = substr($imagesList,0,(strLen($imagesList)-1));

			$result = &$dbConnection->getRecord(TYPE_TABLES_PREFIX.'gallery_photo', '', "`id` in ($imagesList) and `lang_id` = '$langId' and `deleted` = 0");
			while($row = &$dbConnection->fetchArray($result)){
				$image = $row["image"];
				if($requestedDiaContentIndex == $row["id"]) {
					$diaIndex = $i;
				}
			  if(strpos($image, $containerRepository) === false || !is_file($image)) {
					// image does not belong to the current container repository or associated file has been deleted
			  	continue;
			  }
				print("picturesList[$i] = '$image';\n");
				++$i;
			}
			break;
		case "pge" :
/*
			$pagesManager = &$site->getPagesManager();
			$page = $pagesManager->getPage($id);
			$parentId = $page->getParentId();

			$pagesList = getSubPagesList($dbConnection, $parentId);
			$pagesList = "$parentId,$pagesList";
			$pagesList = substr($pagesList,0,(strLen($pagesList)-1));
			$containersList = "";

			$result = &$dbConnection->getRecord(FRAME_CONTAINERS_TABLE, '', '`page_id` in ('.$pagesList.')');
			while($row = &$dbConnection->fetchArray($result)){
				$containersList .= $row["container_id"].',';
			}
			$containersList = substr($containersList,0,(strLen($containersList)-1));

			$result = &$dbConnection->getRecord(CONTAINER_CONTENTS_TABLE, '', '`container_id` in ('.$containersList.')');
			while($row = &$dbConnection->fetchArray($result)){
				$imagesList .= $row["content_id"].',';
			}
			$imagesList = substr($imagesList,0,(strLen($imagesList)-1));

			$result = &$dbConnection->getRecord(TYPE_TABLES_PREFIX.'photo', '', '`id` in ('.$imagesList.')');
			while($row = &$dbConnection->fetchArray($result)){
				$image = $row["image"];
				if($requestedDiaContentIndex == $row["id"]) {
					$diaIndex = $i;
				}
				print("picturesList[$i] = '$image';\n");
				++$i;
			}
*/
			break;
		default :
			exit(0);
			break;
	}
}
?>

function init() {
<?php
	echo "	diaIndex = $diaIndex;\n";
?>
	timeOut = diaDelay;
	diapoState = PLAY;
	setTimeout("mainLoop()", diaDelay);

	document.onkeydown = keyPressedHandler;

	document.getElementById('inputDelay').value = diaDelay;

	document.images.SlideShow.src = picturesList[diaIndex];

	for (i = 0; i < picturesList.length; i++){
	   imagesList[i] = new Image();
	   imagesList[i].src = picturesList[i];
	}

	showMenu();
	setTimeout('hideMenu()', 2000);
}
</script>
</head>

<body <!--onload="javascript:self.moveTo(0,0);self.resizeTo(screen.width,screen.height);"-->>
<table onmouseover="javascript:autoHideMenu();" style="border-collapse: collapse; border:0; padding:0; margin:0; width: 100%; height: 100%;" onmouseover="javascript:cancelBubble(arguments[0]);">
<tr>
<td width="100%" height="100%" align="center" valign="center"><img name="SlideShow" src="" /></td>
<tr>
</table>

<div id="dia_thumb" onmouseover="javascript:showMenu();" onmouseout="javascript:autoHideMenu();"></div>
<div id="dia_menu" onmouseover="javascript:showMenu();" onmouseout="javascript:autoHideMenu();">
  <div class="dia_option"><span onclick="javascript:gotoFirst();"><img border="0" src="includes/images/goto_first.gif" alt="premi&egrave;re dia" /></span></div>
  <div class="dia_option"><span onclick="javascript:gotoPrevious();"><img border="0" src="includes/images/goto_previous.gif" alt="dia pr&eacute;c&eacute;dente" /></span></div>
  <div class="dia_option"><span onclick="javascript:playOrPause();"><img id="dia_play" border="0" src="includes/images/dia_pause.gif" alt="pauser/reprendre le diaporama" /></span></div>
  <div class="dia_option"><span onclick="javascript:gotoNext();"><img border="0" src="includes/images/goto_next.gif" alt="dia suivante" /></span></div>
  <div class="dia_option"><span onclick="javascript:gotoLast();"><img border="0" src="includes/images/goto_last.gif" alt="derni&egrave;re dia" /></span></div>
  <div class="dia_option">
    <div class="dia_option">
      <input id="inputDelay" type="text" value="" size="2" />
    </div>
    <div class="dia_option">
      <div class="button"><span onclick="javascript:increaseDelay();"><img src="includes/images/up.gif" border="0" /></span></div>
      <div class="button"><span onclick="javascript:decreaseDelay();"><img src="includes/images/down.gif" border="0" /></span></div>
    </div>
  </div>
</div>



<script language="javaScript">init();</script>
</body>
</html>