// Animation
swfobject.embedSWF( "/flashs/slides.swf", 
                    "home-slide", 
                    "779", 
                    "252", 
                    "9.0.0", 
                    null, 
                    {}, 
                    { wmode: "transparent" }, 
                    {} );

/*
 * Creations slides.
 */
 
var creationSlideTimeout = null;
var creationSlideDelayBetweenChanges = 2000;
var creationSlideSpeedChange = 500;
 
function changeCreationSlide()
{
 var availableCategories = [];
 
 $.each( $("div#page div.website div.content"), function( i, div )
 {
  if( $(div).children().length > 1 )
   availableCategories.push( $(div).parent() );
 } );
 
 if( availableCategories.length > 0 )
 {
  var index = Math.floor( Math.random() * availableCategories.length );
  
  var currentLink = availableCategories[ index ].find( "h2 a.selected" );
  var currentImage = availableCategories[ index ].find( "div.content a.selected" );
  
  var nextLink = currentLink.next( "a" );
  var nextImage = currentImage.next( "a" );
  
  if( nextImage.length == 0 )
  {
   nextLink = currentLink.parent().find( "a:first" );
   nextImage = currentImage.parent().find( "a:first" );
  }
  
  if( creationSlideTimeout == null )
   return;

  nextImage.css( "z-index", 8 );
  nextImage.addClass( "selected" );
  
  currentLink.removeClass( "selected" );
  nextLink.addClass( "selected" );
  
  currentImage.fadeOut( creationSlideSpeedChange, function()
  {
   currentImage.css( "z-index", 7 );
   currentImage.removeClass( "selected" );
   currentImage.css( "display", "" );
   nextImage.css( "z-index", 10 );
   
   if( creationSlideTimeout != null )
    creationSlideTimeout = setTimeout( "changeCreationSlide()", creationSlideDelayBetweenChanges );
  } );
 }
}

jQuery( function()
{
 $("div#page div.website h2 a").click( function()
 {
  clearTimeout( creationSlideTimeout );
  creationSlideTimeout = null;
  
  var website = $(this).parents( "div.website:first" );
  
  if( !$(this).hasClass( "selected" ) )
  {
   $(this).parent().find( "a.selected" ).removeClass( "selected" );
   $(this).addClass( "selected" );
   
   var currentImage = website.find( "div.content a.selected" );
   
   if( currentImage.length == 2 )
    currentImage = $(currentImage[1]).next().length == 0 ? $(currentImage[0]) : $(currentImage[1]);
   
   var nextImage = website.find( "div.content a:eq(" + $(this).index() + ")" );
   
   nextImage.stop();
   nextImage.css( { "z-index": 8, "opacity": "" } );
   nextImage.addClass( "selected" );
   
   currentImage.fadeOut( creationSlideSpeedChange, function()
   {
    currentImage.css( "z-index", 7 );
    currentImage.removeClass( "selected" );
    currentImage.css( "display", "" );
    nextImage.css( "z-index", 10 );
    
    creationSlideTimeout = setTimeout( "changeCreationSlide()", creationSlideDelayBetweenChanges );
   } );
  }
  else
  {
   creationSlideTimeout = setTimeout( "changeCreationSlide()", creationSlideDelayBetweenChanges );
  }
  
  return false;
 } );
 
 creationSlideTimeout = setTimeout( "changeCreationSlide()", creationSlideDelayBetweenChanges );
} );