// ----------------------------------------------------------------------------
// slideshow.js - updated 2011 December 07
// ----------------------------------------------------------------------------

function Slide( fileName, title, url )
{
   this.fileName = fileName;
   this.title = title;
   this.url = url;

   this.info = function()
   {
      var text = this.title;
      if ( this.url.length )
         text += "<img style=\"width:11px; height:9px; margin-left:0.5em;\" " +
                 "src=\"/home/forward.png\" /><a href=\"" + this.url + "\">Read more</a>";
      return text;
   }
}

var __slides__ = [
   [
      new Slide( "M51-CAHA.jpg",           "A New Look into the Whirlpool (CAHA/Descubre/DSA/OAUV)", "gallery/M51-CAHA/en.html" ),
      new Slide( "M45-ilizaso.jpg",        "Messier 45, the Pleiades Star Cluster (Copyright &copy; I&ntilde;aki Lizaso, 2011)", "http://astrosurf.com/ilizaso/orriak/3maila/M45_FSQ_U16m.htm" ),
      new Slide( "NGC6727-ljulio.jpg",     "NGC 6727: Dust and Gas in Corona Australis (Copyright &copy; 2011 Leonardo Julio)", "http://astronomiapampeana.com.ar/foto/77/NGC-6727-Dust-and-Gas-in-Corona-Australis.html" ),
      new Slide( "M74-CAHA.jpg",           "Face-On Spiral Galaxy Messier 74 (CAHA/Descubre/DSA/OAUV)", "gallery/M74-CAHA/en.html" ),
      new Slide( "NGC6888-manuelj.jpg",    "NGC 6888, the Crescent Nebula (Copyright &copy; 2011 Manuel Jiménez)", "http://www.manuelj.com/Astronomy/Planetary-Nebula/NGC-6888-Crescent/" ),
      new Slide( "M57-CAHA.jpg",           "Messier 57, the Ring Nebula (CAHA/Descubre/DSA/OAUV)", "gallery/M57-CAHA/en.html" ),
      new Slide( "antares-gbachmayer.jpg", "Two-panel mosaic of the Antares - Rho Ophiuchi region (Copyright &copy; 2009 Gerhard Bachmayer)", "http://www.pbase.com/gbachmayer/image/116401787" ),
      new Slide( "NGC6914-CAHA.jpg",       "The NGC 6914 Nebula Complex (CAHA/Descubre/DSA/OAUV)", "gallery/NGC6914-CAHA/en.html" ),
      new Slide( "NGC2237-pbernhard.jpg",  "NGC 2237, the Rosette Nebula (Copyright &copy; 2011 Philippe Bernhard)", "http://astroccd.eu/crbst_78.html" ),
      new Slide( "NGC7331-CAHA.jpg",       "NGC 7331: A Large Spiral Galaxy (CAHA/Descubre/DSA/OAUV)", "gallery/NGC7331-CAHA/en.html" ),
      new Slide( "NGC6727-gbachmayer.jpg", "NGC 6727 and Be 157 in Corona Australis (Copyright &copy; 2009 Gerhard Bachmayer)", "http://www.pbase.com/gbachmayer/image/116073604" ),
      new Slide( "M81-M82-jgallego.jpg",   "Messier 81/82 + IFN (Copyright &copy; 2011 Jordi Gallego)", "http://astrosurf.com/jordigallego/album/Files_dark_sky_SBIG/slides/M81_M82_IFN_2011_widefield.html" ),
      new Slide( "M20-gbachmayer.jpg",     "M20, the Trifid Nebula (Copyright &copy; 2011 Gerhard Bachmayer)", "http://www.pbase.com/gbachmayer/image/135477935" ),
      new Slide( "M97-CAHA.jpg",           "Messier 97, the Owl Nebula (CAHA/Descubre/DSA/OAUV)", "gallery/M97-CAHA/en.html" ),
      new Slide( "NGC4372-ljulio.jpg",     "The Dark Doodad Nebula and NGC 4372, in Musca (Copyright &copy; 2011 Leonardo Julio)", "http://astronomiapampeana.com.ar/foto/75/The-Dark-Doodad-Nebula-and-NGC-4372.html" ),
      new Slide( "veil-pbernhard.jpg",     "The Veil Nebula Complex in Cygnus (Copyright &copy; 2011 Philippe Bernhard)", "http://astroccd.eu/crbst_85.html" ),
      new Slide( "M31-CAHA.jpg",           "Messier 31, the Andromeda Galaxy (CAHA/Descubre/DSA/OAUV)", "http://pixinsight.com/gallery/M31-CAHA/en.html" ),
      new Slide( "M83-gbachmayer.jpg",     "Face-on spiral galaxy M83 (Copyright &copy; 2011 Gerhard Bachmayer)", "http://www.pbase.com/gbachmayer/image/135676425" ),
      new Slide( "NGC7635-manuelj.jpg",    "NGC 7635, the Bubble Nebula (Copyright &copy; 2011 Manuel Jiménez)", "http://www.manuelj.com/Astronomy/Nebula/NGC-7635-Bubble-Nebula/" ),
      new Slide( "VdB152-jgallego.jpg",    "VdB 152 (Copyright &copy; 2010 Jordi Gallego)", "http://astrosurf.com/jordigallego/album/Files_dark_sky_SBIG/slides/VdB_152_widefield.html" ),
      new Slide( "NGC7023-CAHA.jpg",       "NGC 7023, the Iris Nebula (CAHA/Descubre/DSA/OAUV)", "tutorials/NGC7023-HDR/en.html" ),
      new Slide( "IC4592-ljulio.jpg",      "IC 4592: Horse Head Shaped Reflection Nebula in Ophiuchus (Copyright &copy; 2011 Leonardo Julio)", "http://astronomiapampeana.com.ar/foto/74/IC-4592-Horse-Head-Shaped-Reflection-Nebula.html" )
   ]
];

var __slideWaitCount__ = 250;

function SlideImagePath( showIndex, imageIndex )
{
   return "/home/20111207/" + ZeroPad( showIndex+1, 2 ) + "/" + __slides__[showIndex][imageIndex].fileName;
}

function SlideShow( showIndex )
{
   this.showIndex = showIndex;
   this.numberOfSlides = __slides__[showIndex].length;
   this.imageIndex = 0;
   this.imageId = "slideShowImage" + ZeroPad( showIndex+1, 2 );
   this.textId = "slideShowTextt" + ZeroPad( showIndex+1, 2 );
   this.navigatorId = "navigator" + ZeroPad( showIndex+1, 2 );
   this.opacity = 100;
   this.fadingOut = true;
   this.paused = false;
   this.waitCount = __slideWaitCount__;

   this.initialize = function()
   {
      this.image = document.getElementById( this.imageId );
      this.text = document.getElementById( this.textId );

      this.iconFirst = document.getElementById( this.navigatorId + ".first" );
      this.iconFirst.__animatedItem__ = this;
      this.iconFirst.enabled = true;
      this.iconFirst.enable = function()
      {
         this.src = "/home/first.png";
         this.enabled = true;
      };
      this.iconFirst.disable = function()
      {
         this.src = "/home/first.disabled.png";
         this.enabled = false;
      };
      this.iconFirst.onclick = function()
      {
         if ( this.enabled )
            this.__animatedItem__.goFirst();
      };

      this.iconBackward = document.getElementById( this.navigatorId + ".backward" );
      this.iconBackward.__animatedItem__ = this;
      this.iconBackward.enabled = true;
      this.iconBackward.enable = function()
      {
         this.src = "/home/backward.png";
         this.enabled = true;
      };
      this.iconBackward.disable = function()
      {
         this.src = "/home/backward.disabled.png";
         this.enabled = false;
      };
      this.iconBackward.onclick = function()
      {
         if ( this.enabled )
            this.__animatedItem__.goBackward();
      };

      this.iconPausePlay = document.getElementById( this.navigatorId + ".pausePlay" );
      this.iconPausePlay.__animatedItem__ = this;
      this.iconPausePlay.update = function()
      {
         if ( this.__animatedItem__.paused )
            this.src = "/home/play.png";
         else
            this.src = "/home/pause.png";
      };
      this.iconPausePlay.onclick = function()
      {
         if ( this.__animatedItem__.paused )
            this.__animatedItem__.play();
         else
            this.__animatedItem__.pause();
      };

      this.iconForward = document.getElementById( this.navigatorId + ".forward" );
      this.iconForward.__animatedItem__ = this;
      this.iconForward.enabled = true;
      this.iconForward.enable = function()
      {
         this.src = "/home/forward.png";
         this.enabled = true;
      };
      this.iconForward.disable = function()
      {
         this.src = "/home/forward.disabled.png";
         this.enabled = false;
      };
      this.iconForward.onclick = function()
      {
         if ( this.enabled )
            this.__animatedItem__.goForward();
      };

      this.iconLast = document.getElementById( this.navigatorId + ".last" );
      this.iconLast.__animatedItem__ = this;
      this.iconLast.enabled = true;
      this.iconLast.enable = function()
      {
         this.src = "/home/last.png";
         this.enabled = true;
      };
      this.iconLast.disable = function()
      {
         this.src = "/home/last.disabled.png";
         this.enabled = false;
      };
      this.iconLast.onclick = function()
      {
         if ( this.enabled )
            this.__animatedItem__.goLast();
      };

      this.updateImage();
      this.updateNavigator();
   };

   this.updateImage = function()
   {
      this.image.__loaded__ = false;
      this.image.__animatedItem__ = this;

      this.image.onload = function()
      {
         this.__loaded__ = true;
      };

      this.image.onerror = function()
      {
         // force image change on error
         this.__animatedItem__.opacity = 1;
         this.__animatedItem__.fadingOut = true;
      };

      this.image.src = SlideImagePath( this.showIndex, this.imageIndex );
      this.text.innerHTML = __slides__[this.showIndex][this.imageIndex].info();
   };

   this.updateOpacity = function()
   {
      if ( isIE ) // IE detected in utils.js
      {
         this.image.style.filter = 'alpha(opacity=' + this.opacity + ')'; // MSIE bizarre code for opacity
         this.text.style.filter = 'alpha(opacity=' + this.opacity + ')';
      }
      else
      {
         this.image.style.opacity = 0.01*this.opacity; // for real browsers
         this.text.style.opacity = 0.01*this.opacity;
      }
   };

   this.updateNavigator = function()
   {
      if ( this.canGoBackward() )
      {
         this.iconFirst.enable();
         this.iconBackward.enable();
      }
      else
      {
         this.iconFirst.disable();
         this.iconBackward.disable();
      }

      this.iconPausePlay.update();

      if ( this.canGoForward() )
      {
         this.iconLast.enable();
         this.iconForward.enable();
      }
      else
      {
         this.iconLast.disable();
         this.iconForward.disable();
      }
   };

   this.animate = function()
   {
      if ( this.paused )
         return;

      if ( this.waitCount > 0 )
      {
         --this.waitCount;
         return;
      }

      if ( this.fadingOut )
      {
         if ( --this.opacity == 0 )
         {
            this.fadingOut = false;
            if ( ++this.imageIndex == this.numberOfSlides )
               this.imageIndex = 0;
            this.updateImage();
            this.updateNavigator();
         }
      }
      else
      {
         if ( this.image.__loaded__ )
            if ( (this.opacity += 2) == 100 )
            {
               this.fadingOut = true;
               this.waitCount = __slideWaitCount__;
            }
      }

      this.updateOpacity();
   };

   this.goTo = function( imageIndex )
   {
      this.opacity = 100;
      this.fadingOut = true;
      this.waitCount = __slideWaitCount__;
      this.imageIndex = imageIndex;
      this.updateImage();
      this.updateOpacity();
      this.updateNavigator();
   };

   this.canGoForward = function()
   {
      return this.imageIndex < this.numberOfSlides-1;
   };

   this.canGoBackward = function()
   {
      return this.imageIndex > 0;
   };

   this.goForward = function()
   {
      if ( this.canGoForward() )
         this.goTo( this.imageIndex+1 );
   };

   this.goBackward = function()
   {
      if ( this.canGoBackward() )
         this.goTo( this.imageIndex-1 );
   };

   this.goFirst = function()
   {
      this.goTo( 0 );
   };

   this.goLast = function()
   {
      this.goTo( this.numberOfSlides-1 );
   };

   this.pause = function()
   {
      if ( !this.paused )
      {
         this.paused = true;
         this.goTo( this.imageIndex );
      }
   };

   this.play = function()
   {
      if ( this.paused )
      {
         this.paused = false;
         this.goTo( this.imageIndex );
      }
   };
}

var __slideShows__ = new Array;
for ( var i = 0; i < __slides__.length; ++i )
   __slideShows__.push( new SlideShow( i ) );

function PlaySlideShow( showIndex )
{
   setInterval( "__slideShows__[" + showIndex + "].animate();", 25 );
}

function StartSlideShow()
{
   for ( var i = 0; i < __slideShows__.length; ++i )
      __slideShows__[i].initialize();
   for ( var i = 0; i < __slideShows__.length; ++i )
      setTimeout( "PlaySlideShow( " + i + ");", i*1000 );
}

__preloadedImages__ = new Array;

function PreloadSlideShowImages()
{
   for ( var k = 0, i = 0; i < __slides__.length; ++i )
      for ( var j = 0; j < __slides__[i].length; ++j, ++k )
      {
         __preloadedImages__[k] = new Image;
         __preloadedImages__[k].src = SlideImagePath( i, j );
      }
}

function GenerateSlideShow( showIndex )
{
   var s = ZeroPad( showIndex+1, 2 );
   document.writeln( "<img id=\"slideShowImage" + s + "\" class=\"homeAnimatedImage\" src=\"home/" + s + "/01.jpg\" alt=\"\" />" );
   document.writeln( "<p id=\"slideShowTextt" + s + "\" class=\"homeAnimatedText\" style=\"float:left;\">" );
   document.writeln( "   <script type=\"text/javascript\">" );
   document.writeln( "      document.write( __slides__[0][0].info() );" );
   document.writeln( "   </script>" );
   document.writeln( "</p>" );
   document.writeln( "<p class=\"homeAnimatedText\" style=\"float:right; cursor:default;\">" );
   document.writeln( "   <img id=\"navigator" + s + ".first\"     src=\"home/first.png\"    width=\"11\" height=\"9\" />" );
   document.writeln( "   <img id=\"navigator" + s + ".backward\"  src=\"home/backward.png\" width=\"11\" height=\"9\" />" );
   document.writeln( "   <img id=\"navigator" + s + ".pausePlay\" src=\"home/pause.png\"    width=\"11\" height=\"9\" />" );
   document.writeln( "   <img id=\"navigator" + s + ".forward\"   src=\"home/forward.png\"  width=\"11\" height=\"9\" />" );
   document.writeln( "   <img id=\"navigator" + s + ".last\"      src=\"home/last.png\"     width=\"11\" height=\"9\" />" );
   document.writeln( "</p>" );
   document.writeln( "<br style=\"clear:both;\" />" );
}

window.onload = function()
{
   IE6Warning();
   AdjustCSSPageComponents();
   PreloadSlideShowImages();
   StartSlideShow();
}

// ----------------------------------------------------------------------------

