// --------------------------------------------------------------------------
// Copyright Julian Nicholls 2010, All rights reserved.
// This file is licensed for use on one website: fitnessfirst.no.
// --------------------------------------------------------------------------

var pics = [
  '022.png', '030.png', '070.png', '087.png', '101.png', '110.png', '123.png', '146.png',
  '159.png', '182.png', '219.png', '225.png', '241.png', '247.png', '249.png', '279.png',
  '284.png', '366.png', 'Greta002.png', 'PNSeminar.png', 'bjandsadiq1.png', 'ffas040.png',
  'fightclub011.png', 'sparringclass018.png', 'tb030309.png', 'tb030309198.png', 'trev01.png', 'trevropes.png'
];

jQuery(function($) {

  if( $("#PhotoArea").length )
    setTimeout( function() { replaceImage( $("#PhotoArea"), 0 ); }, 3000 );
});


function replaceImage( $place, index )
{
  var nPics   = pics.length,
    newImg  = new Image,
    $paImg  = $place.find( 'img' );

  if( index >= nPics )
    index = 0;

  newImg.src = '/smaller/' + pics[index];

  newImg.onload = function()
  {
    $place.prepend( '<img/>' );

    var $newplace = $place.find( "img:first" );

    $newplace.css( {opacity: '0', left: (Math.random() * ((findLivePageWidth() - 640) - newImg.width))} ).attr( 'src', newImg.src ).fadeTo( 1000, 1 );

    $paImg.fadeTo( 1000, 0, function() {
      $paImg.remove();
    });
  }

  setTimeout( function() { replaceImage( $place, ++index ); }, 4000 );
}


//--------------------------------------------------------------------------
// findLivePageWidth
//
// Return the inside dimensions of the browser window.
//
//--------------------------------------------------------------------------

function findLivePageWidth()
{
    if( window.innerWidth )
        return window.innerWidth;

    if( document.body.clientWidth )
        return document.body.clientWidth;

    return  null;
}



