<?php
$title = "Animations";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . ini_get('include_path'));
include_once("include/header.php");



// Create our imap1m from the index file
$dom = new DOMDocument;
$dom->loadHTMLFile( 'index.htm' );
$xpath = new DOMXPath($dom);

$imap1 = $xpath->query('//map[@name="imap1"]')->item(0);
$ndom = new DOMDocument;
$node = $ndom->importNode($imap1,true);
$node->setAttribute('name','imap1m');
$ndom->appendChild($node);

// Fix the hrefs so they point to the latest image
foreach( $ndom->getElementsByTagName('area') as $rec ) {
	if ($rec->hasAttribute('href')) {
		//$rec->setAttribute('href', '#' );

		$href = $rec->getAttribute('href');

		$dir = dirname($href);

		$adom = new DOMDocument;
		$afile = $dir.'/index.html';
		if (!file_exists($afile)) {
			$rec->parentNode->removeChild( $rec );
			continue;
		}
		$adom->loadHTMLFile( $afile );
		$axpath = new DOMXPath($adom);
		// David changed from "ANIMATION" to .mp4 or .fli etc so we'll change our test
		//$filelist = $axpath->query('//a[text()="[ANIMATION]"]');
		$filelist = $axpath->query('//a[text()="[.mp4]"]');
		if (is_null($filelist) || ( $filelist->length <= 0 )) {
			// we'll delete this node
			$rec->parentNode->removeChild( $rec );
			continue;
		}
		$file = $filelist->item($filelist->length-1)->getAttribute('href');

		// check for mp4
		$nfile = $dir.'/'.substr($file,0,-4).'.mp4';
		if (file_exists($nfile)) $rec->setAttribute('data-mp4-file',$nfile );

		// check for ogg
		$nfile = $dir.'/'.substr($file,0,-4).'.ogg';
		if (file_exists($nfile)) $rec->setAttribute('data-ogg-file',$nfile );

		// check for avi
		$nfile = $dir.'/'.substr($file,0,-4).'.avi';
		if (file_exists($nfile)) $rec->setAttribute('data-avi-file',$nfile );

		$rec->setAttribute('href', '#' );
		$rec->setAttribute('data-dir', $dir);
	}
}

// Write out the map
echo $ndom->saveHTML();


?>

<div class="row">

	<div class="col-md-4">
		<img class="highlight" src="misc/imap1.gif" usemap="#imap1m">
	</div>

	<div class="col-md-8">
		<!--<div id="proxiedPagesContainer" class="displayNone">-->
		<div id="proxiedPagesContainer">
		<h4>Choose map region</h4>
		<a href="#" id="helpbutton" class="btn btn-primary" type="button">If the animations don't work...</a>
		</div>
	</div>
</div>

<?php
include("include/sfooter.php");
?>

<script>
+function ($) {
    $('img.highlight').maphilight({
        'fillColor': 'FF7251',
        'fillOpacity': 0.4,
        'strokeColor': 'FF532A',
        'strokeOpacity': 1,
        'strokeWidth': 2
    });
}(jQuery);

+function ($) {
	$("area").click( function( event ) {
		var mp4_file = $(this).attr('data-mp4-file');
		var file = mp4_file.slice(0,-4);
		$("#proxiedPagesContainer").removeClass("displayNone");
		$("#proxiedPagesContainer").load( "views/animationsTpl.php",{'link': file},
			function() {
				$("#date_selector").click( function() {
					$("#proxiedPagesContainer").load( "views/animationsIndexTpl.php",{'link': file, 'script': 'animations.php'});
		
				});
			}
		);
	});
}(jQuery);

+function ($) {
	$("#helpbutton").click( function( event ) {
		$("#proxiedPagesContainer").load( "animations_help.php", {"script": "animations.php"});
	});
}(jQuery);

</script>

<?php
	// link test
	if (isset($_REQUEST["link"])) {
		$link = $_REQUEST["link"];
		$mp4file = $link.'.mp4';
		$dir = dirname($link);
		echo "<script>\n";
		echo '+function ($) {';
		echo "\n";
/*
		echo '	$("area[data-dir=\''.$dir.'\']").attr("data-mp4-file","'.$mp4file.'");';
		echo '	$("area[data-dir=\''.$dir.'\']").click();';
*/

		echo '	$("#proxiedPagesContainer").removeClass("displayNone");';
		echo "\n";
		echo '	$("#proxiedPagesContainer").load( "views/animationsTpl.php",{"link": "'.$link.'"},';
		echo "\n";
		echo '		function() {';
		echo "\n";
		echo '			$("#date_selector").click( function() {';
		echo "\n";
		echo '				$("#proxiedPagesContainer").load( "views/animationsIndexTpl.php",{"link": "'.$link.'", "script": "animations.php"});';
		echo "\n";
		echo '			});';
		echo "\n";
		echo '		}';
		echo "\n";
		echo '	);';
		echo "\n";

		echo "}(jQuery);\n";
		echo "</script>\n";
	}
?>

