var HOMEPAGE = "default.html";

var pageList = [
  "iED_TranscoderPres01.WalledGardens.xhtml",
  "iED_TranscoderPres02.About_iED.xhtml",
  "iED_TranscoderPres03.Standards.xhtml",
  "iED_TranscoderPres04.COLE.xhtml",
  "iED_TranscoderPres05.Multi_Platform.xhtml",
  "iED_TranscoderPres06.iED_OFFs.xhtml",
  "iED_TranscoderPres07.Architecture.xhtml",
  "iED_TranscoderPres08.ColladaFeatures.xhtml",
  "iED_TranscoderPres09.ColladaSubsetGeometry.xhtml",
  "iED_TranscoderPres10.ColladaSubsetMaterials.xhtml",
  "iED_TranscoderPres11.CodeSample.xhtml",
  "iED_TranscoderPres12.Results.xhtml",
  "iED_TranscoderPres13.Finale.xhtml",
  "iED_TranscoderPres14.References.xhtml"
];


if( window.addEventListener )
  window.addEventListener( 'load', registerEventHandlers, false );


function registerEventHandlers() 
{
  document.addEventListener( 'keypress', loadNewPage, false);
  if( location.pathname.substr(location.pathname.lastIndexOf("/")+1,location.pathname.length) == HOMEPAGE ) 
    alert( "Note: you can use 'w', 'a', 'd' keys to navigate presentation." );
};


function loadNewPage( event )
{
  // TEST FOR ASCII a, d, or w
  if( event.charCode == 97 || event.charCode == 100 || event.charCode == 119  )
  {
    var currentPage = location.pathname.substr(location.pathname.lastIndexOf("/")+1,location.pathname.length);
    for( var i=0; i<pageList.length; i++ )
    {
       if( currentPage == HOMEPAGE )
       {
         var newPage = pageList[0];
         document.location.pathname =
	   document.location.pathname.replace( 
	     new RegExp( currentPage ), newPage 
	   );
       }
       else if( currentPage == pageList[i] )
       {
         switch( event.charCode )
         {
           case 97:
             if( (i-1) >= 0 )
             {
               var newPage = pageList[i-1];
               document.location.pathname =
	         document.location.pathname.replace( 
	           new RegExp( currentPage ), newPage 
	       );
             }
             break;
           case 100:
             if( (i+1) < pageList.length )
	     {
	       var newPage = pageList[i+1];
               document.location.pathname =
	          document.location.pathname.replace( 
	           new RegExp( currentPage ), newPage 
	         );
             }
             break;
           case 119:
             var newPage = HOMEPAGE;
	     document.location.pathname =
	       document.location.pathname.replace( 
	         new RegExp( currentPage ), newPage 
	       );
             break;
         }
         break;
       }
     }
   }
   else
   {
     return;
   }
}

