// -*- javascript -*-

////////////////////////////////////////////////////////////////////////////
// Hide most of the page content.

function hideExtras()
{
   $( "leftColumn" ).style.display     = "none";
   $( "titleBar" ).style.display       = "none";
   $( "footerBar" ).style.display      = "none";
   $( "validBoast" ).style.display     = "none";
   $( "mainContent" ).style.marginLeft = 0;
}

////////////////////////////////////////////////////////////////////////////
// Restore the hidden bits of the page.

function showExtras()
{
   $( "leftColumn" ).style.display     = "";
   $( "titleBar" ).style.display       = "";
   $( "footerBar" ).style.display      = "";
   $( "validBoast" ).style.display     = "";
   $( "mainContent" ).style.marginLeft = "140px";
}

////////////////////////////////////////////////////////////////////////////
// Populate the album option list.

function loadAlbums( doc )
{
   var response = doc.getElementsByTagName( "response" );

   try
   {
      if ( response && response[ 0 ].getAttribute( "code" ) == "0" )
      {
         var albums    = doc.evaluate('//album', doc, null, XPathResult.ANY_TYPE, null );
         var thisAlbum = albums.iterateNext();
         var albumList = $( "albumList" );

         if ( albumList.options.length > 0 )
         {
            albumList.options[ 0 ] = null;
         }
         
         while ( thisAlbum )
         {
            albumList.options.add( new Option( thisAlbum.getAttribute( "name" ), escape( thisAlbum.getAttribute( "name" ) ) ) );
            thisAlbum = albums.iterateNext();
         }
      }
      else
      {
         alertError( "Response error from server." );
      }
   }
   catch ( err )
   {
      alertError( "Can't get album data. I bet you're using IE aren't you? Can I suggest a better browser?" );
   }
}

////////////////////////////////////////////////////////////////////////////
// Alert the user to an error.

function showFeedback( error )
{
   $( "feedback" ).innerHTML = "<p>" + error + "</p>";
}

////////////////////////////////////////////////////////////////////////////
// Things to do when the page first loads.

function onLoad()
{
   // Get the album list.
   new Ajax.Request( "../api/?method=albums.getList",
                     {
                        method: "GET",
                        
                        onSuccess: function( resp )
                        {
                           loadAlbums( resp.responseXML );
                        },
                        on404 : function( resp )
                        {
                           showFeedback( "Oops!" );
                        },
                        onFailure : function( resp )
                        {
                           showFeedback( "Double Oops!" );
                        }
                     } );
}

////////////////////////////////////////////////////////////////////////////
// Variables for keeping track of the slideshow.

var slides;
var slide;
var timer = false;

////////////////////////////////////////////////////////////////////////////
// Central slideshow function.

function showSlide()
{
   if ( slide == slides.length )
   {
      slide = 0;
   }

   $( "feedback" ).innerHTML =
      "<p style=\"text-align: center;\">" + slides[ slide ][ 0 ].getAttribute( "title" ) + " (" + ( slide + 1 ) + "/" + slides.length + ")</p>\n" +
      "<p class=\"photograph\"><img src=\"" + slides[ slide ][ 1 ].src + "\" /></p>";
   slide++;
   
   // Set the timeout to come back here again.
   timer = setTimeout( "showSlide();", 5000 );
}

////////////////////////////////////////////////////////////////////////////
// Kicks off the slideshow.

function kickOffSlideshow( doc )
{
   hideExtras();
   
   showFeedback( "Starting slideshow, please wait a moment..." );
   
   var images     = doc.evaluate('//image', doc, null, XPathResult.ANY_TYPE, null );
   var thisImage  = images.iterateNext();
   var firstImage = true;

   slides = new Array();
   slide  = 0;

   if ( thisImage )
   {
      while ( thisImage )
      {
         var theImage = thisImage.getElementsByTagName( "file" )[ 0 ];
         var img      = new Image();
         
         if ( firstImage )
         {
            img.onload = showSlide;
            firstImage = false;
         }
         
         img.width    = theImage.getAttribute( "width" );
         img.height   = theImage.getAttribute( "height" );
         img.src      = theImage.firstChild.nodeValue;
         slides[ slides.length ] = new Array( thisImage, img );
         
         thisImage = images.iterateNext();
      }
   }
   else
   {
      showFeedback( "No images for chosen album. That's a bit odd." );
   }
}

////////////////////////////////////////////////////////////////////////////
// Called when the user clicks on "Go".

function doSlideshow()
{
   // Stop any running slideshow.
   stopSlideshow();
   
   // Figure out the album they want to watch.
   var album = $( "albumList" ).value;

   // Get the images for that album.
   new Ajax.Request( "../api/?method=albums.images.getList&album=" + album,
                     {
                        method: "GET",
                        
                        onSuccess: function( resp )
                        {
                           kickOffSlideshow( resp.responseXML );
                        },
                        on404 : function( resp )
                        {
                           showFeedback( "Oops!" );
                        },
                        onFailure : function( resp )
                        {
                           showFeedback( "Double oops!" );
                        }
                     } );
}


////////////////////////////////////////////////////////////////////////////
// Called when the user clicks on "Stop".

function stopSlideshow()
{
   if ( timer )
   {
      clearTimeout( timer );
      timer = false;
      $( "feedback" ).innerHTML = "";
      showExtras();
   }
}

/*
 * slideshow.js ends here
 */

